單片機按鍵與數(shù)碼管試驗程序 Proteus仿真
最近溫習一下單片機,通過proteus 7.8仿真了一下,感覺效果不錯。
單片機程序如下:
/*
51單片機 按鍵與數(shù)碼管實驗,用proteus 7.8仿真通過。
通過點按鍵,
K1:數(shù)碼管數(shù)字加一,0~F,加上F后再從0開始。
K2:數(shù)碼管數(shù)字減一,F(xiàn)~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);
}
}