外部中断0的应用
2016-03-11 23:01
232 查看
源程序:
#include<reg51.h>
#define LED P2
void delay(int x);
void left(int x);
void main()
{
IE=0x81;
IP=0x00;
TCON=0x01;
LED=0x00;
while(1)
{
delay(300);
LED=~LED;
}
}
void my_int0() interrupt 0 using 0
{
char saveLED=LED;
left(3);
LED=saveLED;
}
void delay(int x)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<120;j++);
}
void left(int
x)
{
int i,j;
for(i=0;i<x;i++)
{
LED=0xfe;
for(j=0;j<8;j++)
{
delay(3000);
LED=(LED<<1)|0x01;
}
delay(3000);
}
}
运行结果分析:
首先,是LED灯交替闪烁;
外部中断时,LED灯左移3次;
恢复中断前,运行状态。
#include<reg51.h>
#define LED P2
void delay(int x);
void left(int x);
void main()
{
IE=0x81;
IP=0x00;
TCON=0x01;
LED=0x00;
while(1)
{
delay(300);
LED=~LED;
}
}
void my_int0() interrupt 0 using 0
{
char saveLED=LED;
left(3);
LED=saveLED;
}
void delay(int x)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<120;j++);
}
void left(int
x)
{
int i,j;
for(i=0;i<x;i++)
{
LED=0xfe;
for(j=0;j<8;j++)
{
delay(3000);
LED=(LED<<1)|0x01;
}
delay(3000);
}
}
运行结果分析:
首先,是LED灯交替闪烁;
外部中断时,LED灯左移3次;
恢复中断前,运行状态。