计数器模拟。
要求: (1)计数器有3个控制按钮1号、2号、3号,1个计数按钮A。
(2)按完1号按钮,再按A钮,计数暑进入加计数状态,最大计到(5+组号) 2, 时间间隔为0.5s。
按完2号按钮后,再按A按钮,计数器进入减计数状态,从(5+组号) 2开始减计数,时间、间隔为0.5s.
按完3号按钮后,再按A按钮,计数器保持显示“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;
}
|