/*
这是avr上移植程序 动态显示字符,用户可以根据自己需要更改内容
*/
#include <reg51.h>
sbit key0 = P3^0;//定义端口
sbit key1 = P3^1;
sbit key2 = P3^2;
sbit key3 = P3^3;
sbit RS = P3^4;
sbit RW = P3^5;
sbit EN = P3^6;
sbit BEEP = P3^7;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
int count;
int year,month,day,hour,minute,second;
int ayear,amonth,aday,ahour,aminute,asecond;//闹钟时间
int tyear,tmonth,tday,thour,tminute,tsecond;//时间寄存
int alarmon,disp;//闹钟启动标志位,当前页面状态
int monthcount;
unsigned char del;
/*延时函数*/
void delay_us(unsigned int n) //延时 如果需要高精度延时 请嵌入汇编
{
if (n == 0)
{
return ;
}
while (--n);
}
/*延时函数*/
void delay_ms(unsigned char i)
{
unsigned char a, b;
for (a = 1; a < i; a++)
{
for (b = 1; b; b++)
{ ; }
}
}/*显示屏命令写入函数*/
void LCD_write_com(unsigned char com)
{
RS_CLR;
RW_CLR;
EN_SET;
P2 = com;
delay_us(5);
EN_CLR;
}
/*显示屏命令写入函数*/
void LCD_write_Data(unsigned char Data)
{
RS_SET;
RW_CLR;
EN_SET;
P2 = Data;
delay_us(5);
EN_CLR;
}
/*显示屏清空显示*/
void LCD_clear(void)
{
LCD_write_com(0x01);
delay_ms(5);}
/*显示屏字符串写入函数*/
void LCD_write_str(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_write_com(0x80 + x);
}
else
{
LCD_write_com(0xC0 + x);
}
while (*s)
{
LCD_write_Data( *s);
s ++;
}
}
/*显示屏单字符写入函数*/
void LCD_write_char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 0)
{
LCD_write_com(0x80 + x);
}
else
{
LCD_write_com(0xC0 + x);
}
LCD_write_Data( Data);
}
/*显示屏初始化函数*/
void LCD_init(void)
{
LCD_write_com(0x38); /*显示模式设置*/
delay_ms(5);
LCD_write_com(0x38);
delay_ms(5);
LCD_write_com(0x38);
delay_ms(5);
LCD_write_com(0x38);
LCD_write_com(0x08); /*显示关闭*/
LCD_write_com(0x01); /*显示清屏*/
LCD_write_com(0x06); /*显示光标移动设置*/
delay_ms(5);
LCD_write_com(0x0C); /*显示开及光标设置*/
}
/*显示时间函数*/
void showtime()
{
char ys,yg,tmonths,tmonthg,ds,dg,hs,hg,ms,mg,ss,sg;
unsigned char time[10] = {'0','1','2','3','4','5','6','7','8','9'};
ys=time[tyear/10];
yg=time[tyear%10];
tmonths=time[tmonth/10];
tmonthg=time[tmonth%10];
ds=time[tday/10];
dg=time[tday%10];
hs=time[thour/10];
hg=time[thour%10];
ms=time[tminute/10];
mg=time[tminute%10];
ss=time[tsecond/10];
sg=time[tsecond%10];
LCD_clear();
LCD_write_char(0,0,ys);
LCD_write_char(1,0,yg);
LCD_write_char(2,0,'-');
LCD_write_char(3,0,tmonths);
LCD_write_char(4,0,tmonthg);
LCD_write_char(5,0,'-');
LCD_write_char(6,0,ds);
LCD_write_char(7,0,dg);
LCD_write_char(0,1,hs);
LCD_write_char(1,1,hg);
LCD_write_char(2,1,':');
LCD_write_char(3,1,ms);
LCD_write_char(4,1,mg);
LCD_write_char(5,1,':');
LCD_write_char(6,1,ss);
LCD_write_char(7,1,sg);
if(alarmon==1)LCD_write_str(9,0,"ALARM");
if(disp==0)LCD_write_str(9,1,"clock");
if(disp==1)LCD_write_str(9,1,"alarm");
if(disp==2)LCD_write_str(9,1,"edit");
delay_ms(100);
}
void add(int mod)//不同位置下时间加一
{
if(mod==0)
{
tyear=tyear+10;
if(tyear>=100)tyear=tyear-100;
}
if(mod==1)
{
tyear=tyear+1;
if(tyear%10==0)tyear=tyear-10;
}
if(mod==2)
{
if(tmonth/10==0)
{
tmonth=tmonth+10;
if(tmonth>12)tmonth=10;
}
else
{
if(tmonth%10>2)tmonth=1;
else
{
tmonth=tmonth-10;
if(tmonth==0)tmonth=1;
}
}
}
if(mod==3)
{
if(tmonth/10==0)
{
tmonth++;
if(tmonth>9)tmonth=1;
}
else
{
tmonth++;
if(tmonth>12)tmonth=10;
}
}
if(mod==4)
{
if(tmonth==1||tmonth==3||tmonth==5||tmonth==7||tmonth==8||tmonth==10||tmonth==12)
{
tday=tday+10;
if(tday>31)tday=tday%10;
}
else
{
if(month==4||month==6||month==9||month==11)
{
tday=tday+10;
if(tday>30)tday=tday%10;
}
else
{
tday=tday+10;
if((tyear%4==0&&tyear%100!=0)||tyear%400==0)
{
if(tday>29)tday=tday%10;
}
else
{
if(tday>28)tday=tday%10;
}
}
}
}
if(mod==5)
{
if(tmonth==1||tmonth==3||tmonth==5||tmonth==7||tmonth==8||tmonth==10||tmonth==12)
{
tday=tday+1;
if(tday==10)tday=1;
if(tday==20)tday=10;
if(tday==30)tday=20;
if(tday==31)tday=30;
}
else
{
if(month==4||month==6||month==9||month==11)
{
tday=tday+1;
if(tday==10)tday=1;
if(tday==20)tday=10;
if(tday==30)tday=20;
}
else
{
tday=tday+1;
if((tyear%4==0&&tyear%100!=0)||tyear%400==0)
{
if(tday==10)tday=1;
if(tday==20)tday=10;
if(tday==30)tday=20;
}
else
{
if(tday==10)tday=1;
if(tday==20)tday=10;
if(tday==29)tday=20;
}
}
}
}
if(mod==6)
{
thour=thour+10;
if(thour>24)thour=thour%10;
}
if(mod==7)
{
thour=thour+1;
if(thour==10)thour=0;
if(thour==20)thour=10;
if(thour==24)thour=20;
}
if(mod==8)
{
tminute=tminute+10;
if(tminute>60)tminute=tminute%10;
}
if(mod==9)
{
tminute++;
if(tminute%10==0)tminute=tminute-10;
}
if(mod==10)
{
tsecond=tsecond+10;
if(tsecond>60)tsecond=tsecond%10;
}
if(mod==11)
{
tsecond++;
if(tsecond%10==0)tsecond=tsecond-10;
}
}
void scankey()//按键情况
{
int mode;
int hang,lie;
mode = 0;
while(1)
{
if(key0==0)
{
delay_ms(del);
if(key0==0)
{
while(key0==0);
return;
}
}
if(key1==0)
{
delay_ms(del);
if(key1==0)
{
while(key1==0);
mode--;
if(mode==-1)mode=11;
}
}
if(key2==0)
{
delay_ms(del);
if(key2==0)
{
while(key2==0);
mode++;
if(mode>11)mode=0;
}
}
if(key3==0)
{
delay_ms(del);
if(key3==0)
{
while(key3==0);
add(mode);
}
}
switch(mode)
{
case 0:hang=0;lie=0;break;
case 1:hang=0;lie=1;break;
case 2:hang=0;lie=3;break;
case 3:hang=0;lie=4;break;
case 4:hang=0;lie=6;break;
case 5:hang=0;lie=7;break;
case 6:hang=1;lie=0;break;
case 7:hang=1;lie=1;break;
case 8:hang=1;lie=3;break;
case 9:hang=1;lie=4;break;
case 10:hang=1;lie=6;break;
case 11:hang=1;lie=7;break;
default:;
}
LCD_write_char(lie,hang,'_');
delay_ms(100);
showtime();
}
}
void otot()
{
tyear=year;
tmonth=month;
tday=day;
thour=hour;
tminute=minute;
tsecond=second;
}
void ttoo()
{
year=tyear;
month=tmonth;
day=tday;
hour=thour;
minute=tminute;
second=tsecond;
}
void ttoa()
{
ayear=tyear;
amonth=tmonth;
aday=tday;
ahour=thour;
aminute=tminute;
asecond=tsecond;
}
void atot()
{
tyear=ayear;
tmonth=amonth;
tday=aday;
thour=ahour;
tminute=aminute;
tsecond=asecond;
}
void alarm()//闹钟函数
{
otot();
scankey();//定时间
ttoa();
}
void checkalarm()//检查闹钟是否关闭
{
if(ayear==year&&amonth==month&&aday==day&&ahour==hour&&aminute==minute&&asecond==second)
{
alarmon = 0;
BEEP = 0;
ayear=0;
amonth=0;
aday=0;
ahour=0;
aminute=0;
asecond=0;
}
if(key2==0)
{
delay_ms(del);
if(key2==0)
{
while(key2==0);
BEEP = 1;
}
}
}
void change()//显示切换
{
atot();
while(key1)
{
showtime();
checkalarm();
if(key1==0)
{
delay_ms(del);
if(key1==0)
{
while(key1==0);
return;
}
}
}
}
void main(void)
{
del=5;
year=0;
month=1;
day=1;
TMOD=0x01;
ET0=1;
EA=1;
TR0=1;
delay_ms(100);
LCD_init();
while (1)
{
disp=0;
if(key0==0)
{
delay_ms(del);
if(key0==0)
{
while(key0==0);
disp=2;
otot();
scankey();//修改时间
ttoo();
}
}
if(key1==0)
{
delay_ms(del);
if(key1==0)
{
while(key1==0);
disp=1;
change();
}
}
if(key3==0)
{
delay_ms(del);
if(key3==0)
{
while(key3==0);
disp=1;
alarm();
alarmon = 1;
LCD.rar_闹钟液晶
版权申诉
27 浏览量
2022-09-24
02:27:08
上传
评论
收藏 3KB RAR 举报
邓凌佳
- 粉丝: 65
- 资源: 1万+