In 8051 microcontroller 2 timers are present (generally). Those 2 timers can also be used as counters. They are known as timer0 and timer1.
Timer count is incremented at every 1 machine cycle. Here 1 machine cycle is equal to 12 clock cycles. Thus, for clock frequency 12 MHz, timer count will increment by 1 for 1 microsecond.
How to use timer as timer:
When a particular value is loaded in timer, timer count will increment for every machine cycles and when timer count will reach maximum value, overflow flag will be set to 1 for next machine cycle. Hence to decide which value to be loaded in timer, the value corresponding to delay is subtracted from maximum value i.e. 65535. e.g. to obtain 1 millisecond delay, 1000 machine cycles are required. Hence 64535 will be loaded in timer and then we will wait for overflow flag to become 1.
How to use timer as counter:
Timer is used as counter to calculate timer difference between occurrence of 2 events. In 1 event timer as counter is started and in next event timer as counter is stopped. In between, for every machine cycle count in timer is incremented. So, by checking value in timer after it is stopped, we determine the time difference between 2 events.
Here we have to first deal with TMOD register. In TMOD register, lower 4 bits are for timer0 and higher 4 bits are for timer1.
For timer0:
bit 0 and 1 : M0 and M1 bits. They are used to select the mode of timer.
bit 3 : GATE bit controls the run/stop of timer
bit 4 to 7 works for timer 1 same as bits 0 to 3.
Mode 0 : lower 8 bits i.e. TLx works as 5 bit timer/counter and THx works as 8 bit timer/counter.
Mode 1 : 8 bit TLx and 8 bit THx.
Mode 2 : 8 bit autoreload mode. when value in TLx is overflows, value in THx is loaded in TLx.
Mode 3 : Timer1 is stopped and Timer0 is used used as 2 separate 8 bit timers/counters.
Next register to be considered is TCON register.
bit 0 : IT0 - control bit for interrupt 0. It is set or cleared by software and at its falling edge, external interrupt 0 is triggered.
bit 1 : IE0 - flag bit for interrupt 0. It is set or cleared by hardware when external interrupt edge/low level is detected. it can be used to check if external interrupt 0 is triggered or not.
bit 2 and bit 3 are IT1 and IE1 for interrupt 1. they work same as IT0 and IE0.
bit 4 : TR0 - set or cleared to turn on or off timer0
bit 5 : TF0 - flag bit is set when timer0 overflows.
bit 6 and bit 7 are TR1 and TF1 for timer1. They work same as TR0 and TF0.
The timer value is loaded through THx and TLx.
We have to load THx, TLx, TMOD and TCON registers properly to use timer in 8051 microcontroller.
Click here for Timer as timer program
Timer count is incremented at every 1 machine cycle. Here 1 machine cycle is equal to 12 clock cycles. Thus, for clock frequency 12 MHz, timer count will increment by 1 for 1 microsecond.
How to use timer as timer:
When a particular value is loaded in timer, timer count will increment for every machine cycles and when timer count will reach maximum value, overflow flag will be set to 1 for next machine cycle. Hence to decide which value to be loaded in timer, the value corresponding to delay is subtracted from maximum value i.e. 65535. e.g. to obtain 1 millisecond delay, 1000 machine cycles are required. Hence 64535 will be loaded in timer and then we will wait for overflow flag to become 1.
How to use timer as counter:
Timer is used as counter to calculate timer difference between occurrence of 2 events. In 1 event timer as counter is started and in next event timer as counter is stopped. In between, for every machine cycle count in timer is incremented. So, by checking value in timer after it is stopped, we determine the time difference between 2 events.
Here we have to first deal with TMOD register. In TMOD register, lower 4 bits are for timer0 and higher 4 bits are for timer1.
For timer0:
bit 0 and 1 : M0 and M1 bits. They are used to select the mode of timer.
M0 M1 = 0 0 => mode 0
M0 M1 = 0 1 => mode 1
M0 M1 = 1 0 => mode 2
M0 M1 = 1 1 => mode 3bit 2 : C/~T - When this bit is 0, timer works as timer and when this bit is 1, timer works as counter
bit 3 : GATE bit controls the run/stop of timer
bit 4 to 7 works for timer 1 same as bits 0 to 3.
Mode 0 : lower 8 bits i.e. TLx works as 5 bit timer/counter and THx works as 8 bit timer/counter.
Mode 1 : 8 bit TLx and 8 bit THx.
Mode 2 : 8 bit autoreload mode. when value in TLx is overflows, value in THx is loaded in TLx.
Mode 3 : Timer1 is stopped and Timer0 is used used as 2 separate 8 bit timers/counters.
Next register to be considered is TCON register.
bit 0 : IT0 - control bit for interrupt 0. It is set or cleared by software and at its falling edge, external interrupt 0 is triggered.
bit 1 : IE0 - flag bit for interrupt 0. It is set or cleared by hardware when external interrupt edge/low level is detected. it can be used to check if external interrupt 0 is triggered or not.
bit 2 and bit 3 are IT1 and IE1 for interrupt 1. they work same as IT0 and IE0.
bit 4 : TR0 - set or cleared to turn on or off timer0
bit 5 : TF0 - flag bit is set when timer0 overflows.
bit 6 and bit 7 are TR1 and TF1 for timer1. They work same as TR0 and TF0.
The timer value is loaded through THx and TLx.
We have to load THx, TLx, TMOD and TCON registers properly to use timer in 8051 microcontroller.
Click here for Timer as timer program
No comments:
Post a Comment