There are two timer/counters T0 and T1 inside the 8051 microcontroller, which have both timing and counting functions. T0 and T1 do not require CPU participation during the counting process and do not affect other tasks of the CPU. When the count overflows, the timer/counter gives an interrupt signal and requests the CPU to stop the current work to handle the preset interrupt event.
1. Internal structure of T1
Timer operating mode: counting the internal clock signal. Since the clock frequency is a fixed value, the timing time can be calculated based on the count value.
Counter working mode: counting external pulses applied to the T1 (P3.5) pin.
2. Counting function
The counter is used to count the number of negative transitions of input pulses from the TO (P3.4) and Tl (P3.5) pins. A negative transition means that the previous machine cycle sample is high level and the next machine cycle is low level. Every time a negative pulse transition is input, the counter increases by 1.
The high level and low level of the input pulse should be maintained for at least one machine cycle time to ensure correct sampling, so the frequency of the input pulse is at most half the internal pulse frequency of the microcontroller. If the internal pulse frequency is 1 MHZ, the maximum counting frequency is 0.5 MHz.
3. Timing function
The timing function is implemented by the microcontroller by counting internal machine pulse signals. The count value multiplied by the machine cycle is the corresponding time. For example, if the microcontroller uses a 12 MHz crystal oscillator, the internal pulse frequency of the machine is 1 MHz, and the machine cycle is 1 us. If the total count is 1000, the time taken is 1 ms.
4. Working methods
Working mode register TMOD
The working mode register TMOD is used to control the working mode and working mode of the timer/counter. The length is one byte and can only be assigned as a whole by byte.
(1) Ml and M0 are used to set the working mode of TO (T1).
(2) C/T is used to set TRO (TR1) to work in counter or timer mode.
C/T—Counter mode and timer mode selection bit
0: Timer working mode, counting the pulses of the crystal oscillator of the microcontroller divided by 12.
1: Counter working mode, the counter counts external pulses (negative transitions) of external input pin T0 (P3.4) or T1 (P3.5).
(3)GATE (gate control bit) is used to set the startup mode of TO (T1).
GATE—gate control bit.
0: Only the operation control bit TRx (x = 0, 1) controls the timer/counter operation.
1: Use the level on the external interrupt pin INT0 (or INT1) and the operation control bit TRx to control the operation of the timer/counter.
Example: Set timer 1 to the timing working mode and work in mode 2. Timer 0 is in counting mode and work in mode 1. Both start and stop are individually controlled by the program. Please provide the TMOD control word.
untie:
When timer 1 is used as a timer, D6=0; when working in mode 2, D5=1, D4=0; If the program independently controls start and stop, D7=0. If timer 0 is used as a counter, D2=1; when working in mode 1, D0=0, D1=1; If the program independently controls start and stop, D3=0. Therefore, the value of the command word TMOD should be 00100101B, which is 25H.
Timer control register TCON (Timer controller)
TCON is also an 8-bit register. The difference from TMOD is that it can be individually assigned bit by bit. The meaning of each bit is shown in the table below.
1. TF1, TF0—count overflow flag bits.
When the counter overflows, this bit is set to "1".
When using the interrupt mode, this bit is used as the interrupt request flag, and the flag is automatically cleared to "0" by the hardware. When using the query method, you should pay attention to using software to clear this bit to "0" in time after the query is valid.
2. TR1, TR0—counting operation control bits.
TR1 bit (or TR0 bit) = 1, a necessary condition to start the timer/counter operation.
TR1 bit (or TR0 bit) = 0, stop the timer/counter operation.
4.1. Working method 0
Mode 0 is a 13-bit timer/counter, which consists of the lower 5 bits of TL and the 8 bits of TH.
4.2. Working method 1
Mode 1 is a 16-bit timer/counter, consisting of 8 bits of TH and 8 bits of TL. The working principles of mode 0 and mode 1 are basically the same.
Features of Mode 0 and Mode 1:
After T1 is started, "1" is added to the initial counting value stored in TL1 and TH1 until it overflows. When overflow occurs, the T1 register is cleared, TF1 is set, and an interrupt is requested. Afterwards, if T1 is restarted, counting starts again from zero. If you want T1 to start counting from a certain value, you should assign an initial value to the counter.
If the required counting length is N, then the initial counting value X=2M-N (1≤N≤2M), where when working in mode 0, M=13, when working in mode 1, M=16. When assigning an initial value to the counter, the initial value should be split into high and low bytes and sent to TL1 and TH1 respectively.
Example: If you want to use 8051 microcontroller to control 8 LEDs to flash with a 1s cycle at the same time, design the circuit schematic and write the program.
The process of setting timer/counter:
1) First initialize the working mode register TMOD
2) Assign initial value to timer/counter
3) Start or stop through TRO or TRl in the control register TCON.
#include //Include the 8051 microcontroller header file into the file
main(void)
{
unsigned char counter; //Set an unsigned character variable to store the number of timer interrupts.
TMOD=0x01; //Set T0 as timing mode, start and stop controlled by TR0, and work in mode 1
TH0=(65536-46083)/256; //Initialize the high 8 bits of T0
TL0=(65536-46083)%256; //Initialize the lower 8 bits of T0
TF0=0; //Initialize timer overflow flag
P0=0xff; //Turn off LED
counter=0; //Start counting from 0
TR0=1; //Start timer 0
while(1)
{
while(TF0==1) //If the timer overflows
{
counter++; //Add 1 to the count count
if(counter==20) //The timing reaches 1s
{
P0=~P0; //Invert all bits of P0 to make the LED flash
counter=0; //Restart counting from 0
}
TH0=(65536-46083)/256; //Reinitialize the high 8 bits of T0
TL0=(65536-46083)%256; //Reinitialize the lower 8 bits of T0
TF0=0; //Reinitialize the timer overflow flag
}
}
}
Features of Mode 0 and Mode 1:
After T1 is started, "1" is added to the initial counting value stored in TL1 and TH1 until it overflows. When overflow occurs, the T1 register is cleared, TF1 is set, and an interrupt is requested. Afterwards, if T1 is restarted, counting starts again from zero. If you want T1 to start counting from a certain value, you should assign an initial value to the counter.
If the required counting length is N, then the initial counting value X=2M-N (1≤N≤2M), where when working in mode 0, M=13, when working in mode 1, M=16. When assigning an initial value to the counter, the initial value should be split into high and low bytes and sent to TL1 and TH1 respectively.
Example: If you want to use 8051 microcontroller to control 8 LEDs to flash with a 1s cycle at the same time, design the circuit schematic and write the program.
The process of setting timer/counter:
1) First initialize the working mode register TMOD
2) Assign initial value to timer/counter
3) Start or stop through TRO or TRl in the control register TCON.
#include //Include the 8051 microcontroller header file into the file main(void) { unsigned char counter; //Set an unsigned character variable to store the number of timer interrupts. TMOD=0x01; //Set T0 as timing mode, start and stop controlled by TR0, and work in mode 1 TH0=(65536-46083)/256; //Initialize the high 8 bits of T0 TL0=(65536-46083)%256; //Initialize the lower 8 bits of T0 TF0=0; //Initialize timer overflow flag P0=0xff; //Turn off LED counter=0; //Start counting from 0 TR0=1; //Start timer 0 while(1) { while(TF0==1) //If the timer overflows { counter++; //Add 1 to the count count if(counter==20) //The timing reaches 1s { P0=~P0; //Invert all bits of P0 to make the LED flash counter=0; //Restart counting from 0 } TH0=(65536-46083)/256; //Reinitialize the high 8 bits of T0 TL0=(65536-46083)%256; //Reinitialize the lower 8 bits of T0 TF0=0; //Reinitialize the timer overflow flag } }
4.3. Working method 2
The characteristic of method 2 is that it can automatically load the initial counting value. The 16-bit counter is divided into two parts, with TL0 as the counter and TH0 as the memory. During initialization, the initial counting value is loaded into TL0 and TH0 respectively. When the counting overflows, the initial counting value is automatically loaded from the memory TH0 to the counter TL0. If the required counting length is N, then the initial counting value X=28-N (1≤N≤256).
Example: The picture below is a counting system of a product packaging production line. When each product passes through the counting device, the mechanical rod touches the button S1 once. When a pack (5 pieces) is counted for the first time, D1 lights up, when the second pack is filled, D2 lights up..., when the eighth pack is filled, D1~D8 all light up, eight packs are packed into one box, and the above process is repeated thereafter. Write a program to implement this function.
#include //Header file containing 51 microcontroller register definition unsigned char counter; //Count initial value main(void) { TMOD=0x60; //Use T1 working mode 2 TH1=256-5; //The high 8 bits of T1 are assigned an initial value TL1=256-5; //The lower 8 bits of T1 are assigned the initial value counter=0; TR1=1; //Start T1 while(1) { while(TF1==1) //If full { TF1=0; //After the counter overflows, clear TF1 to 0 counter++; //Count plus 1 switch (counter) //Check the interrupt count value { case 1: P0=0xfe; break; //The first light is on case 2: P0=0xfd; break; //The second light is on case 3: P0=0xfb; break; //The third light is on case 4: P0=0xf7; break; //The 4th light is on case 5: P0=0xef; break; //The fifth light is on case 6: P0=0xdf; break; //The 6th light is on case 7: P0=0xbf; break; //The 7th light is on case 8: P0=0x00; counter=0; break; //All 8 lights are on } } } }
4.4. Working mode 3
TO is split into two independent 8-bit counters TLO and THO. TL0 exclusively occupies the control bits, pins and interrupt sources of T0, and can be used as either a timer or a counter. TH0 can only be used as a timer and needs to occupy the control bits TR1 and TF1 of T1 to implement start, stop and interrupt.
Previous article:How to use the C8051F series microcontroller to reduce the total power consumption of the system?
Next article:Explore the STC12C5A60S2 timer
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Zhuhai is looking for embedded engineers
- Baud rate adaptation of TI DSP chip SCI module
- Shenzhen Industry Exhibition Recruitment: Product Engineer, Technical Support Engineer
- Thank you for being there + looking forward to EEWORLD in 2020
- 1. Unboxing & First Experience with Integrated Development Environment CDK
- Playing with Zynq Serial 38——[ex57] Zynq AXI HP bus bandwidth test
- Discussion about littleFS in Micropython official forum
- [GD32E231 DIY Contest] (Part 3): TIMER2_CH1 PWM output
- SensorTile.boxr's personal experience
- Verilog Tutorial Syntax Details 2004