Timers
Timers
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:
• 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.
• 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
• 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:
• 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 :