Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Unit – 5 8 hours

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

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 1


15 A/LED+ Back-light Anode(+)
16 K/LED- Back-Light Cathode(-)
• Apart from the voltage supply connections the important pins from the programming
perspective are the data lines (8-bit Data bus), Register select, Read/Write and Enable
pin.
Data Bus:
• As shown in the above figure and table, an alpha numeric LCD has a 8-bit data bus
referenced as D0-D7.
• As it is a 8-bit data bus, we can send the data/command to LCD in bytes.

Register Select (RS):


The LCD has two register namely a Data register and Command register.
• Any data that needs to be displayed on the LCD has to be written to the data register
of LCD.
• Command can be issued to LCD by writing it to Command register of LCD.
• This signal is used to differentiate the data/command received by the LCD.
• If the RS signal is LOW then the LCD interprets the 8-bit info as Command and
writes it Command register and performs the action as per the command.
• If the RS signal is HIGH then the LCD interprets the 8-bit info as data and copies it to
data register.
RS MODE
0 Command register
1 data register
Read/Write (RW):
• This signal is used to write the data/command to LCD and reads the busy flag of
LCD.
• For write operation the RW should be LOW(0) and
• For read operation the RW should be HIGH (1).

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.

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 2


• A HIGH-to-LOW pulse has to be send on this enable pin, which will latch the info
into the LCD register and triggers the LCD to act accordingly.

16x2 LCD interface to 8051 microcontroller

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 3


C program to display “JAIN” on line 1 and “UNIVERSITY” on line 2.
(LAB EXPERIMENT)
#include<reg51.h>
sbit rs=P3^0;
sbit rw=P3^1;
sbit en=P3^6;

void delay(unsigned int msec)


{
int i,j;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}

void lcdcommand (unsigned char c)


{
P2=c;
rs=0;
rw=0;
en=1;
delay(1);
en=0;
}

void lcddata (unsigned char d)


{
P2=d;
rs=1;
rw=0;
en=1;
delay(1);
en=0;
}

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

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 4


lcdcommand (0x01); // command to clear screen
lcdcommand (0x06);// command to shift display right
lcdcommand (0x86); // command to bring cursor to position 6 of line 1
delay(100);
lcddata('J');
lcddata('A');
lcddata('I');
lcddata('N');
lcdcommand (0xC2); // command to bring cursor to position 2 of line 2
lcddata('U');
lcddata('N');
lcddata('I');
lcddata('V');
lcddata('E');
lcddata('R');
lcddata('S');
lcddata('I');
lcddata('T');
lcddata('Y');
}
}

DAC (Digital to Analog Converter)


The digital-to-analog converter (DAC) is a device widely used to convert digital pulses to
analog signals.
In the DAC0808, it’s a 8 bit DAC, the digital inputs are converted to current (Iout), and by
connecting a resistor to the Iout pin, we convert the result to voltage.
The total current provided by the Iout pin is a function of the binary numbers at the DO – D7
inputs of the DAC0808 and the reference current (I ref), and is as follows:

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.

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 5


2. Write a c program to generate Sine wave using DAC0808 with minimum 10 steps, also
show an interface diagram of DAC0808 with 8051.(LAB EXPERIMENT)
Generating a sine wave:
• To generate a sine wave, we first need a table whose values represent the magnitude
of the sine of angles between 0 and 360 degrees.
Algorithm for Sine wave
• Compute different step values (θ = 00,15o, 30o, 45o,60o …360o) of sine using the
equation

Vout= 5V +5Vsinθ)*
Voltage Magnitude in (decimal)= Vout * 25.6
or
Vout= 128 +(128*sinθ)

• Output the values through P0.


• More the steps smoother will be sine wave.
• E.g.: θ = 0o
• Vout= 128 +128*sin0 = 128

#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];
}
}
}

Interface diagram of DAC0808 with 8051

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)

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 7


{
for(i=0;i<10;i++) /* Output different values with 10 samples*/
{
P0 = a[i];
}
}
}

4. Write a c program to generate triangle wave using DAC0808, also show an interface
diagram of DAC0808 with 8051.(LAB EXPERIMENT)

Algorithm for Triangular wave generation


1. Output the initial value 00 through P0.
2. Increment it in steps of 1 until a count value reaches of FFh (5V).
3. Decrement it in steps of 1 until a zero value is reached and repeat step 1.

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 8


#include <REG51xD2.H>
void main()
{
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while(1)
{
for(i=0;i<0xff;i++) // Increment it in steps of 1 until a count value reaches of FFh (5V).
{
P0 = i;
}
for(i=0xfe;i>0x00;i--) // Decrement it in steps of 1 until a zero value is reached
{
P0 = i;
}
}
}

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 9


5. Write a c program to generate Up-Ramp using DAC0808, also show an interface diagram
of DAC0808 with 8051.(LAB EXPERIMENT)

Algorithm for Up-Ramp wave generation


1. Output the initial value 00 through P0.
2. Increment it in steps of 1 until a count value reaches of FFh (5V).
#include <REG51xD2.H>
void main()
{
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while(1)
{
for(i=0;i<0xff;i++)// Increment it in steps of 1 until a count value reaches of FFh (5V).
{
P0 = i;
}
}
}

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 10


6. Write a c program to generate Down-Ramp using DAC0808, also show an interface
diagram of DAC0808 with 8051.(LAB EXPERIMENT)

Algorithm for Down-Ramp wave generation


1. Output the initial value FF through P0.
2. Decrement it in steps of 1 until a zero value is reached
#include <REG51xD2.H>
void main()
{
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while(1)
{
for(i=0xFF;i>0x00;i--) //// Decrement 0Xff in steps of 1 until a zero value is reached
{
P0 = i;
}
}
}

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 11


Electromechanical Relays
A relay is an electrically operated switch or an electromechanical switch. Relays are used
where it is necessary to control an electronic circuit by a low-power signal or where several
circuits must be controlled by one signal.

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.

Construction and working:

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.

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 12


Code:
#include<REG51.h>
Sbit SW=P3^3;
sbit RELAY1=P2^0;
void main()
{
int j;
RELAY1=0;
SW=1;
while(1)
{
if(SW==1)
{
RELAY1=1;
}
else
{
RELAY1=0;
}
}
}

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 13


Stepper motor

It is a brushless, synchronous DC electric motor. It has many applications in the field


of robotics and mechatronics, which divides the full rotation into a number of equal steps. It
is also known as Step Motor. For example, in the case of a 200 step motor, one complete
rotation (360°) is divided in to 200 steps, which means one step is equal to 1.8°. It can take
only one step at a time and each steps are equal. The exact position of a stepper motor can be
controlled without using any feedback. They are available with steps 200, 180, 144, 72, 24
and 12 which results in stepping angles 1.8°, 2°, 2.5°, 5°, 15° and 30° respectively.

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.

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 14


Bipolar Motors

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.

Driving Unipolar Stepper Motor with 8051

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.

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 15


Wave Drive Stepping Sequence
Step A B C D
1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1

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.

Full Drive Stepping Sequence


Step A B C D
1 1 1 0 0
2 0 1 1 0
3 0 0 1 1
4 1 0 0 1

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.

Half Drive Stepping Sequence


Step A B C D
1 1 0 0 0
2 1 1 0 0
3 0 1 0 0
4 0 1 1 0
5 0 0 1 0
6 0 0 1 1
7 0 0 0 1
7. Write a C program to rotate Bipolar Stepper Motor to rotate clockwise
Continuously, with 8051 Using L293D driver IC
Bipolar stepper motors have no centre tap and having equal coil resistances. It can be easily
interfaced with a microcontroller using L293D DC Motor Driver IC.

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 16


#include<reg51.h>
void delay(int);
void main()
{
P2=0X00;
while(1)
{
P2=0x01; //0001
delay(1000);
P2=0x04; //0100
delay(1000);
P2=0x02; //0010
delay(1000);
P2=0x08; //1000
delay(1000);
}
}

void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
for(j=0;j<100;j++);
}

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 17


8. Show an interface diagram of stepper 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, the stepper motor rotates clock wise
ii) If SW = 1, the stepper motor rotates counter clock wise.
(LAB EXPERIMENT)
#include<reg51.h>
sbit SW=P3^3;
void delay(unsigned char k);
void main()
{
P0=0X00 ;
SW=1;
while(1)
{
if(SW==0)
{
P0=0x01;
delay(100);
P0=0x02;
delay(100);
P0=0x04;
delay(100);
P0=0x08;
delay(100);
}
else
{
P0=0x08;
delay(100);
P0=0x04;
delay(100);
P0=0x02;
delay(100);
P0=0x01;
delay(100);
}
}
}

void delay(unsigned char k)


{

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 18


int i,j;
for(i=0;i<k;i++)
for(j=0;j<5;j++);
}

#include<reg51.h>
void delay(int);
void main()
{
unsigned char i;

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 19


P0=0X00;
for(i=0;i<=24;i++)
{
P0=0x01; //0001
delay(1000);
P0=0x04; //0100
delay(1000);
P0=0x02; //0010
delay(1000);
P0=0x08; //1000
delay(1000);
}
}

void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
for(j=0;j<100;j++);
}

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 20


DC motor

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).

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 21


We can drive two DC Motors using one L293D, but here we are using only one. You can
connect second DC Motor to driver pair 2 according to your needs.

Interfacing DC Motor with 8051 using L293D


The DC Motor is connected to the first pair of drivers and it is enabled by connecting EN1 to
logic HIGH (5V). VSS pin is used to provide logic voltage to L293D. Here 8051
microcontroller, which works at 5v is used to control L293D, hence the logic voltage is 5.
The motor supply is given to Vs pin of the L293D.

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.

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 22


#include<reg51.h>
void delay(void);
sbit SW=P3^3;
sbit motor_pin_1 = P0^0;
sbit motor_pin_2 = P0^1;

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++);
}

Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 23


Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 24
Sunil M P, Assistant Professor, Department of ECE, School of Electrical Engineering 25

You might also like