External interrupt application

Preface

For basic software operations, please refer to this blog:

Static display and dynamic display of LED digital tube (Keil+Proteus)-CSDN Blogicon-default.png?t=N7T8https://blog.csdn.net/weixin_64066303/article/details/134101256?spm =1001.2014.3001.5501Experiment 1: Connect 8 LED lights to the P1 port, and connect a button switch K1 to the external interrupt 0 pin. Using low-level triggering mode, when the program starts, the 8 LED lights on the P1 port are all on. Every time the button switch K1 is pressed, the Yes pin is grounded, generating an external interrupt request. In the interrupt service program, let the low The 4-digit LED and the high-order 4-digit LED flash alternately 5 times. Then the interrupt returns and controls all 8 LEDs to light up again.

Experiment 2: Connect 8 LED lights to the P1 port, connect a button switch K1 to the external interrupt 0 input pin, and connect a button switch K2 to the external interrupt 1 input pin. When K1 and K2 are not pressed, the 8 LEDs of the P1 port show a running light display; when K1 is pressed and released, the 8 LEDs of the P1 port flash alternately 10 times according to the high 4 digits and the low 4 digits, and then Return to the running water lamp state; when K2 is pressed and released, the 8 LEDs of the P1 port flash 10 times as a whole, and then return to the running water lamp display. The two external interrupts have the same priority and both adopt the falling edge trigger method. . (K1 and K2 cannot be pressed at the same time)

Experiment 3: Connect 8 LED lights to the P1 port, connect a button switch K1 to the external interrupt 0 input pin, and connect a button switch K2 to the external interrupt 1 input pin. When K1 and K2 are not pressed, the 8 LEDs of the P1 port will show running lights. When K1 is pressed and released, the 8 LEDs of the P1 port will flash alternately according to the high 4 digits and the low 4 digits. When K2 is pressed When released, the 8 LEDs of the P1 port will flash 5 times as a whole. When K1 is pressed, a low-priority external interrupt 0 request (falling edge trigger) is generated, and the external interrupt 0 service routine is entered. At this time, K2 is pressed, a high-priority external interrupt 1 request (falling edge trigger) is generated. , when the external interrupt 1 ends, it returns to continue executing the external interrupt 0 service routine.

Single external interrupt application

keil

According to the requirements of the experiment, in the main function, the P1 port is always fully bright, and a common anode is used, so giving 0 in the program means full brightness.

In the interrupt function, to alternately flash 5 times is to cycle the flashing part 5 times. To achieve the high 4 bits and low 4 bits to flash alternately, it is actually the high 4 bits to light up and the low 4 bits to light up alternately. As mentioned earlier, give 0 It means that the LED is on, so the P1 port is assigned the values ​​0x0F and 0xF0 successively. What should be paid attention to is the delay part. This may need to be debugged according to the computer. If the delay time is too short, the human eye cannot catch such a fast change, so As a result, what you see is that it ends without alternation, so you need to adjust the delay time yourself.

#include<reg51.h>
#define uchar unsigned char
//延时函数
void Delay(unsigned int i){
	unsigned int j;
	for(;i>0;i--){
		for(;j<1314;j++);
	}
}

//外部中断0,中断向量0003H
void int0() interrupt 0 using 0{
	uchar m;
	EX0=0;//禁止外部中断0
	for(m=0;m<5;m++){//交替闪烁5次
		P1=0x0F;//低4位LED灭,高4位LED亮
		Delay(111213141516171819);//延时时间设置长一点
		P1=0xF0;//低4位LED亮,高4位LED灭
		Delay(111213141516171819);//延时时间设置长一点
	}
	EX0=1;//中断返回前,打开外部中断0
}

void main(){
	EA=1;//总中断允许
	EX0=1;//INT0开中断
	IT0=0;//选择外部中断为低电平触发方式
	while(1){
		P1=0;
	}
}

Proteus

Required Proteus components.

Component name Proteus keyword
51 microcontroller AT89C51
reset button BUTTON
resistance RES
Yellow LED light LED-YELLOW
power supply POWER
resistance GROUND

What should be noted here is to set the resistance value of the resistor on the left. If the resistance value is too high, the LED light will not light up.

Application of two external interrupts

keil

When multiple interrupt sources are needed, you only need to add the corresponding interrupt service function.

In the past, the LED light was fully bright before the button was pressed. Now, the running light is used. Press K1 and the high and low bits will flash alternately 10 times. Press K2 and the whole LED will flash 10 times. After that, the running light will continue. The running light part can use an array. Shifting can also be used.

#include<reg51.h>
#define uchar unsigned char
//延时函数
void Delay(unsigned int i){
	unsigned int j;
	for(;i>0;i--){
		for(;j<1314;j++);
	}
}

void main(){
	uchar i;
	//流水灯显示数据数组
	uchar display[9]={0xFF,0xFe,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};
	EA=1;//总中断允许
	EX0=1;//允许INT0中断
	EX1=1;//允许INT1中断
	IT0=1;//选择INT0为下降沿触发
	IT1=1;//选择INT1为下降沿触发
	IP=0;//两个外部中断均为低优先级
	while(1){
		for(i=0;i<9;i++){
			Delay(111213141516171819);
			P1=display[i];
		}
	}
}
//外中断0的中断服务函数(高、低4位显示10次)
void int0() interrupt 0 {
	uchar i;
	EX0=0;//禁止外部中断0
	for(i=0;i<10;i++){
		P1=0x0F;
		Delay(111213141516171819);
		P1=0xF0;
		Delay(111213141516171819);
	}
	EX0=1;//中断返回前,打开外部中断0
}
//外中断1的中断服务函数(整体闪烁显示)
void int1() interrupt 2 {
	uchar i;
	EX1=0;//禁止外部中断1
	for(i=0;i<10;i++){
		P1=0xFF;
		Delay(111213141516171819);
		P1=0;
		Delay(111213141516171819);
	}
	EX1=1;//中断返回前,打开外部中断1
}

Proteus

The required components refer to the application part of a single external interrupt, which is to add a button on the basis of the former.

Breaking a nested application 

keil

When K1 is pressed, the interrupt 0 service function will be executed. If K2 is pressed again, because the priority of interrupt 1 is higher than interrupt 0, the service function of interrupt 1 will be jumped to execute. After the execution, the service function of interrupt 0 will be executed. The program has not been executed yet (it does not indicate the number of executions, which means it has been executed), so it will jump to the service function that executes interrupt 0, and it will alternately flash high and low 4.

What should be noted here is that if K2 is pressed first, when the service program of interrupt 1 is completed, it will jump to the main function. In short, remember to return to the interrupted program.

#include<reg51.h>
#define uchar unsigned char
//延时
void Delay(unsigned int i){
	unsigned int j;
	for(;i>0;i--){
		for(j=0;j<125;j++){;
		}
	}
}

void main(){
	uchar i;
	uchar display[9]={0xFF,0xFe,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};//流水灯显示数组
	while(1){
		EA=1;//总中断允许
		EX0=1;//允许外部中断0
		EX1=1;//允许外部中断1
		IT0=1;//选择外部中断0为下降沿触发
		IT1=1;//选择外部中断1为下降沿触发
		PX0=0;//设置中断0为低优先级
		PX1=1;//设置中断1为高优先级
		for(i=0;i<9;i++){
			Delay(500);
			P1=display[i];//流水灯
		}
	}
}
//外部中断0,高低4位交换闪烁
void int0() interrupt 0{
	while(1){
		P1=0x0F;
		Delay(400);
		P1=0xF0;
		Delay(400);
	}
}

//外部中断1,整体闪烁
void int1() interrupt 2{
	uchar i;
	for(i=0;i<5;i++){
		P1=0x00;
		Delay(500);
		P1=0xFF;
		Delay(500);
	}
}

The schematic diagram is the same as "the two external interrupts are the same", but the effect is different.​ 

Reference link

2. Explanation of basic knowledge of single-chip microcomputer - interrupt service function - Zhihu (zhihu.com)icon-default.png?t=N7T8https://zhuanlan.zhihu.com/p/618472480?utm_id=0

51 microcontroller interrupt notes - Zhihu (zhihu.com)icon-default.png?t=N7T8https://zhuanlan.zhihu.com/p/593229489

Summarize

Keep up the good work, everything will be arranged for the best.

Guess you like

Origin blog.csdn.net/weixin_64066303/article/details/134360733