51 MCU interrupt level
Interrupt Enable Register IE
EA---Global enable bit.
EA=1, turn on global interrupt control. Under this condition, the corresponding interrupt is turned on or off by each interrupt control bit.
EA=0, turn off all interrupts.
-------, invalid bit.
ET2---Timer/Counter 2 interrupt enable bit. EA is the total interrupt switch, set to 1 to turn on;
ET2=1, turn on T2 interrupt. EX0 is the external interrupt 0 (INT0) switch, ...
ET2=0, turn off T2 interrupt. ET0 is the timer/counter 0 (T0) switch, ...
ES---Serial port interrupt enable bit. EX1 is the external interrupt 1 (INT1) switch, ...
ES=1, turn on serial port interrupt. ET1 is the timer/counter 1 (T1) switch, ...
ES=0, turn off serial port interrupt. ES is the serial port (TX/RX) interrupt switch, ...
ET1---Timer/Counter 1 interrupt enable bit. ET2 is the timer/counter 2 (T2) switch, ...
ET1=1, turn on T1 interrupt.
ET1=0, turn off T1 interrupt.
EX1---External interrupt 1 interrupt enable bit.
EX1=1, turn on external interrupt 1 interrupt.
EX1=0, turn off external interrupt 1 interrupt.
ET0---Timer/Counter 0 interrupt enable bit.
ET0=1, turn on T0 interrupt.
ET0=0, turn off T0 interrupt.
EX0---External interrupt 0 interrupt enable bit.
EX0=1, turn on external interrupt 0 interrupt.
EX0=0, turn off external interrupt 0 interrupt.
Interrupt priority register IP
-------, invalid bit.
PS---Serial port interrupt priority control bit.
PS=1, serial port interrupt is defined as high priority interrupt.
PS=0, serial port interrupt is defined as low priority interrupt.
PT1---Timer/Counter 1 interrupt priority control bit.
PT1=1, timer/counter 1 interrupt is defined as high priority interrupt.
PT1=0, timer/counter 1 interrupt is defined as low priority interrupt.
PX1---External interrupt 1 interrupt priority control bit.
PX1=1, external interrupt 1 interrupt is defined as high priority interrupt.
PX1=0, external interrupt 1 interrupt is defined as low priority interrupt.
PT0---Timer/Counter 0 interrupt priority control bit.
PT0=1, timer/counter 0 interrupt is defined as high priority interrupt.
PT0=0, timer/counter 0 interrupt is defined as low priority interrupt.
PX0---External interrupt 0 interrupt priority control bit.
PX0=1, external interrupt 0 interrupt is defined as high priority interrupt.
PX0=0, external interrupt 0 interrupt is defined as low priority interrupt.
Timer/Counter Operation Mode Register TMOD
|-----------------Timer 1------------------------|--------------------Timer 0----------------------|
GATE---Gate control bit.
GATE=0, the start and stop of the timer/counter are only controlled by TRX (X=0,1) in the TCON register.
GATE=1, the start and stop of the timer counter are jointly controlled by TRX (X=0,1) in the TCON register and the level state on the external interrupt pin (INT0 or INT1).
C/T---Timer and counter mode selection bit.
C/T=1, counter mode; C/T=0, timer mode.
M1M0---Working mode selection bit.
Timer/Controller Control Register TCON
TF1---Timer 1 overflow flag.
When timer 1 overflows, TF1 is set to 1 by hardware and an interrupt is requested. After entering the interrupt service routine, it is automatically cleared to 0 by hardware. It should be noted that if the timer interrupt is used, this bit does not need to be operated manually, but if the software query method is used, when the position is found to be 1, it needs to be cleared to 0 by software.
TR1---Timer 1 run control bit.
Timer 1 is cleared to 0 by software. When GATE=1 and INIT is high, TR1 is set to 1 to start timer 1; when GATE=0, TR1 is set to 1 to start timer 1.
TF0---Timer 0 overflow flag, its function and operation method are the same as TF1.
TR0---Timer 0 run control bit, its function and operation method are the same as TR1.
IE1---External interrupt 1 request flag.
When IT1=0, the bit level trigger mode is used. S5P2 samples the INT1 pin in each machine cycle. If the NIT1 pin is at a fixed level, it is set to 1, otherwise IE1 is cleared to 0.
When IT1=1, INT1 is in edge trigger mode. When the first machine cycle samples INIT1 as low level, IE1 is set to 1. IE1=1 indicates that external interrupt 1 is requesting an interrupt from the CPU. When the CPU responds to the interrupt and turns to the interrupt service routine, this bit is cleared to 0 by hardware.
IT1 External interrupt 1 trigger mode selection bit.
IT1=0, level trigger mode, low level on pin INT1 is valid.
IT1=1, transition edge trigger mode, negative transition from high to low on pin INT1 is valid.
IE0---External interrupt 0 request flag, its function and operation method are the same as IE1. IT0
---External interrupt 0 trigger mode selection bit, its function and operation method are the same as IT1.
From the above knowledge points, we can know that each timer has 4 working modes, which can be selected by setting the M1M0 bits in the TMOD register.
The counting bit number of mode 1 is 16 bits. For T0, the TL0 register is used as the lower 8 bits and the TH0 register is used as the upper 8 bits to form a 16-bit plus 1 counter.
Regarding how to determine the initial value of timer T0. Once the timer is started, it starts counting by adding 1 to the original value. If we do not set TH0 and TL0 at the beginning of the program, their default values are both 0. Assuming the clock frequency is 12MHz, 12 clock cycles are one machine cycle, then the machine cycle is 1us. It takes 216 -1 numbers to fill TH0 and TL0, and then a pulse counter overflows, and then an interrupt is requested to the CPU. Therefore, an overflow takes a total of 65536us, which is approximately equal to 65.6ms. If we want to time 50ms, we need to first install an initial value for TH0 and TL0. After recording 50,000 numbers based on this initial value, the timer overflows. At this time, it is just a 50ms interrupt. When we need to time 1s, when we write the program, when 20 50ms timer interrupts are generated, it is considered to be 1s, so that the timing time can be accurately controlled. To count 50,000 numbers, the total number that should be loaded into TH0 and TL0 is 65536-50,000=15536. Load 15536 modulo 256: 15536/256=60 into TH0, and load 15536 remainder over 256: 15536/256=176 into TL0.
The above is the calculation method of the initial value of the timer. After summarizing, the following conclusions are drawn: when using timer mode 1, let the machine cycle be TCY, and the time for the timer to generate an interrupt be t, then the number of counts required is N=t/TCY, and the numbers loaded into THX and TLX are:
THX=(65536-N)/256, TLX=(65536-N)%256
How to write the interrupt service program
void function name () interrupt interrupt number using working group
{
interrupt service routine content
}
When writing a timer program for a single-chip microcomputer, the timer and interrupt registers need to be initialized at the beginning of the program. The timer initialization process is usually as follows:
(1) Assign a value to TMOD to determine the working mode of T0 and T1.
(2) Calculate the initial value and write it to TH0, TL0 or TH1, TL1.
(3) When in interrupt mode, assign a value to IE to enable interrupts.
(4) Set TR0 and TR1 to start the timer/counter timing or counting.
Example: Use timer 0 working mode 1 to achieve a light-emitting diode flashing on and off for 1 second.
The program code is as follows:
#include
#define uchar unsigned char
#define uint unsigned int
sbit led1=P1^0;
uchar num;
void main()
{
TMOD=0x01; //Set timer 0 to work mode 1 (M1, M0 bits 0, 1)
TH0=(65536-45872)/256; //Install the initial value of 11.0592M crystal oscillator timing 50ms number is 45872
TL0=(65536-45872)%256;
EA=1; //Open the total interrupt
ET0=1; //Open the timer 0 interrupt
TR0=1; //Start timer 0
while(1)
{
if(num==20) //If it reaches 20 times, it means 1 second
{
led1=~led1; //Invert the state of the light-emitting tube
num=0;
}
}
}
void T0_time()interrupt 1
{
TH0=(65536-45872)/256; //Reload initial value
TL0=(65536-45872)%256;
num++;
}
Previous article:51 MCU CS1237 electronic scale source program with detailed comments
Next article:Proteus simulation version and program source code of TX-1C microcontroller development board
Recommended ReadingLatest update time:2024-11-15 15:12
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- 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
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Questions about the power supply of the communication system
- [Shanghai Hangxin ACM32F070 development board] The second part lights up the LCD screen and operates the touch button functions, serial port, ADC.
- EEWORLD University ---- TE Smart City
- Develop IoT in 10 minutes - Smoke sensor monitoring (Wifi version)
- Hearing aid chips
- IMX6 MfgTool Guide
- EEWORLD University Hall----Automatic Control System (Henan Polytechnic University)
- TI's AWR1243 radar learning record
- Help max30001 FIFO ECG
- RIGOL FPGA Technology Talent Recruitment Call