Control interface of microcontroller timer/controller
5 Steps to Timer/Event Counter Programming
1. Select the working mode according to the requirements, determine the mode control word, and write it into the TMOD register
2. Calculate the count value of the timer/counter, find the initial count value, and write it into the initial value register
3. Open the interrupt option of the timer/counter as needed
4. Set the timer/counter control register TCON and start the timer
5. Wait for the timing time to expire and use query or interrupt mode to handle the situation accordingly
Timer/Counter Application Examples
The basic idea of using timers to generate periodic actions:
Generate periodic timing
When the scheduled time comes, take corresponding measures
For example, if the system clock frequency is 12MHz, the timer/counter T0 is required to output a square wave with a period of 500μs from P1.0 (i.e., periodic high and low level switching).
analyze:
To output a square wave with a period of 500μs from P1.0, it is only necessary to invert P1.0 every 250μs.
When the system clock is 12MHz, the oscillation frequency is (1/12M)s, that is, (1/12)μs. One machine cycle is 12 oscillation cycles, so one machine cycle is 1μs, that is, one count is 1μs, then the count value is 250, select counting mode 2, and the initial count value is 256 of mode 2 - 250 = 6
Working mode 2, mode control word 0x02, count initial value 6, TH0 = TL0 = 0x06
Code
Query method: always query the status of TF and handle overflow
/*
*12MHz crystal oscillator makes the P1.0 port output a square wave with a period of 500μs
*/
#include sbit P1_0 = P1^0; void main(){ TMOD = 0x02; TH0 = TL0 = 0x06; TR0 = 1; for(;;){ if(TF0){//Query overflow status TF0 = 0; //Home P1_0 = ~P1_0; //Processing } } } Interrupt method: The overflow state triggers an interrupt, and the interrupt service is used to handle the overflow #include sbit P1_0 = P1^0; void main(){ TMOD = 0x02; TH0 = TL0 = 0x06; EA = 1; // ET0 = 1; //Interrupt control, see the interruption chapter of this series of blogs for details TR0 = 1; while(1); //Keep the program running } //Interrupt service routine void time0_int(void) interrupt 1 { P1_0 = !P1_0; } Long time timing Generally, the crystal frequency is in MHz, and the machine cycle is in μs. Such a short cycle sometimes cannot meet the needs, but we can use programming to achieve longer timing, such as setting a count variable, and adding one to the count variable every 100μs. When the count variable reaches 10000, the timing of 1s is achieved. This can flexibly adapt to the needs
Previous article:Microcontroller Basics (IV): C51 Extensions to C Language
Next article:MCU Basics (VII): Serial Communication Concept and Working Principle
Recommended ReadingLatest update time:2024-11-16 13:22
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Summary of the basic knowledge of lead-acid batteries
- Want to build a smart home, how to start? Receive this collection of information
- Problems with the 5V3-pin DCDC voltage reference chip
- TI prize live broadcast pre-registration is waiting for you to watch~
- EEWORLD University Hall----Live Replay: Future Perception is Predicted by Me-The Latest Application of Sensors in the Internet of Things
- Design of Power Amplifier Circuit for MSP430F449 Single Chip Microcomputer
- Senior Sales Engineer
- [MM32 eMiniBoard Review] + Reunion with Smart Development Board
- [2022 Digi-Key Innovation Design Competition] Home Smart Dashboard - LVGL transplantation and testing
- 5G system core capability indicators