Timers are very important in microcontroller applications. Microcontrollers can count external pulses and accurately time them. 51 has two timers, T0 and T1, with four working modes. They use TH0, TL0 and TH1 respectively, and TL1 as special function registers. TMOD is used to set the working mode, and TCON controls the start and stop of the counter. They are all 0 when the microcontroller is reset.
TCON
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
TF1 and TF0 - overflow flag, = set to 1 when overflow
TR1 and TR0-counting operation control, =1 starts
TMOD
GATE C/T M1 M0 GATE C/T M1 M0
High 4 bits for timer 1, low 4 bits for timer 0
GATE gate control bit
When GATE=0, TR0/TR1 starts the counter; when GATE=1, TR0/TR1 and INT0/INT1 start the timer together.
C/T counting and timing mode selection
C/T=0 timer; C/T=1 counts external pin (T0 P3.4 or T1 P3.5) pulse (negative transition) counts
M1 M0 Working mode selection (0-4)
Mode 0 (M1=0, M0=0)
13-bit counter (THx low 5 bits and TLx 8 bits), 0~8191
After overflow, the software needs to set the initial count value
Method 1 (M1=0, M0=1)
16-bit counter (THx8 bits and TLx8 bits), 0~65535
After overflow, the software needs to set the initial count value
Method 2 (M1=1, M0=0)
8-bit counter (THx8 bits and TLx8 bits), 0~255
Automatically restore the initial value of the count after overflow (THx stores the initial value)
Method 3 (M1=1, M0=1)
Only applicable to T0, treat T0 as two 8-bit counters
TLx8 bits, similar to mode 0
THx8 bits, occupying TR1 and TF0 of T1
Machine cycle T = 12/fosc
Timer/Counter Initialization
Since the function of the timer/counter is determined by software programming, it is usually necessary to initialize the timer/counter before using it so that it works according to the set function. The steps for initialization are generally as follows:
1. Determine the working mode (i.e. assign value to TMOD);
2. Preset the initial value of timing or counting (the initial value can be directly written into TH0, TL0 or TH1, TL1);
3. Open the interrupt of the timer/counter as needed (assign a value to the IE bit directly);
4. Start the timer/counter (if it is specified to be started by software, TR0 or TR1 can be set to "1"; if it is specified to be started by the external interrupt pin level, the start level needs to be added to the external pin. When the start requirements are met, the timer starts counting or timing according to the specified working mode and initial value).
Example: Timer C program example
#include unsigned int t0=0; unsigned int t1=0; sbit l0=P1^0; //LED 0 sbit l1=P1^1; //LED 1 void main() { TMOD = 0x02; //t0 working mode 2; T1 working mode 1 TH0=0x00; TL0=0x00; TH1=0x00; TL1=0x00; EA=1; //Open interrupt ET0=1; //T0 interrupt is enabled ET1=1; //T1 interrupt enabled TR0=1; //Start T0 TR1=1; //Start T1 l0=0; l1=0; while(1) { //idling, waiting for interruption } } void timer0() interrupt 1 using 2 //Timer 0 interrupt, using register bank 2 { //Method 2: Dispose in TH, no need to assign initial value t0++; //Too fast, need delay if(t0>=100) { t0=0; l0=~l0; } } void timer1() interrupt 3 using 3 //Timer 1 interrupt, using register bank 2 { TH1=0; TL1=0; t1++; if(t1>=100) { t1=0; l1=~l1; } }
Previous article:Study Notes: Learning MCU from Scratch (2) Full eight-segment digital tube display
Next article:Study Notes: Learning MCU from Scratch (5) A/D Conversion
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- Let's take a look at the GNU C __attribute__ mechanism
- i.MX6ULL Terminator uses Yocto file system to develop QT-QT project compilation and testing
- Introduction to low-power Bluetooth technology solutions for lithium battery intelligent protection boards
- Sony employee stole $154 million in public funds and exchanged them for Bitcoin: when it was recovered, it appreciated to over $180 million
- Get a gift for grabbing the post: Watch the live broadcast and gain an in-depth understanding of the principle, operation, and waterproof structure design of ST's latest MEMS barometer
- CPAFC Carrier Synchronization
- When should I use a PWM controller?
- Goodbye 2019, hello 2020!
- SensorTile.box unpacking issues
- Are the EX0 bit in the IE register and the IE0 bit in the TCON register in the 51 microcontroller duplicated?