1.10.4.3、TLx、THx
TL0+TH0: 16-bit register of timer 0
TL1+TH1: 16-bit register of timer 1
Suppose our pulse width is 0.5us, and we want to set the timing to 0.5ms and 4.444ms, then the counts are:
1000 = 0x3E8 = High 0x3 Low 0xE8 => TL0 = 0xE8 TH0 = 0x3
8888 = 0x22B8 = High 0x22 Low 0xB8 => TL0 = 0xB8 TH0 = 0x22
Correct a previous mistake, overflow means exceeding the maximum number of bits of the timer!
1.10.5. Timer Programming Practice 1
1.10.5.1 Experimental tasks
The delay function is used to implement the flashing delay. During the delay process, the CPU has to be used here and cannot do other things. This is the disadvantage of the previous
Interrupt + Timing:
The CPU can do the main task (static digital tube displays 0-F), the timer is set to 500ms, and an interrupt is generated when the timer expires. In the interrupt handler isr, the LED is made to flash! !
1.10.5.2 How to program
1. Ideas:
(1) Timing (timer initialization)
Assign a value to TMOD to determine the operating mode of T0.
Calculate the initial value of the timer count and write it into TH0 and TL0
Open the timer interrupt and assign values to ET0 and EA
Set TR0 to start the timer.
(2) What should the main program do?
(3) Interrupt Handler
You can modify it directly in the key interrupt program in the previous lesson! [MCU Learning] Lesson 9: Key
2. Question to think about: How to calculate 0.5s?
The machine cycle is the time required for the CPU to complete a basic operation. Machine cycle = 1/MCU clock frequency.
Our microcontroller's external crystal oscillator is 12MHz and works in 12T mode, so the internal clock frequency is 1MHz and the machine cycle is 1us. A maximum of 65535 (in 16-bit timer working mode) pulses can be set, which means the maximum timing time is 65535*1us=65535us=65.535ms.
If you need to set a longer time (for example, 500ms), the timer cannot meet this requirement directly. The solution is to add up multiple timings to form a long time.
Timing method: first use the timer to set 50ms, then *10
What is the initial value of our timing 50ms?
12T mode, external crystal oscillator 12MHz, internal clock frequency 1MHz, internal machine cycle: 1us
The number of timings is: 50ms/1us=50000
Initial value: Start counting from 65535-50000+1 (because the counter actually overflows when it counts to 65536) = 15536 = 0x3CB0, and the initial value is equal to 0x3CB0
TL0 = 0xB0, TH0 = 0x3C (You can also understand it this way: TH0 = 15536/256, TL0 = 15536%256)
Let’s look at these two pictures together to analyze how we need to program!
MCU works at 12T, no choice
We use timer 0
Working mode configuration
Because it is an internal clock count, it is equivalent to the timer mode, GATE = 0
Because it is timer mode, C/T = 0
Because the 16-bit timer mode is selected, M1 = 0, M0 = 1
Combining the above three points: TMOD = 0x01
Set the timer initial value (note the manual, 16-bit timer mode, TH0 and TL0 need to be reloaded)
TH0 = 0x3C
TL0 = 0xB0
Interrupt Enable
ET0 = 1
EA = 1
Timer/Counter Control Configuration
Enable counting, TR0 = 1
No external interruption involved, no need for IT and IE
IF will automatically change when the count overflows, so don't worry about it.
Timer Interrupt Handler Reference Manual P141
The final procedure is as follows
#include /**************Code description************************** This program uses the timer interrupt method to realize the static digital tube display 0-F and LED1 flashes once every 0.5s. P0--Static digital tube P1.0 LED1 *************************************************/ sbit led = P1^0; //LED is connected to P1.0 unsigned char CNT = 10; //Count 50ms each time, repeat 10 times unsigned char count; //Segment code table of independent digital tube unsigned char val[16] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e }; /*************Delay function*************/ void delay() { unsigned char i = 200,j = 100; while(i--) while(j--); } /******************************Timer interrupt service routine*************************/ void tm0_isr() interrupt 1 using 1 { TH0 = 0x3C; TL0 = 0xB0; //Manual reload if (count--== 0) { // It means that the interruption has been 10 times and 0.5s has expired led = !led; // LED inverted count = CNT; } } /***************************Main function************************************/ void main(void) { unsigned char i = 0; /************Timer initialization*************/ TMOD = 0x01; TH0 = 0x3C; TL0 = 0xB0; ET0 = 1; EA = 1; TR0 = 1; led = 1; count = CNT; while (1) { //main mission for (i=0; i<16; i++) { P0 = choices[i]; delay(); } } } 1.10.6.3, Use of 51 Timer Widget
Previous article:Application of STC15F2KA60S2 chip dust sensor
Next article:[STC MCU Learning] Lesson 17: LCD1602 Display
Recommended ReadingLatest update time:2024-11-16 13:00
- Popular Resources
- Popular amplifiers
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
- 【i.MX6ULL】Driver Development 4——Light up the LED (Register Version)
- [Raspberry Pi 3B+ Review] Connect to WIFI & Log in to VNC & Update System Support
- Detailed explanation of the connection technology between CF card and CPLD in ARM embedded system
- IAR FOR AVR crashes within seconds when running under WIN10, please help me solve it
- Unboxing
- i.MX6ULL Embedded Linux Development 2-uboot Porting Practice
- A Guide to Hooking Up with Electronic Gangsters
- PCB related knowledge
- Snek: A Python-based language for embedded devices
- Open source 4G module CMUX+PPP dial-up Internet access detailed explanation