自己总觉得程序有待优化,且星期显示有问题,对7求余为0时,星期不变化,请高手给予指点,谢谢!
#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; //
static uint code table1[] = {0, 31, 59, 90, 120, 151,181, 212, 242, 273, 303, 334};
static uchar code *Week[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
uchar sec = 0, hour = 12, minu = 0;
uchar count = 0, week = 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(uchar day, uchar month) //星期与日期同步
{
week = (table1[(month - 1)] + day - 1);
week = week % 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);
}
}
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(day, month);
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);
Write_Cmd(0x80 + 0x40 + 6);
Write_Dat(':');
Write_Cmd(0x80 + 0x40 + 9);
Write_Dat(':');
}
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++;
week++;
if(week == 7)
week = 0;
switch(month)
{
case 1:
if(day == 32)
{
day = 0;
month++;
break;
}
case 2:
if(day == 29)
{
day = 0;
month++;
break;
}
case 3:
if(day == 32)
{
day = 0;
month++;
Write_Num(6, 0, month);
break;
}
case 4:
if(day == 31)
{
day = 0;
month++;
break;
}
case 5:
if(day == 32)
{
day = 0;
month++;
break;
}
case 6:
if(day == 31)
{
day = 0;
month++;
break;
}
case 7:
if(day == 32)
{
day = 0;
month++;
break;
}
case 8:
if(day == 32)
{
day = 0;
month++;
break;
}
case 9:
if(day == 31)
{
day = 0;
month++;
break;
}
case 10:
if(day == 32)
{
day = 0;
month++;
break;
}
case 11:
if(day == 31)
{
day = 0;
month++;
Write_Num(6, 0, month);
break;
}
case 12:
if(day == 32)
{
day = 0;
month = 1;
Write_Num(6, 0, month);
break;
}
}
}
}
}
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 == 13)
month = 1;
Write_Num(6, 0, month);
Write_Cmd(0x80 + 7);
break;
case 5:
day++;
if(day == 32)
day = 1;
Write_Num(9, 0, day);
Write_Cmd(0x80 + 10);
break;
}
}
}
}
}
void main()
{
uchar i = 0;
uchar code table[] = {"2017-01-01 Sun"};
EA = 1;
Lcd_Init(); //1602初始化
T0_Init(); //定时器0初始化
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();
}
|