我用我的板子,完成了你的任务。程序如下:涉及的电路如图。
#include<reg51.h>
#define GPIO P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit k1=P3^1;
sbit k2=P3^0;
//void Delay(unsigned int i);
void Delay10ms(unsigned int c);
void DigDisplay();
unsigned char code DIG_CODE[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char DisplayData[2];
void main(void)
{
unsigned int i,j;
GPIO=j;
while(1)
{
if(k1==0)//扫描键盘k1
{
Delay10ms(1);
if(k1==0)
{
j++;
if (j>99)
{
j=0;
}
while((i<50)&&(k1==0))
{
Delay10ms(1);
i++;
}
i=0;
}
}
if(k2==0)//扫描键盘k2
{
Delay10ms(1);
if(k2==0)
{
j=j+2;
if (j>99)
{
j=0;
}
while((i<50)&&(k2==0))
{
Delay10ms(1);
i++;
}
i=0;
}
}
DisplayData[1]=DIG_CODE[j/10];
DisplayData[0]=DIG_CODE[j%10];
DigDisplay();
}
}
void Delay10ms(unsigned int c)
{
unsigned char a,b;
for(c;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
// void Delay(unsigned int i)//延时19.3us
//{
// char j;
// for(i; i > 0; i--)
// for(j = 200; j > 0; j--);
//}
void DigDisplay()
{
unsigned char i;
unsigned char j;
for(i=0;i<2;i++)
{
switch(i)
{
case(0):
LSA=0;LSB=0;LSC=0; break;
case(1):
LSA=1;LSB=0;LSC=0; break;
}
GPIO=DisplayData[i];
j=10;
while(j--);
GPIO=0x00;
}
}
|