资源描述
六.按键中断识别
[实验任务]
采用中断技术,每按一下按键,计数器加1,并用LED显示出来。
[硬件电路]
注意:我们只用4位数码管中的两位。
注意:a接P0.0;b接P0.1;c接P0.3……
注意: 2H接P2.0; 1H接P2.1; 中断按键已经接好。
[C语言源程序]
#include<reg52.h>
unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90};
unsigned char dispcount=0; //计数
sbit gewei=P2^0; //个位选通定义
sbit shiwei=P2^1; //十位选通定义
void Delay(unsigned int tc) //延时程序
{ while( tc != 0 )
{
unsigned int i;
for(i=0; i<100; i++);
tc--;
}
}
void ExtInt0() interrupt 0 //中断服务程序
{
dispcount++; //每按一次中断按键,计数加一
if (dispcount==100) //计数范围0-99
{dispcount=0;}
}
void LED( ) //LED显示函数
{
if(dispcount>=10) //显示两位数
{
shiwei=0;
P0=table[dispcount/10];
Delay(8);
shiwei=1;
gewei=0;
P0=table[dispcount%10];
Delay(5);
gewei=1;
}
else //显示一位数
{
shiwei=1;
gewei=0;
P0=table[dispcount];
Delay(8);
}
}
void main()
{ TCON=0x01; //中断设置
IE=0x81;
while(1) //循环执行
{
LED(); //只须调用显示函数
}
}
展开阅读全文