Timer/Counter Microcontroller 8051
8051 has two 16-bit programmable UP timers/counters, T0 and T1 respectively. we know 8051 processes on 8 bit data hence we have access to timer using 8-bit special function registers, TL0 and TH0 for timer 0 and TL1 and TH1 for timer 1 respectively. We can use timer as an timer or counter. If we use timer as a timer then then clock source is on chip oscillator otherwise if we want to use timer as a counter then clock source is external clock.
When we want to create delay using timer then we set some value in respective SFR's of timer. When timer is started the value of SFR i.e value of count gets incremented in every machine cycle. Thus, one can think of it as counting machine cycles. Hence we can say the clock rate for timer is 1/12 th of the oscillator frequency.
When we use timer as a counter, the respective SFR is incremented in response to a 1 to 0 transition at its corresponding external input pin (T0 or T1). It requires 2 machine cycles to detect a high to low transition. Hence maximum count rate is 1/24 th of oscillator frequency. To control operation of the timers/counter 2 special function registers, TMOD and TCON are used.
Timer Mode control (TMOD): TMOD is 8 bit register , it is not bit addressable. The lower 4 bits are used for timer 0 control operation while upper 4 bits are used to control operation of timer 1.
Gate : Gate bit set to '1' when we want start /stop control of timer using external interrupt otherwise it is set to '0' if we want to start/stop using software instruction like SETB TR1/TR0 and CLR TR1/TR0
C/T bar : When this bit is '1' then counter mode is selected otherwise when it is '0' timer mode get selected.
Mode Select Bits: M1 and M0 are mode select bits.There are four modes as shown in table above:
Mode 0 : 13 bit mode, The lower 5 bits of TLX and 8 bits of THX are used for the 13 bit count
Mode 1 : This mode 16-bit mode.The 8 bits of TLX and 8 bits of THX are used for the 16 bit count.
Mode 2 :This mode is 8 bit auto reload mode 8 bit of TLX are used for counting operation while THX stores a constant value.
Mode 3 : Timer mode 3 is split timer mode.
TCON register : TCON is another register which we use to control timer/counter operation. funcion of each bit is shown below:
TF1: Timer 1 overflow flag bit, it is automatically set on timer overflow.
TR1: This bit used to start the Timer 1. When we set this bit equal to 1, timer 1 will start.
TF0: Timer 0 overflow flag bit, it is automatically set on timer overflow.
TR0: This bit used to start the Timer 0. When we set this bit equal to 1, timer 0 will start.
IT1: This bit used to set timer 1 interrupt edge sensitive by setting this pin to high.
IR1: This pin indicates edge detection flag when negative going edge is detected on timer 1 pin of port 3.
IT0: This bit used to set timer 0 interrupt edge sensitive by setting this pin to high.
IR0: This pin indicates edge detection flag when negative going edge is detected on timer 0 pin of port 3.
If we want to timer as a timer , it will count the on chip clock frequency of 8051 oscillator divided by 12. e.g if the crystal frequency is 6.0 megahertz, then we will gate timer clock frequency of 500 kilohertz. The resultant timer clock is gated to the timer by means of the circuit shown in Figure 11. In order to oscillator clock pulses to reach the timer we have to clear the C/T bit in the TMOD register. We must set Bit TRX in the TCON register to '1' to run the timer run, and we should clear gate bit in the TMOD register, or external pin (INTX) must be a 1 . In short, the timer is configured as a timer, then the timer pulses are gated to the timer by the run bit and the gate bit or the external input bits (INTX).
Timer Mode 0 : Timer mode "0" is a 13-bit timer. 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. This also means, in essence, the timer can only contain 8192 values. If you set a 13-bit timer to 0, it will overflow back to zero 8192 machine cycles later.
Timer Mode 1 : Timer mode "1" is a 16-bit timer. This is a very commonly used mode. It functions just like 13-bit mode except that all 16 bits are used.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. Timer Mode 2 : Timer mode "2" is an 8-bit auto-reload mode. What is that, you may ask? Simple. When a timer is in mode 2, THx holds the "reload value" and TLx is the timer itself. Thus, TLx starts counting up. 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.
Timer Mode 3 : Timer mode "2" is an 8-bit auto-reload mode.When a timer is in mode 2, THx holds the "reload value" and TLx is the timer itself. Thus, TLx starts counting up. 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.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--however, you may not start or stop the real timer 1 since the bits that do that are now linked to TH0. The real timer 1, in this case, will be incremented every machine cycle no matter what.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--however, you may not start or stop the real timer 1 since the bits that do that are now linked to TH0. The real timer 1, in this case, will be incremented every machine cycle no matter what.
Comments
Post a Comment