Unit - 5 8 Hours Interfacing Programming in C - 2: Pin Description
Unit - 5 8 Hours Interfacing Programming in C - 2: Pin Description
Interfacing programming in C - 2
LCD, Keyboard, parallel and serial ADC, DAC, electromechanical relays, stepper motor, DC
motor.
LCD:
1. With neat diagram, explain briefly the 16x2 LCD interface to 8051 microcontroller with
its Pin description and all its commands. And also write a C program to display “JAIN”
on line 1 and “UNIVERSITY” on line 2.
16x2 has 2 lines with 16 characters on each line.
It supports all the ASCII characters and is basically used for displaying the alpha
numeric characters.
Here each character is displayed in a matrix of 5x7 pixels.
Apart from alpha numeric characters it also provides the provision to display the
custom characters by creating the pattern.
Pin description
Pin
Symbol Pin Function
Number
1 VSS Ground
2 VCC +5v
3 VEE Contrast adjustment (VO)
4 RS Register Select---0:Command, 1: Data
5 R/W Read/Write, R/W=0 Write & R/W=1 Read
6 EN Enable-Falling edge triggered
7 D0 Data Bit 0
8 D1 Data Bit 1
9 D2 Data Bit 2
10 D3 Data Bit 3
11 D4 Data Bit 4
12 D5 Data Bit 5
13 D6 Data Bit 6
14 D7 Data Bit 7/Busy Flag
RW OPERATION
0 WRITE
1 READ
Enable(EN):
This pin is used to send the enable trigger to LCD.
• After sending the data/command, selecting the data/command register, selecting the
Write operation.
void main()
{
P2=0X00;
rs=0;
rw=0;
en=0;
while(1)
{
lcdcommand (0x38); // command to for using 8-bit, 2 row mode of LCD
lcdcommand (0x0e); // command to turn display ON, for cursor blinking
Where DO is the LSB, D7 is the MSB for the inputs, and I ref is the input current that must be
applied to pin 14. The Iref current is generally set to 2.0 mA. Figure shows the generation of
current reference (setting Iref = 2 mA) by using the standard 5-V power supply and IK and
1.5K-ohm standard resistors.
Vout= 5V +5Vsinθ)*
Voltage Magnitude in (decimal)= Vout * 25.6
or
Vout= 128 +(128*sinθ)
#include <REG51.H>
void main()
{
static int a[13]={128,192,238,255,238,192,128,64,17,0,17,64,128};
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while (1)
{
for(i=0;i<13;i++) /* Output different values with 13 samples */
{
Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 6
P0 = a[i];
}
}
}
3. Write a c program to generate Square wave using DAC0808, also show an interface
diagram of DAC0808 with 8051.(LAB EXPERIMENT)
#include <REG51.H>
void main()
{
static int a[13]={255,255,255,255,255,0,0,0,0,0};
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while (1)
4. Write a c program to generate triangle wave using DAC0808, also show an interface
diagram of DAC0808 with 8051.(LAB EXPERIMENT)
Principle:
Current flowing through the coil of the relay creates a magnetic field which attracts a lever
and changes the switch contacts. The coil current can be on or off so relays have two switch
positions and most have double throw (changeover) switch contacts.
Relays are made up of electromagnet and a set of contacts generally based on Single Pole
Double Throw (SPDT) or Double Pole Double Throw (DPDT) switching method. It has 3
pins to perform function –
• COM = Common, always connect to NC; it is the moving part of the switch.
• NC = Normally Closed, COM is connected to this when the relay coil is off.
• NO = Normally Open, COM is connected to this when the relay coil is on.
Relays allow one circuit to switch a second circuit which can be completely separate from
the first. For example a low voltage battery circuit can use a relay to switch a 230V AC
mains circuit. There is no electrical connection inside the relay between the two circuits; the
link is magnetic and mechanical.
Usually electromagnets of stepper motor are energized using special controlling circuits, such
as microcontrollers.
Stepper Motors are classified into two, based on its winding arrangement.
1. Unipolar Motors
2. Bipolar Motors
Unipolar Motors
Unipolar Stepper Motor Windings- A unipolar motor contains centre tapped windings.
Usually centre connection of coils are tied together and used as the power connection. By
using this arrangement a magnetic poles can be reversed without reversing the direction of
current. Thus the commutation circuit can be made very simple. This ease of operation makes
Unipolar Motor popular among electronics hobbyists.
Bipolar Stepper Motor Windings- Bipolar motors have no center tap connections. Current
through a winding should be reversed to reverse the magnetic poles. So the driving circuit
should be more complicated. We can solve this by using a H-bridge connection or by using
readymade chips such as L293D. We can distinguish bipolar motors from Unipolar motors
by measuring the coil resistance. In bipolar motors we can find two wires with equal
resistance.
Unipolar stepper motors can be used in three modes namely the Wave Drive, Full Drive and
Half Drive mode. Each drive have its own advantages and disadvantages, thus we should
choose the required drive according to the application and power consumption.
Wave Drive
In this mode only one electromagnet is energized at a time. Generated torque will be less
when compared to full drive in which two electromagnets are energized at a time but power
consumption is reduced. It has same number of steps as in the full drive. This drive is
preferred when power consumption is more important than torque. It is rarely used.
Full Drive
In this mode two electromagnets are energized at a time, so the torque generated will be
larger when compared to Wave Drive. This drive is commonly used than others. Power
consumption will be higher than other modes.
Half Drive
In this mode alternatively one and two electromagnets are energized, so it is a combination of
Wave and Full drives. This mode is commonly used to increase the angular resolution of the
motor but the torque will be less, about 70% at its half step position. We can see that the
angular resolution doubles when using Half Drive.
void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
for(j=0;j<100;j++);
}
#include<reg51.h>
void delay(int);
void main()
{
unsigned char i;
void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
for(j=0;j<100;j++);
}
In some of the electronics projects you may want to control a DC Motor with 8051
microcontroller. The maximum current that can be sourced or sunk from a 8051
microcontroller is 15 mA at 5v. But a DC Motor need currents very much more than that and
it need voltages 6v, 12v, 24v etc, depending upon the type of motor used. Another problem is
that the back EMF produced by the motor may affect the proper functioning of the
microcontroller. Due to these reasons we can’t connect a DC Motor directly to a
microcontroller.
To overcome these problems you may use a H-Bridge using transistors. Freewheeling diodes
or Clamp diodes should be used to avoid problems due to back EMF. Thus it requires
transistors, diodes and resistors, which may make our circuit bulky and difficult to assembly.
T1 T2 MOTOR STATUS
1 0 CLOCKWISE
0 1 COUNTER CLOCKWISE
0 0 STOPS
1 1 STOPS
To overcome this problem the L293D driver IC is used. It is a Quadruple Half H-Bridge
driver and it solves the problem completely. You needn’t connect any transistors, resistors or
diodes. We can easily control the switching of L293D using a microcontroller. There are two
IC’s in this category L293D and L293. L239D can provide a maximum current of
600mA from 4.5V to 36V while L293 can provide up to 1A under the same input conditions.
All inputs of these ICs are TTL compatible and clamp diodes are provided with all outputs.
They are used with inductive loads such as relays solenoids, motors etc.
L293D contains four Half H Bridge drivers and are enabled in pairs. EN1 is used to enable
pair 1 (IN1-OUT1, IN2-OUT2) and EN2 is used to enable pair 2 (IN3-OUT3, IN4-OUT4).
9. Interface DC motor to 8051 along with driver circuit and also write a C program to
monitor the status of a switch( ‘SW’) which is connected to pin P3.3 and perform the
following:
i) If SW = 0, rotate DC motor clock wise
ii) If SW = 1, rotate DC motor counter clock wise.
void main()
{
motor_pin_1=0;
motor_pin_2=0;
SW=1;
while(1)
{
if(SW==0)
{
motor_pin_1 = 1; //Rotates Motor Clockwise
motor_pin_2 = 0;
delay();
}
else
{
motor_pin_1 = 0;
motor_pin_2 = 1; //Rotates Motor Anit Clockwise
delay();
}
}
void delay()
{
int i,j;
for(i=0;i<10;i++)
for(j=0;j<1000;j++);
}