Now that we know the registers related to the timer, let's make a timer program to consolidate what we have learned. Our program in this class will first use timer 0. When using a timer, the following steps are required:
Step 1: Set the special function register TMOD and configure the working mode.
Step 2: Set the initial value of counting registers TH0 and TL0.
Step 3: Set TCON and set TR0 to 1 to start the timer counting.
Step 4: Check the TF0 bit of the TCON register to monitor the timer overflow.
Before writing a program, we must first learn how to calculate how to use a timer to set time. Our crystal oscillator is 11.0592M, the clock cycle is 1/11059200, and the machine cycle is 12/11059200. If we want to set a time of 20ms, that is 0.02 seconds, we need x machine cycles to get 0.02 seconds. Let's calculate x*12/11059200=0.02, and get x= 18432. The overflow value of a 16-bit timer is 65536 (because 65535 plus 1 is an overflow), so we can do this, first give TH0 and TL0 an initial value, let them reach 65536 after 18432 machine cycles, that is, overflow, and after overflow, we can know by detecting the value of TF0, which is exactly 0.02 seconds. Then the initial value y = 65536 - 18432 = 47104, which is converted into hexadecimal as 0xB800, that is, TH0 = 0xB8, TL0 = 0x00.
In this way, we have made a 0.02 second timer. Careful students will find that if the initial value is directly 0x0000, until 65536 overflows, the maximum timer value is about 71ms. So what should we do if we want to time longer? The logic you learned in elementary school and the relationship between multiples can solve this problem.
Okay, let's use a program to implement this function.
#include
sbit LED = P0^0;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
void main(){
unsigned char cnt = 0; //Define a counting variable to record the number of T0 overflows
ENLED = 0; // Enable U3 and select independent LED
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
TMOD = 0x01; //Set T0 to mode 1
TH0 = 0xB8; //Assign the initial value 0xB800 to T0
TL0 = 0x00;
TR0 = 1; //Start T0
while (1){
if (TF0 == 1){ //Judge whether T0 overflows
TF0 = 0; //After T0 overflows, clear the interrupt flag
TH0 = 0xB8; // and re-initialize
TL0 = 0x00;
cnt++; //Count value increases by 1
if (cnt >= 50){ //Judge whether T0 overflows 50 times
cnt = 0; //The count value is cleared after reaching 50 times
LED = ~LED; //LED inversion: 0-->1, 1-->0
}
}
}
}
There are comments in the program. Combined with the content learned in the previous chapters, it is not difficult to understand it by analyzing it yourself. The result achieved by this program is that the small light on the far right of the development board lights up for one second and turns off for one second, that is, it flashes at a frequency of 0.5Hz.
Previous article:MCU timer register
Next article:The truth table of the single chip digital tube
- 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
- How to get the free page block of STM32' MCU internal flash
- Using MicroPython to run app-like functions on a single chip
- What software do you usually use when doing circuit simulation?
- AM335x Evaluation Board Quick Test (3)
- The "GaN-do" attitude of the network
- Industry Consulting
- Problems with the ping command
- About LoRa networking issues?
- Comparison of MC34063 circuit structures
- [Evaluation and experience of Zhongke Yihaiwei EQ6HL45 development platform] + Create a new FPGA project——ehiway_fpga_test0