单片机按键与数码管试验程序 Proteus仿真
扫描二维码
随时随地手机看文章
最近温习一下单片机,通过proteus 7.8仿真了一下,感觉效果不错。
单片机程序如下:
/*
51单片机 按键与数码管实验,用proteus 7.8仿真通过。
通过点按键,
K1:数码管数字加一,0~F,加上F后再从0开始。
K2:数码管数字减一,F~0,减到0后再从F开始减
K3:复位这零.
*/
#include
//#include
unsigned char RunMode;
unsigned char code SegCode[] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,//共阳
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};//0~F
#define SegDataPort P3
/********************************/
void delay_1ms(unsigned int i) //最小延时1ms
{
unsigned char j;
while(i--)
for(j=0;j<125; j++);
}
void Display_Seg(unsigned char Value)
{
SegDataPort = SegCode[Value];
}
unsigned char GetKey(void)
{
unsigned char KeyTemp,CheckValue,Key=0x00;
CheckValue = P2&0x32;
if(CheckValue==0x32)
return 0x00;
delay_1ms(10);
KeyTemp = P2&0x32;
if(KeyTemp == CheckValue)
return 0x00;
if(!(CheckValue&0x02))
Key"=0x01;
if(!(CheckValue&0x10))
Key|=0x02;
if(!(CheckValue&0x20))
Key|=0x04;
return Key;
}
void KeyDispose(unsigned char Key)
{
if(Key&0x01)
{
RunMode = (RunMode+1)%16;
Display_Seg(RunMode);
}
if(Key&0x02)
{
RunMode = (RunMode-1)%16;
Display_Seg(RunMode);
}
if(Key&0x04)
{
RunMode = 0x00;
Display_Seg(RunMode);
}
}
void main(void)
{
unsigned char Key;
P2=0xff;
Display_Seg(0);
while(1)
{
Key = GetKey();
KeyDispose(Key);
}
}