刚开始学单片机,自己写了一个用按键控制模式切换的数码管显示程序,就是按键后数码管就暗了,可是现在按键后数码管是十位和个位数字相同了,求指教。
#include<reg52.h>
sbit ADDR0 = P2^2;
sbit ADDR1 = P2^3;
sbit ADDR2 = P2^4;
sbit ADDR3 = P2^5;
sbit ENLED = P1^4;
sbit key1 = P3^1;
unsigned char LedChar[16]={
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71
};
unsigned char LedBuff[2]={
0xFF, 0xFF
};
void delay(unsigned int xms) // xms代表需要延时的毫秒数
{
unsigned int x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
unsigned int cnt = 0;
unsigned long sec = 0;
unsigned char i = 0;
unsigned char j =0;
ENLED = 0;
ADDR3 = 1;
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
TR0 = 1; //打开定时器T0
key1 = 1;
delay(5);
while(1)
{
if(key1 == 1)
{
if(TF0 == 1)
{
TF0 = 0;
TH0 = 0xFC;
TL0 = 0x67;
cnt++;
if(cnt >= 1000)
{
cnt = 0;
sec++;
LedBuff[0] = LedChar[sec%10];
LedBuff[1] = LedChar[sec/10%10];
}
P0 = 0xFF;
switch(i)
{
case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=LedBuff[0];break;
case 1: ADDR2=0; ADDR1=0; ADDR0=1; i=0; P0=LedBuff[1];break;
default:break;
}
}
}
else
{
TR0 == 0;
ADDR2=0; ADDR1=0; ADDR0=0;
LedBuff[0] = 0xFF;
ADDR2=0; ADDR1=0; ADDR0=1;
LedBuff[1] = 0xFF;
}
}
}
|