独立按键处理起来是非常简单的,一般都是IO口单独控制,所以只需要消抖和判断按键按下抬起状态就好。比如:
sbit key=P1^0;
if(key==0)
{
delay(10);
if(key==0)
{
keyval=1;
while(key==0);
}
}
#include
#define uchar unsigned char
#define uint unsigned int
sbit k1=P2^0;
sbit k2=P2^7;
uint z,shu,bai,shi,ge,shu1,a,b,c;
uchar num,tt,temp;
uchar code table[]={
0x03,0x9f,0x25,0x0d,0x99,
0x49,0x41,0x1f,0x01,0x09,0x11};
void delay(uint z);
void main()
{
k2=0;
k1=1;
shu1=998;
shu=876;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
P2=0xc1;
P1=0x03;
while(1)
{
P2=0xc1;
P1=table[a];
delay(1);
P2=0xa1;
P1=table[b];
delay(1);
P2=0x91;
P1=table[c];
delay(1);
P2=0x89;
P1=table[bai];
delay(1);
P2=0x85;
P1=table[shi];
delay(1);
P2=0x83;
P1=table[ge];
delay(1);
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tt++;
if(tt==5);
{ tt=0;
shu++;
a=shu1/100;
b=shu1%100/10;
c=shu1%10;
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
if(shu==999)
{
shu=0;
shu1++;
}
if(shu1==999)
shu1=0;
}
}
一个完整的数码管程序,刚毕业时的,好多年了.
按键接个下拉电阻,单片机IO口检测低电平就可以了