收藏 分销(赏)

电子时钟.doc

上传人:pc****0 文档编号:5882023 上传时间:2024-11-22 格式:DOC 页数:23 大小:796.50KB
下载 相关 举报
电子时钟.doc_第1页
第1页 / 共23页
电子时钟.doc_第2页
第2页 / 共23页
点击查看更多>>
资源描述
我的单片机课程设计:AT89S52+DS1302+DS18B20多功能数字电子钟 一 单片机 2010-07-16 17:35:55 阅读145 评论0 字号:大中小 实现功能: 1.显示年、月、日、时、分、秒、星期,闰年自动调整 2.可手动调整上述各项的值 3.实时显示当前温度 4. 可设定两个闹铃,每个有独立开关,并可以设置只响闹一次还是每天都响闹 5. 闹钟共有两首音乐可供选择 6. 可闹铃音乐放完可自动止闹,也可中途按键手动止闹 7. 贪睡功能,有开关,并可设置贪睡时间 8. 背光控制,有开关,并可设置多少秒后自动关闭背光,也可以按键开背光,然后按照设置的时间后自动关闭 9. 秒表功能 10. 掉电模式,同时液晶关背光,节省能源 源代码第一部分: #include<at89x52.h> //2010.4.6-2010.4.13 Designed By Lu. QQ:475600597 2010.6.14再次修改 #include<intrins.h> typedef unsigned char uchar; typedef unsigned int uint; #define LCD1602_DATA P0 //1602液晶数据口 sbit LCD1602_RS=P1^0; sbit LCD1602_RW=P1^1; sbit LCD1602_EN=P1^2; //1602液晶使能控制 sbit DS1302_CLK = P3^0; //实时时钟时钟线引脚 sbit DS1302_IO = P3^1; //实时时钟数据线引脚 sbit DS1302_RST = P3^2; //实时时钟复位线引脚 sbit DQ = P2^3 ; //18B20接口 sbit BeepIO = P2^7 ; //蜂鸣器 sbit esc=P1^7; //取消播放音乐 sbit mode = P1^4 ; //模式键 sbit inc = P1^5 ; //增加键 sbit dec = P1^6 ; //减少键 sbit ok = P1^7 ; //OK键 sbit light=P2^0; //背光控制 sbit ACC0 = ACC^0; //累加器位定义 sbit ACC7 = ACC^7; bit first_flag=1, //一级主界面显示标志 second_flag, //二级菜单显示标志 third_flag, //三级菜单显示标志 playmusic, //播放音乐的标志 light_flag1=1, //背光第一次计时标志 light_flag=1 ; // 二三级界面开背光标志 uchar count1,sec1,min1,hour1;//定时器2使用的变量 停表用 uchar count,sec,min;//定时器1使用的变量 贪睡用 uchar temp; //温度暂存 //////以下为播放音乐的设置 #define SYSTEM_OSC 12000000 #define SOUND_SPACE 4/5 uint code FreTab[12] = { 262,277,294,311,330,349,369,392,415,440,466,494 }; //原始频率表 uchar code SignTab[7] = { 0,2,4,5,7,9,11 }; //1~7在频率表中的位置 uchar code LengthTab[7]= { 1,2,4,8,16,32,64 }; uchar Sound_Temp_TH0,Sound_Temp_TL0; //音符定时器初值暂存 uchar Sound_Temp_TH1,Sound_Temp_TL1; //音长定时器初值暂存 bit stop; char menu_num;//菜单号 char num;//按键加减调整暂存数 char position1,position2,position3,position4,position5,position6,position7; //菜单1-7的光标位置标志 char code menu[][16]={"1.Time Setting ","2.Alarm Setting","3.Alarm Music ","4.Snooze ", "5.Back Light ","6.Stop Watch ","7.LowPower Mode","8.Exit "}; char code on_off_flag[][4]={"OFF","ON "}; char code alarm_times[][7]={"Once ","Repeat"}; char code week[][4]={"MON","TUE","WED","THU","FRI","SAT","SUN"}; uchar code self_table[]={0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,//年 0 0x1f,0x11,0x1f,0x11,0x1f,0x11,0x15,0x17,//月 1 0x1f,0x11,0x11,0x1f,0x11,0x11,0x1f,0x00,//日 2 0x01,0x0c,0x17,0x14,0x17,0x0c,0x01,0x00,//闹钟标志 3 0x10,0x18,0x1c,0x1e,0x1f,0x1c,0x18,0x10,//三角形符号,第五个自定义字符 4 0x1f,0x15,0x0a,0x04,0x0a,0x15,0x1f,0x00}; //贪睡标志 5 /***********************延时函数(单位ms)**************************/ void delay(uint z) { uint x; uchar y; for(x=z;x>0;x--) for(y=112;y>0;y--); } /*********************************************************** ******************LCD1602驱动程序****************** ************************************************************/ /*********************************************************** 忙检测 ************************************************************/ void LCD1602_check_busy() { LCD1602_DATA = 0xff; LCD1602_RS = 0 ; LCD1602_RW = 1 ; LCD1602_EN = 1 ; while(LCD1602_DATA & 0x80) ; LCD1602_EN = 0 ; } /********************************************************** 写指令 ************************************************************/ void LCD1602_write_cmd(uchar cmd) { LCD1602_check_busy(); LCD1602_RS = 0 ; LCD1602_RW = 0 ; LCD1602_DATA = cmd ; LCD1602_EN = 1 ; _nop_(); LCD1602_EN = 0 ; } /*********************************************************** 写数据 *************************************************************/ void LCD1602_write_data(uchar dat) { LCD1602_check_busy(); LCD1602_RS = 1 ; LCD1602_RW = 0 ; LCD1602_DATA = dat ; LCD1602_EN = 1 ; _nop_(); LCD1602_EN = 0 ; } /*********************************************************** 1602初始化 ************************************************************/ void LCD1602_init() { /****************写入自定义字符***************************/ uchar i ; LCD1602_write_cmd(0x40);//CGRAM起始地址 for(i=0;i<48;i++) LCD1602_write_data(self_table[i]);//写入6个自定义字符 /*********************************************************/ LCD1602_write_cmd(0x38); //8位数据,双列显示,5*7字形 LCD1602_write_cmd(0x0c); //开显示,关光标,光标不闪烁 LCD1602_write_cmd(0x01); //清屏 } /************************************************************ 设置显示坐标 ************************************************************/ void LCD1602_set_postion(uchar x , uchar y) { if(!y) LCD1602_write_cmd(0x80+x) ; else if(y) LCD1602_write_cmd(0xc0+x) ; } /************************************************************ 指定位置写字符 *************************************************************/ void LCD1602_write_char(uchar x , uchar y , uchar chardata) { LCD1602_set_postion(x,y) ; LCD1602_write_data(chardata) ; } /************************************************************ 指定位置写字符串 *************************************************************/ void LCD1602_write_string(uchar x , uchar y , uchar *string) { LCD1602_set_postion(x,y) ; while((*string) != '\0') { LCD1602_write_data(*string) ; string++ ; } } /*********************************************************** ******************DS1302驱动程序****************** ************************************************************/ void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数) { unsigned char i; ACC = d; for(i=8; i>0; i--) { DS1302_IO = ACC0; //相当于汇编中的 RRC DS1302_CLK = 1; DS1302_CLK = 0; ACC = ACC >> 1; } } unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数) { unsigned char i; for(i=8; i>0; i--) { ACC = ACC >>1; //相当于汇编中的 RRC ACC7 = DS1302_IO; DS1302_CLK = 1; DS1302_CLK = 0; } return(ACC); } void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据 { DS1302_RST = 0; DS1302_CLK = 0; DS1302_RST = 1; DS1302InputByte(ucAddr); // 地址,命令 DS1302InputByte(ucDa); // 写1Byte数据 DS1302_CLK = 1; DS1302_RST = 0; } unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据 { unsigned char ucData; DS1302_RST = 0; DS1302_CLK = 0; DS1302_RST = 1; DS1302InputByte(ucAddr|0x01); // 地址,命令 ucData = DS1302OutputByte(); // 读1Byte数据 DS1302_CLK = 1; DS1302_RST = 0; return(ucData); } void DS1302_init() { if(Read1302(0xc1) != 0xf0) { Write1302(0x8e,0x00) ;//允许写操作 Write1302(0xc0,0xf0) ; //写入初始化标志 ,系统上电后检测此标志,即此子函数只会在第一次初始化一次。 /******时间初始值******/ Write1302(0x8c,0x10) ;//年 Write1302(0x8a,0x03) ;//星期 Write1302(0x88,0x05) ;//月 Write1302(0x86,0x26) ;//日 Write1302(0x84,0x22) ;//时 Write1302(0x82,0x59) ;//分 Write1302(0x80,0x55) ;//秒 /////////////////// Write1302(0xd2,0); //歌曲选择初始 Write1302(0xd4,5); // 贪睡初始延时时间 5分钟 Write1302(0xd6,0); //贪睡初始值 关闭 /******闹钟初始值******/ Write1302(0xc2,0) ;//十时 Write1302(0xc4,0) ;//时 Write1302(0xc6,0) ;//闹钟标志 Write1302(0xc8,0) ;//闹钟次数标志 ////第二个闹钟 Write1302(0xca,0) ;//十时 Write1302(0xcc,0) ;//时 Write1302(0xce,0) ;//闹钟标志 Write1302(0xd0,0) ;//闹钟次数标志 //////////// Write1302(0xd8,0) ;// 背光控制开关 Write1302(0xda,0x10) ;// 背光延时时间 Write1302(0x90,0xa4) ;//涓流充电 ////////////////// Write1302(0x8e,0x80) ;//禁止写操作 } } /*********************************************************** ******************DS18B20驱动程序****************** ************************************************************/ void delay_18b20(unsigned int i)//延时函数 { while(i--); } /***************************************************************************************/ //18b20初始化函数 void Init_DS18B20(void) { unsigned char x=0; DQ = 1; //DQ复位 delay_18b20(8); //稍做延时 DQ = 0; //单片机将DQ拉低 delay_18b20(80); //精确延时 大于 480us DQ = 1; //拉高总线 delay_18b20(10); x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败 delay_18b20(5); } //读一个字节 unsigned char ReadOneChar(void) { unsigned char i=0; unsigned char dat = 0; for (i=8;i>0;i--) { DQ = 0; // 给脉冲信号 dat>>=1; DQ = 1; // 给脉冲信号 if(DQ) dat|=0x80; delay_18b20(5); } return(dat); } //写一个字节 void WriteOneChar(unsigned char dat) { unsigned char i=0; for (i=8; i>0; i--) { DQ = 0; DQ = dat&0x01; delay_18b20(5); DQ = 1; dat>>=1; } delay_18b20(5); } //读取温度 unsigned char ReadTemperature(void) { unsigned char a=0; unsigned char b=0; unsigned char t=0; Init_DS18B20(); WriteOneChar(0xCC); // 跳过读序号列号的操作 WriteOneChar(0x44); // 启动温度转换 delay_18b20(200); Init_DS18B20(); WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度 a=ReadOneChar(); b=ReadOneChar(); b<<=4; b+=(a&0xf0)>>4; t=b; return(t); } /******************************************************************** ************一级主界面显示函数*************** ********************************************************************/ void first_interface() { LCD1602_write_string(0,0,"20"); LCD1602_write_data(Read1302(0x8d)/16+0x30); // 十年 LCD1602_write_data(Read1302(0x8d)%16+0x30); // 年 LCD1602_write_data(0); //显示自定义字符“年” LCD1602_write_data(Read1302(0x89)/16+0x30); // 十月 LCD1602_write_data(Read1302(0x89)%16+0x30); // 月 LCD1602_write_data(1); //显示自定义字符“月” LCD1602_write_data(Read1302(0x87)/16+0x30); // 十日 LCD1602_write_data(Read1302(0x87)%16+0x30); // 日 LCD1602_write_data(2); //显示自定义字符“日” if(Read1302(0xd7)) LCD1602_write_char(9,1,5); //显示自定义字符贪睡标志 else LCD1602_write_char(9,1,' '); LCD1602_write_string(12,0,week[Read1302(0x8b)-1]); //显示星期 if(Read1302(0xc7)||Read1302(0xcf)) LCD1602_write_char(10,1,3); //显示自定义字符闹钟标志 else LCD1602_write_char(10,1,' '); LCD1602_write_char(0,1,Read1302(0x85)/16+0x30); // 十时 LCD1602_write_data(Read1302(0x85)%16+0x30); // 时 LCD1602_write_data(':'); LCD1602_write_char(3,1,Read1302(0x83)/16+0x30); // 十分 LCD1602_write_data(Read1302(0x83)%16+0x30); // 分 LCD1602_write_data(':'); LCD1602_write_char(6,1,Read1302(0x81)/16+0x30); //十秒 LCD1602_write_data(Read1302(0x81)%16+0x30); //秒 temp=ReadTemperature(); LCD1602_write_char(12,1,temp/10+0x30); //温度显示 LCD1602_write_data(temp%10+0x30); LCD1602_write_data(0xdf); LCD1602_write_data('C'); } /*********************************************************** ******************定时器2初始化函数******************停表用 ************************************************************/ void init_timer2() { T2CON=0; T2MOD=0; RCAP2H=(65536-10000)/256; //10ms RCAP2L=(65536-10000)%256; TH2=(65536-10000)/256; //10ms TL2=(65536-10000)%256; EA = 1; ET2 = 1; TR2 = 1; } /*********************************************************** ******************定时器1初始化函数****************** 贪睡用 ************************************************************/ void init_timer1() { TMOD=0x10; TH1=(65536-10000)/256; //10ms TL1=(65536-10000)%256; EA = 1; ET1 = 1; TR1 = 1; } /******************************************************************** ************二级菜单显示函数************ ********************************************************************/ void second_interface() { LCD1602_write_char(0,(menu_num*3)%2,4); //三角形指示符号 LCD1602_write_char(0,!((menu_num*3)%2),' '); //清除残留显示三角形 if(!((menu_num*3)%2)) //判断是否要往液晶写入显示,menu_num每跳变2时写一次 { LCD1602_write_string(1,0,menu[menu_num]); // 1602液晶第一行显示 LCD1602_write_string(1,1,menu[menu_num+1]); // 1602液晶第二行显示 } else if((menu_num*3)%2) //防止从三级界面回来时二级界面不显示了 { LCD1602_write_string(1,0,menu[menu_num-1]); // 1602液晶第一行显示 LCD1602_write_string(1,1,menu[menu_num]); // 1602液晶第二行显示 } } /******************************************************************** ************三级菜单显示函数************ ********************************************************************/ void third_interface() { switch(menu_num) { case 0 : { if(!position1) //时间设置界面,仅进入三级菜单界面时刻显示一下 { LCD1602_write_string(0,0,"20"); LCD1602_write_data(Read1302(0x8d)/16+0x30); // 十年 LCD1602_write_data(Read1302(0x8d)%16+0x30); // 年 LCD1602_write_data(0); //显示自定义字符“年” LCD1602_write_data(Read1302(0x89)/16+0x30); // 十月 LCD1602_write_data(Read1302(0x89)%16+0x30); // 月 LCD1602_write_data(1); //显示自定义字符“月” LCD1602_write_data(Read1302(0x87)/16+0x30); // 十日 LCD1602_write_data(Read1302(0x87)%16+0x30); // 日 LCD1602_write_data(2); //显示自定义字符“日” LCD1602_write_string(12,0,week[Read1302(0x8b)-1]); //显示星期 LCD1602_write_char(0,1,Read1302(0x85)/16+0x30); // 十时 LCD1602_write_data(Read1302(0x85)%16+0x30); // 时 LCD1602_write_data(':'); LCD1602_write_char(3,1,Read1302(0x83)/16+0x30); // 十分 LCD1602_write_data(Read1302(0x83)%16+0x30); // 分 LCD1602_write_data(':'); LCD1602_write_char(6,1,Read1302(0x81)/16+0x30); //十秒 LCD1602_write_data(Read1302(0x81)%16+0x30); //秒 LCD1602_write_cmd(0x80+3);//第一次进入三级菜单时将光标放的位置 position1=1; } }break; case 1 : { if(!position2) //闹钟设置界面 { LCD1602_write_char(0,0,Read1302(0xc3)/16+0x30); // 十时 LCD1602_write_data(Read1302(0xc3)%16+0x30); // 时 LCD1602_write_data(':'); LCD1602_write_char(3,0,Read1302(0xc5)/16+0x30); // 十分 LCD1602_write_data(Read1302(0xc5)%16+0x30); // 分 LCD1602_write_string(6,0,on_off_flag[Read1302(0xc7)]); // 闹钟标志 LCD1602_write_string(10,0,alarm_times[Read1302(0xc9)]); // 闹钟次数标志 ////////第二个闹钟 LCD1602_write_char(0,1,Read1302(0xcb)/16+0x30); // 十时 LCD1602_write_data(Read1302(0xcb)%16+0x30); // 时 LCD1602_write_data(':'); LCD1602_write_char(3,1,Read1302(0xcd)/16+0x30); // 十分 LCD1602_write_data(Read1302(0xcd)%16+0x30); // 分 LCD1602_write_string(6,1,on_off_flag[Read1302(0xcf)]); // 闹钟标志 LCD1602_write_string(10,1,alarm_times[Read1302(0xd1)]); // 闹钟次数标志 LCD1602_write_cmd(0x80+1);//第一次进入三级菜单时将光标放的位置 position2=1; } }break; case 2 : { //闹铃音乐选择 if(!position3) { LCD1602_write_string(1,0,"Alarm Music Is");
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 行业资料 > 医学/心理学

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服