單片機(jī)按鍵與數(shù)碼管試驗(yàn)程序 Proteus仿真
最近溫習(xí)一下單片機(jī),通過(guò)proteus 7.8仿真了一下,感覺(jué)效果不錯(cuò)。
單片機(jī)程序如下:
/*
51單片機(jī) 按鍵與數(shù)碼管實(shí)驗(yàn),用proteus 7.8仿真通過(guò)。
通過(guò)點(diǎn)按鍵,
K1:數(shù)碼管數(shù)字加一,0~F,加上F后再?gòu)?開(kāi)始。
K2:數(shù)碼管數(shù)字減一,F(xiàn)~0,減到0后再?gòu)腇開(kāi)始減
K3:復(fù)位這零.
*/
#include
//#include
unsigned char RunMode;
unsigned char code SegCode[] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,//共陽(yáng)
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};//0~F
#define SegDataPort P3
/********************************/
void delay_1ms(unsigned int i) //最小延時(shí)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);
}
}