0% found this document useful (0 votes)
12 views37 pages

Timers

The document provides an overview of timers, UART, and interrupts in the 8051 microcontroller, detailing the functionality and programming of Timer 0 and Timer 1, including their modes of operation. It explains how to set baud rates for serial communication and the importance of specific registers like SBUF and SCON for data transmission and reception. Additionally, it includes programming examples for generating delays and transmitting data serially.

Uploaded by

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

Timers

The document provides an overview of timers, UART, and interrupts in the 8051 microcontroller, detailing the functionality and programming of Timer 0 and Timer 1, including their modes of operation. It explains how to set baud rates for serial communication and the importance of specific registers like SBUF and SCON for data transmission and reception. Additionally, it includes programming examples for generating delays and transmitting data serially.

Uploaded by

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

Timers , UART & Interrupts in

8051
8051 TIMERS
• 8051 has two timers/Counters
TIMER 0 and TIMER 1.
• Timer - generate time delays
Counter - event counters.
BASIC REGISTERS OF THE TIMER
1.TIMER REGISTERS:
It is a16 bit register
- can be accessed as 8 bit registers say Timer
High(THx) and Timer Low (TLx)
2. TMOD (Timer Mode) Register:

-8-bit register used to control the mode of operation


of both timers.
-lower two bits are used to set the timer mode and
upper two bits to specify the operation
• The hardware way of starting and stopping the
timer by an external source is achieved by making
GATE=1 in the TMOD register and if GATE=0 then
we do start and stop the timers by programming.
• C/T bit is used to decide whether a timer is used as
a time delay generator or an event counter.
• bit = 0 then it is used as a timer and if it is 1 then it
is used as a counter.
• In upper or lower 4 bits, the last bits third and
fourth are known as M1 and M0 respectively. These
are used to select the timer mode.
TIMER’S CLOCK FREQUENCY AND ITS PERIOD

• In 8051-based system, the crystal oscillator has a


frequency of 11.0592 MHz when C/T bit of
TMOD is 0.
• Each machine cycle is made up of 12 clock cycles.
• Hence for a single machine cycle, the frequency
becomes 1/12 × 11.0529 MHz = 921.6 KHz.
• For a single machine cycle, the time taken is T =
1/921.6 KHz = 1.085 us, so the oscillator takes
1.085us for completing a single machine cycle.
MODES OF OPERATION:
MODE 1:
• It is a 16-bit timer;
• It allows values from 0000 to FFFFH to be
loaded into the timer’s registers TL and TH.
• After TH and TL are loaded with a 16-bit initial
value, the timer must be started.
• We can do it by “SETB TR0” for timer 0 and “SETB TR1” for
timer 1.
• After the timer is started, it starts count up until it reaches its
limit of FFFFH.
• When it rolls over from FFFF to 0000H, it sets high a flag bit
called TFx (timer flag). This timer flag can be monitored.
• When this timer flag is raised, one option would be stop the
timer with the instructions “CLR TR0“ or CLR TR1 for timer 0
and timer 1 respectively.
• Again, it must be noted that each timer flag TF0 for timer 0
and TF1 for timer1. After the timer reaches its limit and rolls
over, in order to repeat the process the registers TH and TL
must be reloaded with the original value and TF must be reset
to 0.
MODE 0 :
• Mode 0 is exactly same like mode 1 except that it
is a 13-bit timer instead of 16-bit.
• The 13- bit counter can hold values between
0000 to 1FFFH in TH-TL.
• Therefore, when the timer reaches its maximum
of 1FFH, it rolls over to 0000, and TF is raised.
MODE 2

• It is an 8 bit timer that allows only values of 00 to FFH to


be loaded into the timer’s register TH .
• After THx is loaded with 8 bit value, the 8051 gives a copy
of it to TLx. Then the timer must be started.
• It is done by the instruction “SETB TR0” for timer 0 and
“SETB TR1” for timer1. This is like mode 1.
• After timer is started, it starts to count up by
incrementing the TLx register. It counts up until it reaches
its limit of FFH.
• When it rolls over from FFH to 00, it sets high the TFx
(timer flag). If we are using timer 0, TF0 goes high; if using
TF1 then TF1 is raised.
• When TLx register rolls from FFH to 00 and TF is set
to 1, TLx is reloaded automatically with the original
value kept by the THx register.
• To repeat the process, we must simply clear TFx and
let it go without any need by the programmer to
reload the original value.
• This makes mode 2 auto reload, in contrast in mode
1 in which programmer has to reload THx and TLx.
MODE 3:

• Mode 3 is also known as a split timer mode.


Timer 0 and 1 may be programmed to be in
mode 0, 1 and 2 independently of similar mode
for other timer.
• This is not true for mode 3; timers do not operate
independently if mode 3 is chosen for timer 0.
• Placing timer 1 in mode 3 causes it to stop
counting;
• the control bit TR1 and the timer 1 flag TF1 are
then used by timer 0.
TIMER PROGRAMMING
MODE 1 PROGRAMMING:
• It is a 16 bit Timer mode.
STEPS TO PROGRAM IN MODE 1:
• Load the TMOD value register with mode and timer 0 or 1.
• Load registers TLx and THx with initial count corresponding to
delay.
• Start the timer.
• Continuously monitor the timer flag (TFx) with the “JNB TFx,
target” instruction to see if it is raised, if it is raised(TFx=1)
then get out of the loop.
• Stop the timer.
• Clear the TFx flag for the next round.
• Go back to Step 2 to load THx and TLx again.
• Example:
• Generate Delay =10ms with Clock frequency=
11.0592 MHz, using Timer 0 in mode1.
• Solution:
Given:
Time delay=10ms ,Clock frequency=11.0592 MHz
• Step 1: Divide the desired time delay by 1.085 us
Count =10 ms/ 1.085 us=9216 =n
• Step 2: Perform 65536 – n
• 65536-9216=56320= DC00H
• Step 3: Set TH = DC H and TL = 00 H
• Following is the assembly code program to generate a
delay of 10ms.
MOV TMOD, #01 ;Timer 0, mode 1, 16-bitmode
HERE: MOV TL0, #00 ;TL0=0, the low byte
MOV TH0,#0DCH ;TH0=DC, the high byte
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
SJMP HERE
Program to generate a square wave of 5 kHz frequency on pin P1.0, clock frequency =11.0592 MHz

• Given:
• Square wave freq=5 kHz , Clock freq=11.0592 MHz
• Step 1: Calculate the Time delay
T=1/f=1/5 kHz =0.2 ms
T=0.2 ms which is the period of square wave T/2 =0.2/2=0.1
ms delay for high and low.

• Step 2: Divide the desired time delay by 1.085 us


Count=0.1ms/1.085 us = 921

• Step 3: Perform 65536 – n


TH-TL= 65536-921=64615=FC67H

• Step 4: Set TH = FC H and TL = 67 H


MOV TMOD,#10H ;Timer 1, mode 1, 16-bitmode
AGAIN: MOV TL1,#67H ; TL1=67H, low byte of timer
MOV TH1,#0FCH ;TH1=FCH, the high byte
SETB TR1 ;Start timer 1 ;
BACK: JNB TF1,BACK ; until timer rolls over
CPL P1.0 ; compliment P1.0
CLR TR1 ;Stop the timer 1
CLR TF1 ;Clear timer 1 flag
SJMP AGAIN ;Reload timer
STEPS FOR GENERATING DELAY IN MODE 2

• Select timer in TMOD register indicating which


timer (timer 0 or timer 1) is to be used, and the
timer mode (mode 2) to be selected
• THx register loaded with initial count value
• Start timer
• Continuously monitoring the timer flag (TFx) with
the JNB TFx,target instruction to see whether TFx is
‘1’ (high) .
• Get out of the loop when TF goes high
• Clear the TFx flag
• Go back to Step4, since mode 2 is auto-reload
Toggle LED connected at P1.0 with 5 microsec delay using timer1 and mode 2

MOV TMOD,#20 H ;Timer 1 mode 2, 8-bit auto


reload
AGAIN: MOV TH1,#0FBH ;TL1=256-5 = FBH, low byte of timer
SETB TR1 ;Start timer 1
BACK: JNB TF1, BACK ;until timer rolls over
CPL P1.0 ; compliment P1.0 to toggle LED
CLR TF1 ;Stop the timer 1
SJMP BACK ; loop
UART
SERIAL PORT PROGRAMMING:
• Data transfer between the PC and an 8051 system
without any error is possible, if the baud rate of the
8051 system matches the baud rate of the PC’s COM
port.
BAUD RATE IN THE 8051
• The 8051 transfers and receives data serially at many
different baud rates.
• Serial communications of the 8051 is established
with PC through the COM port.
• The baud rate in the 8051 is programmable. This is done with
the help of Timer.
• When used for serial port, the frequency of timer is determined
by (XTAL/12)/32 and 1 bit is transmitted for each timer period.
• The relationship between the crystal frequency and the baud
rate in the 8051 is that the 8051 divides the crystal frequency
by 12 to get the machine cycle frequency which is shown in
Figure 1.
• Here the oscillator is XTAL = 11.0592 MHz, the machine cycle
frequency is 921.6 kHz.
• 8051's UART divides the machine cycle frequency of 921.6 kHz
by 32 once more before it is used by Timer 1 to set the baud
rate. 921.6 kHz divided by 32 gives 28,800 Hz. Timer 1 in mode
2 is used to set the baud rate.
CALCULATION OF BAUD RATE:

• In serial communication if data transferred with a baud rate of


9600 and XTAL used is 11.0592 MHz, then following steps to
be followed to find the TH1 value to be loaded.
• Clock frequency of timer clock:
f = (11.0592 MHz / 12)/32 = 28,800Hz
• Time period of each clock tick: T0 = 1/f = 1/28800
• Duration of timer : n*T0 ->duration of 1 symbol: 1/9600
• 1/9600 = n*T0 = n*1/28800
• n = f/9600 = 28800/9600 = 3 ->TH1 =-3
• Similarly, for baud 2400
• n = f/2400 = 12 ->TH1 = -12
BAUD RATE SELECTION

• Baud rate is selected by timer1 and when


Timer 1 is used to set the baud rate it must be
programmed in mode 2 that is 8-bit, auto-
reload.
• To get baud rates compatible with the PC, we
must load TH1 with the values shown in Table
REGISTERS FOR SERIAL COMMUNICATION
SBUF (SERIAL BUFFER) REGISTER:
• It is an 8 bit register used solely for serial communication
in the 8051.
• A byte of data to be transferred via the TxD line must be
placed in the SBUF register.
• SBUF holds the byte of data when it is received by the RxD
line.
• When a byte is written, it is framed with the start and stop
bits and transferred serially via the TxD pin.
• when the bits are received serially via RxD, it is deframed
by eliminating the stop and start bits, making a byte out of
the data received, and then placing it in the SBUF.
SCON (SERIAL CONTROL) REGISTER:

• It is an 8 bit register used to program start bit,


stop bit, and data bits of data framing, among
other things.
SCON Register
STEPS TO SEND DATA SERIALLY:

• Set baud rate by loading TMOD register with the value 20H, this
indicating timer 1 in mode 2 (8-bit auto-reload) to set baud rate
• The TH1 is loaded with proper values to set baud rate for serial data
transfer
• The SCON register is loaded with the value 50H, indicating serial
mode 1, where an 8- bit data is framed with start and stop bits
• TR1 is set to 1 to start timer 1
• Check for TI flag.
• TI is cleared by CLR TI instruction
• The character byte to be transferred serially is written into SBUF
register
• The TI flag bit is monitored with the use of instruction JNB TI,xx to
see if the character has been transferred completely
• To transfer the next byte, go to step 5
Program to transfer letter “A” serially at 9800baud, continuously

MOV TMOD,#20H ; timer 1,mode 2(auto reload)


MOV TH1, # FDH ; 9600 baud rate
MOV SCON, #50H ; 8-bit, 1 stop, REN enabled
SETB TR1 ; start timer 1
AGAIN: MOV SBUF, #”A” ; letter “A” to transfer
HERE: JNB TI, HERE ; wait for the last bit
CLR TI ;clear TI for next char
SJMP AGAIN ; keep sending A
IMPORTANCE OF THE TI FLAG:

• Check the TI flag bit, we know whether or not


8051 is ready to transfer another byte.
• TI flag bit is raised by the 8051 after transfer of
data.
• TI flag is cleared by the programmer by
instruction like “CLR TI”.
• When writing a byte into SBUF, before the TI
flag bit is raised, it may lead to loss of a
portion of the byte being transferred
STEPS TO RECEIVE DATA SERIALLY:

• Set baud rate by loading TMOD register with the value 20H, this
indicating timer 1 in mode 2 (8-bit auto-reload) to set baud
rate .
• The TH1 is loaded with proper values to set baud rate.
• The SCON register is loaded with the value 50H, indicating serial
mode 1, where an 8- bit data is framed with start and stop bits
• TR1 is set to 1 to start timer 1
• RI is cleared by CLR RI instruction
• The RI flag bit is monitored with the use of instruction JNB RI,xx
to see if an entire character has been received yet.
• When RI is raised, SBUF has the byte; its contents are moved
into a safe place.
• To receive next character, go to step 5
Program to receive bytes of data serially, and put them in P2, set the baud rate at 9600,8-bit data, and 1 stop bit:

MOV TMOD, #20H ; timer 1,mode 2(auto reload)

MOV TH1, # FDH ; 9600 baud rate


MOV SCON, #50H ; 8-bit, 1 stop, REN enabled
SETB TR1 ; start timer 1
HERE: JNB RI, HERE ; wait for char to come in
MOV A, SBUF ; saving incoming byte in A
MOV P2, A ; send to port 2
CLR RI ; get ready to receive next
byte
SJMP HERE ; keep getting data
IMPORTANCE OF THE RI FLAG BIT:

• It receives the start bit, next bit is the first bit of the
character about to be received.
• When the last bit is received, a byte is formed and
placed in SBUF.
• when stop bit is received, it makes RI = 1 indicating
entire character byte has been received and can be
read before overwritten by next data.
• When RI=1, received byte is in the SBUF register,
copy SBUF contents to a safe place.
• After the SBUF contents are copied the RI flag bit
must be cleared to 0.
Interrupts
• An interrupt is an external or internal event that disturbs
the microcontroller to inform it that a device needs its
service.
• The program which is associated with the interrupt is
called the interrupt service routine (ISR) or interrupt
handler.
• Upon receiving the interrupt signal the Microcontroller ,
finish current instruction and saves the PC on stack.
• Jumps to a fixed location in memory depending on type of
interrupt Starts to execute the interrupt service routine
until RETI (return from interrupt)
• Upon executing the RETI the microcontroller returns to the
place where it was interrupted. Get pop PC from stack.
• The 8051 microcontroller has FIVE interrupts
in addition to Reset. They are
• Timer 0 overflow Interrupt
• Timer 1 overflow Interrupt
• External Interrupt 0(INT0)
• External Interrupt 1(INT1)
• Serial Port events (buffer full, buffer empty, etc)
Interrupt
• Each interrupt has a specific place in code memory where
program execution (interrupt service routine) begins.
• External Interrupt 0 : 0003 H
• Timer 0 overflow : 000B H
• External Interrupt 1 : 0013 H
• Timer 1 overflow : 001B H
• Serial Interrupt : 0023 H
• Upon reset all Interrupts are disabled & do not respond to
the Microcontroller.
• These interrupts must be enabled by software in order for
the Microcontroller to respond to them.
• This is done by an 8-bit register called Interrupt Enable
Register (IE).
Interrupt Enable Register :

• EA : Global enable/disable. To enable the interrupts


this bit must be set High.
• --- : Undefined-reserved for future use.
• ET2 : Enable /disable Timer 2 overflow interrupt.
• ES : Enable/disable Serial port interrupt.
• ET1 : Enable /disable Timer 1 overflow interrupt.
• EX1 : Enable/disable External interrupt1.
• ET0 : Enable /disable Timer 0 overflow interrupt.
• EX0 : Enable/disable External interrupt0
• Upon reset the interrupts have the following
priority(Top to down). The interrupt with the highest
PRIORITY gets serviced first.
• External interrupt 0 (INT0)
• Timer interrupt0 (TF0)
• External interrupt 1 (INT1)
• Timer interrupt1 (TF1)
• Serial communication (RI+TI)
• Priority can also be set to “high” or “low” by 8-bit IP
register.- Interrupt priority register
Interrupt Priority Register

• IP.7: reserved IP.6: reserved


• IP.5: Timer 2 interrupt priority bit (8052 only)
• IP.4: Serial port interrupt priority bit
• IP.3: Timer 1 interrupt priority bit
• IP.2: External interrupt 1 priority bit
• IP.1: Timer 0 interrupt priority bit
• IP.0: External interrupt 0 priority bit

You might also like