Timer and Counter are input devices that are less noticed by microcontroller users. With these input devices, microcontrollers can easily be used to measure pulse width, generate pulses with a certain width, used in PWM (Pulse Width Modulation) voltage control and are very necessary for infrared remote control applications.
Basically, this input device is a set of binary counters that are directly connected to the microcontroller data channel, so that the microcontroller can read the position of the counters, if necessary the microcontroller can also change the position of the counters.
Like a binary counter, when the pulse signal (clock) that is fed exceeds the counter capacity, then at the end of the counter chain an overflow signal will appear, this signal is a very important thing in the use of the counter. The occurrence of this counter overflow is recorded in a separate flip-flop.
In addition, the pulse signal fed to the counter must also be easily controlled. The above points are summarized in Figure 1.
The MCS-51 has two 16-bit timer/counter registers, namely Timer 0 and Timer 1. Both can be configured to operate as timers or counters, as seen in the figure below.
Figure 1.9. Basic concept of Timer/Counter as an input device
The pulse signal fed to the counter can be divided into 2 types, the first is a pulse signal with a fixed frequency whose magnitude is known and the second is a pulse signal with a variable frequency.
If a counter works with a fixed frequency whose value is known, the counter is said to work as a timer, because the position of the counter is equivalent to time which can be determined with certainty.
If a counter works with an inconsistent frequency, it is said that the counter works as a counter, the position of the counter only states the number of pulses that the counter has received.
The binary counter string used can be an ascending binary counter (count up binary counter) or a descending binary counter (count down binary counter).
1. Timer/Counter Facility
The MCS51 microcontroller family, for example the AT89C51 and AT89Cx051, are equipped with two Timer/Counter devices, each named Timer 0 and Timer 1. While for the larger type, for example the AT89C52, it has an additional Timer/Counter device named Timer 2.
The Timer/Counter device is a hardware device that is integrated into the MCS51 microcontroller chip. For MCS51 microcontroller users, this device is known as SFR (Special Function Register) which functions as internal data memory.
The binary counter for Timer 0 is formed with the TL0 register (Timer 0 Low Byte, internal data memory number $6A) and the TH0 register (Timer 0 High Byte, internal data memory number $6C). The binary counter for Timer 1 is formed with the TL1 register (Timer 1 Low Byte, internal data memory number $6B) and the TH1 register (Timer 1 High Byte, internal data memory number $6D). The binary counter forming the MCS51 Timer/Counter is a count up binary counter that counts from $0000 to $FFFF, when the counter position changes from $FFFF back to $0000 an overflow signal will occur.
To regulate the operation of the Timer/Counter, 2 additional registers are used which are shared by Timer 0 and Timer 1. These additional registers are the TCON register (Timer Control Register, internal data memory number $88, can be addressed bit by bit) and the TMOD register (Timer Mode Register, internal data memory number $89).
Timer 0 and 1 binary counter
TL0, TH0, TL1 and TH1 are SFR (Special Function Register) which are used to form the binary counter of Timer 0 and Timer 1 devices. The capacity of the four registers is 8 bits each, and can be arranged into 4 types of binary counter modes as shown in Figure 2a to Figure 2d.
In Mode 0, Mode 1 and Mode 2, Timer 0 and Timer 1 each work independently, meaning that Timer 0 can work in Mode 1 and Timer 1 can work in Mode 2, or other combinations of modes as needed.
In Mode 3 TL0, TH0, TL1 and TH1 are used together to form a timer system that cannot be combined in any other way.
The arrangement of TL0, TH0, TL1 and TH1 in each mode is as follows:
Mode 0 -- 13 bit Binary Counter
Figure 1.10 Mode 0 - 13 Bit Binary Counter
The binary counter is formed with TLx (meaning either TL0 or TL1) as a 5-bit binary counter (although the actual capacity is 8 bits), the overflow from this 5-bit binary counter is connected to THx (meaning either TH0 or TH1) forming a 13-bit binary counter chain, the overflow from this 13-bit counter is stored in the TFx flip-flop (meaning either TF0 or TF1) which is in the TCON register.
This mode continues the Timer facility on the MCS48 microcontroller (the predecessor of the MCS51 microcontroller), with the intention that the design of the device made with the MCS48 can be easily adapted to the MCS51. This mode is not widely used anymore.
This mode is the same as Mode 0, except that the TLx register is fully used as an 8-bit binary counter, so that the capacity of the binary counter formed is 16 bits. Along with the pulse signal, the position of this 16-bit binary counter will move from $0000 (binary 0000 0000 0000 0000), $0001, $0002 ... to $FFFF (binary 1111 1111 1111 1111), then overflows back to $0000.
Mode 2 -- 8 bit Binary Counter with Reload
Figure 1.12 Mode 2 - 8 Bit Binary Counter with Reload
TLx is used as an 8-bit binary counter, while THx is used to store a value that is reloaded into TLx, every time the TLx state overflows (changes from $FF to $00). In this way, an overflow signal can be obtained whose frequency is determined by the value stored in TH0.
Mode 3 -- Combined 16 bit and 8 bit binary counters
Figure 1.13 Mode 3 -- Combined 16 Bit and 8 Bit Binary Counters
In Mode 3 TL0, TH0, TL1 and TH1 are used to form 3 counter chains, the first is a 16-bit binary counter chain without overflow signal monitoring facility formed by TL1 and TH1. The second is TL0 which is used as an 8-bit binary counter with TF0 as the overflow monitoring facility. The third binary counter is TH0 which is used as an 8-bit binary counter with TF1 as the overflow monitoring facility.
Timer Control Register
The TMOD register and the TCON register are assistant registers to regulate the operation of Timer 0 and Timer 1. These two registers are used together by Timer 0 and Timer 1.
Figure 1.14 Bit layout in the TMOD register
The TMOD register is divided into 2 parts symmetrically, bits 0 to 3 of the TMOD register (TMOD bit 0 .. TMOD bit 3) are used to set Timer 0, bits 4 to 7 of the TMODE register (TMOD bit 4 .. TMOD bit 7) are used to set Timer 1, its usage is as follows:
- Bit M0/M1 is used to determine the Timer Mode as shown in the Table in Figure 3a.
- The C/T* bit is used to set the source of the pulse signal fed to the binary counter. If C/T = 0 the pulse signal is obtained from a crystal oscillator whose frequency has been divided by 12, while if C/T = 1 then the pulse signal is obtained from the T0 leg (for Timer 0) or the T1 leg (for Timer 1).
- The GATE bit is the bit that controls the pulse signal channel. If the GATE bit = 0, the pulse signal channel is only controlled by the TRx bit (meaning TR0 or TR1 in the TCON register). If the GATE bit = 1, the INT0 leg (for Timer 0) or INT1 leg (for Timer 1) is also used to control the pulse signal channel (see Figure 4).
Figure 1.15 Bit layout in the TCON register
The TCON register is divided into 2 parts, the first 4 bits (bit 0 .. bit 3, the shaded part in Figure 3b) are used for the purpose of setting the INT0 and INT1 pins, these four bits are discussed in another section.
| MSB | | | | | | | LSB |
|-----|-----|-----|-----|-----|-----|-----|-----|
| TF1 | TR1 | TF0 | TR0 | IE1 | IT1 | IE0 | IT0 |
| BIT | SYMBOL | FUNCTION |
|--------|--------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| TCON.7 | TF1 | Timer 1 overflow flag. Set by hardware on Timer/Counter overflow. Cleared by hardware when processor vector to interrupt routine, or clearing the bit in software. |
| TCON.6 | TR1 | Timer 1 Run control bit . Set/ cleared by software to turn Timer/ Counter on/off |
| TCON.5 | TF0 | Timer 0 overflow flag. Set by hardware on Timer/Counter overflow. Cleared by hardware when processor vector to interrupt routine, or clearing the bit in software. |
| TCON.4 | TR0 | Timer 1 Run control bit . Set/ cleared by software to turn Timer/ Counter on/off |
| TCON.3 | IE1 | Interrupt 1 Edge flag. Set by hardware when external interrupt edge detected. Cleared when interrupt processed. |
| TCON.2 | IT1 | Interrupt 1 type control bit. Set/ cleared by software to specefy falling edge/ low level trigerred external interupts |
| TCON.1 | IE0 | Interrupt 0 Edge flag. Set by hardware when external interrupt edge detected. Cleared when interrupt processed. |
| TCON.0 | IT0 | Interrupt 0 type control bit. Set/ cleared by software to specefy falling edge/ low level trigerred external interupts |
The remaining 4 bits of the TCON register (bit 4..bit 7) are divided into 2 symmetrical parts which are used to set Timer0/Timer 1, as follows:
1. TFx bit (meaning TF0 or TF1)
As an overflow bit (see Figure 2), TFx will be '1' whenever the binary counter connected to it overflows (the counter changes from $FFFF back to $0000). The TFx bit is zeroed by the CLR TF0 or CLR TF1 instruction. If the interrupt facility of Timer 0/Timer 1 is used, TRx is zeroed when the MCS51 executes the interrupt service routine (ISR).
2. TRx bit (meaning TR0 or TR1)
It is a bit that controls the pulse signal channel, if this bit = 0 the pulse signal is not distributed to the binary counter so that the counter stops counting. If the GATE bit in the TMOD register = 1, then this pulse signal channel is regulated together by TRx and the signal on the INT0/INT1 leg (see Figure 4).
2. Set the Timer
Figure 4 is a complete diagram of the circuit that can occur in Timer 1, also showing the relationships between all registers that form and control Timer 1. This image also applies to Timer 0.
In actual use, the circuit used is only part of the complete circuit, according to the needs of the system being built. The desired circuit is formed by setting the TMODE register, while the Timer's work is controlled through the TCON register.
Figure 1.16. Timer/counter block diagram
After the MCS51 is reset the TMOD register is set to $00, this means:
- bit C/T* ='0', according to Figure 4 this condition makes switch S1 to the upper position, the pulse signal source comes from a crystal oscillator whose frequency has been divided by 12, the binary counter formed by TL1 and TH1 functions as a timer. If the designed system does require Timer 1 to work as a timer then bit C/T* does not need to be set again. But if the designed system requires Timer 1 to work as a counter to count the pulses that enter through leg T1 (P3.5), then the position of switch S1 must be lowered by making bit C/T* become '1'.
- GATE bit = '0', this makes the OR gate output always '1' regardless of the '0' or '1' state on the INT1 leg (P3.3). In this state, switch S2 is only controlled via the TR1 bit in the TCON register. If TR1 = '1' switch S2 is closed so that the pulse signal from S1 is channeled to the binary counter system, the pulse signal flow will be stopped if TR = '0'. Conversely if the GATE bit = '1', the OR gate output will follow the state of the INT1 leg, when INT1 = '0' whatever the state of the TR1 bit the AND gate output is always = '0' and switch S1 is always open, so that switch S1 can be closed the INT1 leg and the TR1 bit must = '1' simultaneously. If the designed system requires the work of the timer / counter to be controlled from a signal originating from outside the chip, then the GATE bit must be made to '1'
- Bit M1 and M0 = '0', meaning TL1 and TH1 are arranged into a 13 bit binary counter (Mode 0), if Timer 1 is desired to work in mode 1 as shown in Figure 4, then bit M1 must be made to '0' and bit M0 to '1'.
- The above knowledge is used as a basis for setting and controlling the Timer as seen in the following examples:
After resetting, TMOD is valued at $00, meaning Timer 1 works as a 13-bit binary counter, the pulse signal source from the crystal oscillator or Timer 1 works as a 'timer', the GATE = '0' bit means that the INT1 leg has no effect on the circuit so that Timer 1 is only controlled from the TR1 bit.
In normal use, a 16-bit binary counter is used, for this purpose the instructions required to set the TMOD are:
MOV TMOD,#010000
Note in the instruction above the '#' sign indicates the part behind it is a constant number that will be filled into TMOD, '%' is a prefix that indicates that the number behind it is a binary number. Writing with binary numbers like this makes it easy to quickly recognize what bits are filled into TMOD.
The binary number 010000 is loaded into the TMOD, resulting in bit 7 of the TMOD (GATE bit) being '0', bit 6 (bit C/T*) being '0', bits 5 and 4 (bits M1 and M0) being '01', these four bits are used to set Timer 1, so that Timer 1 works as a timer with a 16-bit binary counter controlled only by TR1.
If you want the binary counter to be used as a counter to count the number of pulses that come in through pin T1 (P3.5), the instruction becomes:
MOV TMOD,#%01010000
The difference with the instruction above is that in this instruction bit 6 (bit C/T*) has a value of '1'. Furthermore, if you want a signal from hardware outside the MCS51 chip to be able to control Timer 1, the Timer 1 control instruction will be:
MOV TMOD,#%11010000
In this case bit 7 (GATE bit) is '1'.
After setting the Timer 0 configuration as above, the binary counter does not start counting until it is commanded with the instruction:
SETB TR1
It should be noted that if the GATE bit = '1', as long as the INT1 leg is '0' the binary counter will not count. To stop the counting process, the CLR TR1 instruction is used.
Above only Timer 1 is discussed, the layout for Timer 0 is exactly the same. What needs to be noted is that the TMOD register is used to set Timer 0 and also Timer 1, while TMOD cannot be addressed bit by bit (non bit addressable) so that if both Timers are used, filling the bits in the TMOD register must be considered at once for Timer 0 and Timer 1.
The TR1 and TR0 bits used to control the counting process are located in the TCON register (internal data memory number $88) which is bit addressable. So TR0 and TR1 can be set separately (with the SETB or CLR command), unlike setting TMOD which must be done simultaneously.
Likewise, the overflow bits of the binary counters TF0 and TF1 are also located in the TCON register, each of which can be monitored separately.
As shown in Figure 1.11, the microcontroller can act as a timer or counter, as needed. Pay attention to the left and right switches in the block diagram. The microcontroller will function as a timer when the switch is positioned up and vice versa will function as a counter when the switch is positioned down, by controlling the C/T bit in the TMOD register. The position of the right switch depends on the GATE bit (TMOD register), TR1 (TCON0 and INT1 registers).
Timer & Counter Microcontroller Experiment
OBJECTIVE
- Students understand the function of timers and counters on microcontrollers
- Students understand the interface circuit for timer and counter applications.
- Students can use the counter function to count credits.
- Students can use the timer function to generate a clock with a certain period.
Counter Function
In the MCS-51 family there are two 16 bit timers/counters, which can be configured as timers or counters, 8 bit, 13 bit or 16 bit according to the mode selected. The following image is an example of an 8 bit Counter application using mode 3.
Counter Function
Experiment 9.1. Mode 3 application as an 8-bit counter with LED output
In this experiment, pulses are taken through a clock generator generated by the IC 555 which has a frequency of 1 Hz, the binary counting results in the counter register will be displayed on the LED.
Mode 3 application as 8 bit counter with LED output
Do some trial steps as follows:
- Connect 1 cable between P3.5 and the clock output of IC 555.
- Connect the LED_EN connector jumper
- Connect the Microcontroller Trainer module to the +5V power supply.
- Connect the Microcontroller Trainer module to the programmer circuit
- Open the M-IDE Studio for MCS-51 program, as a program editor and compiler.
- Type the following program: ( download file prog91a.asm )
org 0h
Start: Mov TMOD,#01110000b ; mode 3 counter 8 bit counter 1
Setb TR1 ; TR1 = 1, start counting
Get: Mov A, TL1 ; A = TL1
CPL A ; A = NOT A
Mov P0, A ; P0 = A
Sjmp Get ; Looping Forever
End
- Save the program you typed and name it: prog91a.asm
- In the MIDE program, select Build /F9 or to compile the program from *.asm to *.hex.
- Program the microcontroller using the ISP Software Program (See Instructions for Use)
- Make observations on the LED ? and complete the following table.
| INPUT Clock | DISPLAY LED | | | | | | | |
|-------------|-------------|----|----|----|----|----|----|----|
| | D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8 |
| 1 | | | | | | | | |
| 2 | | | | | | | | |
| 3 | | | | | | | | |
| 4 | | | | | | | | |
| : | | | | | | | | |
| 20 | | | | | | | | |
Experiment 9.2. Timer 1, Mode 3 application as an 8-bit counter with 7-segment output.
In this experiment, the pulse is taken through a clock generator generated by IC 555 which has a frequency of 1 Hz, the binary count results on the counter register will be displayed on 7 Segments. Do the following experimental steps:
- Connect 1 cable between P3.5 and the clock output of IC 555.
- Connect the 7Segment_EN connector jumper
- Connect the Microcontroller Trainer module to the +5V power supply.
- Connect the Microcontroller Trainer module to the programmer circuit
- Open the M-IDE Studio for MCS-51 program, as a program editor and compiler.
- Type the following program: ( download file prog92a.asm )
Org 0h Mov TMOD,#01110000b ; mode 3 counter 8 bit timer 1 Setb TR1 ; TR1 = 1, start counting Get: Mov A, TL1 ; A = TL1 Call Bin2Dec Call Display7Segmen Sjmp Get ;Looping Forever ; Bin2Dec: mov b,#100d div ab mov hundreds,a mov a,b mov b,#10d div ab mov tens,a mov ones,b ret ; Display7Segmen: clr P3.5 clr P3.6 clr P3.7 mov A, Hundreds mov DPTR,#Data7segmen movc A,@A+DPTR mov P0,A call delay ; setb P3.5 clr P3.6 clr P3.7 mov A, Tens mov DPTR,#Data7segmen movc A,@A+DPTR mov P0,A call delay ; clr P3.5 ; setb P3.6 clr P3.7 mov A, Ones mov DPTR,#Data7segmen movc A,@A+DPTR mov P0,A call delay ret ; Data7segmen: db 11000000b,11111001b,10100100b,10110000b,10011001b db 10010010b,10000010b,11111000b,10000000b,10010000b ; end
- Save the program you typed and name it: prog92a.asm
- In the MIDE program, select Build /F9 or to compile the program from *.asm to *.hex.
- Program the microcontroller using the ISP Software Program (See Instructions for Use)
- Make observations on the 7 segment display? and complete the following table.
| Clock ke | Display 7 Segmen |
|----------|------------------|
| 1 | ? |
| 2 | ? |
| 3 | ? |
| 4 | ? |
| 5 | ? |
| : | : |
| 20 | ? |
Timer Function
In the MCS-51 family there are two 16 bit timers/counters, which can be configured as timers or counters, 8 bit, 13 bit or 16 bit according to the selected mode. The following image is an example of a 16 bit Counter application using mode 1.
Timer Function
Experiment 9.3. Timer 1 application, as a 16-bit timer with LED output
In this experiment, the clock will be generated using a 16-bit timer function, with a period of 1 second, Ton = 0.5 seconds and Toff = 0.5 seconds, with an LED display.
In this mode, with a 12MHz crystal, the timer will overflow every 65,536 useconds. In this experiment, to generate an interrupt every 1000 useconds, the data that must be filled in the TL1 and TH1 registers are as follows:
65536 - 10000 = 55536 d or D8F0h
Then the TF1 interrupt will be generated immediately every 1000 x 1 usecond = 0.01 seconds, in this programming, RO is implemented as a software counter, Register R0 will increment every Timer 1 overflow. If register R7 has detected a value of 50 then port P0.0 (LED D0) will light up for = 0.01 x 50 seconds = 0.5 seconds.
Do some trial steps as follows:
- Remove the cable that connects P3.5 to the IC 555 clock output
- Connect the LED_EN connector jumper
- Connect the Microcontroller Trainer module to the +5V power supply.
- Connect the Microcontroller Trainer module to the programmer circuit
- Open the M-IDE Studio for MCS-51 program, as a program editor and compiler.
- Type the following program: ( download file prog93a.asm )
Org 0h Start: Setb P0.0 ;P0.0 = 1 call Delay ;call delay time Clr P0.0 ;P0.0 = 0 call delay ;call delay time Sjmp Start ;Looping Forever ; Delay: Mov R0,#0 ;R0 = 0 Mov TMOD,#00010000b ;Mode 1, Fungsi =Timer 1 16 bit Load: Mov TH1, #0D8h ;TH1 = D8h Mov TL1, #0F0h ; TL1 = F0h Setb TR1 ; TR1 = 1, Start Running OFlow: JNB TF1, OFlow ; jump to OFlow if TF1 =0 Clr TR1 ; TR1 = 0 Clr TF1 : TF1 = 0 Inc R0 ; R0 = R0+1 CJNE R0,#50,Load Ret ; End
- Save the program you typed and name it: prog93a.asm
- In the MIDE program, select Build /F9 or to compile the program from *.asm to *.hex.
- Program the microcontroller using the ISP Software Program (See Instructions for Use)
- Make observations on the LED light
- Modify the program to generate frequencies of 10 Hz, 100 Hz, and 500 Hz and observe them on the oscilloscope screen.