Technology Sharing

05: Timer interrupt

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina


1. When the central processing unit (CPU) is processing something, an emergency event request occurs from the outside world, requiring the CPU to suspend the current work and deal with the emergency event. After processing, it returns to the original interrupted place and continues the original work. This process is called interruption.
2. Usually, the interrupt sources are queued according to their importance and urgency, and the interrupt request source with the most urgent event is processed first, that is, each interrupt source is specified to have a priority level. The CPU always responds to the interrupt request with the highest priority level first.
3. The C51 microcontroller has 8 interrupt request sources:

insert image description here

1. Timer T0 interrupt

insert image description here

As shown in the figure: Interrupts also need to configure registers. There are two types of registers, the first is the interrupt control register (IE and XICON), and the second is the priority control register. Here we only study an interrupt of timer T0.
To use the timer T0 interrupt:

ET0 = 1;ET0为定时器T0的中断开关,为1时打开中断
EA = 1;  EA为中断源的总开关。
  • 1
  • 2

When the set time is up, the program in the interrupt function is executed. So how do you know which function is the interrupt function? — Query the interrupt number (different interrupt sources generate different interrupt numbers)
insert image description here

2. Example: Using timer T0 interrupt to turn the light on and off every 1s

#include <REGX52.H>

sbit LED1 = P3^7;
int cnt = 0;

void Timer0_Init_10ms(void)		//10毫秒@11.0592MHz
{
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x01;		//设置定时器模式
	TL0 = 0x00;		//设置定时初值
	TH0 = 0xDC;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}

void Timer0_interrupt_Init(void)//定时器T0中断初始化
{
	ET0 = 1;
	EA = 1;
}

void main(void)
{ 
	LED1 = 1;//先让灯熄灭的状态
	Timer0_Init_10ms();//打开定时器T0
	Timer0_interrupt_Init();//打开定时器T0中断
	while(1)
	{
		
	 }
}

/*定义中断函数*/
void Timer0Hander() interrupt 1
{
	TF0 = 0;//软件清零
	TL0 = 0x00; //重新给初值
	TH0 = 0xDC;
	cnt++;
	if(cnt == 100)//数100次,相当于1s
	{
		cnt = 0;
		LED1 = !LED1;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • When the interrupt is turned on, the main function will be jumped out every 10ms to execute the program in the interrupt function.
  • When the first 10ms of the timing is reached, the interrupt function is executed, and the timer is re-timed for 10ms. cnt = 1, and the light is still off.
  • The second 10ms of the timing is up, the interrupt function is executed, the timer is re-timed for 10ms, cnt = 2, the light is still off
  • When the 100th 10ms of the timing arrives, the interrupt function is executed, the timer is re-timed for 10ms, cnt = 100, and the light turns on.

This achieves a 1s interval on and off.