Last time we briefly understood the concept of interruption of microcontroller
This time we focus on understanding the timer interrupt of the microcontroller
First add some knowledge
Supplement: Several cycles of the microcontroller
1: Clock cycle: also known as oscillation period, which is the reciprocal of the external crystal oscillator. For example, for a 12M crystal oscillator, the clock cycle is 1/12um, which is the most basic and smallest unit of time.
2: State cycle: twice the clock cycle.
3: Machine cycle. The basic operation cycle of the microcontroller. The microcontroller completes a basic operation in one cycle. It consists of 12 clock cycles. For example, for a 12M crystal oscillator, the machine cycle is 1um. In fact, the machine cycle is the time to complete a single instruction.
4: Instruction cycle. It refers to the time required for the CPU to execute an instruction. For example, there are single-cycle instructions, two-cycle instructions, three-cycle instructions, and so on.
1. Simple understanding of the concept of timer
1 The timer system is an independent hardware part inside the microcontroller
2 The timer is connected to the crystal oscillator CPU
3 Once the start timing is set, the timer automatically starts timing under the action of the crystal oscillator
Note: 1 The byte address 89H of TMOD is not addressable.
So when writing code, you can't do bit operations, you can only set TMOD = 0x01; like this
2 The upper four bits of TMOD are used to set timer 1 and the lower four bits are used to set timer 0
3 TMOD commonly used modes are mode 1 (common) 2 (occasionally) and the rest are rarely used
4 TMOD only controls the working mode and function selection of the timer
For example:
TMOD = 0x01; // 0000 0001
(Timer 0, GATE=0 start is only controlled by register TRX C/T=0 timer mode M1=0 M0=1 working mode 1 16-bit timer)
2 TCON
Note: 1 TCON byte address bit 88H can be bit-addressed
2 MCU reset TCON is all cleared
Here we first focus on a few
1 TF1 Timer 1 overflow flag
2 TF0 Timer 0 overflow flag
3 TR1 Timer 1 run control bit
4 TR0 Timer 0 run control bit
3 Working Principle
4 Initial Value Problem
Note: 1. TH0 and TL0 are not set. The default values are both 0.
So 65536 numbers are needed to overflow: 1111 1111 1111 1111
(65535 reaches the full value, 65536 overflows and sets TF0 to 1)
For example:
If we want to time it to 50ms (a more commonly used time)
The crystal oscillator of the microcontroller is 12MHz. 12 clock cycles are 1 machine cycle (1/12MHz)*12 = 1 microsecond
We time it every 50ms, that is, after the initial value is loaded, it overflows after (50/1)*10^3 = 50000 numbers.
So the initial value is 65536-50000 = 15536 numbers
15536 needs to be converted to hexadecimal and the high and low 8 bits are separated first.
15536/256=60 loaded into TH0 15536%256=176 loaded into TL0
5 How to write an interrupt service routine
The timer interrupt sequence number is 3 Timer 1
2 Programming
1 Objective: Make the first LED flash on and off for 1s
2 Code
#include
2 #define uint unsigned int
3 #define uchar unsigned char
4sbit LED1 = P1^0;
5 uchar num;
6 void main()
7 {
8 TMOD = 0x01; // Timer 0 working mode 1
9 TH0 = (65536 - 45872)%256;
10 TL0 = (65536 - 45872)/256;
11 EA = 1; //switch is always off
12 ET0 = 1; // Enable timer 0 interrupt
13 TR0 = 1; //Start timer 0
14 while(1); //The program stops and waits for an interrupt to occur
15 }
16
17 void T0_time() interrupt 1
18 {
19 TH0 = (65536 - 45872)%256;
20 TL0 = (65536 - 45872)/256;
21 num++;
22 if(num == 20)
twenty three {
24 num = 0;
25 LED1 = ~LED1;
26 }
27
28 }
3 Analysis:
1 After setting TMOD, correspond to ET0 TR0 TH0 TL0
2 Don't save trouble by writing 45872 as (5000/(12/11.0592))
11.0592 is a decimal and cannot be converted into hexadecimal.
3 In order to ensure that the timer interrupt is 50ms each time, we need to set TH0 and TL0 in the interrupt function each time
Reload initial value
4 is set to 50ms. Use a variable num to count every 20 executions of the program, that is, every 1s.
Improved code:
#include2 #define uint unsigned int 3 #define uchar unsigned char 4sbit LED1 = P1^0; 5 uchar num; 6 void main() 7 { 8 TMOD = 0x01; // Timer 0 working mode 1 9 TH0 = (65536 - 45872)%256; 10 TL0 = (65536 - 45872)/256; 11 EA = 1; //switch is always off 12 ET0 = 1; // Enable timer 0 interrupt 13 TR0 = 1; //Start timer 0 14 while(1); //The program stops and waits for an interrupt to occur 15 if(num == 20) 16 { 17 num = 0; 18 LED1 = ~LED1; 19 } 20 twenty one } twenty two 23 void T0_time() interrupt 1 twenty four { 25 TH0 = (65536 - 45872)%256; 26 TL0 = (65536 - 45872)/256; 27 num++; 28 }
Leave an assignment for everyone to write.
Use timer 0 to flash the first LED at 200ms intervals and use timer 1 to cycle the first two digits of the digital tube for 59s.
The above content is the blogger's own thoughts after reading books. You should read more books to understand better.
Previous article:MCU simulation software reset
Next article:51 MCU Learning Road—— 1.8 Matrix Keyboard
- 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
- [AT-START-F425 Review] + Software and Hardware SPI Driver (ST7735) 1.8-inch TFT LCD
- Temperature Sensor
- 【CH579M-R1】+BH1750 light intensity detection
- Introduction to Several Common Laser Sensor Applications
- Both ends are TYPEC ports
- 5G Overview and Fundamentals
- TI's F28379D controlCARD for C2000 Real-Time Control Development Kit
- Please help, how does the FPGA Ethernet data receiver perform CRC check?
- A Brief Analysis of Wireless Positioning Technology
- [TI recommended course] #C2837x Getting Started Guide#