我现在显示的时分秒都没有问题,走到60秒秒清零00;分++;等1;现在的问题的加到24小时之后就成了1月0日;这不对啊!!我定义的数组是0-9;
#include<reg52.h>
#define unint unsigned int
#define unchar unsigned char
unchar code table0[]="2010-01-01 WeeK";
unchar code table1[]="00:00:00";
unchar code table2[]={'0','1','2','3','4','5','6','7','8','9'};
sbit rw=P2^5;
sbit rs=P2^6;
sbit lcden=P2^7;
char jsnum=0,sec,ff,riri,yue,hh,key0num=0;
void write_sfh(unchar ,unchar );
void write_nianyueri(unchar ,unchar );
void delay(unint z) //延迟
{
unint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(unchar com) // 写入指令数据到LCD
{
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(unchar date) //写入字符显示数据到LCD
{
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void lcd_init()
{
unint t;
write_com(0x38); //LCD初始化设定
write_com(0x0c);
write_com(0x06);
write_com(0x01); //清除LCD的显示内容
write_com(0x80); //第一行显示
for(t=0;t<15;t++) // 输入年,月,日
{
write_date(table0[t]);
delay(5);
}
write_com(0x80+0x40); //第二行显示
for(t=0;t<8;t++) //输入时,分,秒
{
write_date(table1[t]);
delay(5);
}
}
void time_init() //定时器中断
{
TMOD=0x01;
TH0=(65536-50000)/256; //定时器T0 50毫秒
TL0=(65536-50000)%256;
EA=1; //开总中断
ET0=1; //开定时器0中断
TR0=1;
} // 启动定时器0
void main()
{
rw=0;
sec=0;
ff=0;
hh=0;
lcd_init(); //LCD初始化
time_init(); //定时器中断
while(1)
{
if(jsnum==2) //20次50毫秒为1秒
{
jsnum=0;
sec++;
if(sec==2) //秒加到60则进位分钟
{
sec=0; //同时秒数清0
ff++;
if(ff==2) //分钟加到60则进位小时
{
ff=0; //同时分钟清0
hh++;
if(hh==2) //小时加到24则小时清0
{
hh=0;
riri++;
if(riri==2)
{
riri=0;
yue++;
if(yue==12)
{
yue=0;
}
write_nianyueri(5,yue);
}
write_nianyueri(8,riri);
}
write_sfh(0,hh);
}
write_sfh(3,ff);
}
write_sfh(6,sec);
}
}
}
void time0() interrupt 1 //定时器中断
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
jsnum++;
}
void write_sfh(unchar add,unchar date) //写时分秒函数
{
unchar shi,ge;
shi=date/10; //分解一个2位数的十位和个位
ge=date%10;
write_com(0x80+0x40+add); // 设置显示位置
write_date(table2[shi]); // 送去液晶显示十位
write_date(table2[ge]); // 送去液晶显示个位
}
void write_nianyueri(unchar ad,unchar date) //写时分秒函数
{
unchar ri,riri1;
riri1=date/10;
ri=date%10;
write_com(0x80+ad); // 设置显示位置
write_date(table2[riri1]); // 送去液晶显示个位
write_date(table2[ri]);
}
|