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

0% found this document useful (0 votes)
5 views51 pages

Module 4 - Updated

Uploaded by

Nirmala Y N
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)
5 views51 pages

Module 4 - Updated

Uploaded by

Nirmala Y N
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/ 51

MICROCONTROLLERS MODULE 4

DEPARTMENT OF

ELECTRONICS & COMMUNICATION ENGINEERING

MICROCONTROLLERS

(Theory Notes) Autonomous

Course Prepared by

Dr Suma M R

Module – 4 Contents
8051 Timers and serial port:
8051 Timers and Counters Operation and Assembly language programming to generate a
pulse using Mode-1 and a square wave using Mode- 2 on a port pin.
8051 Serial Communication- Basics of Serial Data Communication, RS- 232 standard, 9
pin RS232 signals, Simple Serial Port programming in Assembly.
8051 interrupts. Interfacing to ADC and DAC, Stepper motor interfacing.

Dayananda Sagar College of Engineering

Shavige Malleshwara Hills, Kumaraswamy Layout,


Banashankari, Bangalore-560078, Karnataka
Tel : +91 80 26662226 26661104 Extn : 2731 Fax : +90 80 2666 0789
Web - http://www.dayanandasagar.edu
Email : [email protected]
(An Autonomous Institute Affiliated to VTU, Approved by AICTE &
ISO 9001:2008 Certified ) ( Accredited by NBA, National Assessment &
Accreditation Council (NAAC) with 'A' grade )
MICROCONTROLLERS MODULE 4

Software Delay Programs


Time delay:

⮚ The number of clock cycle taken to excute an instruction is called machine cylae.

⮚ The length of a machine cycle depends on frequency of an crystal oscillator.

⮚ Frequency can vary from 4MHz to 30MHz

⮚ 11.0592MHz crystal oscillator is used to to make 8051 system compatible with serial
port of IBM PC.
⮚ In 8051 systems 12 clock cycles combination is called as one machine cycle.

⮚ So the time taken to complete one machine cycle is 1/12*clock frequency.

Example:1
The following shows the crystal frequency of 3 different 8051 based systems. Find the period of
machine cycle in each case.
a. 11.0592 MHz
b. 16MHz
c. 20MHz
Ans.
a. 11.0592MHz/12=921.6KHz=1/921.6KHz=1.085µ
b. 16MHz/12=1.33MHz;MC cyce=1/1.333MHz=0.75 µ Sec
c. 20MHZ/12=1.66MHz;Machine cyle=1/1.666MHz=060 µSec
Example:2
For 8051 system of 11.0592 MHz, find how long it takes to execute each instruction.
(a) MOV R3,#55
(b) DEC R3
(c) DJNZ R2 target
(d) LJMP
(e) SJMP
(f) NOP
(g) MUL AB
Solution: Delay calculation for 8051:
Machine cycles Time to execute
(a) MOV R3,#55 1 1x1.085μs = 1.085μs
(b) DEC R3 1 1x1.085μs = 1.085μs
(c) DJNZ R2 target 2 2x1.085μs = 2.17μs
(d) LJMP 2 2x1.085μs = 2.17μs
MICROCONTROLLERS MODULE 4

(e) SJMP 2 2x1.085μs = 2.17μs


(f) NOP 1 1x1.085μs = 1.085μs
(g) MUL AB 4 4x1.085μs = 4.34μs
MICROCONTROLLERS MODULE 4

Find the size of the delay in following program, if the crystal frequency is 11.0592MHz.
MOV A,#55H AGAIN:
MOV P1,A
ACALL DELAY
CPL A
SJMP AGAIN
;---time delay-------
DELAY: MOV R3,#200
HERE: DJNZ R3,HERE RET
Solution:
Machine cycle
DELAY: MOV R3,#200 1
HERE: DJNZ R3,HERE 2
RET 2
Therefore, [(200x2)+1+2]x1.085μs = 436.255μs
4.1 Introduction to 8051 timers

 8051 microcontroller have two timers timer 0 and timer 1.These are 16 bit timers, but 8051
is a 8 bit controller so they are referred are lower byte and higher byte shown below TL0,
TH0, Tl1,TH1.
 They can be used as either timers to generate time delay or as counters to count events.
 The required delay is obtained by loading the number machine cycles into those registers.
Normally delay count value will be decremented in software delay program, but count value
will be incremented in timers. i.e.( means delay count need to be subtracted from max count
value and then timer has to be activated.)
 Timer 0 and Timer 1 register formats are as shown below in fig 4.1

D D D D D D D D8 D D D D D D D D0
15 14 13 12 11 10 9 7 6 5 4 3 2 1
TH0/ TL0/
TH1 TL1
Fig 4.1 bit format for Timer 0 and Timer 1 registers

4.2 TMOD register(Timer mode):


 Both the timers,timer0 and timer 1 use TMOD register to set various timer operation
modes.
 TMOD is an 8 bit register with lower 4 bits set aside for timer 0 and higher 4 bits for timer
1.
 Lower two bits are used are to set the timer mode and higher 2 bits specify the operation.
 TMOD register format is shown in fig 2 below.
Gat C/ M1 M0 Gat C/T M1 M0
MICROCONTROLLERS MODULE 4

e T e
Timer1 Timer0

Figure 4.2: TMOD register format

Gate Bit:
• Timer control will be decided through this bit value
• Gate-=0 : indicates that timer can be controlled through program by TR and TF bit’s in
TCON register.Bit TR is used to start the timer by making TR=1 and stop the timer by
making TR=0.This is called software control.
• Gate=1: indicates that timer control is done by external source. This is called hardware
control.
C/T (Counter/ Timer) Bit:
This bit in the TMOD register is used to decide whether the timer is used
As a delay generator or an event counter.
• If C/T =0, Then timer is working as delay generator
• If C/T=1, Then that timer is working as event counter
MICROCONTROLLERS MODULE 4

MODE bit 1 and Mode bit 0(M1 and M0):

These two bits are used to select different timer modes. The below table 1 shows the different
modes.

M1 M0 Mode Operation
0 0 0 13 bit timer mode
8 bit timer/counter THx with TLx as 5 bit prescalar..
0 1 1 16 bit timer mode.16 bit timer/counter with THx and TLx
cascaded: no prescalar
1 0 2 8 bit auto reload mode.8 bit auto reload timer/counter THx
holds a value that is to be reloaded to TLx each time as it
overflows.
1 1 3 Split timer mode.
Table 1: Timer operating modes

Example 1:Indicate which mode and which timer/counter are selected for each of the following?

a) MOV TMOD,#01h
TMOD=00000001H,mode 1 timer 0 is selected.
b) MOV TMOD,#20h
TMOD=00100000H,mode 2 ,timer 1 is selected.
c) MOV TMOD,#69h
TMOD=01101001,counter1 is selected with mode2 and software gating control.
Timer 0 is selected with mode1 and gating control is through external

Clock source for timer:


The frequency of timer is always 1/12th the frequency of the crystal attached to 8051.
Example: If Master CLK=12 MHz,
Timer Clock frequency = Master CLK/12 = 1
MHz Timer Clock Period = 1micro second
This indicates that one increment in count will take 1 micro second.
Similarly for clock frequencies for 12MHZ and 16MHZ.
1/12X12M=1MHz= and time =1 us
1/12X16M= 1.333MHz and
time=.75 us.
MICROCONTROLLERS MODULE 4

4.3 Timer MODE 1 Programming :( 16 bit)

 Load the TMOD register value indicating which timer (timer 0 or timer 1) is to
be used and which timer mode (0 or 1) is selected.
 Load registers TL and TH with initial count value. Since it’s a 16 bit timer values
from 0000h to FFFFh can be loaded into timer register.
 Start the timer using “SETB TR0” and “SETB TR1” for timer0 and timer1 respectively.
 Keep monitoring the timer overflow flag (TF) with the “JNB TFx, target “instruction to
check if it is raised.
 Stop the timer using “CLR TR0” and “CLR TR1” instructions for timer0 and
timer1 respectively.
 Clear the TF flag for the next round.
 Go back to Step 2 to load TH and TL again.
The block diagram for mode1 is shown in fig 4.3 below.
pulses
XTAL / TH TLx TFx
12 x

TR TF goes high when


Overflow FFFF 0
Fig 4.3:Block diagram for mode 1

8051 Timer Control (TCON) Special Function Register


Bi Symbol TCON Bit Function
t
7 TF1l Timer 1 Overflow flag. Set when timer rolls from all 1's to 0. Cleared when processor
vectors to execute interrupt service routine located at program address 001Bh.
6 TR1l Timer 1 run control bit. Set to 1 by program to enable timer to count; cleared to 0 by
program to halt timer.
5 TF0 l Timer 0 Overflow flag. Set when timer rolls from all 1's to 0. Cleared when processor
vectors to execute interrupt service routine located at program address 000Bh.
4 TR0l Timer 0 run control bit. Set to 1 by program to enable timer to count; cleared to 0 by
program to halt timer.
3 IE1 l External interrupt 1 Edge flag. Set to 1 when a high-to-low edge signal is received on port
3.3 (INT1). Cleared when processor vectors to interrupt service routine at program
address 0013h. Not related to timer operations.
2 IT1 l External interrupt 1 signal type control bit. Set to 1 by program to enable external
interrupt 1 to be triggered by a falling edge signal. Set to 0 by program to enable a low-
MICROCONTROLLERS MODULE 4

level signal on external interrupt 1 to generate an interrupt.


1 IE0 l External interrupt 0 Edge flag. Set to 1 when a high-to-low edge signal is received on port
3.2 (INT0). Cleared when processor vectors to interrupt service routine at program
address 0003h. Not related to timer operations.
0 IT0 l External interrupt 0 signal type control bit. Set to 1 by program to enable external
interrupt 1 to be triggered by a falling edge signal. Set to 0 by program to enable a low-
level signal on external interrupt 0 to generate an interrupt.
Bit addressable as TCON.0 to TCON.7
Direct Byte Address is 88h.
Example 1: In the following program, we create a square wave of 50% duty cycle (with equal
portions high and low) on the P1.5 bit. Timer 0 is used to generate the time delay. Analyze the
program.
LABEL Code Comments
MOV TMOD,#01 Timer 0, mode 1(16-bit mode)
HERE: MOV TL0,#0F2H TL0=F2H, the low byte
MOV TH0,#0FFH TH0=FFH, the high byte
CPL P1.5 toggle P1.5 to create a square wave.
ACALL DELAY call a delay subroutine
SJMP HERE
DELAY: SETB TR0 start the timer 0
AGAIN: JNB TF0,AGAIN monitor timer flag 0 until it rolls over
CLR TR0 stop timer 0
CLR TF0 clear timer 0 flag
RET Return from subroutine.
The program is analyzed as follows:
1. TMOD is loaded.
2. FFF2H is loaded into TH0-TL0.
3. P1.5 is toggled for the high and low portions of the pulse.
4. The DELAY subroutine using the timer is called.
5. In the DELAY subroutine, timer 0 is started by the SETB TR0 instruction.
6. TMOD is loaded.
7. FFF2H is loaded into TH0-TL0.
8. P1.5 is toggled for the high and low portions of the pulse.
9. The DELAY subroutine using the timer is called.
10. In the DELAY subroutine, timer 0 is started by the SETB TR0 instruction.
MICROCONTROLLERS MODULE 4

11. Timer 0 counts up with the passing of each clock, which is provided by the crystal oscillator.
As the timer counts up, it goes through the states of FFF3,FFF4, FFF5, FFF6, FFF7, FFF8, FFF9,
FFFA, FFFB, and so on until it reaches FFFFH. One more clock rolls it to 0, raising the timer flag
(TF0=1).At that point, the JNB instruction falls through.
12. Timer 0 is stopped by the instruction CLR TR0. The DELAY subroutine ends, and the process
is repeated. Notice that to repeat the process, we must reload the TL and TH registers, and start the
process is repeated.
Delay calculation:
⮚ The timer works with a clock frequency of 1/12 of the XTAL frequency;

⮚ therefore, we get 11.0592 MHz / 12 = 921.6 kHz as the timer frequency.

⮚ So each clock has a period of T = 1/921.6kHz = 1.085us.

⮚ Delay = number of counts × 1.085us.

⮚ The number of counts for the roll over is FFFFH – FFF2H = 0DH (13 decimal).

⮚ However, we add one to 13 because of the extra clock needed when it rolls over from FFFF
to 0 and raise the TF flag.
⮚ This gives 14 × 1.085us = 15.19us for half the pulse.

⮚ For the entire period it is T = 2 × 15.19us = 30.38us as the time delay generated by the
timer.
⮚ The count to be loaded can be calculated using the formula
Initial count value= [Maximum value – Required delay *crystal freq/12]

2. Calculate the frequency of the square wave generated on pin P1.5.


Solution:
In the above delay calculation overhead due to instruction was not included. To get a more
accurate timing, add clock cycles due to these instructions are included. This is as shown below
.
Label Code cycles
HERE: MOV TL0,#0F2H 2
MOV TH0,#0FFH 2
CPL P1.5 1
ACALL DELAY 2
SJMP HERE 2
DELAY: SETB TR0 1
AGAIN: JNB TF0,AGAIN 2*14
CLR TF0 1
CLR TR0 1
RET 2
MICROCONTROLLERS MODULE 4

Total =42 cycles


T = 2 × 42 × 1.085 us = 91.94 us and
Freq = 10.9 kHz
MICROCONTROLLERS MODULE 4

3. Assume that XTAL = 11.0592 MHz What value to be loaded to the timer’s register to create a
time delay of 5 ms (milliseconds)? Show the program for timer 0 MODE 1 to create a pulse width
of 5 ms on P2.3.
Calculation:
⮚ Since XTAL = 11.0592 MHz, the counter counts up every 1.085 us.
⮚ This means that out of many 1.085 us intervals 5 ms pulse to be
created. So 5 ms / 1.085 us = 4608 clocks.
⮚ The value to be loaded into TL and TH is
65536 – 4608 = EE00H, where TH = EE and TL = 00.
Program to create pulse width of 5ms

ORG 00H
CLR P2.3 Clear P2.3
MOV TMOD,#01H Timer 0, 16-bitmode
HERE: MOV TL0,#00H TL0=0, the low byte
MOV TH0,#0EEH TH0=EE, the high byte
SETB P2.3 SET high P2.3

SETB TR0 Start timer 0


AGAIN: JNB TF0,AGAIN Monitor timer flag 0
CLR TR0 Stop the timer 0
CLR TF0 Clear timer 0 flag
END

4. Assume that XTAL = 11.0592 MHz, Write an 8051 ALP to generate a square wave of 2 kHz
frequency on pin P1.5.

Solution:
Calculation:
(a) T = 1 / f = 1 / 2 kHz = 500 us the period of square wave.
(b) 1 / 2 of it for the high and low portion of the pulse is 250 us.
(c) 250 us / 1.085 us = 230 and 65536 – 230 = 65306 which in hex is FF1AH.
(d) TL = 1Ah and TH = FFh.
Program
ORG 00H
MOV TMOD,#10H
AGAIN: MOV TL1,#1AH
MOV TH1,#0FFH
SETB TR1
BACK: JNB TF1,BACK
CLR TR1
CPL P1.5
MICROCONTROLLERS MODULE 4

CLR TF1
SJMP AGAIN
END
MICROCONTROLLERS MODULE 4

Timers are programmed not only in assembly language; they can also programmed in C.

1. Write an 8051 C program to toggle all the bits of port P1 continuously with some
delay in between. Use Timer 0, 16-bit mode to generate the delay.
Solution:
#include <reg51.h>
Void T0Delay (void); //Delay
function Void main (void){
While (1)
{ P1=0x5
5;
T0Delay (); To toggle all pins of P1 by loading 55h and AAh into it and calling
a delay P1=0xAA; function in between.
T0Delay ();
}
}
Void T0Delay(){
TMOD=0x01; //timer 0 mode
1 TL0=0x00;
TH0=0x35;
TR0=1; //start timer
While (TF0==0); //wait for timer flag to get raised by
monitoring it. TR0=0; //stop timer
TF0=0; //clear timer flag.
}

2.Write an 8051 C program to toggle only bit P1.5 continuously every 50 ms. Use Timer 0, mode
1 (16-bit) to create the delay.

Calculation:

Count value=65536-(50ms/1.085µs)
=4BFEH
Solution:
#include <reg51.h>
Void T0M1Delay
(void); sbit
mybit=P1^5;
Void main
(void){ While
(1)
{ mybit=~mybi
t; T0M1Delay
();
}
MICROCONTROLLERS MODULE 4

}
Void
T0M1Delay(void)
{ TMOD=0x01;
TL0=0xFE;
TH0=0x4
B;
TR0=1;
While
(TF0==0);
TR0=0;
TF0=0;
}
MICROCONTROLLERS MODULE 4

4.4 Timer Mode 2 Programming :(8 bit auto reload):

1. Load the TMOD value register indicating which timer (timer 0 or timer 1)
is to be used, and the timer mode (mode 2) is selected.
2. Load the TH registers with the initial count value.
3. Start timer.
4. Keep monitoring the timer flag (TF) with the JNB TFx, target instruction to see whether it is
raised .Get out of the loop when TF goes high
5. Clear the TF flag.
6. Go back to Step4; since mode 2 is auto reload.

The block diagram for mode 2 is shown in below figure 4.4

Pulses
XTAL / TLx TFx Overflo
Oscillat
oOverfloww 12 w flag
or
TR THx Reload TF goes

High
when FF
0

Fig4.4: Mode 2 programming block diagram.

1. Assume XTAL = 11.0592 MHz, find the frequency of the square wave generated on pin P1.0
in the following program. Also find the smallest achievable frequency in this program and TH
value to do that.
ORG 00H
MOV TMOD, #20H T1/8-bit/auto reload
MOV TH1, #5 TH1 = 5
SETB TR1 start the timer 1
BACK: JNB TF1,BACK till timer rolls over
CPL P1.0 Create square wave
CLR TF1 clear Timer 1 flag
SJMP BACK Since mode 2 is auto-reload jump
to label back
to monitor the overflow flag .
END
First notice the target address of SJMP. In mode 2 so no need to reload TH since
it is auto-reload. Now (256 - 05) × 1.085 us = 251 × 1.085 us = 272.33 us is the
high portion of the pulse. Since it is a 50% duty cycle square wave, the period T
is twice that; as a result T = 2 × 272.33 us = 544.67 us.
MICROCONTROLLERS MODULE 4

Frequency (F) = 1.83597 kHz


MICROCONTROLLERS MODULE 4

2.Write an 8051 ALP to generate a square wave of frequency 10Khz on pin P1.4.Use timer
0 mode2 with XTAL Frequency=22MHz.

Time period of square wave =1/f=1/10K=0.1ms


Ton=Toff=0.05ms
Count
value=256-(0.05m/0.54µ)
=256-94
=A4h
ORG 00H
MOV TMOD,#02H
MOV TH0,#0A4H
SETB TR0
BACK: JNB TF0,BACK
CPL P1.4
CLR TF0
SJMP BACK
END

3.Write an 8051 ALP to toggle all bits of port 2 continuously every 50ms.Use timer1
mode2 with XTAL Freq=11.0592MHz.
Calculation:
Maximum delay possible in mode 2 is 0.277ms but given is 50ms .So we have to go for
multiple delay count. Take 0.25ms,now to get 50ms ,50ms/0.25ms=200.
Count value to be loaded to get 0.25ms delay i.e.,
=256-0.25m/1.085µ
=20h or 26 in decimal.
Program:
ORG 00H
MOV TMOD,#20H
MOV TH1,#20H
SETB TR1
AGAIN: MOV P2,#00H
MOV R0,#200
ACALL DELAY
MOV P2,#0FFH
MOV R0,#200
ACALL DELAY
SJMP AGAIN
DELAY: JNB TF1,DELAY
CLR TF1
DJNZ R0,DELAY
CLR TR1
RET
MICROCONTROLLERS MODULE 4

4. Write an 8051 C program to toggle only pin P1.5 continuously every 250ms.Use timer 0 mode 2 to
create delay.
Calculation:
Maximum delay possible in mode 2 -
0.277ms Consider 0.25ms.
To create 250ms multiple count
required=250/0.25=1000(250*4) 0.25ms count to be
loaded=233(256-23)

Program:
#include<reg51.h>
void
ToM2Delay(void);
sbit mybit=P1^5;
void main(void)
{
unsigned char
x,y; while(1)
{
mybit= ˜mybit;
for(x=0;x<250;x
++)
for(y=0;y<4;y+
+)
ToM2Delay
();
}
}
void ToM2Delay(void)
{ TMOD=0
x02; TH0=-
23; TR0=1;
while(TF0==
0); TR0=0;
TF0=0;
}

3.2 8051 Interrupts:

During program execution if peripheral devices needs service from microcontroller, device will generate
interrupt and gets the service from microcontroller. When peripheral device activates the interrupt
signal, the processor branches to a program called interrupt service routine. After executing the interrupt
service routine, the processor returns to the main program.
3.3.1 Interrupt Service Routine (ISR):
MICROCONTROLLERS MODULE 4

⮚ For every interrupt there must be an ISR or interrupt handler. When an interrupt is invoked
controller runs the ISR.
⮚ For every interrupt there is a fixed location in the memory that holds the address of the
ISR.The group of memory locations set aside to hold the addresses of ISR’s is called the
Interrupt Vector Table (IVT)

3.3.2Steps taken while processing an interrupt:


⮚ It completes the execution of the current instruction.

⮚ PSW is pushed to stack.

⮚ PC content is pushed to stack.

⮚ Interrupt flag is reset.

⮚ PC is loaded with ISR address.

⮚ ISR will always ends with RETI instruction. The execution of RETI instruction results
in the
⮚ following.

⮚ POP the current stack top to the PC.

⮚ POP the current stack top to PSW.

3.3.1 8051 Interrupt Structure:


8051 has five interrupts. They are maskable and vectored interrupts. Out of these five, two are
external interrupt and three are internal interrupts.8051 interrupt vector table is shown below as table
3.3.3.1

Interrupt source Type Vector address Priority


External interrupt 0 External 00 Highest
03
Timer 0 interrupt Internal 00
0B
External interrupt 1 External 00
13
Timer 1 interrupt Internal 00
1B
Serial interrupt Internal 00 Lowest
23
MICROCONTROLLERS MODULE 4
MICROCONTROLLERS MODULE 4

8051 es use of two registers to deal with interrupts.


1. IE Register (Interrupt Enable)
This is an 8- bit register used for enabling or disabling the interrupts. Its bit addressable. The
structure of IE register is shown below fig 3.3.3.1

E - - E ET1 EX1 ET0 EX0


A S
Fig 3.3.3.1:8051 IE register format
EA-Enable or disable all interrupts. If EA=0 it disables all interrupts. If EA=1,all interrupts
can be individually enabled or disabled by setting or clearing its enabling bit.
- Reserved for future use
- Reserved for future use
ES-Enable or disable serial port interrupt
ET1-Enable or Disable Timer 1 Overflow Interrupt. EX1-
Enable or Disable External Interrupt 1
ET0- Enable or Disable Timer 0 Overflow Interrupt. EX0-
Enable or Disable External Interrupt 0
Example: Show the instructions to
a) enable the serial interrupt, Timer 0 interrupt and external hardware interrupt 1.
b) Disable(mask) Timer1 interrupt.
c) Disable all interrupts.
a) MOV IE,#10010110B
b) CLR IE.1
c) CLR IE.7
2. IP Register (Interrupt Priority Register):
This is an 8-bit register used for setting the priority of the interrupts. If the bit is 0, the
corresponding interrupt has a lower priority and if the bit is 1 the corresponding interrupt has a
highest priority. IP register format is as shown in below fig 3.2

- - - P PT1 PX1 PT0 PX0


S
Fig 3.3.3.2: IP register format.
PS-Defines the serial port interrupt priority level
PT1-Defines the Timer1 Interrupt Priority level PX1-
Defines External Interrupt 1 Priority level. PT0- Defines
the Timer0 Interrupt Priority level PX0-Defines
External Interrupt 0 Priority level.
Example1:
a) Program the IP register to assign the highest priority to INT1 (external interrupt 1),
MOV IP, #00000100B or SETB IP.2
MICROCONTROLLERS MODULE 4

b) Discuss what happens if INT0, INT1, and TF0 are activated at the same time.
First INT0 is serviced followed by TF0 and then INT1 as per default priority of interrupts.
Example 2:
Assume that after reset, the interrupt priority is set by the instruction
MOV IP, #00001100B”. Discuss the sequence in which the interrupts are serviced.
Solution: The sequence in which the interrupts are serviced is shown below.
Highest Priority External Interrupt 1 (INT1)
Timer Interrupt 1(TF1) External
Interrupt 0(INT0) Timer Interrupt
0(TF0)
Lowest Priority Serial Communication (RI+TI)

3.3.3Programming External Hardware Interrupts:


⮚ 8051 has two external hardware interrupts. Pins P3.2 and P3.3 of 8051 named as INT0 and
INT1.
⮚ On activation of these pins,8051gets interrupted in whatever it is doing and jumps to vector
table to perform the ISR.
⮚ There are two types of activations for external hardware interrupts
o Level triggered
o Edge triggered
Activation of INT0 and INT1 interrupts using the 2 methods is diagrammatically shown in below figure
3.3.4.1
MICROCONTROLLERS MODULE 4

3.3.4.1 Level Triggered Interrupt:


⮚ In level triggered mode, INT0 and INT1 pins are normally high and if low level signal is
applied to them, it triggers the input interrupt.
⮚ The controller then stops whatever it is doing and jumps to the ISR to service the interrupts.
This is called level triggered or level activated interrupt and is the default mode upon reset
of 8051.
⮚ This low-level signal at the INT pin must be removed before the execution of ISR, RETI, else
another interrupt will be generated
Program 1
Assume that INT1 pin is connected to a switch that is normally high, whenever it goes low, it should
turn on the LED. The LED is connected to P1.3 and is normally off. When it is turned on it should stay
on for fraction of a second.

ORG 00h LJMP MAIN


ORG 0013h SETB P1.3
MOV R3, #255
BACK: DJNZ R3, BACK CLR
P1.3
RETI ORG 30H
MOV IE, #10000100B HERE:
SJMP HERE
END

Sampling the low-level triggered interrupt:

⮚ Once the hardware interrupts in IE register are enabled, the controller keeps sampling the
INTx pin for a low-level signal once each machine cycle.
⮚ To ensure the activation of hardware interrupt at the INTx pin, the duration of low level signal
should be around 4 machine cycles and is as shown in fig 3.3.4.1.1 below.
MICROCONTROLLERS MODULE 4
MICROCONTROLLERS MODULE 4

3.3.4.1 Edge Triggered interrupts:


⮚ To make interrupts edge triggered, we must program the bits of TCON register.

⮚ In TCON register we have 4 bits related to interrupts i.e., IE1, IT1, IE0, IT0. IT1 and
IT0=0, Implies interrupts are low level triggered.
IT1 and IT0=1, Implies interrupts are edge triggered and are to be done by the
programmer.
⮚ When there is a high to low signal applied on pin P3.3 the collector will be interrupted and
forced to jump to the location 0013H in vector table. Similarly, for P3.2.

Program 2:
Assuming P3.3(INT1) is connected to pulse generator, write an 8051 ALP in which falling edge
of pulse will send a high to P1.3 which is connected to buzzer. In other words, LED is turned on
and off at the same rate as pulses are applied to INT1.

ORG 00H LJMP MAIN


ORG 0013H SETB P1.3
MOV R3, #255
BACK: DJNZ R3, BACK CLR
P1.3
RETI ORG 30H
MAIN: SETB TCON.2
MOV IE, #10000100B SJMP $
END
MICROCONTROLLERS MODULE 4

4.5 Serial Port Programming

INTRODUCTION:

 Computer can transfer data in two ways [Parallel and serial].


 8 bits of data can transfer at a time in parallel and bit by bit can transfer in serial.
 Parallel transfer can be used for short distances like printer and scanner, and each one uses
cables with many wire strips, even there is chance of signal distortion and it is to
expensive.
 Serial data transfer can be used between two computers which for away, like connected
through internet.
 Here we can discuss basics of serial communication, 8051 interfacing to RS232 via
MA232 line drivers, and serial port programming.
 It is shown diagrammatically in fig 4.6.1

Fig4.6.1: Serial and Parallel data transfer

Basics of Serial Communication:

 For serial data communication to work, the byte of data must be converted to serial bits
using PISO shift register, then it can transmitted over a single data line.
 Receiver end should have SIPO shift register, then only serial data can packed in to a byte of
data.
MICROCONTROLLERS MODULE 4

 If the data is to be transferred on the telephone line, it must be converted from 0s and 1s to
audio tones, which are sinusoidal shaped signal. This conversion is performed by a
peripheral device called a modem, which modulator/demodulator.
 Serial data communication uses two methods, asynchronous and synchronous.
 The synchronous method transfers a block of data at a time, where as asynchronous
method transfers single byte at a time, but programs can be tedious and long.
 For this reason, there are special IC chips made refereed as UART [ universal asynchronous
receiver transmitter] and USART [universal synchronous asynchronous receiver
transmitter]
 The 8051 has inbuilt UART.

Half and full duplex transmission:

Simplex mode is to only send data, whereas duplex transmission is the one where both
transmission and reception of data can happen. Duplex can be either half or full duplex.In
half duplex data transfer not simultaneous whereas in full duplex data transfer is
simultaneous. The concept is as shown in below figure 4.6.2

Fig:4.6.2 Half and Full duplex transmission.

Asynchronous serial Communication and data framing:

 A protocol is a set of rules agreed by both the sender and receiver on.
 How the data is packed
 How many bits constitute a character
 When the data begins and ends
 Asynchronous serial data communication is widely used for character-oriented transmissions
 Each character is placed in between start and stop bits, this is called framing.
 Block-oriented data transfers use the synchronous method
 The start bit is always one bit, but the stop bit can be one or two bits
 The start bit is always a 0 (low) and the stop bit(s) is 1 (high).
 The data asynchronous data framing format is as shown in below figure 4.6.3
MICROCONTROLLERS MODULE 4

Fig:4.6.3 Asynchronous data format.

 Assuming that we are transferring a text file of ASCII characters using 1 stop bit, we have
a total of 10 bits for each character. This gives 25% overhead, i.e. each 8-bit character
with an extra 2 bits.
 UART chips allow programming of the parity bit for odd-, even-, and no-parity
options

The rate of data transfer in serial data communication is stated in bps (bits per second)
Another widely used terminology for bps is baud rate.The data transfer rate of given computer system
depends on communication ports incorporated into that system
 IBM PC/XT could transfer data at the rate of 100 to 9600 bps.
 Pentium-based PCs transfer data at rates as high as 56K bps.
 In asynchronous serial data communication, the baud rate is limited to 100K bps.

CONNECTIONS TO RS-232

RS-232 standards:
 To allow compatibility among data communication equipment made by various
manufactures, an interfacing standard called RS232 was set by the Electronics Industries
Association (EIA) in 1960.
 The standard was set long before the advent of logic family, so its input and output voltage
levels are not TTL compatible.
 In RS232, a logic one (1) is represented by -3 to -25V and referred as MARK while logic
zero (0) is represented by +3 to +25V and referred as SPACE.
 To connect any RS232 to a microcontroller system voltage converters such as MAX232 to
convert the TTL logic level to RS232 voltage levels and vice-versa are used. MAX232 IC
chips are commonly referred as line drivers.
 In RS232 standard there are two types of connectors. DB9 connector or DB25 connector
which are as shown in below fig.4.6.5 and 4.6.6
MICROCONTROLLERS MODULE 4

Fig 4.6.5:DB9 Male Connector Fig 4.6.6: DB25 Male Connector


 The pin description of DB9 and DB25 Connectors are as follows:

Current terminology classifies data communication equipment as

 DTE (data terminal equipment) refers to terminal and computers that send and
receive data.
 DCE (data communication equipment) refers to communication equipment, such
 as modems
 The simplest connection between a PC and microcontroller requires a minimum
of three pins, TxD, RxD, and ground.

DB-9 RS232 PIN explanation :


 DTR (data terminal ready): When terminal is turned on, it sends out signal DTR to
indicate that it is ready for communication.
 DSR (data set ready): When DCE is turned on and has gone through the self-test, it assert
DSR to indicate that it is ready to communicate.
 RTS (request to send) When the DTE device has byte to transmit, it assert RTS to signal
the modem that it has a byte of data to transmit.
 CTS (clear to send): When the modem has room for storing the data it is to receive, it sends
out signal CTS to DTE to indicate that it can receive the data now .
 DCD (data carrier detect): The modem asserts signal DCD to inform the DTE that a valid
carrier has been detected and that contact between it and the other modem is established.
 RI (ring indicator): An output from the modem and an input to a PC indicates that the
telephone is ringing.It goes on and off in synchronous with the ringing sound.
MICROCONTROLLERS MODULE 4

The 8051 connection to MAX232 is as follows.

 The 8051 has two pins that are used specifically for transferring and receiving data
serially. These two pins are called TXD, RXD. Pin 11 of the 8051 (P3.1) assigned to TXD
and pin 10 (P3.0) is designated as RXD.
 These pins TTL compatible; therefore they require line driver (MAX 232) to make
them RS232 compatible.
 MAX 232 converts RS232 voltage levels to TTL voltage levels and vice versa. One
advantage of the MAX232 is that it uses a +5V power source which is the same as
the source voltage for the 8051.
 The typical connection diagram between MAX 232 and 8051 is shown below in fig 4.6.7

Fig 4.6.7:Connection diagram between MAX232,8051 and RS232


Baud rate calculation:

With XTAL = 11.0592 MHz, find the TH1 value needed to have the following baud rates. (a)
9600 (b) 2400 (c) 1200

Solution: The machine cycle frequency of 8051 = 11.0592 / 12 = 921.6 kHz,


and 921.6 kHz / 32 = 28,800 Hz is frequency by UART to timer 1 to set baud rate.
(a) 28,800 / 3 = 9600 where -3 = FD (hex) is loaded into TH1.
(b) 28,800 / 12 = 2400 where -12 = F4 (hex) is loaded into TH1.
(c) 28,800 / 24 = 1200 where -24 = E8 (hex) is loaded into
MICROCONTROLLERS MODULE 4

SCON register format(8 bit):


The SCON register format is as shown in below
SM0 SM1 SM2 REN TB8 RB8 TI RI

• SM0, SM1 They determine the framing of data by specifying the number of bits per
character and the start and stop bits.

⮚ SM2: This enables the multiprocessing capability of the 8051

⮚ REN (receive enable): It is a bit-addressable register When it is high, it allows 8051 to


receive data on RxD pin .If low, the receiver is disable.
⮚ TI (transmit interrupt): When 8051 finishes the transfer of 8-bit character.It raises TI
flag to indicate that it is ready to transfer another byte.TI bit is raised at the beginning
of the stop bit.
⮚ RI (receive interrupt): When 8051 receives data serially via RxD, it gets rid of the
start and stop bits and places the byte in SBUF register. It raises the RI flag bit to
indicate that a byte has been received and should be picked up before it is lost.RI is
raised halfway through the stop bit.
SERIAL COMMUNICATION PROGRAMMING IN ASSEMBLY AND C:
Steps to programming the 8051 to transfer data serially

1. The TMOD register is loaded with the value 20H, indicating the use of the Timer 1 imode
MICROCONTROLLERS MODULE 4

2 (8-bit auto reload) to set the baud rate.


MICROCONTROLLERS MODULE 4

2. The TH1 is loaded with one of the values in table 5.1 to set the baud rate for serialdata
transfer.
3. The SCON register is loaded with the value 50H, indicating serial mode 1, where an8-bit data
is framed with start and stop bits.
4. TR1 is set to 1 start timer 1.
5. TI is cleared by the “CLR TI” instruction.
6. The character byte to be transferred serially is written into the SBUF register.
7. The TI flag bit is monitored with the use of the instruction JNB TI, target to see if
the Character has been transferred completely.
8. To transfer the next character, go to step 5.

1. Write an 8051 ALP to receive data serially and put them in P1. Set the baud rate as 4800,
8bit data, 1stop and start bit.
Algorithm:
i. TMOD =20H, Timer1 in mode2
ii. TH1 is loaded with one of values to set baud rate
iii. SCON =50H
iv. Start TR1
v. RI is cleared with ‘CLR RI’ instruction
vi. RI flag bit is monitored with use of instruction ‘JNB RI, XX’ to see if an entire
character has been received yet.
vii. When TI is raised, SBUF has the byte contents are moved into safe place
viii. To receive next character.
LABEL MNEMONIC AND
OPERANDS
MOV TMOD,#20H
MOV TH1,#-6
MOV SCON,#50H
SETB TR1
HERE: JNB RI,HERE
MOV A, SBUF
MOV P1,A
CLR RI
SJMP HERE
2. Write an ALP to implement following operation do a once, and b & c continuously 4800 baud
rate.
a. Send to the PC message “we are ready’
b. Receive any data sent by PC and put it on LED’s connected to P1
c. Get data on swiitches connected to P2 and send to PC
MICROCONTROLLERS MODULE 4

LABEL MNEMONIC AND OPERANDS


MOV P2,#0FFH
MOV TMOD#20H
MOV TH1,#0FAH
MOV SCON,#50H
SERTB TR1
MOV DPTR,#MYDATA
H1: CLR A
MOVC A, @A+DPTR
JZ B1
ACALL SEND
INC DPTR
SJMP H1
B1: MOV A,P2
ACALL SEND
ACALL RECV
MOV P1,A
SJMP B1 MOV
SEND: LABEL
SBUF,A MNEMONIC AND
H2: JNB TI,H2 OPERANDS
CLR TI ORG 00
RET LJMP
RECV: MAIN
JNB RI,RECV
MOV A,SBUFORG
CLR RI 23H
MAIN:RET LJMP
MYDATA: SERIA
BD “ WE ARE READY”, 0
END L ORG
30H
3. Write an 8051 ALP to read data
MOV
P1,#0FFH from P1 and write it to P2
BACK: MOV continually while giving a copy of
it TMOD,#20 to the serial port to be transferred
H MOV serially.
TH1,#-3
MOV
SCON,#50H
MOV
IE,#10010000
B SETB TR1
MOV A,P1

MOV SBUF,A
TRANS: MOV P2,A SJMP
BACK ORG 100H JB
TI,TRANS MOV
A,SBUF CLR RI
RETI CLR TI
RETI
END
MICROCONTROLLERS MODULE 4

4. Write a program using interrupts to do the following


a. Receive data serially and send it to P0
b. Have port P1 read and txed serially and a copy given to P2
c. Make Timer 0 generate a square wave of 5 KHz on P0.1, when XTAL=11.059MHzand
baud rate is 4800.
LABEL MNEMONIC AND OPERANDS
ORG 00
LJMP MAIN
ORG OOOBH
CPL P0.1 RETI
ORG 23H
LJMP SERIAL
ORG 30H
MOV P1,#OFFH
MOV TMOD,#22H
MOV TH1,#0F6H
MOV SCON,#50H
MOV TH0,#-92
MOV IE,#10010010b
SETB TR1
SETB TR0
BACK: MOV A,P1
MOV SBUF, A
MOV P2,A
SKMP BACK
ORG 100H
SERIAL: JB TI,TRANS
MOV A,SBUF
MOV P0,A
CLR RI
RETI
TRANS: CLR TI
RETI
END
Interfacing DAC with 8051 Microcontroller

Digital to Analog converter (DAC) is a device, that is widely used for converting digital pulses to analog
signals. There are two methods of converting digital signals to analog signals. These two methods are
MICROCONTROLLERS MODULE 4

binary weighted method and R/2R ladder method. MC1408 (DAC0808) is Digital to Analog Converter.
This chip uses R/2R ladder method. This method can achieve a much higher degree of precision. DACs are
judged by its resolution. The resolution is a function of the number of binary inputs. The most common
input counts are 8, 10, 12 etc. Number of data inputs decides the resolution of DAC. So if there are n
digital input pin, there are 2n analog levels. So 8 input DAC has 256 discrete voltage levels.
The MC1408 DAC (or DAC0808)

In this chip the digital inputs are converted to current. The output current is known as Iout by connecting a
resistor to the output to convert into voltage. The total current provided by the Iout pin is basically a
function of the binary numbers at the input pins D 0 - D7 (D0 is the LSB and D7 is the MSB) of DAC0808
and the reference current Iref. The following formula is showing the function of Iout

IOut=Iref⟮D7/2+D6/4+D5/8+D4/16+D3/32+D2/64+D1/128+D0/256⟯

The Iref is the input current. This must be provided into the pin 14. Generally 2.0mA is used as Iref
MICROCONTROLLERS MODULE 4

We connect the Iout pin to the resistor to convert the current to voltage.

Generating Sinewave using DAC and 8051 Microcontroller

For generating sinewave, at first we need a look-up table to represent the magnitude of the sine value of
angles between 0° to 360°. The sine function varies from -1 to +1. In the table only integer values are
applicable for DAC input. In this example we will consider 30° increments and calculate the values from
degree to DAC input. We are assuming full-scale voltage of 10V for DAC output. We can follow this
formula to get the voltage ranges.

Vout = 5V + (5 ×sinθ)
MICROCONTROLLERS MODULE 4

Let us see the lookup table according to the angle and other parameters for DAC.

Angle(in θ ) sinθ Vout (Voltage Magnitude) Values sent to DAC


0 0 5 128
30 0.5 7.5 192
60 0.866 9.33 238
90 1.0 10 255
120 0.866 9.33 238
150 0.5 7.5 192
180 0 5 128
210 -0.5 2.5 64
240 -0.866 0.669 17
270 -1.0 0 0
300 -0.866 0.669 17
330 -0.5 2.5 64
360 0 5 128

Circuit Diagram −
MICROCONTROLLERS MODULE 4

Algorithm for Square wave generation


1. Let initial, amplitude of the square wave be 2.5V (7F) and frequency count 100.
2. Output the values 00h (0ff) and 7fh (on) Values through P0.
3. If amplitude key is pressed then increase the voltage in steps of 0.15V (8).
4. If the frequency key is pressed increment the count in steps of 50. If the count exceeds 1000 reset it
back to 100.
5. Every time amplitude and frequency changes output the value thro P0 and note the waveform on
CRO.
To generate different Square waveform using DAC interface to 8051; change the frequency and
amplitude.

1. Write a C program to generate square wave using DAC interface to 8051 measure the
frequency and amplitude
#include <REG51xD2.H>
void delay(unsigned int x) /* delay routine */
{
for(;x>0;x--);
}

main()
{
unsigned char ON = 0xff;
OFF=0x00;
P0 = on;
delay(500);
P0 = off;
delay(500);

Output

Algorithm for Triangular wave generation


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

2. Write a C program to generate triangular wave using DAC interface to 8051 measure the
frequency and amplitude
#include <REG51xD2.H>
MICROCONTROLLERS MODULE 4

main()
{
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */

while(1)
{
for (i=0;i<0xff;i++)
{ /* Generate ON pulse */
P0 = i;
}
for (i=0xfe;i>0x00;i--) /* Generate OFF pulse */
{
P0 = i;
}
}
}
Output

Algorithm for Ramp wave generation


1. Output the initial value 00 through P0.
2. Increment it in steps of 1 until a count value of FFh (5V) is reached. Every time repeat step 1.
3. Repeat step 1 & 2 continuously.

3. Write a C program to generate ramp wave using DAC interface to 8051 measure the
frequency and amplitude
#include <REG51xD2.H>
main()
{
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while(1)
{
for(i=0;i<0xff;i++)
{ /* Generate ON pulse */
P0 = i;
}
}
}
Output
MICROCONTROLLERS MODULE 4

Interfacing Stepper Motor with 8051Microcontroller

Stepper Motor
Stepper motors are used to translate electrical pulses into mechanical movements. In some disk drives, dot
matrix printers, and some other different places the stepper motors are used. The main advantage of using
the stepper motor is the position control. Stepper motors generally have a permanent magnet shaft (rotor),
and it is surrounded by a stator.

Normal motor shafts can move freely but the stepper motor shafts move in fixed repeatable increments.

Some parameters of stepper motors −


● Step Angle − The step angle is the angle in which the rotor moves when one
pulse is applied as an input of the stator. This parameter is used to determine
the positioning of a stepper motor.
MICROCONTROLLERS MODULE 4

● Steps per Revolution − This is the number of step angles required for a complete
revolution. So the formula is 360° /Step Angle.
● Steps per Second − This parameter is used to measure a number of steps
covered in each second.
● RPM − The RPM is the Revolution Per Minute. It measures the frequency of
rotation. By this parameter, we can measure the number of rotations in one
minute.

The relation between RPM, steps per revolution, and steps per second is like below:

Steps per Second = rpm x steps per revolution / 60

Interfacing Stepper Motor with 8051 Microcontroller

Weare using Port P0 of 8051 for connecting the stepper motor. HereULN2003 is used. This is basically a
high voltage, high current Darlington transistor array. Each ULN2003 has seven NPN Darlington pairs. It
can provide high voltage output with common cathode clamp diodes for switching inductive loads.

The Unipolar stepper motor works in three modes.

● Wave Drive Mode − In this mode, one coil is energized at a time. So all four coils
are energized one after another. This mode produces less torque than full step
drive mode.

The following table is showing the sequence of input states in different windings.

Steps Winding A Winding B Winding C Winding D


1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1
● Full Drive Mode − In this mode, two coils are energized at the same time. This
mode produces more torque. Here the power consumption is also high

The following table is showing the sequence of input states in different windings.

Steps Winding A Winding B Winding C Winding D


1 1 1 0 0
2 0 1 1 0
3 0 0 1 1
4 1 0 0 1
MICROCONTROLLERS MODULE 4

● Half Drive Mode − In this mode, one and two coils are energized alternately. At
first, one coil is energized then two coils are energized. This is basically a
combination of wave and full drive mode. It increases the angular rotation of
the motor

The following table is showing the sequence of input states in different windings.

Steps Winding A Winding B Winding C Winding 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
8 1 0 0 1

The circuit diagram is like below: We are using the full drive mode.
MICROCONTROLLERS MODULE 4

Stepper motor unlike DC motor rotates in steps.


•Stepper motor has 4 coils which forms the stator and a central rotor.
•Rotation depends on excitation of stator coils.

Ste Coil A Coil B Coil C Coil D


p
1 0 0 0 1
2 1 0 0 0
3 0 1 0 0
4 0 0 1 0
Anyone of these values forms the initial value. To get
o o o
360 revolution 200 steps are required. Step angle= 360 /200 = 1.8 . (Difference between 2 teeth).

Algorithm for Stepper Motor


•Configure P0 as output.
•Apply the initial excitation of 11 to motor coils through P0.
•For clockwise motion -Rotate right once the excitation and repeat step 2.
MICROCONTROLLERS MODULE 4

•For anticlockwise motion -Rotate left once the excitation and repeat step 2

1. Objective: To control the speed of Stepper motor using 8051 micro controller.

Program for Stepper motor


#include <REG51xD2.H>

void delay(unsigned int x) /* Delay Routine */


{
for(;x>0;x--);
return;
}
main()
{
unsigned char Val, i;
P2=0x00;
while(1)
{
Val = 0x11;
for(i=0;i<4;i++)
{
P2 = Val; /* Write data for clock wise direction*/
Val = Val>>1; Val = Val<<1; for counter clock wise
delay(575);
}
}
}
MICROCONTROLLERS MODULE 4

ADC Interfacing with 8051 MC

•Digital computers use binary values but in physical world everything is Analog (continuous) eg.
Temperature, Pressure
•A physical quantity is converted into electrical (voltage, current) signals using a device called as
Transducer (also referred as Sensors).
•We need Analog-to-Digital converter (ADC) to translate Analog signals to Digital number so that
Microcontroller can read and process them.
•ADC0808 allows us to monitor upto 8 different analog input channels using single chip
MICROCONTROLLERS MODULE 4

Prepared By
Address Lines (A,B,C)
•ADC0808 has 8-bit data output

•8 analog input channels are multiplexed and selected using three address pins A, B & C.
IN0 0 0 0
IN1 0 0 1
IN2 0 1 0
IN3 0 1 1
IN4 1 0 0
IN5 1 0 1
IN6 1 1 0
IN7 1 1 1

Signals for A/D Conversion


•Address Latch Enable (ALE): A LOW-TO-HIGH signal at this pin will latch the above-selected address
and selected the respective channel for ADC conversion.
•START Conversion (SC): The A/D converter‟s successive approximation register (SAR) is reset on the
positive edge of the start conversion (SC) pulse. Thus we need to generate a LOW-HIGH pulse for starting
the ADC conversion.
•End of Conversion (EOC): Once the conversion is over, this pin is pulled HIGH by ADC0808. This pin
needs to be monitored for the conversion to complete and then read the data.
•Output Enable(OE): ADC0808 does the A/D conversion and holds the data in the internal registers. A
HIGH signal on this pin will bring the data on the output lines.
•D0-D7 are the digital Data output lines

.
MICROCONTROLLERS MODULE 4

● N0-IN7: Analog Input channels

● D0-D7: Data Lines

● A, B, C: Analog Channel select lines; A is LSB and C is MSB

● OE: Output enable signal

● ALE: Address Latch Enable

● EOC: End of Conversion signal

● Vref+/Vref-: Differential Reference voltage input

● Clock: External ADC clock input

C Code
1.
include <reg51.h>
2.
3. #define ALE P2_4
4. #define OE P2_5
5. #define START P2_6
6. #define EOC P2_7
7. #define SEL_A P2_0
MICROCONTROLLERS MODULE 4

8. #define SEL_B P2_1


9. #define SEL_C P2_2
10. #define ADC_DATA P1
11.
12. void main()
13. {
14. unsigned char adc_data;
15.
16. /* Data port to input */
17. ADC_DATA = 0xFF;
18.
19. EOC = 1; /* EOC as input */
20. ALE = OE = START = 0;
21.
22. while (1) {
23. /* Select channel 1 */
24. SEL_A = 1; /* LSB */
25. SEL_B = 0;
26. SEL_C = 0; /* MSB */
27.
28. /* Latch channel select/address */
29. ALE = 1;
30.
31. /* Start conversion */
32. START = 1;
33.
34. ALE = 0;
35. START = 0;
36.
37. /* Wait for end of conversion */
38. while (EOC == 1);
39. while (EOC == 0);
40.
41. /* Assert Read signal */
42. OE = 1;
43.
44. /* Read Data */
45. adc_data = ADC_DATA;
46.
47. OE = 0;
48.
49. /* Now adc data is stored */
50. /* start over for next conversion */
51. }
52. }

Assembly Code:

1. ale equ P2.4


2. oe equ P2.5
MICROCONTROLLERS MODULE 4

3. start equ P2.6


4. eoc equ P2.7
5. sel_a equ P2.0
6. sel_b equ P2.1
7. sel_c equ P2.2
8. adc_data equ P1
9.
10. org 0H
11.
12. ;Data port to input
13. mov adc_data, #0FFH
14.
15. ;EOC as Input
16. setb eoc
17. ;rest of output signals
18. clr ale
19. clr oe
20. clr start
21.
22. main_loop:
23. ;Select Analog Channel 1
24. setb sel_a
25. clr sel_b
26. clr sel_c
27.
28. ;Latch channel select
29. setb ale
30.
31. ;Start conversion
32. setb start
33.
34. clr ale
35. clr start
36.
37. ;Wait for end of conversion
38. jb eoc, $ ; $ means jump to same location
39. jnb eoc, $
40.
41. ;Assert read signal
42. setb oe
43.
44. ; Read Data
45. mov A, adc_data
46.
47. clr oe
48.
49. ;ADC data is now in accumulator
50. ;Start over for next conversion
51.
52. sjmp main_loop
53.
54. end
MICROCONTROLLERS MODULE 4

You might also like