- # include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit rs = P3^5;
- sbit en = P3^4;
- sbit rw = P3^6;
- sbit we = P2^7;
- sbit du = P2^7;
- sbit S2 = P3^0;
- sbit S3 = P3^1;
- sbit S4 = P3^2;
- sbit S5 = P3^3;
- sbit fm = P2^3;
- sbit led = P1^3;
- uchar count, S2num, tian;
- uchar sec = 55, min = 33, hour = 16;
- uchar code table[] = " 2020-03-19";
- uchar code table1[] = " 16:33:55 MJW";
- void delay(uint z) //延时函数
- {
- uint x, y;
- for(x = z; x > 0;x --)
- for (y = 110; y > 0; y --);
- }
- void write_com(uchar com) //写命令函数
- {
- rs = 0;
- P0 = com;
- delay(5);
- en = 1;
- delay(5);
- en = 0;
- }
- void write_dat(uchar date) //写数据函数
- {
- rs = 1;
- P0 = date;
- delay(5);
- en = 1;
- delay(5);
- en = 0;
- }
- void init() //初始化函数
- {
- uchar num;
- rw =0;
- en = 0;
- write_com(0x38);
- write_com(0x0c);
- write_com(0x06);
- write_com(0x01);
- write_com(0x80);
- for(num = 0; num < 13; num ++)
- write_dat(table[num]);
- write_com(0x80 + 0x40);
- for(num = 0; num < 15; num ++)
- write_dat(table1[num]);
- TMOD = 0x01;
- TH0 = (65536 - 50000) / 256;//再次装定时器初值
- TL0 = (65536 - 50000) % 256;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void timer0() interrupt 1 //定时器函数
- {
- TH0 = (65536 - 50000) / 256;//再次装定时器初值
- TL0 = (65536 - 50000) % 256;
- count ++;
- }
- void write_sfm(uchar add, uchar date) //时分秒函数
- {
- uchar shi, ge;
- shi = date / 10;
- ge = date % 10;
- write_com(0x80 + 0x40 + add); //显示的位置
- write_dat(0x30 + shi);
- write_dat(0x30 + ge);
- }
- void naozhong() // 蜂鸣器/闹钟函数
- {
- if(hour == 18) //设置闹钟时间:时
- {
- if(min == 30) //设置闹钟时间:分
- {
- if(sec == 0) //设置闹钟时间:秒
- fm = 0; //0打开蜂鸣器,1关闭蜂鸣器
- }
- }
- if(S5 == 0)
- {
- delay(5);
- if(S5 == 0)
- fm = 1; //按下S5停止蜂鸣器
- }
- }
- void Key() //按键函数
- {
- if(S2 == 0) //按下按键
- {
- delay(5); //软件消抖
- if(S2 == 0) //确认按下
- {
- S2num ++; //按键次数增加
- while(!S2); //松开按键
- TR0 = 0; //关闭定时器
- write_com(0x0f); //打开光标且闪烁
- if(S2num == 1)
- write_com(0x80 + 0x40 + 9); //修改时间秒数
- if(S2num == 2)
- write_com(0x80 + 0x40 + 6); //修改时间分钟数
- if(S2num == 3)
- write_com(0x80 + 0x40 + 3); //修改时间小时数
- if(S2num == 4)
- write_com(0x80 + 12); //修改日期天数
- if(S2num == 5)
- write_com(0x80 + 9); //修改日期月份
- if(S2num == 6)
- write_com(0x80 + 6); //修改日期年份
- if(S2num == 7)
- {
- S2num = 0; //按键次数清零
- write_com(0x0c); //关闭光标且停止闪烁
- TR0 = 1; //打开定时器
- }
- }
- }
- if(S2num != 0) //按下修改按键,增加和减少按键才有效
- {
- if(S3 == 0) //增加键按下
- {
- delay(5); //软件消抖
- if(S3 == 0)
- {
- while(!S3); //确定按键按下
- if(S2num == 1)
- {
- sec ++;
- if(sec == 60)
- sec = 0;
- write_sfm(8, sec);
- write_com(0x80 + 0x40 + 9);
- }
- if(S2num == 2)
- {
- min ++;
- if(min == 60)
- min = 0;
- write_sfm(5, min);
- write_com(0x80 + 0x40 + 6);
- }
- if(S2num == 3)
- {
- hour ++;
- if(hour == 24)
- hour = 0;
- write_sfm(2, hour);
- write_com(0x80 + 0x40 + 3);
- }
- }
- }
- if(S4 == 0) //减少键按下
- {
- delay(5);
- if(S4 == 0)
- {
- while(!S4); //确定按键按下
- if(S2num == 1)
- {
- sec --;
- if(sec == -1)
- sec = 59;
- write_sfm(8, sec);
- write_com(0x80 + 0x40 + 9);
- }
- if(S2num == 2)
- {
- min --;
- if(min == -1)
- min = 59;
- write_sfm(5, min);
- write_com(0x80 + 0x40 + 6);
- }
- if(S2num == 3)
- {
- hour --;
- if(hour == -1)
- hour = 23;
- write_sfm(2, hour);
- write_com(0x80 + 0x40 + 3);
- }
- }
- }
- }
- }
- void main() //主函数
- {
- init();
- while(1)
- {
- Key(); //插入按键函数
- naozhong(); //插入闹钟函数
- if(count == 18) //定时1秒
- {
- count = 0;
- led = ~led;
- sec ++;
- if(sec == 60)
- {
- sec = 0;
- min ++;
- if(min == 60)
- {
- min = 0;
- hour ++;
- if(hour == 24)
- {
- hour = 0;
- }
- write_sfm(2, hour); //小时送回LED屏幕显示
- }
- write_sfm(5, min); //分钟送回LED屏幕显示
- }
- write_sfm(8, sec); //秒送回LED屏幕显示
- }
- }
- }
复制代码
|