程序如下,上电能够显示SUN,依次显示到STA后,轮到SUN时,还是STA,跳过SUN,可以显示其它星期。但到了一定时间,显示到某个星期后再也不动了,不知是哪里原因,高手指点,谢谢(图片怎么不能传?)
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit EP = P3^2; //使能端
sbit RW = P3^1; //数据/命令选择端
sbit RS = P3^0; //读写选择端
sbit KEY_1 = P3^4; //设置模式选择
sbit KEY_2 = P3^5; //调整时间
uint code table1[] = {0, 31, 59, 90, 120, 151,181, 212, 242, 273, 303, 334};
uchar code *Week[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
uchar sec = 0, hour = 12, minu = 0;
uchar count = 0, month = 1, day = 1;
void T0_Init()
{
ET0 = 1;
TMOD = 0x01;
TH0 = (65536 - 46071)/256;
TL0 = (65536 - 46071)%256;
TR0 = 1;
}
void delay(uint ms)
{
uint i, j;
for(i=ms; i>0; i--)
for(j=110; j>0; j--);
}
void Lcd_Ready() //测忙函数
{
uchar sta;
RS = 0;
RW = 1;
do {
EP = 1;
sta = P1;
EP = 0;
}while(sta & 0x80); //检测P1最高位是否为1,若为1,则1602忙,为0则可以读写操作
}
void Write_Cmd(uchar cmd) //写入命令
{
Lcd_Ready();
RS = 0;
RW = 0;
P1 = cmd;
delay(5);
EP = 1;
delay(5);
EP = 0;
}
void Write_Dat(uchar dat) //写入数据
{
Lcd_Ready(); //LCD1602 不忙则跳出
RS = 1;
RW = 0;
P1 = dat;
delay(5);
EP = 1;
delay(5);
EP = 0;
}
void Write_Week() //星期与日期同步
{
static uchar week = 0;
week = ((table1[month - 1]) + day - 1) % 7;
Write_Cmd(0x80 + 12);
while(*Week[week] != '\0')
Write_Dat(*Week[week]++);
}
void Write_Num(uchar x, uchar y, uchar num) //将时分秒和日期写入1602
{
uchar unit, decad;
unit = num % 10;
decad = num /10;
if(y == 1)
{
Write_Cmd(0x80 + 0x40 + x);
Write_Dat(0x30 + decad); //即加48,将数字转换为ASC||字符数字
Write_Dat(0x30 + unit);
}
else
{
Write_Cmd(0x80 + x);
Write_Dat(0x30 + decad);
Write_Dat(0x30 + unit);
}
}
uchar Get_Day(uchar m)
{
if(m == 1||m == 3||m == 5||m == 7||m == 8||m == 10||m == 12)
return 31;
if(m == 4||m == 6||m == 9||m == 11)
return 30;
if(m == 2)
return 28;
return 0;
}
void Lcd_Init() //LCD初始化设定
{
Write_Cmd(0x38); // 16*2显示,5*7点阵,8位数据
Write_Cmd(0x0c); // 显示开,关光标
Write_Cmd(0x06); // 读写地址自动加1,光标加1
Write_Cmd(0x01); //清除屏幕
}
void Display()
{
Write_Week();
Write_Num(9, 0, day);
Write_Num(6, 0, month);
Write_Num(4, 1, hour);
Write_Num(7, 1, minu);
Write_Num(10, 1, sec);
}
void TimerCount()
{
if(count == 20)
{
count = 0;
sec++;
if(sec == 60)
{
sec = 0;
minu++;
if(minu == 60)
{
minu = 0;
hour++;
if(hour == 24)
{
hour = 0;
day++;
if(day > Get_Day(month))
{
day = 1;
month++;
if(month >12)
month = 1;
}
}
}
}
Display();
}
}
void KeyScan()
{
static uchar key1cnt = 0; //KEY_1按下的次数
if(KEY_1 == 0)
{
delay(10);
if(KEY_1 == 0)
{
key1cnt++;
while(!KEY_1);
switch(key1cnt)
{
case 1:
TR0 = 0;
Write_Cmd(0x80 + 0x40 + 5);
Write_Cmd(0x0F); //显示光标且光标闪烁
break;
case 2:
Write_Cmd(0x80 + 0x40 + 8);
break;
case 3:
Write_Cmd(0x80 + 0x40 + 11);
break;
case 4:
Write_Cmd(0x80 + 7);
break;
case 5:
Write_Cmd(0x80 + 10);
break;
case 6:
TR0 = 1;
Write_Cmd(0x0C); //不显示光标
key1cnt = 0;
break;
}
}
}
if(key1cnt != 0)
{
if(KEY_2 == 0)
{
delay(10);
if(KEY_2 == 0)
{
while(!KEY_2);
switch(key1cnt)
{
case 1:
hour++;
if(hour == 24)
hour = 0;
Write_Num(4, 1, hour);
Write_Cmd(0x80 + 0x40 + 5);
break;
case 2:
minu++;
if(minu == 60)
minu = 0;
Write_Num(7, 1, minu);
Write_Cmd(0x80 + 0x40 + 8);
break;
case 3:
sec++;
if(sec == 60)
sec = 0;
Write_Num(10, 1, sec);
Write_Cmd(0x80 + 0x40 + 11);
break;
case 4:
month++;
if(month > 12)
month = 1;
Write_Num(6, 0, month);
Write_Week();
Write_Cmd(0x80 + 7);
break;
case 5:
day++;
if(day > Get_Day(month))
day = 1;
Write_Num(9, 0, day);
Write_Week();
Write_Cmd(0x80 + 10);
break;
}
}
}
}
}
void main()
{
uchar i = 0;
uchar code table[] = {"2017-01-01"};
EA = 1;
Lcd_Init(); //1602初始化
T0_Init(); //定时器0初始化
Write_Cmd(0x80 + 0x40 + 6);
Write_Dat(':');
Write_Cmd(0x80 + 0x40 + 9);
Write_Dat(':');
Write_Cmd(0x80 + 1);
while(table[i] != '\0')
{
Write_Dat(table[i]);
i++;
}
while(1)
{
KeyScan();
}
}
void Timer0() interrupt 1
{
TH0 = (65536 - 46071)/256;
TL0 = (65536 - 46071)%256;
count++;
TimerCount();
}
|