这次实验的是使用单片机的外部中断0和外部中断1来进行触发从而执行不同的代码片段
设置外部中断的一般过程如下
0.STC90C52的外部中断0在管脚P3.2 外部中断1在管脚P3.3 初始电平为高电平
1.设置中断的触发方式,这里设置为下降沿(高电平转低电平触发,至少保持两个时钟周期)IT0=1 IT1=1
2.打开对应允许中断位 EX0=1 EX1=1
3.打开总中断 EA=1
4.设置对应的中断函数 void int0() interrupt 0{P32_STATUS = 0;},这里触发中断之后设置变量的值为0
最后只需要在while主循环中片段这个变量的值即可
代码如下,效果是默认数码管显示1,当触发外部中断0显示一会6之后继续显示1,触发外部中断2之后显示一会8之后继续显示1
#include <reg52.h>
#define LED P0
unsigned char P32_STATUS = 1;
unsigned char P33_STATUS = 1;
unsigned char code DIG_CODE[17] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void Delay10ms(unsigned int);
void intconfig();
void main(void){
intconfig();
while(1){
if(P32_STATUS == 0){
LED = DIG_CODE[6];
P32_STATUS = 1;
}else if(P33_STATUS == 0){
LED = DIG_CODE[8];
P33_STATUS = 1;
}else{
LED = DIG_CODE[1];
}
Delay10ms(100);
}
}
void intconfig(){
IT0 = 1;
EX0 = 1; //允许中断
IT1 = 1;
EX1 = 1; //允许中断
EA = 1; //开启总中断
}
void Delay10ms(unsigned int c)
{
unsigned char a^b;
for(;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
void int0() interrupt 0{
P32_STATUS = 0;
}
void int1() interrupt 2{
P33_STATUS = 0;
}