一切功能正常,刷新时间时会有错误显示
下图是17点42分30秒,和17点42分49秒的错误显示,只是闪一下,一秒不到后恢复正常,所以有点难截图
#include<reg51.h>
#define uChar unsigned char
#define uInt unsigned int
uChar a[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uChar b[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uChar second=30,minute=42,hour=17,year=23,month=05,day=22,count;
sbit Key1 = P3^0;
sbit Key2 = P3^1;
sbit Key3 = P3^2;
sbit Key4 = P3^3;
sbit Key5 = P3^4;
sbit Buzzer=P1^1;
void Delay(uInt t)
{
while(t)
{
t--;
}
}
void Dispaly1(uChar second,uChar minute,uChar hour)
{
P2=b[0];
P0=a[hour/10];
Delay(10);
P2=b[1];
P0=a[hour%10];
Delay(10);
P2=b[2];
P0=0xbf;
Delay(10);
P2=b[3];
P0=a[minute/10];
Delay(10);
P2=b[4];
P0=a[minute%10];
Delay(10);
P2=b[5];
P0=0xbf;
Delay(10);
P2=b[6];
P0=a[second/10];
Delay(10);
P2=b[7];
P0=a[second%10];
Delay(10);
}
void Dispaly2(uChar day,uChar month,uChar year)
{
P2=b[0];
P0=a[day/10];
Delay(10);
P2=b[1];
P0=a[day%10];
Delay(10);
P2=b[2];
P0=0xbf;
Delay(10);
P2=b[3];
P0=a[month/10];
Delay(10);
P2=b[4];
P0=a[month%10];
Delay(10);
P2=b[5];
P0=0xbf;
Delay(10);
P2=b[6];
P0=a[year/10];
Delay(10);
P2=b[7];
P0=a[year%10];
Delay(10);
}
void Keyscan1()
{
static uChar i=0,j=0;
if(Key1==0)
{
Delay(10);
if(Key1==0)
{
while(!Key1);
i++;
}
}
if(i%2==0)
{
TR0=1;
}
if(i%2==1)
{
TR0=0;
}
if(i%2==1)
{
TR0=0;
if(Key2==0)
{
Delay(10);
if(Key2==0)
while(!Key2);
j++;
}
if(j%4==1)
{
if(Key3==0)
{
Delay(10);
if(Key3==0)
while(!Key3);
second++;
if(second==60)
second=0;
}
else if(Key4==0)
{
Delay(10);
if(Key4==0)
while (!Key4);
second--;
if(second==0)
second=59;
}
}
if(j%4==2)
{
if(Key3==0)
{
Delay(10);
if(Key3==0)
while(!Key3);
minute++;
if(minute==60)
minute=0;
}
else if(Key4==0)
{
Delay(10);
if(Key4==0)
while (!Key4);
minute--;
if(minute==0)
minute=59;
}
}
if(j%4==3)
{
if(Key3==0)
{
Delay(10);
if(Key3==0)
while(!Key3);
hour++;
if(hour==24)
hour=0;
}
else if(Key4==0)
{
Delay(10);
if(Key4==0)
while (!Key4);
hour--;
if(hour==0)
hour=23;
}
}
}
}
void Keyscan2()
{
static uChar m=0,n=0;
if(Key1==0)
{
Delay(10);
if(Key1==0)
while(!Key3);
m++;
}
if(m%2==1)
{
TR0=0;
}
if(m%2==0)
{
TR0=1;
}
if(m%2==1)
{
TR0=0;
if(Key2==0)
{
Delay(10);
if(Key2==0)
while(!Key2);
n++;
}
if(n%4==1)
{
if(Key4==0)
{
Delay(10);
if(Key4==0)
while (!Key4);
day--;
if(day==0)
day=30;
}
if(Key3==0)
{
Delay(10);
if(Key3==0)
while(!Key3);
day++;
if(day==30)
day=0;
}
}
if(n%4==2)
{
if(Key4==0)
{
Delay(10);
if(Key4==0)
while (!Key4);
month--;
if(month==0)
month=12;
}
if(Key3==0)
{
Delay(10);
if(Key3==0)
while(!Key3);
month++;
if(month==12)
month=0;
}
}
if(n%4==3)
{
if(Key4==0)
{
Delay(10);
if(Key4==0)
while (!Key4);
year--;
if(year==0)
year=99;
}
if(Key3==0)
{
Delay(10);
if(Key3==0)
while(!Key3);
year++;
if(year==99)
year=0;
}
}
}
}
void main()
{
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
count=0;
while(1)
{
static uChar h=0;
if(Key5==0)
{
Delay(10);
if(Key5==0)
while(!Key5);
h++;
}
if(h%2==0)
{
Dispaly1(second,minute,hour);
Keyscan1();
}
if(h%2==1)
{
Dispaly2(year,month,day);
Keyscan2();
}
}
}
void time0_int(void) interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
count++;
if(count==100)
{
count=0;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
{
hour=0;
day++;
if(day==30)
{
day=0;
month++;
if(month==12)
{
month=0;
year++;
if(year==99)
{
year=0;
}
}
}
}
}
}
}
if(second==00&&minute==00)
Buzzer=0;
else
Buzzer=1;
}
根据代码,我觉得,猜测一下错误可能发生在定时器0中断中,具体原因可能是秒钟、分钟和小时的值超出了其允许的范围,或者定时器的频率设置不正确。
在定时器0中断中,会根据计数器count的值来累加秒钟、分钟和小时的值,如果超出了其允许的范围,就会导致错误的显示。例如,如果秒钟的值超过了59,就会被重置为0,并且分钟的值会加1;如果分钟的值超过了59,就会被重置为0,并且小时的值会加1;如果小时的值超过了23,就会被重置为0,并且日期的值会加1,以此类推。
另外,定时器的频率设置也可能导致错误的显示。在本代码中,定时器0的工作模式为方式1,计数器的初值为65536-10000,即定时1ms。如果定时器的频率不正确,就会导致计数器溢出的时间不准确,从而影响秒钟、分钟和小时的计数。
你要么 再检查秒钟、分钟和小时的计数范围,确保不会超出允许的范围。或者使用调试工具来检查定时器的频率设置是否正确,以确保计数器的溢出时间准确。
同时,建议在定时器0中断中加入一些错误处理代码,比如在发生错误时输出调试信息或者重置秒钟、分钟和小时的值,以避免错误的显示。
如果我的回答对你有所帮助,麻烦帮忙给个解决,或者关注一下
👍 点赞 ⭐ 收藏 📝留言 都是我创作的最大的动力!
通讯录