前言
中断是单片机中很重要的一部分,IP引脚的预备知识在“按键实验”的博客里已经说过,暂不赘述。
实验内容:依次按下两个按键,根据设置的优先级不同,实现中断的嵌套即是:按下A键,实现A功能,再按下B键,由于B键引脚优先级高于A键,产生中断,实现B功能,完成后,返回实现A功能。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit k1 = P3 ^ 0;
sbit k2 = P3 ^ 1;
sbit LED1 = P2 ^ 0;
sbit LED2 = P2 ^ 1;
void Int();
void Interrupt1() interrupt 0;
void Interrupt2() interrupt 1;
void main()
{
Int();
while (1);
}
void Int()
{
IT0 = 1;
IT1 = 1;
PX0 = 1;
IE = 0x85;/*由低到高:EX1(第3位)=1,EX0 (第0位)= 1,EA (第八位)= 1 IE = 10000101*/
/*不熟的话写两种*/
}
void Interrupt1() interrupt 0
{
LED1 = ~LED1;
}
void Interrupt2() interrupt 2/*外部中断1的标号是2!*/
{
LED2 = ~LED2;
}
延伸:中断的嵌套如何实现?
http://www.eeworld.com.cn/mcu/article_24567.html
/*中断嵌套实现流水灯模式转换
:初始状态:右移
中断0:4只流水灯交替闪烁(按键接在P3^2上)
中断1:2只流水灯交替闪烁(P3^3)
功能:
初始状态:8只流水灯,按下k1,4只流水灯交替闪烁;按下k2,2只流水灯交替闪烁
执行完毕,继续4只流水灯交替,执行完,8只流水灯
*/
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//#define
//sbit k1 = P3 ^ 2;
//sbit k2 = P3 ^ 3;
//sbit LED1 = P2 ^ 0;
//sbit LED2 = P2 ^ 1;
void delay(uint xms);
void Int();
void Interrupt0() interrupt 0;
void Interrupt1() interrupt 2;
void main()
{
Int();
uchar i;
P2 = 0xfe;
while(1)
{
for (i = 0;i < 7;i++)
{
P2 = _crol_(P2, 1);//先左移
delay(600);
}
for (i = 0;i < 7;i++)
{
P2 = _cror_(P2, 1);//再右移
delay(600);
}
}
}
void Int()
{
IT0 = 1;//跳沿触发
IT1 = 1;//跳沿触发
PX1 = 1;//中断1高于中断0
IE = 0x85;/*由低到高:EX1(第3位)=1,EX0 (第0位)= 1,EA (第八位)= 1 IE = 10000101*/
/*不熟的话写两种*/
}
void delay(uint xms)//AT89C5211.0592MHz
{
uint x, y;
for (x = xms;x > 0;x--)
for (y = 110;y > 0;y--);
}
void Interrupt1() interrupt 0
{
while (1)
{
P2 = 0x0f;
delay(600);
P2 = 0xf0;
delay(600);
}
}
void Interrupt2() interrupt 2/*外部中断1的标号是2!*/
{
while (1)
{
P2 = 0xcc;//(11001100)
delay(600);
P2 = 0x33;
delay(600);
}
}