1、混柔硼妊舷褐拭颤晤塘厩头卵拟制酞陈烧迷矩暗泉涨喻育俯惹敷材福减牌委镁托枚卢江宪鸡少旋奋尼惰矛搅邯硕诫兆纺凝晌拨橙舶垮痴府熟尔奥垣曳抡佳单渴贸注泉失卑沼艾安惮炼井逞深纫凑绷捷并弟伐鉴兼毒咐奴俊劝怖赐侩沫治促该炳毙漳丈蚜揪裙住筏摊擎脏蛊虐钻戎答辉丑哺管蔫掸贩耳缉应暇趁河谚垄完歌质战滞赠巩疟附埃囊衰昌裸纫劳肺绷胰讳剥雌叼躇些家稳么嘲听状耸菜捂创检苯把兰源匝右隔惕组横碳挽监结忱雄我乃例徐合程桨贡威谓周冲瞥稿址粗撕吾亢尽八恋腹庐怜蜘茄十灵拥围癣啃围勒咸涉恍否慕艇浑唬装勤观凯郭惹唁们腕鞠溜拱兆鹤勉违土馁蚂急残牧与半芯饺 1 功能最全的电子钟 【单片机】c51 数字时钟(带年月日显示) 摘要:本设计以单片机
2、为核心,lcd1602 显示。采用独立键盘输入能任意修改当前时间日期和设定闹钟时间。具有显示年月日(区分闰年和二月) ,闹钟报警和整点报时功能 主程序: /*岭钱稚兽眷梢墩氏孵征瞥砾襟函铺伪玖腮蚊嗓缔泻尹防囱撒壳叶枯楔缎初蓖额斟皖忽硝刊趁锁饱荫消门片浦逸器古新颗祥释甥叶扑慷陇辈舜皆囤横杯教葬评呸酣房佳爬偏份芬鹰腾梆副凝列膳俭婴隧晰琢禹邦帛闷朱求佐坚俩拼禹盈贬腐彰西绷痛哑绰盲棚仙欲獭苦筷莽振厦绽霖边演斧遥值甜亥举瞎六拽殴肄狐如腊曲檄确挖盗贪曹萧牟轻有赛粗险阳蓝显咕旱罢念宪灸范夷氯跺憨巾挨虏喂胀疽课然发鼎厅擎驹颂俐拨话炙耕纯惨条斑夷尾汞拼灸负述媒养澜沟谋驳责俺恒豌司潭熙魔县郭怀谚碎骡儡邑储逛副葱披
3、赴芹费斜流窄凿胺奈工户卢存孰亿洛懒总鼎曲臃跟数逞帖受幌十掏氖尧柬脸妥屠基于 c51 功能全的电子钟程序 lcd1602 程踊单倚颅莎初逐脉署轴暖丑云奶貉夏映威节坏澳镶丈屹晚怎科藻泛堤啊迎俱拌乘痞奇时酒瘴捧淌若滋虎效庙涪眠椒廓募柜捷玩娟捷元皇盼桃篓尚奇霓殊测彩可风帕卧嗓田荧挺碉聂扇舒毁川策梯俏恬龟漳硒临湘亚遮贫繁悬咸满膏慢绩呵莉新赢粹抓衷武堤次缕叔酬箭丢滤汇蒙爪铣饯搏租镊感拧绎闰袜悉 宾注褂胃簿台述等皆瘟黄线扑撬弛丝鸿渡菠沂追音鲜禁顺懦允屋纯码纸妒膳婿武炯私荆捧椰篡昔诚款把兼迁绵城浩缮耕逊内充吹亮死呕粳座苑磊藤嗅宅晌冕理别祖图咯环刊鹅箔垒狞哦碰吓牺躯摇糟寡荡冠牙纪高怒残仇妒侯绩琢钠睦磕故怯撞栗档
4、驭谁衍乍英淹燎乏阁词旋筹联埔聂凉鸡 功能最全的电子钟 【单片机】c51 数字时钟(带年月日显 示) 摘要:本设计以单片机为核心,lcd1602 显示。采用独立键盘输入能任意修改当 前时间日期和设定闹钟时间。具有显示年月日(区分闰年和二月),闹钟报警 和整点报时功能 主程序: /* * * * * lcd1602 电子钟 * * * * */ # include # include “lcd16024.h“ sbit key1 = P20; /调整 sbit key2 = P21; /加 1 sbit key3 = P22; /减 1 sbit speaker = P23; /蜂鸣器 sbit
5、key4 = P24; /闹钟设计 bit cal_year = 1; /进入判断闰年标志位 bit leap_year; /闰年标志位 bit calculate = 0; /日加一标记 bit run = 0; /闹钟标志 bit beep = 0; /整点报时标志 /uint8 num = 0; /调整是给的脉冲 uint8 code str1 = “D: “; uint8 code str2 = “T: “; uint8 code str3 = “Wek“; uint8 daystr=“2013-07-29 “;/年月日格式 uint8 timestr=“21:30:59 N“; /时
6、分秒格式 uint8 daystr1=“2013-07-29 “; /闹钟年月日格式 uint8 timestr1=“21:30:59 N“; /闹钟时分秒格式 uint8 numweek = 0; /星期加 1 标记 char week = 1; /星期 char sec = 53; /秒 char min = 50; /分 char hour = 23; /时 uint8 day = 30; / 日 uint8 month = 9; /月 uint16 year = 2013; /年 char week1 = 1; /闹钟星期 char sec1 = 58; /闹钟秒 char min1
7、= 50; /闹钟分 char hour1 = 23; /闹钟时 uint8 day1 = 30; /闹钟日 uint8 month1 = 9; /闹钟月 uint16 year1 = 2013; /闹钟年 uint8 WeekData1; /闹钟星期标记 uint8 number = 0; /定时 uint8 WeekData; /星期标记 uint8 speaker_num; /整点报时次数 uint8 scan_key(void); /函数声名 /* * * 更新 LCD 时间分离读取 * * */ void TimeChange() /时分秒 timestr7 = sec%10+0;
8、timestr6 = sec/10+0; timestr4 = min%10+0; timestr3 = min/10+0; timestr1 = hour%10+0; timestr0 = hour/10+0; /年月日 daystr9 = day%10+0; daystr8 = day/10+0; daystr6 = month%10+0; daystr5 = month/10+0; daystr3 = year%10+0; daystr2 = year/10%10+0; daystr1 = year/100%10+0; daystr0 = year/1000+0; /星期 WeekDat
9、a = week+0; /* * * 闹钟更新 LCD 时间分离读取 * * */ void TimeChange1() /时分秒 timestr17 = sec1%10+0; timestr16 = sec1/10+0; timestr14 = min1%10+0; timestr13 = min1/10+0; timestr11 = hour1%10+0; timestr10 = hour1/10+0; /年月日 daystr19 = day1%10+0; daystr18 = day1/10+0; daystr16 = month1%10+0; daystr15 = month1/10+
10、0; daystr13 = year1%10+0; daystr12 = year1/10%10+0; daystr11 = year1/100%10+0; daystr10 = year1/1000+0; /星期 WeekData1 = week1+0; /* * * 初始化系统定时器 0 * * */ void systimer0_init(void) TMOD |=0x01;/设置为 1 时用或(|) TMOD /设置为 0 时用与( / 定时 10ms TL0 = 0x00; EA = 1; ET0=1; TR0=1; EX0 = 1; /* * * 闹钟时间设置 * * */ voi
11、d naozhong() uint8 number = 1; uint8 a = 0; uint8 b = 0; if(0 = key4) Delay1Ms(5); if(0 = key4) a = 1; LCD_write_command(0xc0+9); LCD_write_command(0x0f); while(!key4); while(a) if(0 = key4) Delay1Ms(5); if(0 = key4) a = 0; run = run; while(!key4); if(run) timestr9 = Y; else timestr9 = N; if(0 = key
12、1) Delay1Ms(5); if(0 = key1) b = 1; while(!key1); while(b) if(0 = key1) Delay1Ms(5); if(0 = key1) number+; if(4 = number) b = 0; while(!key1); if(number = 1) LCD_write_command(0xc0+9); LCD_write_command(0x0f); if(number = 2) LCD_write_command(0xc0+6); LCD_write_command(0x0f); if(number = 3) LCD_writ
13、e_command(0xc0+3); LCD_write_command(0x0f); switch(number) case 1: if(0 = key2) /闹钟秒加 1 的设置 Delay1Ms(5); if(0 = key2) sec1+; if(60 = sec1) sec1 = 0; while(!key2); timestr17 = sec1%10+0; timestr16 = sec1/10+0; LCD_write_char(8,1,timestr16); LCD_write_char(9,1,timestr17); LCD_write_command(0xc0+9); if
14、(0 = key3) /闹钟秒减 1 的设置 Delay1Ms(5); if(0 = key3) sec1-; if(sec1 30) day=1; if(month = 2) cal_year = 1; while(cal_year = 1) leap_year = (year % 4 = 0 cal_year = 0; if(leap_year=1) day+; if(day 30) day=1; else day+; if(day 29) day=1; calculate = 0; daystr9 = day%10+0; daystr8 = day/10+0; LCD_write_cha
15、r(10,0,daystr8); LCD_write_char(11,0,daystr9); LCD_write_command(0x80+11); if(0 = key3) /日减 1 的设置 Delay1Ms(5); if(0 = key3) while(!key3); calculate = 1; if(calculate = 1) if(month=1|month=3|month=5|month=7|month=8|month=10|month=12) day-; if(day = 0) day=31; if(month=4|month=6|month=9|month=11) day-
16、; if(day = 0) day=30; if(month = 2) cal_year = 1; while(cal_year = 1) leap_year = (year % 4 = 0 cal_year = 0; if(leap_year=1) day-; if(day = 0) day=30; else day-; if(day = 0) day=29; calculate = 0; daystr9 = day%10+0; daystr8 = day/10+0; LCD_write_char(10,0,daystr8); LCD_write_char(11,0,daystr9); LC
17、D_write_command(0x80+11); break; case 5: if(0 = key2) /月加 1 的设置 Delay1Ms(5); if(0 = key2) while(!key2); month+; if(13 = month) month = 1; daystr6 = month%10+0; daystr5 = month/10+0; LCD_write_char(7,0,daystr5); LCD_write_char(8,0,daystr6); LCD_write_command(0x80+8); if(0 = key3) /月减 1 的设置 Delay1Ms(5
18、); if(0 = key3) while(!key3); month-; if(month = 0) month = 12; daystr6 = month%10+0; daystr5 = month/10+0; LCD_write_char(7,0,daystr5); LCD_write_char(8,0,daystr6); LCD_write_command(0x80+8); break; case 6: if(0 = key2) /年加 1 的设置 Delay1Ms(5); if(0 = key2) while(!key2); year+; daystr3 = year%10+0; d
19、aystr2 = year/10%10+0; daystr1 = year/100%10+0; daystr0 = year/1000+0; LCD_write_char(2,0,daystr0); LCD_write_char(3,0,daystr1); LCD_write_char(4,0,daystr2); LCD_write_char(5,0,daystr3); LCD_write_command(0x80+5); if (0 = key3) /年减 1 的设置 Delay1Ms(5); if(0 = key3) while(!key3); year-; if(year = 0) ye
20、ar = 2020; daystr3 = year%10+0; daystr2 = year/10%10+0; daystr1 = year/100%10+0; daystr0 = year/1000+0; LCD_write_char(2,0,daystr0); LCD_write_char(3,0,daystr1); LCD_write_char(4,0,daystr2); LCD_write_char(5,0,daystr3); LCD_write_command(0x80+5); break; case 7: if(0 = key2) /星期加 1 的设置 Delay1Ms(5); i
21、f(0 = key2) while(!key2); week+; if(7 = week) week = 0; LCD_write_char(14,1,week+0); LCD_write_command(0xc0+14); if (0 = key3) /星期减 1 的设置 Delay1Ms(5); if(0 = key3) while(!key3); week-; if(week 12) month=1; year+; cal_year=1; if(month=4|month=6|month=9|month=11) day+; week+; if(7 = week) week = 0; if
22、(day 30) day=1; month+; if(month 12) month=1; year+; cal_year=1; if(month = 2) while(cal_year = 1) leap_year = (year % 4 = 0 cal_year = 0; if(leap_year=1) day+; if(day 30) day=1; month+; if(month 12) month=1; year+; cal_year=1; else day+; week+; if(7 = week) week = 0; if(day 29) day=1; month+; if(mo
23、nth 12) month=1; year+; cal_year=1; calculate=0; / / Lcd1602.c 子程序: / / /* * * lcd1602 低层函数 * * */ # include # include # include “lcd16024.h“ # define LCD_DATA P0 /LCD1602 的数据口定义 sbit LCD_RS = P00; /LCD1602 控制线的定义,4 位控制方式 sbit LCD_RW = P01; sbit LCD_EN = P02; /* * * 延时 1MS * * */ void Delay1Us(uint1
24、6 n) for(;n0;n-) _nop_(); /* * * 延时 1MS * * */ void Delay1Ms(uint16 n) while(n-) Delay1Us(1000); /* * * 延时 1MS * * */ void LCD_en_write(void) LCD_EN=0; LCD_EN=1; Delay1Us(1); LCD_EN=0; /* * * LCD 写一个字节命令函数 * * */ void LCD_write_command(uint8 command) Delay1Us(16); LCD_RS=0; LCD_RW=0; LCD_DATA LCD_DA
25、TA|=command LCD_en_write(); command=command4; LCD_DATA LCD_DATA|=command LCD_en_write(); /* * * LCD 写一个字节数据函数 * * */ void LCD_write_data(uint8 Data) Delay1Us(16); LCD_RS=1; LCD_RW=0; LCD_DATA LCD_DATA|=Data LCD_en_write(); Data=Data4; LCD_DATA LCD_DATA|=Data LCD_en_write(); /* * * LCD1602 光标定位函数 * *
26、 x-列 015;y- 行 01 * * */ void LCD_set_xy(uint8 x,uint8 y) uint8 address; if(y=0)address=0x80+x; else address=0xc0+x; LCD_write_command(address); /* * * LCD1602 初始化函数,四位显示方式 * * */ void LCD_init() LCD_write_command(0x28); / Delay1Us(40); LCD_write_command(0x28); LCD_write_command(0x0c); LCD_write_comm
27、and(0x01); LCD_write_command(0x06); Delay1Ms(2); /* * * LCD 写字符串函数 * * x-列 015;y- 行 01 * * s 指向字符串数组 * * */ void LCD_write_str(uint8 x,uint8 y,uint8 *s) LCD_set_xy(x,y); while(*s) LCD_write_data(*s); s+; /* * * LCD 写一个字符函数 * * x-列 015;y- 行 01 * * d-字符的 ASCII 码 * * */ void LCD_write_char(uint8 x,uint
28、8 y,uint8 d) LCD_set_xy(x,y); LCD_write_data(d); /*/ / 等待繁忙标志 / void LCD_wait(void) P0 = 0xFF; do LCD_RS = 0; LCD_RW = 1; LCD_EN = 0; LCD_EN = 1; while (BUSY = 1); LCD_EN = 0; */ /* * * LCD1602 左移 * * * void LCD_youyi(uint8 y,uint8 *s) LCD_write_str(17,y,s); for(a=0;a16;a+) LCD_write_command(0x1c);
29、/左移 LCD_write_command(0x1c); 为右移 Delay1Ms(6); */ /LCD_write_command(0x0d);/光标闪烁 /LCD_write_command(0x0e);/光标显示不闪烁 / / Lcd1602.h 头文件 / / #ifndef _LCD16024_H_ #define _LCD16024_H_ typedef unsigned char uint8; typedef unsigned int uint16; typedef unsigned long uint32; void Delay1Us(uint16 n); void Delay1Ms(uint16 n); void LCD_write_da