Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
39 views47 pages

Unit 4

mpmc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views47 pages

Unit 4

mpmc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

UNIT - 4

LED (Light Emitting Diode)


LED is a semiconductor device used in many electronic devices, mostly used for signal
transmission /power indication purposes. It is very cheaply and easily available in a variety of
shape, color, and size. The LEDs are also used for design message display boards and traffic
control signal lights etc.

It has two terminals positive and negative as shown in the figure.

LED Polarity

LED Interfacing to 8051


There are two ways which we can interface LED to the Microcontroller 8051. But the
connections and programming techniques will be different.
Interfacing LED to 8051 Methods

Observe carefully the interface LED 2 is in forward biased because the input voltage of 5v
connected to the positive terminal of the LED, So here the Microcontroller pin should be at
LOW level. And vice versa with the interface 1 connections.

The resistor is important in LED interfacing to limit the flowing current and avoid damaging the
LED and/or MCU.

Interface 1 will glow LED, only if the PIN value of the MC is HIGH as current flows towards the
ground.

Interface 2 will glow LED, only if the PIN value of the MC is LOW as current flows towards
PIN due to its lower potential.

Example Program

#include<reg51.h>
sbit LED = P1^0;
void delay_ms(unsigned int ms_count)
{
unsigned int i, j;
for(i=0; i<ms_count; i++)
{
for(j=0; j<1275; j++);
}
}
void main()
{
while(1)
{
LED = 1;
delay_ms(100);
LED = 0;
delay_ms(100);
}
}
SWITCH INTERFACE
We can connect a switch to 8051 MCU using different configurations but the basic
functionality remains the same. Always remember this cool fact that the user is never concerned
about the actual implementation. He/she just wants something to turn ON when the switch is ON
and vice versa OFF.

GOOD - Safe to use

FAIR - Can be used without much


consideration
POOR - too much current to the MCU

These are the conventional methods of switch interface that you will find around in the internet. The
good news is that at least they work. There are certain configurations that you need to avoid and
must never use. For instance, have a look at these

VERY POOR - circuit is SHORTED when Switch is ON


SERIOUSLY? Circuit SHORTED again

Now how about this one that I am thinking of?

AWESOME - Safe to use


It looks quite simple and still works effectively. The tricky part is that turning ON the switch will
actually generate a LOW signal (connected to ground) but you can use that to your advantage as
discussed in the program code below.

SWITCH INTERFACE CODE

LCD INTERFACING:

Figure 5.1 16X2 LCD Module


 16×2 LCD module is a very common type of LCD module.
 It consists of 16 rows and 2 columns of 5×7 or 5×8 LCD dot matrices.
 It is available in a 16 pin package with back light, contrast adjustment
function and each dot matrix has 5×8 dot resolution.
 The pin numbers, their name and corresponding functions are shown in the
table 5.1.
Table 5.1 LCD Pin Description
Pin No: Name Function
1 VSS This pin must be connected to the
ground
2 VCC Positive supply voltage pin (5V DC)
3 VEE Contrast adjustment
4 RS Register selection
5 R/W Read or write
6 E Enable
7 DB0 Data
8 DB1 Data
9 DB2 Data
10 DB3 Data
11 DB4 Data
12 DB5 Data
13 DB6 Data
14 DB7 Data
15 LED+ Back light LED+
16 LED- Back light LED
VCC,VSS & VEE Pin:

 VCC and VSS provide +5V and Ground


 VEE pin is meant for adjusting the contrast of the LCD display and the contrast
can be adjusted by varying the voltage at this pin.
 This is done by connecting one end of a POT to the Vcc (5V), other end to the
Ground and connecting the center terminal (wiper) of of the POT to the VEE
pin. (Refer Figure 5.2)
RS:
 LCD has two built in registers namely data register and command register.
 Data register is for placing the data to be displayed, and the command
register is to place the commands.
 High logic at the RS pin will select the data register and Low logic at the
RS pin will select the command register.
 If we make the RS pin high and the put a data in the 8 bit data line (DB0 to
DB7), the LCD module will recognize it as a data to be displayed.
 If we make RS pin low and put a data on the data line, the module will
recognize it as a command.

R/W:
 R/W pin is meant for selecting between read and write modes.
 High level at this pin enables read mode and low level at this pin enables write
mode.
Enable (E):
 E pin is for enabling the module.
 The enable pin is used by the LCD to latch information presented to its data
pins.
 When data is supplied to data pins, a high to low pulse must be applied to
this pin in order for the LCD to latch in the data present at the data pins.
 This pulse must be a minimum of 450ns wide.
Data Pin:
 The 8-bit data pins, DB0 to DB7 are used to send information to the LCD
or read the contents of the LCD’s internal register.
 To display letters and numbers, send ASCII codes for the letters A-Z; a-z and
numbers 0-9 to these pins while making RS=1.
 There are also instruction command codes that can be sent to the LCD to
clear the display or force the cursor to the home position or blink the cursor.
 Table 5.2 Lists the instructions command codes.
Table 5.2 LCD Command Codes
CODE(Hexa Decimal) COMMAND
01 Clear display screen
02 Return Home
04 Decrement cursor (shift cursor to left)
05 Increment cursor (shift cursor to right)
06 shift display right
07 shift display left
08 Display off, cursor off
0A Display off, cursor on
0C Display on, cursor off
0E Display on, cursor blinking
0F Display on, cursor blinking
10 Shift cursor position to left
14 Shift cursor position to right
18 Shift the entire display to the left
1C Shift the entire display to the right
80 Force cursor to the beginning of 1st line
C0 Force cursor to the beginning of 2nd line
38 2 lines and 5 x 7 matrix

 We also use RS=0 to check the busy flag bit to see if the LCD is ready
to receive information’s.
 The busy flag id D7 and can be read when R/W=1 and RS=0, as follows:
if R/W=1, RS=0.
 When D7=1 (busy flag =1), the LCD is busy taking care of internal
operations and will not accept any new information.

LED+ & LED-:


 LED+ is the anode of the back light LED and this pin must be connected to
Vcc through a suitable series current limiting resistor.
 LED- is the cathode of the back light LED and this pin must be connected to
ground.
Figure 5.2 LCD Interfacing With 8051

LCD initialization
 The steps that has to be done for initializing the LCD display is given below
and these steps are common for almost all applications.
o Send 38H to the 8 bit data line for initialization
o Send 0FH for making LCD ON, cursor ON and cursor blinking ON.
o Send 06H for incrementing cursor position.
o Send 80H for displaying the character from 1st row and 1st column in
LCD
o Send 01H for clearing the display and return the cursor.

Sending data to the LCD.


 The steps for sending data to the LCD module is given below.
 It is the logic state of the pins (RS, R/W and E) that make the module to
determine whether a given data input is a command or data to be displayed.
o Make R/W low.
o Make RS=0 if data byte is a command and make RS=1 if the data
byte is a data to be displayed.
o Place data byte on the data register.
o Pulse E from high to low.
o Repeat above steps for sending another data
#include <reg51.h>
#define lcd_data P2
sbit rs=P0^0;
sbit rw=P0^1;
sbit en=P0^2;
void lcd_init();
void cmd(unsigned char a);
void dat(unsigned char b);
void show(unsigned char *s);
void lcd_delay();
void lcd_init()
{
cmd(0x38);
cmd(0x0e);
cmd(0x01);
cmd(0x06);
cmd(0x0c);
cmd(0x80);
}
void cmd(unsigned char a)
{
lcd_data=a;
rs=0;
rw=0;
en=1;
lcd_delay();
en=0;
}
void dat(unsigned char b)
{
lcd_data=b;
rs=1;
rw=0;
en=1;
lcd_delay();
en=0;
}
void show(unsigned char *s)
{
while(*s) {
dat(*s++);
}
}
void lcd_delay()
{
unsigned int lcd_delay;
for(lcd_delay=0;lcd_delay<=6000;lcd_delay++);
}
int main()
{
unsigned int j;
lcd_init();
while(1) {
cmd(0x80);
show(" Welcome To ");
cmd(0xc0);
show(" EMBETRONICX.COM");
for(j=0; j<30000; j++);
cmd(0x01);
for(j=0; j<30000; j++);
}
}

KEYBOARD INTERFACING:
 At the lowest level, keyboards are organized in a matrix of rows and columns.
 The CPU accesses both rows and columns through ports; therefore, with two
8-bit ports, an 8 x 8 matrix of keys can be connected to a microprocessor.
 When a key is pressed, a row and a column make a contact; otherwise,
there is no connection between rows and columns
Scanning and identifying the key
 Figure 5.3 shows a 4 x4 matrix connected to two ports.
 The rows are connected to an output port and the columns are connected
to an input port.
 If no key has been pressed, reading the input port will yield 1s for all
columns since they are all connected to high (Vcc).
 If all the rows are grounded and a key is pressed, one of the columns will
have 0 since the key pressed provides the path to ground.
 It is the function of the microcontroller to scan the keyboard continuously to
detect and identify the key pressed, How it is done is explained next.

Figure 5.3 Matrix Keyboard Connection to Ports


Grounding rows and reading the columns
 To detect a pressed key, the microcontroller grounds all rows by
providing 0 to the output latch, then it reads the columns.
 If the data read from the columns is D3 - D0 =1111, no key has been
pressed and the process continues until a key press is detected.
 However, if one of the column bits has a zero, this means that a key press has
occurred.
 For example, if D3 - D0 = 1101, this means that a key in the D1 column
has been pressed.
 After a key press is detected, the microcontroller will go through the
process of identifying the key.
 Starting with the top row, the microcontroller grounds it by providing a
low to row D0 only; then it reads the columns.
 If the data read is all 1s, no key in that row is activated and the process is
moved to the next row.
 It grounds the next row, reads the columns, and checks for any zero.
 This process continues until the row is identified.
 After identification of the row in which the key has been pressed, the next
task is to find out which column the pressed key belongs to.
 This should be easy since the microcontroller knows at any time which row
and column are being accessed.
 Given keyboard program is the 8051 Assembly language program for
detection and identification of key activation.
 In this program, it is assumed that P1 and P2 are initialized as output
and input, respectively.
 Program goes through the following four major stages:
o To make sure that the preceding key has been released, 0s are output
to all rows at once, and the columns are read and checked repeatedly
until all the columns are high. When all columns are found to be high,
the program waits for a short amount of time before it goes to the next
stage of waiting for a key to be pressed.
o To see if any key is pressed, the columns are scanned over and over in
an infinite loop until one of them has a 0 on it. Remember that the
output latches connected to rows still have their initial zeros (provided
in stage 1), making them grounded. After the key press detection, the
microcontroller waits 20 ms for the bounce and then scans the
columns again. This serves two functions: (a) it ensures that the first
key press detection was not an erroneous one due to a spike noise,
and (b) the 20-ms delay prevents the same key press from being
interpreted as a multiple key press. If after the 20-ms delay the key is
still pressed, it goes to the next stage to detect which row it belongs
to; otherwise, it goes back into the loop to detect a real key press.
o To detect which row the key press belongs to, the microcontroller
grounds one row at a time, reading the columns each time. If it finds
that all columns are high, this means that the key press cannot belong
to that row; therefore, it grounds the next row and continues until it
finds the row the key press belongs to. Upon finding the row that the
key press belongs to, it sets up the starting address for the look-up
table holding the scan codes (or the ASCII value) for that row and goes
to the next stage to identify the key.
o To identify the key press, the microcontroller rotates the column bits,
one bit at a time, into the carry flag and checks to see if it is low. Upon
finding the zero, it pulls out the ASCII code for that key from the look-
up table; otherwise, it increments the pointer to point to the next
element of the look-up table. Figure
5.4 flowcharts this process.
 While the key press detection is standard for all keyboards, the process for
determining which key is pressed varies.
 The look-up table method shown in keyboard Program can be modified to
work with any matrix up t0 8 x 8.
 Figure 5.4 provides the flowchart for keyboard interfacing Program for
scanning and identifying the pressed key.
Figure 5.4 Flowchart for Programming Keyboard Interfacing

#include <reg51.h>

sbit X1=P2^0;
sbit X2=P2^1;
sbit X3=P2^2;
sbit X4=P2^3;

sbit Y1=P2^4;
sbit Y2=P2^5;
sbit Y3=P2^6;
sbit Y4=P2^7;

unsigned char scankeys();


unsigned char decodekey(unsigned char key);
void delay();
unsigned char key;
void main()
{
//Initialize Serial
TH1=0xFD; //9600 Baud rate at 11.0592MHz
TMOD=0x20; //Timer 1 in 8-bit Auto relaod mode
SCON=0x50; //REN=1 and Serial in 8-bit UART mode 1
TR1=1; //Start Timer

while(1)
{
key=scankeys(); //Get which key is pressed
if(key != 0) //If key is pressed then only send it to
serial
{
SBUF=key;
while(TI==0); //Wait until serial transmission completes
TI=0;
}
}

unsigned char scankeys()


{
key=0;

//Keep X1 High and check status of Y


P2=0xFE;
if(P2 != 0xFE)
{key=P2;}

//Keep X2 High and check status of Y


P2=0xFD;
if(P2 != 0xFD)
{key=P2;}

//Keep X3 High and check status of Y


P2=0xFB;
if(P2 != 0xFB)
{key=P2;}
//Keep X5 High and check status of Y
P2=0xF7;
if(P2 != 0xF7)
{key=P2;}

if(key !=0)
{key=decodekey(key);} //Send the hex value to get ASCII code

delay(); //Delay for key


return key;
}

void delay()
{
int i;
for(i=0;i<10000;i++); //Adjust delay for key repetition rate
}

unsigned char decodekey(unsigned char key)


{
unsigned char dkey;
if(key==0xEE) {dkey = '1';} //ASCII Codes for keys
if(key==0xDE) {dkey = '2';}
if(key==0xBE) {dkey = '3';}
if(key==0x7E) {dkey = '4';}

if(key==0xED) {dkey = '5';}


if(key==0xDD) {dkey = '6';}
if(key==0xBD) {dkey = '7';}
if(key==0x7D) {dkey = '8';}

if(key==0xEB) {dkey = '9';}


if(key==0xDB) {dkey = '0';}
if(key==0xBB) {dkey = 'A';}
if(key==0x7B) {dkey = 'B';}

if(key==0xE7) {dkey = 'C';}


if(key==0xD7) {dkey = 'D';}
if(key==0xB7) {dkey = 'E';}
if(key==0x77) {dkey = 'F';}

return dkey;
}

ANALOG-TO-DIGITAL CONVERTER (ADC) INTERFACING:


 ADCs (analog-to-digital converters)are among the most widely used
devices for data acquisition.
 A physical quantity, like temperature, pressure, humidity, and velocity, etc.,
is converted to electrical (voltage, current)signals using a device called a
transducer or sensor
 We need an analog-to-digital converter to translate the analog signals to
digital numbers, so microcontroller can read and process them.
 An ADC has n-bit resolution where n can be 8, 10, 12, 16 or even 24 bits.
 The higher-resolution ADC provides a smaller step size, where step size is
the smallest change that can be discerned by an ADC. This is shown in table
5.3
Table 5.3 Resolution Vs Step Size for ADC

 In addition to resolution, conversion time is another major factor in judging an


ADC.
 Conversion time is defined as the time it takes the ADC to convert the
analog input to a digital (binary) number.
 The ADC chips are either parallel or serial.
 In parallel ADC, we have 8 of more pins dedicated to bringing out the binary
data, but in serial ADC we have only one pin for data out.
ADC804 chip:
 ADC804 IC is an 8-bit parallel analog-to-digital converter.
 It works with +5 volts and has a resolution of 8bits.
 In ADC804 conversion time varies depending on the clocking signals applied
to the CLK R and CLK IN pins, but it cannot be faster than 110μs.
 Figure 5.5 Pin out of ADC0804 in free running mode.
 The following is the ADC0804 pin description.

Figure 5.5 ADC0804 Chip (Testing ADC0804 in Free Running Mode)


 CLK IN and CLK R:
 CLK IN is an input pin connected to an external clock source when an
external clock is used for timing.
 However, the 0804 has an internal clock generator.
 To use the internal clock generator (also called self-clocking), CLK IN and
CLK R pins are connected to a capacitor and a resistor and the clock
frequency is determined by:

 Typical values are R = 10K ohms and C =150pF.


 By substituting, we get f = 606 kHz and the conversion time is 110μs.
 Vref/2: (Pin 9)
 It is used for the reference voltage.
 If this pin is open (not connected), the analog input voltage is in the
range of 0 to 5 volts (the same as the Vcc pin).
 If the analog input range needs to be 0 to 4 volts, Vref/2 is connected to 2
volts.
 Table 5.4 shows the Vin range for various Vref/2 inputs.
Table 5.4 Vref/2 Relation to Vin Range (ADC0804)

 D0-D7:
 D0-D7 are the digital data output pins.
 These are tri-state buffered and the converted data is accessed only when
CS =0 and RD is forced low.
 To calculate the output voltage, use the following formula

o Dout = digital data output (in decimal),


o Vin = analog voltage, and
o Step size (resolution) is the smallest change, which is (2 *
Vref/2)/256 for ADC 0804
 Analog ground and digital ground:
 Analog ground is connected to the ground of the analog Vin and digital
ground is connected to the ground of the Vcc pin.
 The reason that to have ground pin is to isolate the analog Vin signal from
transient voltages caused by digital switching of the output D0 – D7. This
contributes to the accuracy of the digital data output.
 Vin(+) & Vin(-):
 Differential analog inputs where Vin= Vin (+) – Vin (-).
 Vin (-) is connected to ground and Vin(+) is used as the analog input to
be converted.
 RD:
 This is an input signal and is active low.
 The ADC converts the analog input to its binary equivalent and holds it in
an internal register.
 RD is, used to get the converted data out of the ADC0804 chip.
 Is “output enable” a high-to-low RD pulse is used to get the 8-bit
converted data out of ADC804.
 INTR:
 This is an output pin and is active low.
 It is “end of conversion” When the conversion is finished, it goes low to
signal the CPU that the converted data is ready to be picked up.
 WR:
 This is an active low input
 It is “start conversion” When WR makes a low-to-high transition, ADC804
starts converting the analog input value of Vin to an 8-bit digital number.
 When the data conversion is complete, the INTR pin is forced low by the
ADC0804.
 CS:
 It is an active low input used to activate ADC804.
 Steps to Be followed For Data Conversion:
 The following steps must be followed for data conversion by the ADC804
chip:
o Make CS= 0 and send a L-to-H pulse to pin WR to start conversion.
o Monitor the INTR pin, if high keep polling but if low, conversion is
complete, go to next step.
o Make CS= 0 and send a H-to-L pulse to pin RD to get the data out.
 Figure 5.6 shows the timing diagram for ADC process.

Figure 5.6 Read and Write Timing for ADC08804

Clock source for ADC0804:


 The speed at which an analog input is converted to the digital output
depends on the speed of the CLK input.
 According to the ADC0804 datasheets, the typical operating frequency is
approximately 640kHz at 5 volts.
 Figures 5.7 and 5.8 show two ways of providing clock to the ADC0804.
 In Figure 5.8, notice that the clock in for the ADC0804 is coming from the
crystal of the microcontroller.
 Since this frequency is too high, we use D flip-flops (74LS74) to divide the

A single D flip-flop divides the frequency by 2 if we connect its 𝑄̅ to the D


frequency.

input.
 For a higher-frequency crystal, you can use 4 flip-flops

Figure 5.7 8051 Connection to ADC0804 with Self-Clocking

Figure 5.8 8051 Connection to ADC0804 with Clock from XTAL2 of the 8051

ADC0808:
 While the ADC0804 has only one analog input, this chip has 8 of them.
 The ADC0808/0809 chip allows us to monitor up to 8 different analog inputs
using only a single chip.
 Notice that the ADC0808/0809 has an 8-bit data output just like the ADC804.
 The 8 analog input channels are multiplexed and selected according to
Table 5.5 using three address pins, A, B, and C.
Table 5.5 Channel Selection in ADC0808

 In the ADC0808/0809, Vref (+) and Vref.(-) set the reference voltage.
 If Vref(-) = Gnd and Vref (+) = 5 V, the step size is 5 V/256 = 19.53 mV.
 Therefore, to get a l0 mV step size we need to set Vref (+) = 2.56 V and Vref.
(-) = Gnd.
 From Figure 5.9, notice the ALE pin.
 We use A, B, and C addresses to select.IN0 - IN7, and activate ALE to
latch in the address.
 SC is for start conversion.
 SC is the same as the WR pin in other ADC chips.
 EOC is for end-of-conversion, and OE is for output enable (READ).
 The EOC and OE are the same as the INTR and RD pins respectively.
 Table 5.6 shows the step size relation to the Vref voltage.
 Notice that there is no Vref/2 in the ADC0808/0809 chip.

Figure 5.9 ADC0808/0809

Table 5.6 ADC0808/0809 Analog Channel Selection


Steps to program the ADC0808/0809
 The following are steps ro get data from an ADC0808/0809.
o Select an analog channel by providing bits to A, B, and C addresses
according to Table 5.6.
o Activate the ALE (address latch enable) pin. It needs an L-to-H pulse
to latch in the address.
o Activate SC (start conversion) by an L-to-H pulse to initiate conversion.
o Monitor EOC (end of conversion) to see whether conversion is finished.
H-to- L output indicates that the data is converted and is ready to be
picked up. If we do not use EOC, we can read the converted digital
data after a brief time delay. The delay size depends on the speed of
the external clock we connect to the CLK pin. Notice that the EOC is
the same as the INTR pin in other ADC chips.
o Activate OE (output enable) to read data out of the ADC chip. An L-to H
pulse to the OE pin will bring digital data out of the chip. Also notice
that the OE is "the same as the RD pin in other ADC chips.
 The speed of conversion depends on the frequency of the clock connected to
the CLK pin, it cannot be faster than 100 microseconds
SENSOR INTERFACING:
LM35 Temperature sensors:
 The LM35 series sensors are precision integrated-circuit temperature
sensors whose output voltage is linearly proportional to the celsius
(centigrade) temperature.
 The LM35 requires no external calibration since it is internally calibrated.
 It outputs 10mV for each degree of centigrade temperature.
 Table 5.7 is the selection guide for the LM35
Table 5.7 LM35 Temperature Sensor Series Selection Guide

 The sensors of the LM34 series are precision integrated-circuit


temperature sensors whose output voltage is linearly proportional to the
Fahrenheit temperature.
 It also internally calibrated.
 It outputs 10mV for each degree Fahrenheit temperature.
Signal Conditioning and Interfacing the LM35 to the 8051

Figure 5.10 Getting Data from Analog World

 The above figure 5.10 shows the steps involved in acquiring data from analog
world.
 Signal conditioning is widely used in the world of data acquisition.
 The most common transducers produce an output in the form of voltage,
current, charge, capacitance, and resistance.
 However, we need to convert these signals to voltage in order to send input
to an A-to-D converter.
 This conversion (modification) is commonly called signal conditioning.
 Signal conditioning can be a current-to-voltage conversion or a signal
amplification.
 For example, the thermistor changes resistance with temperature.
 The change of resistance must be translated into voltages in order to be of
any use to an ADC.
 Look at the case of connecting an LM35 to an ADC0848.
 Since the ADC0848 has 8-bit resolution with a maximum of 256 (2 8) steps and
the LM35 (or LM34) produces l0 mV for every degree of temperature change,
we can condition Vin of the ADC0848 to produce a Vout, of 2560 mV (2.56 V)
for full-scale output.
 Therefore, in order to produce the full-scale Vout of 2.56 V for the ADC0848,
we need to set Vref = 2.56.
 This makes Vout, of the ADC0848 correspond directly to the temperature as
monitored by the LM35. Refer the table 5.8
Table 5.8 Temperature vs. Vout for ADC0848

 Figure 5.11 shows the connection of a temperature sensor to the ADC0848.


 The LM336-2.5 zener diode to fix the voltage across the 10K pot at 2.5V.
 The use of the LM336-2.5 should overcome any fluctuations in the power

supply.
Figure 5.11 8051 Connection to ADC0848 and Temperature sensor
Progra
m: RD BIT P2.5 ;RD
WR BIT P2.6 ;WR
INTR BIT P2.7 ; END OF CONVERSION
MYDATA EQU P1 ; P1.0-P1.7 = D0-D7 OF THE ADC0848
MOV P1,#0FFH ;make P1 =
input SETB INTR
BACK: CLR WR ;WR = 0
SETB WR ;WR = 1 L-to-H to start
conversion HERE: JB INTR,HERE ;wait
for end of conversion
CLR RD ;conversion finished, enable
RD MOV A,MYDATA ;read the data
ACALL CONVERSION ;hex-to-ASCII
conversion ACALL DATA_DISPLAY ;display
the data
SETB RD ;make RD=1 for next
round SJMP BACK

CONVERSION:
MOV
B,#10 DIV
AB MOV
R7,B MOV
B,#10 DIV
AB MOV
R6,B MOV
R5,A RET

DATA_DISPLAY:
MOV P0,R7
ACALL DELAY
MOV P0,R6
ACALL DELAY
MOV P0,R5
ACALL DELAY
RET

DIGITAL-TO-ANALOG (DAC) CONVERTER:


 The DAC is a device widely used to convert digital pulses to analog signals.
 In this section we will discuss the basics of interfacing a DAC to 8051.
 The two method of creating a DAC is binary weighted and R/2R ladder.
 The Binary Weighted DAC, which contains one resistor or current source for
each bit of the DAC connected to a summing point.
 These precise voltages or currents sum to the correct output value.
 This is one of the fastest conversion methods but suffers from poor accuracy
because of the high precision required for each individual voltage or current.
 Such high-precision resistors and current-sources are expensive, so this
type of converter is usually limited to 8-bit resolution or less.
 The R-2R ladder DAC, which is a binary weighted DAC that uses a repeating
cascaded structure of resistor values R and 2R.
 This improves the precision due to the relative ease of producing equal
valued matched resistors (or current sources).
 However, wide converters perform slowly due to increasingly large RC-
constants for each added R-2R link.
 The first criterion for judging a DAC is its resolution, which is a function of the
number of binary inputs.
 The common ones are 8, 10, and 12 bits.
 The number of data bit inputs decides the resolution of the DAC since the
number of analog output levels is equal to 2n, where n is the number of data
bit inputs.
 Therefore, an 8-input DAC such as the DAC0808 provides 256 discrete
voltage (or current) levels of output.
 Similarly, the 12-bit DAC provides 4096 discrete voltage levels.
 There also 16-bit DACs, but they are more expensive.

DAC0808:
 The digital inputs are converter to current (Iout), and by connecting a
resistor to the Iout pin, we can convert the result to voltage.
 The total current provided by the Iout pin is a function of the binary numbers
at the D0-D7 inputs of the DAC0808 and the reference current (Iref), and is as
follows

 Usually reference current is 2mA.


 Ideally we connect the output pin to a resistor, convert this current to
voltage, and monitor the output on the scope.
 But this can cause inaccuracy; hence an opamp is used to convert the
output current to voltage.
 The 8051 connection to DAC0808is as shown in the below figure 5.12.
 Now assuming that Iref = 2mA, if all the inputs to the DAC are high, the
maximum output current is 1.99mA.
Figure 5.12 8051 Connection to DAC808
Example 1:
Assuming that R=5K and Iref=2mA, calculate Vout for the following
binary inputs: (a) 10011001B
(b) 11001000B
Solution:
(a) Iout = 2mA(153/256) = 1.195mA and Vout = 1.195mA * 5K
=5.975V (b) Iout = 2mA(200/256) = 1.562mA and Vout =
1.562mA * 5K =7.8125V

Converting Iout to voltage in DAC0808:


 Ideally we connect the output pin lout, to a resistor, convert this current to
voltage, and monitor the output on the scope.
 In real life, however, this can cause inaccuracy since the input resistance
of the load where it is connected will also affect the output voltage.
 For this reason, the lref current output is isolated by connecting it to an op-
amp such as the 741 with Rf = 5K ohms for the feedback resistor.
 Assuming that R= 5K ohms, by changing the binary input, the output voltage
changes as shown in Example 2.

Example 2:
Inorder to generate a stair-step ramp, set up the circuit in figure 5.12 and
connect the output to an oscilloscope. Then write a program to send data to
the DAC to generate a stair-step ramp.
Solution:
CLR A
AGAIN: MOV P1,A ; SEND DATA TO DAC
INC A ; COUNT FROM 0 TO
FFH ACALL DELAY ; LET DAC RECOVER
SJMP AGAIN
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.
 The values for the sine function vary from -1.0 to +1.0 for 0- to 360-degree
angles.
 Therefore, the table values are integer numbers representing the voltage
magnitude for the sine of theta.
 This method ensures that only integer numbers are output to the DAC
by the 805l microcontroller.
 Table 5.9 shows the angles, the sine values, the voltage magnitudes, and
the integer values representing the voltage magnitude for each angle (with
30-degree increments).
 To generate Table 5.9,we assumed the full-scale voltage of 10 V for DAC
output (as designed in Example 4 Figure).
 Full-scale output of the DAC is achieved when all the data inputs of the DAc
are high.
 Therefore, to achieve the full-scale 10 V output, we use the following equation
Vout= 5V(1+sinθ)
 Vout of DAC for various angles is calculated and shown in Table 5.9. See
Example 3 for verification of the calculations
Table 5.9 Angle Vs Voltage Magnitude for Sine Wave

Example 3:
Verify the values given for the following angles: (a) 30º (b)
60º Solution:
(a) Vout = 5V+(5V * sin30) =7.5V
DAC input values = 7.5V * 25.6 = 192 (Decimal)
(b) Vout = 5V+(5V * sin60) =9.33V
DAC input values = 9.33V * 25.6 = 238 (Decimal)
 To find the values sent to the DAC for various angles, we simply multiply Vout
voltage by
25.6 because there are 256 steps and full scale Vout is
10 volts. 256 steps/10V = 25.6 steps per volt
 The following examples 9, 10 and 11 will show the generation of
waveforms using DAC0808.

Example 4:
Write an ALP to generate a sine waveform.
Vout= 5V(1+sinθ)
Solution:
Calculate the decimal values for every 10 degree of the sine wave. These
values can be maintained in a table and simply the values can be sent to port
P1. The sine wave can be observed on the CRO.

Example 5:
Write an ALP to generate a triangular waveform.
Stepper Motor Interfacing:
Stepper motor is a widely used device that translates electrical pulses into
mechanical movement. Stepper motor is used in applications such as; disk
drives, dot matrix printer, robotics etc,. The construction of the motor is as
shown in figure 1 below.

Figure 1: Structure of stepper motor

It has a permanent magnet rotor called the shaft which is surrounded by a


stator. Commonly used stepper motors have four stator windings that are paired
with a center – tapped common. Such motors are called as four-phase or
unipolar stepper motor.
The stator is a magnet over which the electric coil is wound. One end of the coil
are connected commonly either to ground or +5V. The other end is
provided with a fixed
sequence such that the motor rotates in a particular direction. Stepper motor
shaft moves in a fixed repeatable increment, which allows one to move it to a
precise position. Direction of the rotation is dictated by the stator poles. Stator
poles are determined by the current sent through the wire coils.

Step angle:
Step angle is defined as the minimum degree of rotation with a
single step. No of steps per revolution = 360° / step angle
Steps per second = (rpm x steps per
revolution) / 60 Example: step angle = 2°
No of steps per revolution = 180

Switching Sequence of Motor:


As discussed earlier the coils need to be energized for the rotation. This can be
done by sending a bits sequence to one end of the coil while the other end is
commonly connected. The bit sequence sent can make either one phase ON or
two phase ON for a full step sequence or it can be a combination of one and
two phase ON for half step sequence. Both are tabulated below.

Full Step:
Two Phase ON

One Phase ON

Half Step (8 – sequence):

The sequence is tabulated as below:


8051 Connection to Stepper Motor: (explanation of the diagram can be done)

Figure 2: 8051 interface to stepper motor

The following example 1 to example 6 shown below will elaborate on the


discussion done above:

Example 1: Write an ALP to rotate the stepper motor clockwise /


anticlockwise continuously with full step sequence.

Program:
MOV
A,#66H BACK:
MOV P1,A
RR A
ACALL DELAY
SJMP BACK

DELAY: MOV
R1,#100 UP1:
MOV R2,#50
UP: DJNZ R2,UP
DJNZ R1,UP1
RET
Note: motor to rotate in anticlockwise use instruction RL A instead of RR A
Example 2: A switch is connected to pin P2.7. Write an ALP to monitor
the status of the SW. If SW = 0, motor moves clockwise and if SW = 1,
motor moves anticlockwise.
Program:
ORG 0000H
SETB P2.7
MOV A,
#66H MOV
P1,A
TURN: JNB P2.7, CW
RL A
ACALL DELAY
MOV P1,A
SJMP TURN
CW: RR A
ACALL DELAY
MOV P1,A
SJMP TURN
DELAY: as previous example

Example 3: Write an ALP to rotate a motor 90° clockwise. Step angle


of motor is 2°.

Solution:
Step angle = 2°
Steps per revolution =
180 No of rotor teeth
= 45
For 90° rotation the no of steps is 45

Program:
ORG 0000H
MOV A,
#66H MOV
R0, #45
BACK: RR A
MOV P1, A
ACALL DELAY
DJNZ R0, BACK
END
Example 4: Rotate the stepper motor continuously clockwise using half-
step 8-step sequence. Say the sequence is in ROM locations.

Program:
ORG 0000H
START: MOV R0,
#08
MOV DPTR, #HALFSTEP
RPT: CLR A
MOVC A, @A+DPTR
MOV P1, A
ACALL DELAY
INC DPTR
DJNZ R0, RPT
SJMP START
ORG 0200H
HALFSTEP DB 09, 08, 0CH, 04, 06, 02, 03,
01 END

Programming Stepper Motor with 8051 C


The following examples 5 and 6 will show the programming of stepper motor using

Example 5: Problem definition is same as example 1.

Program:
#include
<reg51.h> void
main ()
{
while (1)
{
P1=0x66;
MSDELAY (200);
P1=0x33;
MSDELAY (200);
P1=0x99;
MSDELAY (200);
P1=0xCC;
MSDELAY (200);
}
}
void MSDELAY (unsigned char value)
{
unsigned int x,y;
for(x=0;x<1275;x++)
for(y=0;y<value;y++);
}
8051 C.
Example 6: Problem definition is same as example 2.

Program:
#include
<reg51.h> sbit
SW=P2^7; void
main ()
{
SW=1;
while (1)
{
if(SW==0){
P1=0x66;
MSDELAY (100);
P1=0x33;
MSDELAY (100);
P1=0x99;
MSDELAY (100);
P1=0xCC;
MSDELAY (100);
}
else {
P1=0x66;
MSDELAY (100);
P1=0xCC;
MSDELAY (100);
P1=0x99;
MSDELAY (100);
P1=0x33;
MSDELAY (100);

}
void MSDELAY (unsigned char value)
{
unsigned int x,y;
for(x=0;x<1275;x++)
for(y=0;y<value;y++);
}

SEVEN SEGMENT DISPLAY


A display consisting of seven LEDs arranged in seven segments is called
seven segment display. It is shown in the Fig. The seven LEDs are
arranged in a
rectangular fashion and are labeled A through G. Each LED is called a segment
because it forms a part of the digit being displayed. An additional LED is used for
the indication of a decimal point (DP).

By forward biasing different LEDs we can display the digits 0 through 9. For example,
to display a zero, the LEDs A, B, C, D, E and F are forward biased. To light up a 5,
we need to forward bias segments A, F, C, C, D. Thus in a seven segment display
depending upon the digit to be displayed, the particular set of LEDs is forward
biased. The various digits from 0 to 9 which can be displayed using seven segment
display are shown in the Fig.
Types of Seven Segment Display

The two types of seven segment display are available called,

1) Common anode type

2) Common cathode

type Common

Anode Type

In this type, all anodes of LEDs are connected together and common point is
connected to + V which is positive supply voltage. A current limiting resistor
is required i be connected between each LED and ground.

Common Cathode Type

In this type, all cathodes of LEDs are connected together and common point
is connected to the ground. A current limiting resistor is connected between
each
LED and the supply + Vcc. The anodes of the respective segments are to be
connected to + for the required operation of LEDs.
Number g fedcba Hex code

0 1000000 C0

1 1111001 F9

2 0100100 A4

3 0110000 B0

4 0011001 99

5 0010010 92

6 0000010 82

7 1111000 F8

8 0000000 80

9 0010000 90

Table: Display numbers on a seven-segment display in common


anode configuration

Things change for common cathode configuration.


Number gfedcba Hex Code

0 0111111 3F

1 0000110 06

2 1011011 5B

3 1001111 4F

4 1100110 66

5 1101101 6D

6 1111101 7D

7 0000111 07

8 1111111 7F

9 1001111 4F

CIRCUIT DIAGRAM OF COMMON ANODE 7 SEGMENT DISPLAY WITH 8051


#include<reg51.h>

void main()

while(1)

P1= 0x3f;

delay(); P1=0x06;

delay(); P1=0x5b;

delay(); P1=0x4f;

delay(); P1=0x66;

delay(); P1=0x6d;
delay(); P1=0x7d;

delay(); P1=0x07;

delay(); P1=0x7f;

delay(); P1=0x6f;

delay();

void delay()

unsigned int i,j;

for(i=0;i<100;i++)

for(j=0;j<1000;j++);

You might also like