Module 4 - Updated
Module 4 - Updated
DEPARTMENT OF
MICROCONTROLLERS
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.
⮚ The number of clock cycle taken to excute an instruction is called machine cylae.
⮚ 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.
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
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
e T e
Timer1 Timer0
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
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
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
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;
⮚ 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]
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
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
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.
Pulses
XTAL / TLx TFx Overflo
Oscillat
oOverfloww 12 w flag
or
TR THx Reload TF goes
High
when FF
0
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
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.
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;
}
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)
⮚ ISR will always ends with RETI instruction. The execution of RETI instruction results
in the
⮚ following.
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)
⮚ 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
⮚ 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.
INTRODUCTION:
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.
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
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
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
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.
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
With XTAL = 11.0592 MHz, find the TH1 value needed to have the following baud rates. (a)
9600 (b) 2400 (c) 1200
• SM0, SM1 They determine the framing of data by specifying the number of bits per
character and the start and stop bits.
1. The TMOD register is loaded with the value 20H, indicating the use of the Timer 1 imode
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
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
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.
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.
Circuit Diagram −
MICROCONTROLLERS MODULE 4
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
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
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
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.
● 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:
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.
● 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.
The following table is showing the sequence of input states in different windings.
● 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.
The circuit diagram is like below: We are using the full drive mode.
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.
•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
.
MICROCONTROLLERS MODULE 4
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
Assembly Code: