External interrupt - interrupt nesting

1. Before first understand the external interrupt should understand some of the associated register **

  • Interrupt enable register IEHere Insert Picture Description

1.EA: cpu global interrupt enable control bits. EA = 1, cpu break open.
2.ET0 / ET1: Timer T0 and T1 timer overflow interrupt enable bit. ET0 / ET1 = 1; Enable interrupts.
3.EX0 / EX1: external interrupt 0 and external interrupt 1 interrupt enable bit. EX0 / ET1 = 1; Enable interrupts.

  • Timer / counter control register TCON
    Here Insert Picture Description

With external parties only the last four, with the top four timers related, are not described in this.
IE0: External Interrupt 0 request flag, IE0 = 1 External Interrupt 0 to cpu request interrupt, the interrupt response automatically cleared by hardware 0;
IE1: External Interrupt Request flag, IE0 = 1 external interrupt 1 to the cpu request interrupts, the interrupt hardware automatically cleared to 0;
IT0: external interrupt source interrupt type select bit 0, IT0 = 0; low level interrupt. IT0 = 1; falling trigger.
IT1: External Interrupt Source Interrupt Type Select Bit 0, IT1 = 0; low level interrupt. IT1 = 1; falling trigger.

  • Interrupt priority register IP
    Here Insert Picture Description

PS: serial interrupt priority control bit.
PT1: Timer 1 interrupt priority control bit.
PX1: External Interrupt 1 Priority Control.
PT0: Timer 0 interrupt priority control bit.
PX0: External Interrupt 0 Priority Control.

2. external interrupt 0


1. Simple Control P0 ^ 0 Ethernet LED lamp blinking external interrupt

sbit led=P0^0;
void Init0();
void delayms(unsigned int ms);
int main()
{
	Init0();
	while(1);
}
void delayms(unsigned int ms)//延迟函数
{
	int i,j;
	for(i=0;i<ms;i++)
	 for(j=100;j>0;j--);
}
void Init0()//外部中断0的初始化
{
	IT0=1;//选择跳沿触发方式
	EX0=1;
	EA=1;
}
void Int0() interrupt 0
{
	delayms(5);//按键的消抖
	if(P3^2==0)//外部中断0的管脚在P3^2
	{
		led=~led;
	}

}

3. Interrupt priority configuration

The default MCU 51 (the registers are not provided in this case IP) interrupt priority:
External Interrupt 0> timer / counter 0> External Interrupt 1> timer / counter 1> serial interrupt;
but this is only logically priority priority, when the interrupt arrives while there are several high-priority interrupt will be serviced first. This is actually the case priority interrupt arrives at the same time, who should get priority service, rather than the ability to provide priority interrupt nesting. Such priority logic is called priority. [^ 1]

To achieve true nesting in the form of priority, i.e. higher priority interrupt can interrupt lower priority interrupt services, the interrupt priority must be implemented by providing IP register; this is called the physical priority priority .


  • For example: when the counter 0 and Interrupt 1 external interrupt (Interrupt Priority Counter 0> External Interrupt 1) arrive simultaneously, it will enter the timer 0 interrupt service routine; External Interrupt 1 but in the case where the serving service functions, At this time any interruption is not interrupted it, including logic priority over its external interrupt 0 counter 0 interrupt.
    To achieve true nesting in the form of priority, i.e. higher priority interrupt can interrupt lower priority interrupt services, the interrupt priority must be implemented by providing IP register; this is called the physical priority priority .

- Set External Interrupt 1 external interrupt priority is greater than 0 priority. Two interrupt nesting to achieve, when performing the external interrupt 0 1 external interrupt can interrupt the operation of the external interrupt 0 program.

void Init();
void delayms(unsigned int ms);
int main()
{
	Init();
	while(1);
}
void delayms(unsigned int ms)//延迟函数
{
	int i,j;
	for(i=ms;i>0;i--)
	 for(j=100;j>0;j--);
}
void Init()//外部中断初始化
{
	IT0=1;//选择跳沿触发方式
	EX0=1;//打开外部中断0允许位
	IT1=1;//选择跳沿触发方式
	EX1=1;//打开外部中断1允许位
	EA=1;//打开总中断
    IP=0X01;//设置外部中断0的优先级大于外部中断1
}

void Int0() interrupt 0 using 0//外部中断0的执行程序。
{ 
	unsigned char i;
	delayms(5);
	if(P3^2==0)
	{
		for(i=0;i<8;i++)
		{
			P0=~(0x01<<i);
			delayms(1000);
		}
			
	}

}
void Int1() interrupt 2 using 1//外部中断1的执行程序。using x代表用那个工作区。
{
	unsigned char i;
	delayms(5);
	if(P3^3==0)
	{
			for(i=8;i>0;i--)
		{
			P0=~(0x01<<i);
			delayms(500);
		}
	}
}

- The main content of external interrupts

Here Insert Picture Description
Let's do a little practice it interrupt nesting. Link: Link .
If this article helpful to you, on the point of a small ♥ it!

Released two original articles · won praise 5 · Views 186

Guess you like

Origin blog.csdn.net/qq_46292418/article/details/105121876