1. UNIT V - INTERFACING MICROCONTROLLER
Programming 8051 Timers - Serial Port
Programming - Interrupts Programming – LCD &
Keyboard Interfacing - ADC, DAC & Sensor
Interfacing - External Memory Interface-
Stepper Motor and Waveform generation.
2. Programming 8051 Timers:
•8051 has two timers/counters
•Used as timer to generate time delays or as counters to
count events happening outside microcontroller
•Has timer 0 and timer 1 (16 bit wide)
•8051 is 8 bit microcontroller and 16 bit timer accessed by
•TL0 – Timer 0 lower byte
•TL1 – Timer 1 lower byte
•TH0 – Timer 0 higher byte
•TH1 – Timer 1 higher byte
7. 13-bit Time Mode (mode 0)
•When the timer is in 13-bit mode, TLx will count from 0 to
31.
•When TLx is incremented from 31, it will "reset" to 0 and
increment THx.
•Thus, effectively, only 13 bits of the two timer bytes are
being used: bits 0-4 of TLx and bits 0-7 of THx.
•If you set a 13-bit timer to 0, it will overflow back to zero
8192 machine cycles later.
8. 16-bit Time Mode (mode 1)
•TLx is incremented from 0 to 255.
•When TLx is incremented from 255, it resets to 0 and
causes THx to be incremented by 1.
•Since this is a full 16-bit timer, the timer may contain
up to 65536 distinct values.
•If you set a 16-bit timer to 0, it will overflow back to
0 after 65,536 machine cycles.
9. 8-bit Time Mode (mode 2)
Timer mode "2" is an 8-bit auto-reload mode.
When TLx reaches 255 and is subsequently incremented, instead
of resetting to 0 (as in the case of modes 0 and 1), it will be reset
to the value stored in THx.
10. Split Timer Mode (mode 3)
•When Timer 0 is placed in mode 3, it essentially becomes two
separate 8-bit timers.
•That is to say, Timer 0 is TL0 and Timer 1 is TH0.
•Both timers count from 0 to 255 and overflow back to 0.
•All the bits that are related to Timer 1 will now be tied to TH0.
•While Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1)
can be put into modes 0, 1 or 2 normally
12. Serial Port Programming
Basics of Serial Communication:
Microprocessor communicates with the outside world through the data
bus (8 bit) and transfers data in parallel manner through bus.
Since 8-bit data path is expensive and parallel communication is suitable
for shorter distance (since long cables diminish and distort signals),
serial communication is preferred for data transfer located at distances
of hundreds to millions miles
13. Serial Port Programming
Basics of Serial Communication:
Working of serial communication – byte of data must be converted into
serial bits using parallel-in-serial-out register and transmitted over single
data line
In receiver side, there must be serial-in-parallel-out register to pack the
serial data into byte
Types of Communication:
Synchronous – transfers block of data at a time (extra bits (start and stop
bits) are used
Asynchronous – transfers single byte at a time (no extra bits are used)
15. Serial Port Programming
Asynchronous Communication:
Data Framing – placing the character values (0’s and 1’s) between start
and stop bits.
e.g. transferring character “A” (value 41H) in asynchronous
No transfer – referred by mark state (high-1)
Start bit – starting of transmission
Stop bit – ending of transmission
16. Serial Port Programming
Data Transfer Rate:
Bps – bits per second (another term is baud rate but not the same
sometimes)
Baud rate – number of signal changes per second
In conductor both are same but in MODEM a single signal change may
transfers several bytes of data
In asynchronous serial data communication, the baud rate is generally
limited to 100, 000 bps)
17. Serial Port Programming
RS232 standard:
In 1960, Electronics Industries Association (EIA) set an interfacing
standard called RS232 and later modified in several types like RS232A,
RS232B, RS232C etc.
RS232 standard:
1 – represented by -3 to -25V
0 – represented by +3 to +25V
-3 to +3 is undefined
18. Serial Port Programming
RS232 25 pin connector:
Communication Equipment are classified as
Data Terminal Equipment (DTE) – terminals and computers that send
and receive data
Data Communication Equipment (DCE) – equipments such as modems
that are responsible for data transfer
20. RS232 Pin description:
PIN 1: (Protective Ground); It is a ground Pin.
PIN 2 & PIN 3: These pins are most important pins for data transmitting
and receiving. The 1 & 2-pins are used to data transmitting and pin-3
used to data receiving purpose.
PIN 4, PIN 5, PIN 6, PIN 20: These pins are the handshaking pins(flow of
control).Normally terminals cannot transmit the data until clear to send
transmission is received from the DCE.
PIN 7: This pin is the common reference for all signals, including data,
timing, and control signals. The DCE and DTE works properly across the
serial interface and the pin-7 must be connected both ends without
interface would not work.
21. RS232 Pin description:
PIN 8: also known as received line signal detect or carrier detect and
activated when a suitable carrier is established between the local and
remote DCE devices.
PIN9: This pin is DTE serial connector, this signal follows the incoming
ring to an extent. Normally this signal is used by DCE auto answer mode.
PIN 10: Test Pin.
PIN 11: stand by select .
PIN 12: Data Carrier Detect.
PIN 13: Clear to send.
PIN 14: Transmit data.
PIN 15: Transmit clock.
PIN 17: Receive clock.
PIN 24: External Clock.
22. RS232 Pin description:
PIN 15, 17, 24; Synchronous modems use the signals on these pins.
These pins are control bit timing.
PIN 16: Receive data.
PIN 18: Test Pin.
PIN 19: Request to send.
PIN 24: External Clock.
PIN 21: (Signal Quality Detector); This pin Indicates the quality of the
received carrier signal because the transmitting modem must be send 0
or either 1 at each bit time , the modem controls the timing of the bits
from the DTE.
23. RS232 Pin description:
PIN 22: (Ring Indicator): The ringing indicator means the DCE informs
the DTE that the phone is ringing.
All the modems designed for direct connected to the phone network
equipped with auto answer.
PIN 23: Data Signal Rate Detector
All 25 pins are not used always and hence introduced DB-9 version
24. Baud Rate in 8051:
8051 baud rate is programmable by Timer 1 under mode 2 i.e. 8-bit auto
reload mode
25. SBUF register:
The serial port of 8051 is full duplex, i.e., it can transmit and receive
simultaneously.
SBUF is used to hold the data. Data before transmission needs to placed in
SBUF
Special function register SBUF is physically two registers.
One is, write-only and is used to hold data to be transmitted out of the 8051
via TXD.
The other is, read-only and holds the received data from
external sources via RXD.
MOV SBUF, #’D’ – moves ascii value of D to SBUF
MOV SBUF, A – moves accumulator value to SBUF
MOV A, SBUF – moves SBUF value to accumulator
26. SCON register – controls serial data communication (Address: 98H)
TI – set to 1 when transmission completes and indicates that 8051 is
ready to transfer another byte
RI – indicates that a byte is received and placed in SBUF register to pick
up
27.
28.
29.
30.
31. Programming the timer
Assuming that XTAL = 11.0592MHz, program Timer 0 to generate a time
delay of 2.5ms.
Soln:
Time taken for TF1 flag is raised td= 2.5ms
T = 12/11.0592 MHz = 1.085us ( Timer f = 1/12 of XTAL freq
t= 1/f )
N= td/T = (2.5ms)/(1.085us)=2304.147
The number of machine cycle elapsed : 2^16 = 65,536
65,536–N
65,536–2304(ROUNDED UP) = 63232 = F700 H
so TH0=F7
TL0=00
Program:
MOV TMOD, #01H; Timer 0 Mode 1 (16-bit)
MOV TL0, 00H
MOV TH0, F7H
SETB TR0
32. Write a program to generate a square wave of 50% duty cycle (with equal
portions high and low) on the PI.5 bit. Timer 0 is used to generate the time
delay.
33. Solution:
In the above program notice the following steps.
1.TMOD is loaded.
2.FFF2H is loaded into THO – TLO.
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 TRO” instruction.
1.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 (TFO = 1). At that point, the JNB instruction
falls through.
2.Timer 0 is stopped by the instruction “CLR TRO”. 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 timer again.
34. Example 4: Assume that XTAL = 11.0592 MHz, write a program to generate a square
wave of 2 kHz frequency on pin P1.5.
Solution:
steps.
(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 = 1A and TH = FF, all in hex.
The program is as follow.
MOV TMOD,#01 ;Timer 0, 16-bitmode
AGAIN: MOV TL0,#1AH ;TL0=1A, low byte of timer
MOV TH0,#0FFH ;TH1=FF, the high byte
CPL P1.5 ; toggle the pulse on and off
CALL DELAY
SJMP AGAIN
DELAY:
SETB TR0 ;Start timer 1
BACK: JNB TF0, BACK ;until timer rolls over
CLR TR0 ;Stop the timer 1
CLR TF0 ;Clear timer 1 flag
RET
35. Program timer 1 of 8051 to generate 1 KHz square wave on P2.1
using mode 1. Assume crystal frequency to 12 MHz.
Soln:
frequency = 1KHz
1 pulse = 1/1KHz = 1 msec
500 μsec ON time and 500 μsec OFF line.
Crystal Frequency = 12MHz f= 1/12*crystal freq = 1MHz t= 1/1MHz = 1 μsec
1 clock pulse = 1μsec.
Count = 500μsec/1μsec = 500
Counter initial value = 65536 - 500 = 65036 = FE0C
MOV TMOD, #10H; Timer 1 Mode 1 (16-bit)
MOV TL0, 0CH
MOV TH0, FEH
SETB TR0
36. INTERRUPT PROGRAMMING
An interrupt is an external or internal event that
interrupts the microcontroller to inform it that a device
needs its service.
A set of program instructions written to service an
interrupt is called the Interrupt Service Routine
8051 has six different sources of interrupts
External: Power-up reset, INT0, INT1
Internal: Timer0, Timer1, Serial Port
37. INTERRUPT PROGRAMMING
There are two methods of writing software
microcontroller can serve devices:
which
Interrupts and
Polling
Interrupt: whenever any device needs service, it notifies
the 8051 by sending an interrupt signal (microcontroller
is free to execute other tasks)
Polling: 8051 continuously monitors the status of a
device, until some pre-determined condition is met, and
then serves the device (microcontroller often wasting
time in checking the device)
38. INTERRUPT PROGRAMMING
Interrupt Service Routine:
When microcontroller receives an interrupt signal from
any of the six interrupt sources it executes a call to
interrupt service routine
For every interrupt, there must be an interrupt service
routine
The interrupt service routine for every interrupt must be
located at a fixed location in program memory, called
interrupt vector.
39. Enabling and Disabling an
Interrupt
The register called IE (interrupt enable) that is responsible
for enabling (unmasking) and disabling (masking) the
interrupts.
40. Interrupt Enable (IE) Register
• EA : Global enable/disable.
• --- : Reserved for additional interrupt hardware.
• ES : Enable Serial port interrupt.
• ET1 : Enable Timer 1 control bit.
• EX1 : Enable External 1 interrupt.
• ET0 : Enable Timer 0 control bit.
• EX0 : Enable External 0 interrupt.
MOV IE,#08h
or
SETB ET1
--
42. INTERRUPT PROGRAMMING
Enabling / Disabling Interrupt:
•Upon reset, all interrupts are disabled
•The interrupts must be enabled by software, only
then 8051 will respond to them
•A register called IE( Interrupt Enable ) is responsible for
enabling and disabling the interrupts
•Upon reset, all bits of IE register are 0
45. INTERRUPT PROGRAMMING
Programming Timer Interrupts:
If timer interrupt bit in IE register is enabled,
whenever the timer rolls over, TF flag is SET, and the
8051 is interrupted.
The interrupt service routine for timer can be placed
at interrupt vector location if it is small enough, or
elsewhere by using proper redirection at interrupt
vector location
48. 8051 Interrupt Priority upon RESET
Highest to Lowest Priority
External Interrupt 0 INT0
Timer Interrupt 0 TF0
External Interrupt 1 INT1
Timer Interrupt 1 TF1
Serial Communication RI + TI
49. LCD and Keyboard Interfacing
•The key board here we are interfacing is a matrix keyboard.
•This key board is designed with a particular rows and
columns connected to the microcontroller through its ports of
the micro controller 8051.
•We normally use 8*8 matrix key board. So only two ports of
8051 can be easily connected to the rows and columns of the
key board.
•When ever a key is pressed, a row and a column gets shorted
through that pressed key and all the other keys are left open.
•When a key is pressed only a bit in the port goes high which
indicates microcontroller that the key is pressed.
52. LCD and Keyboard
Interfacing
1.P1 as an I/P port for microcontroller 8051, port P0 as an O/P port of
microcontroller 8051 and port P2 is used for displaying the number of
pressed key.
2.Make all rows of port P0 high so that it gives high signal when key is pressed.
3.See if any key is pressed by scanning the port P1 by checking all columns for
non zero condition.
4. Initiate a counter to hold the count so that each key is counted.
5. Check port P1 for nonzero condition. If any nonzero number is there in
[accumulator], start column scanning by following step 9.
6. Otherwise make next row high in port P1.
7.Add a count of 08h to the counter to move to the next row by repeating steps
from step 6.
8.If any key pressed is found, the [accumulator] content is rotated right through
the carry until carry bit sets, while doing this increment the count in the
counter till carry is found.
9.Move the content in the counter to display in data field or to memory
location
10. To repeat the procedures go to step 2.
57. Stepper Motor Interfacing
Stepper motor is used in applications such as;
dot matrix printer, robotics etc
It has a permanent magnet rotor called the shaft which is surrounded
by a stator. Commonly used stepper motors have 4 stator windings
Such motors are called as four-phase or unipolar stepper motor.
61. 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
62. 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
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: MOV R1,#20
L2: MOV R2,#50
L1: DJNZ R2,L2
DJNZ R2,L1
RET
63. ADC, DAC and Sensor Interfacing
ADC Devices
Transducer or Sensor - converts physical quantity like temperature, pressure,
humidity, and velocity, etc., in to electrical (voltage, current) signals
Analog-to-digital converter is needed to translate the analog signals to digital
numbers, so microcontroller can read them
ADC804 IC is an analog-to-digital converter
Conversion time is defined as the time it takes the ADC to convert the analog
input to a digital (binary) number, depending upon the clocking signals applied
to CLK R and CLK IN pins.
Minimum conversion time is 110 micro seconds to convert the signals
66. ADC, DAC and Sensor Interfacing
Steps to program ADC804:
Select an analog channel by providing bits to A, B, and C addresses
67. ADC, DAC and Sensor Interfacing
Steps to program ADC804:
Activate the ALE pin It needs an L-to-H pulse to latch in the address
Activate SC (start conversion ) by an H-to-L pulse to initiate conversion
Monitor EOC (end of conversion) to see whether conversion is finished
Activate OE (output enable ) to read data out of the ADC chip
68. ADC, DAC and Sensor Interfacing
DAC Interfacing:
Digital-to-analog converter (DAC) is a device widely used to convert
digital pulses to analog signals.
DAC0808 converts the digital inputs to current (Iout), and by connecting
a resistor to the Iout pin, converts 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)
72. program to send data to the DAC to
generate a stair-step ramp
73. ADC, DAC and Sensor
Interfacing
Temperature Sensor (LM35) Interfacing:
A thermistor responds to temperature change by changing resistance
Transducer or Sensor - converts physical quantity like temperature,
pressure, humidity, and velocity, etc., in to electrical (voltage, current)
signals
Analog-to-digital converter is needed to translate the analog signals to
digital numbers, so microcontroller can read them