The timer is a very important function for each MCU. To implement a precise timer, measure the frequency, and generate a PWM, the timer depends on the timer. The number of timer digits determines the timer precision, currently, we have encountered an 8-bit timer, a 16-bit timer, and a 32-bit timer. Features are more powerful than others. The higher the number of digits, the higher the accuracy of PWM, the more delicate the changes, easy to control. For at89x51, there are two timers T0 and T1, And the 52 microcontroller adds a T2. Each timer has a four-digit working mode. The first is a 13-bit timer, the second is a 16-bit timer, and the third is an 8-bit automatic heavy load timer. The fourth type is the two eight-bit timers separated by t0. T1 stops working.
If you want to skillfully use these things, you don't rely on rote memorization. Things are dead and live. To learn this kind of things, you can debug and read the header file, and study the header file and debugging window, so that you can quickly master a single-chip microcomputer. After reading the data manual, you can easily understand them.
This figure shows a lot of things clearly. The timer oscillator frequency is the 12-frequency division of the crystal oscillator, which means that when we have a 12 MHz crystal oscillator at ordinary times, every counter of the timing is 1us. it can be seen that the 16-bit timer is actually composed of two 8-bit timer. Finally, overflow is set to tf1. as for the external interrupt, the pulse width of the square wave is measured.
# Include <reg52.h> void t0_init (void) {tmod | = 0x01; // scheduled working mode and scheduled or counting th0 =-50000> 8; tl0 =-50000% 255; // allocate a number to two 8-bit registers, et0 = 1; // enable the partial interrupt enabling of the timer 0} void main (void) {t0_init (); EA = 1; // enable global interrupt tr0 = 1; // The timer starts counting for (;) // wait for the timer to overflow and interrupt. This is the method of timer interruption. It is hardware. We can use the query method, that is, software. Determine tf0, {pcon = 0x01; // stop CPU activity, saving energy. Reduce power consumption. } Void t0_overflow_isr (void) interrupt 1 // interrupt the serviceProgram{Th0 =-50000> 8; tl0 =-50000 & 255; // The timer function of 51 is weak. You must manually reinstall the software ,. However, each time the count starts from 0 to 0 xFFFF P1 ^ = 0x01; // retrieve the reverse p1.0 port each time .}
It is worth noting that the 51 timer is a timer that is always added up until it overflows, while many other single-chip microcomputer timer modes, set a value for comparison, or overflow, whether it is always counting until the second trigger, or subtraction, and so on, do not think that the single-chip microcomputer has only one timer overflow. Think about it and update it occasionally.