您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 基于51单片机的实用闹钟程序
#includereg52.h#includekey.h#includeLCD1602.h#includeDS1302.h#includemusic.h#includeDS18B20.H#definenormal0//正常显示时钟界面#definesettime1//设定时钟界面#definesetalarm2//设定闹钟界面#definedisplayalarm3//显示闹钟界面#definenormal_124//显示十二小时制界面#definecaidan5unsignedcharsystem=normal;//我一开始就把界面切换为正常显示时钟界面externunsignedchari,k;externunsignedintcodesong[3][300];unsignedcharcode*week[8]={NO,Mon,Tue,Wed,Thu,Fri,Sat,Sun};unsignedcharcode*clockzifu[4]={ON,OFF,REP,NRE};structTimetimeset={0x18,0x01,0x16,0x14,0x30,0x50,0x03};//第一个闹钟//第二个闹钟//第三个闹钟//第四个闹钟//第五个闹钟structalarmtimexdataclock[5]={{1,16,14,31,0,1,0,1},{1,8,14,32,3,0,0,0},{1,7,18,49,1,1,1,0},{0,0,0,0,0,0,1,0},{1,25,0,0,0,0,1,2}};unsignedcharalarmnum;//闹钟unsignedchara;unsignedcharflag=0;unsignedcharxinghao=0;unsignedchartentoBCD(unsignedchardat)//十进制转换为BCD码函数{unsignedchardat1,dat2;dat1=dat/10;dat2=dat%10;dat2=dat2+dat1*16;returndat2;}unsignedcharBCDtoten(unsignedchardat)//BCD码转为十进制函数{unsignedchardat1,dat2;dat1=dat/16;dat2=dat%16;dat2=dat2+dat1*10;returndat2;}voidDS18B20deal(inttemp)//DS18B20数据处理函数显示温度更新温度{//先判断温度值是否大于0或小于0等于0floattp;//保存的数据可能带小数if(temp0)//当温度值为负数{LCD1602_writechar(9,1,0x2D);//ASCII码中的-符号temp=temp-1;//因为读取的温度是实际温度的补码,所以减1,再取反求出原码temp=~temp;//还原读取的数据tp=temp;//保存到变量里面temp=tp*0.0625*100+0.5;//强制转换成一个整形的数据.留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就算加上0.5,还是在小数点后面。}else//当温度值为正数{LCD1602_writechar(9,1,0x20);//ASCII码中的空格tp=temp;//因为数据处理有小数点所以将温度赋给一个浮点型变量。如果温度是正的那么,那么正数的原码就是补码它本身temp=tp*0.0625*100+0.5;//留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点。后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就算加上0.5,还是在小数点后面。}LCD1602_writechar(10,1,temp/10000+'0');//温度百位LCD1602_writechar(11,1,temp%10000/1000+'0');//温度十位LCD1602_writechar(12,1,temp%1000/100+'0');//温度个位LCD1602_writechar(13,1,0x2E);//ASCII码小数点LCD1602_writechar(14,1,temp%100/10+'0');//小数点后一位LCD1602_writechar(15,1,temp%10+'0');//小数点后两位}voidupdatetime()//更新时钟上的时间{unsignedchartime[9];//存储变量DS1302_gettime(×et);//读取时间time[0]=BCDtoten(timeset.hour)/10+'0';//小时的十位time[1]=BCDtoten(timeset.hour)%10+'0';//小时的个位time[2]=':';time[3]=BCDtoten(timeset.min)/10+'0';//分的十位time[4]=BCDtoten(timeset.min)%10+'0';//分的个位time[5]=':';time[6]=BCDtoten(timeset.sec)/10+'0';//秒的十位time[7]=BCDtoten(timeset.sec)%10+'0';//秒的个位time[8]='\0';//字符串结束标志LCD1602_writestr(0,1,time);//在LCD上面打印时间}voidupdatetime_12()//更新十二小时制界面的时间{unsignedchartime[9];DS1302_gettime(×et);//读取时间time[0]=BCDtoten(timeset.hour)%12/10+'0';//小时的十位time[1]=BCDtoten(timeset.hour)%12%10+'0';//小时的个位time[2]=':';time[3]=BCDtoten(timeset.min)/10+'0';//分的十位time[4]=BCDtoten(timeset.min)%10+'0';//分的个位time[5]=':';time[6]=BCDtoten(timeset.sec)/10+'0';//秒的十位time[7]=BCDtoten(timeset.sec)%10+'0';//秒的个位time[8]='\0';//字符串结束标志LCD1602_writestr(3,1,TIME:);//打印出TIME的字符串LCD1602_writestr(8,1,time);//在第二行第四列显示时间if(BCDtoten(timeset.hour)12)//要是小时大于12时{LCD1602_writestr(0,1,PM);//则显示PM}else//小时不大于12{LCD1602_writestr(0,1,AM);//则显示AM}}voidupdatedate()//更新日历{unsignedchardate[12];date[0]='2';date[1]='0';date[2]=BCDtoten(timeset.year)/10%10+'0';//年的十位date[3]=BCDtoten(timeset.year)%10+'0';//年的个位date[4]='.';date[5]=BCDtoten(timeset.mon)/10+'0';//月的十位date[6]=BCDtoten(timeset.mon)%10+'0';//月的个位date[7]='.';date[8]=BCDtoten(timeset.day)/10+'0';//日的十位date[9]=BCDtoten(timeset.day)%10+'0';//日的个位date[10]='\0';LCD1602_writestr(0,0,date);//显示年月日LCD1602_writestr(11,0,week[timeset.week]);//显示星期}voidupdatealarm()//更新闹钟设定值{unsignedcharstr0[14];unsignedcharstr1[2];str0[0]=alarmnum+'0';str0[1]='';str0[2]=clock[alarmnum].mon/10+'0';//月份十位str0[3]=clock[alarmnum].mon%10+'0';//月份个位str0[4]='-';str0[5]=clock[alarmnum].day/10+'0';//日期十位str0[6]=clock[alarmnum].day%10+'0';//日期个位str0[7]='';str0[8]=clock[alarmnum].hour/10+'0';//小时十位str0[9]=clock[alarmnum].hour%10+'0';//小时个位str0[10]=':';str0[11]=clock[alarmnum].min/10+'0';//分钟十位str0[12]=clock[alarmnum].min%10+'0';//分钟个位str0[13]='\0';LCD1602_writestr(0,1,str0);str1[0]=clock[alarmnum].music+'0';//铃声str1[1]='\0';LCD1602_writestr(0,0,U);LCD1602_writestr(1,0,str1);//显示铃声LCD1602_writestr(3,0,clockzifu[clock[alarmnum].alarmre+2]);//显示重不重复LCD1602_writestr(7,0,week[clock[alarmnum].week]);//显示星期LCD1602_writestr(11,0,clockzifu[clock[alarmnum].alarmon]);//显示开关}voidanjian4();voidanjian1(){if(system==normal){system=caidan;xinghao=0;LCD1602_clear();LCD1602_writechar(0,0,'*');LCD1602_writestr(1,0,12);LCD1602_writestr(1,1,alarm);LCD1602_writestr(8,0,setclock);LCD1602_writestr(8,1,setalarm);}elseif(system==caidan&&xinghao==0){system=normal_12;//切换到12小时制LCD1602_clear();//清屏updatetime_12();//显示12小时制的时间updatedate();//显示日期}elseif(system==caidan&&xinghao==1){system=displayalarm;//切换到闹钟显示LCD1602_clear();//清屏updatealarm();//显示闹钟界面}elseif(system==caidan&&xinghao==2){system=settime;//进入设置时间界面LCD1602_clear();updatetime();//刷新时间updatedate();//刷新日期DS18B20deal(DS18B20readtemp());//DS18B20数据处理函数同时输出温度flag=0;//光标索引设置到秒钟上anjian4();//显示光标位置LCD1602_openguangbiao();//LCD1602打开光标显示}elseif(system==caidan&&xinghao==3){system=setalarm;//进入设置闹钟LCD1602_clear();//清屏updatealarm();//显示闹钟界面flag=0;//光标索
本文标题:基于51单片机的实用闹钟程序
链接地址:https://www.777doc.com/doc-6836376 .html