Detailed explanation of 51 single-chip microcomputer interrupt system (timer, counter)

Publisher:数据梦想Latest update time:2019-11-18 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

51 MCU interrupt level

Interrupt Sources
Default interrupt level
Serial number (for C language)
INT0---External interrupt 0
Highest

T0---Timer/Counter 0 Interrupt
2nd
1
INT1---External interrupt 1
No. 3
2
T1----Timer/Counter 1 Interrupt
No.4
3
TX/RX---Serial port interrupt
number 5
4
T2---Timer/Counter 2 Interrupt
lowest
5

Interrupt Enable Register IE

Bit number
DB7
DB6
DB5
DB4
DB3
DB2
DB1
DB0
Sign bit
EA
-------
ET2
ES
ET1
EX1
ET0
EX0

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

Bit number
DB7
DB6
DB5
DB4
DB3
DB2
DB1
DB0
Bit Address
---
---
---
PS
PT1
PX1
PT0
PX0

-------, 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

Bit number
DB7
DB6
DB5
DB4
DB3
DB2
DB1
DB0
Bit Notation
GATE
C/T
M1
M0
GATE
C/T
M1
M0

|-----------------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.

M1
M0
Operating mode
0
0
Mode 0, 13-bit timer/counter
0
1
Mode 1, 16-bit timer/counter
1
0
Mode 2, 8-bit timer/counter with 8-bit initial value auto-reload
1
1
Mode 3, only applicable to T0, divided into two 8-bit counters, T1 stops working

Timer/Controller Control Register TCON

Bit number
DB7
DB6
DB5
DB4
DB3
DB2
DB1
DB0
Sign bit
TF1
TR1
TF0
TR0
IE1
IT1
IE0
IT0

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++; 
}


Reference address:Detailed explanation of 51 single-chip microcomputer interrupt system (timer, counter)

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

msp430 MCU timer Timer_A
Timer_A: Note: msp430 has two 16-bit timers Timer_A and Timer_B. They are basically the same. There are mainly several registers: TACTL, TAR, CCTL0, CCR0, CCTL1, CCR1, CCTL2, CCR2, TAIV. The most important one is the TACTL register, which determines the input clock signal of Timer_A, the working mode of Timer_A, t
[Microcontroller]
Microcontroller C language programming: TIMER0 and TIMER1 control strip LED
/*  Name: TIMER0  and  TIMER1  control the strip  LED   Description: Timer  T0  controls the previous set of  LED strips at a faster scrolling speed Timer  T1  controls the next set of  LED strips at a slower scrolling speed. */ #include reg51.h #include intrins.h #define uchar
[Power Management]
Microcontroller C language programming: TIMER0 and TIMER1 control strip LED
LPC1114_Timer16_0 interrupt program_MDK compilation environment
About Timer16_0 code:     LPC_SYSCON- SYSAHBCLKCTRL |= (1 7); //Start the timer clock   LPC_TMR16B0- PR=1000;//1000-1; //Prescaler counter   LPC_TMR16B0- MR0=12000; //match register   LPC_TMR16B0- TCR=0x1; //Start the timer counter   LPC_TMR16B0- MCR=3;//(1 0)&(1 1); //Match control--matching triggers an int
[Microcontroller]
Issues to be aware of when using PIC microcontroller TIMER1
1. When TMR1H and TMR1L are initialized, the prescaler will be automatically cleared. 2. When the register is written to TMR1H and TMR1L, the prescaler will be cleared. When TMR1 is in operation, writing to TMR1H or TMR1L value may write undesired values. 3. When TMR1 works in asynchronous counting mode, it cannot b
[Microcontroller]
Teach you how to write S12XS128 program (23) -- Timer register description 5
Timer Core Register (TCNT) Register offset: $0004-$0005 TCNT is an incremental counter that keeps counting the internal clock signal. The program can read it at any time, but writing is prohibited in normal mode. TCNT should be accessed by word. Accessing the high and low bytes separately may result in incorrect r
[Microcontroller]
Teach you how to write S12XS128 program (23) -- Timer register description 5
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号