Today I started to learn the counter of the microcontroller. Haha, it may be because I always couldn't set the timer accurately when programming in C language. When I learned that I could make the microcontroller C language timer accurate after learning the timer/counter, I felt really motivated to learn it. Although it took me a long time to write a simple program, it felt so refreshing. I think the key to learning the timer/counter is to learn how to program and control TCOM, TMOD, TH and TL. The main thing is the accurate calculation of TH and TL. And how to use it with the loop statement after the completion of the time.
The program I wrote today uses the microcontroller timer to control the LED light to flash at a speed of 1S. My crystal oscillator is 12MHZ. First, let the timer overflow once in 0.02S to make TF1=1. Then use I++ to loop 50 times, which adds up to 1S. The program is as follows:
#include
sbit LED = P1^2;
void main()
{ unsigned char i;
TMOD |= 0x10;
TMOD &= 0xdf;
TH1 = 0xb1;
TL1 = 0xdf;
TCON |= 0x40;
while(1)
{
if(TF1==1)
{
TF1 = 0;
TH1 = 0xb1;
TL1 = 0xdf;
i++;
if(i==50)
{
i=0;
LED=~LED;
}
}
}
}
Previous article:Digital tube dynamic display
Next article:Single chip traffic light experiment
- 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
- Crazy Shell AI open source drone ADC (joystick control)
- How to delete the top level in AD
- MicroPython adds USB and JTAG support to the ESP32-C3
- How much does a 5G base station cost?
- RF PA circuit design based on gallium nitride (GaN) devices
- The magical problem encountered by KICAD
- X-NUCLEO-IKS01A3 Official Technical Data
- AT32F425-Evaluation Report-Serial Port and Waveform Display Host Computer Debugging-09
- There is a car image simulation project for paid consultation, using the Freescale solution
- Take a look at the USB Host microcontroller in gaming peripherals