|
計(jì)數(shù)器模擬。
要求: (1)計(jì)數(shù)器有3個(gè)控制按鈕1號(hào)、2號(hào)、3號(hào),1個(gè)計(jì)數(shù)按鈕A。
(2)按完1號(hào)按鈕,再按A鈕,計(jì)數(shù)暑進(jìn)入加計(jì)數(shù)狀態(tài),最大計(jì)到(5+組號(hào)) 2, 時(shí)間間隔為0.5s。
按完2號(hào)按鈕后,再按A按鈕,計(jì)數(shù)器進(jìn)入減計(jì)數(shù)狀態(tài),從(5+組號(hào)) 2開(kāi)始減計(jì)數(shù),時(shí)間、間隔為0.5s.
按完3號(hào)按鈕后,再按A按鈕,計(jì)數(shù)器保持顯示“Pp”字樣。
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
sbit P13=P1^3;
uchar LED_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar BitCode[]={0x01,0x02};
uchar Huanchong[]={0x00,0x00};
uchar count=0;
uchar keycode1,keycode2;
void delay(uint m)
{
unsigned int n;
for(n=0;n<m;n++);
}
void Xianshi()
{
Huanchong[1]=count/10;
Huanchong[0]=count%10;
}
void Limian()
{
uchar h;
for(h=0;h<2;h++)
{
P0=LED_code[Huanchong[h]];
P2=BitCode[h];
delay(1);
P2&=0x00;
}
}
uchar keyscan(void)
{keycode1=0;
if(P10==0)
{delay(10);
if(P10==0)
{
while(P10==0)
keycode1=1;
}
}
if(P11==0)
{delay(10);
if(P11==0)
{
while(P11==0)
keycode1=2;
}
}
if(P12==0)
{delay(10);
if(P12==0)
{
while(P12==0)
keycode1=3;
}
}
return keycode1;
}
uchar keyscan2(void)
{ keycode2=0;
if(P13==0)
{delay(10);
if(P13==0)
{
while(P13==0)
keycode2=1;
}
}
return keycode2;
}
main()
{
int i;
while(1)
{Xianshi();
Limian();
delay(1);
keyscan();
keyscan2();
}
i=keycode1+keycode2;
if(i==2)
{count=0;
for(i=0;i<36;i++)
{
count++;
delay(500);
}
}
if(i==3)
{count=36;
for(i=0;i<36;i++)
{
count--;
delay(500);
}
}
if(i==4)
P0=0x73;
}
|
|