|
电子万年历是一种非常广泛日常计时工具,给人们的带来了很大的方便,在社会上越来越流行。 它可以对年、月、日、时、分、秒进行计时,采用直观的数字显示,可以同时显示年月日时分秒和温度等信息,还有时间校准、闹钟等功能。该电子万年历主要采用STC89C52单片机作为主控核心,用DS1302时钟芯片作为时钟、液晶12864显示屏显示。STC89C52单片机是由宏晶公司推出的,功耗小,电压可选用4~6V电压供电;DS1302时钟芯片是美国DALLAS公司推出的具有细电流充电功能的低功耗实时时钟芯片,它可以对年、月、日、星期、时、分、秒进行计时,还具有闰年补偿等多种功能,而且DS1302的使用寿命长,误差小;数字显示是采用的12864液晶显示屏来显示,可以同时显示年、月、日、星期、时、分、秒和温度等信息。此外,该电子万年历还具有时间校准等功能。
1. 系统总体功能
本设计采用的是宏晶公司的STC89C52单片机为核心,通过单片机与时钟芯片DS1302通信来对时间的读写,保证时钟时间的稳定性,并带有内部电源模块,使系统断电时时钟数据不会丢失,以DS18B20温度感应芯片作为温度电路的核心,向单片机发出获取到的温度数据,并通过LCD1602将时间、显示出来,并可以通过按键调整时间日期和闹钟时间
2. 总体电路图
3. 程序设计
(1) LCD1602驱动程序
- sbit rs = P2^0;
- sbit rw = P2^1;
- sbit e = P2^2;
- #define lcddata P0
- sbit busy=P0^7; //lcd busy bit
- void wr_d_lcd(uchar content);
- void wr_i_lcd(uchar content);
- void clrram_lcd (void);
- void init_lcd(void);
- void busy_lcd(void);
- void rev_row_lcd(uchar row);
- void rev_co_lcd(uchar row,uchar col,uchar mode);
- void clr_lcd(void);
- void wr_co_lcd(uchar row,uchar col,uchar lcddata1,uchar lcddtta2);
- void wr_row_lcd(uchar row,char *p);
- //**********************************
- //液晶初始化
- //**********************************
- void init_lcd(void)
- {
- wr_i_lcd(0x06); /*光标的移动方向*/
- wr_i_lcd(0x0c); /*开显示,关游标*/
- }
- //***********************************
- //填充液晶DDRAM全为空格
- //**********************************
- void clrram_lcd (void)
- {
- wr_i_lcd(0x30);
- wr_i_lcd(0x01);
- }
- //***********************************
- //对液晶写数据
- //content为要写入的数据
- //***********************************
- void wr_d_lcd(uchar content)
- {
- busy_lcd();
- rs=1;
- rw=0;
- lcddata=content;
- e=1;
- ;
- e=0;
- }
- //********************************
- //对液晶写指令
- //content为要写入的指令代码
- //*****************************
- void wr_i_lcd(uchar content)
- {
- busy_lcd();
- rs=0;
- rw=0;
- lcddata=content;
- e=1;
- ;
- e=0;
- }
- //********************************
- //液晶检测忙状态
- //在写入之前必须执行
- //********************************
- void busy_lcd(void)
- {
- lcddata=0xff;
- rs=0;
- rw=1;
- e =1;
- while(busy==1);
- e =0;
- }
- //********************************
- //指定要显示字符的坐标
- //*******************************
- void gotoxy(unsigned char y, unsigned char x)
- {
- if(y==1)
- wr_i_lcd(0x80|x);
- if(y==2)
- wr_i_lcd(0x90|x);
- if(y==3)
- wr_i_lcd((0x80|x)+8);
- if(y==4)
- wr_i_lcd((0x90|x)+8);
- }
- //**********************************
- //液晶显示字符串程序
- //**********************************
- void print(uchar *str)
- {
- while(*str!='\0')
- {
- wr_d_lcd(*str);
- str++;
- }
- }
- //***************************************
- //液晶显示主程序模块
- //***************************************
- void show_time()
- {
- DS1302_GetTime(&CurrentTime); //获取时钟芯片的时间数据
- TimeToStr(&CurrentTime); //时间数据转换液晶字符
- DateToStr(&CurrentTime); //日期数据转换液晶字符
- ReadTemp(); //开启温度采集程序
- temp_to_str(); //温度数据转换成液晶字符
- gotoxy(4,0);
- print("温度");
- gotoxy(4,2); //液晶字符显示位置
- print(TempBuffer); //显示温度
- gotoxy(4,6);
- print("℃");
- gotoxy(3,0);
- print("时间:");
- gotoxy(3,3);
- print(CurrentTime.TimeString); //显示时间
- gotoxy(2,3);
- print(CurrentTime.DateString); //显示日期
- gotoxy(2,0);
- print("星期");
- gotoxy(2,2);
- print(week_value); //显示星期
- gotoxy(1,1);
- print("【万年历】");
- mdelay(500); //扫描延时
- }
复制代码 (2) DS1302驱动程序
- //***********************************
- //DS1302时钟部分子程序模块
- //***********************************
- typedef struct __SYSTEMTIME__
- {
- uchar Second;
- uchar Minute;
- uchar Hour;
- uchar Week;
- uchar Day;
- uchar Month;
- uchar Year;
- uchar DateString[11];
- uchar TimeString[9];
- }SYSTEMTIME; //定义的时间类型
- SYSTEMTIME CurrentTime;
- #define AM(X) X
- #define PM(X) (X+12) // 转成24小时制
- #define DS1302_SECOND 0x80 //时钟芯片的寄存器位置,存放时间
- #define DS1302_MINUTE 0x82
- #define DS1302_HOUR 0x84
- #define DS1302_WEEK 0x8A
- #define DS1302_DAY 0x86
- #define DS1302_MONTH 0x88
- #define DS1302_YEAR 0x8C
- //**********************************
- //实时时钟写入一字节(内部函数)
- //**********************************
- void DS1302InputByte(uchar d)
- {
- uchar i;
- ACC = d;
- for(i=8; i>0; i--)
- {
- DS1302_IO = ACC0; //相当于汇编中的 RRC
- DS1302_CLK = 1;
- DS1302_CLK = 0;
- ACC = ACC >> 1;
- }
- }
- //*************************************
- //实时时钟读取一字节(内部函数)
- //*************************************
- uchar DS1302OutputByte(void)
- {
- uchar i;
- for(i=8; i>0; i--)
- {
- ACC = ACC >>1; //相当于汇编中的 RRC
- ACC7 = DS1302_IO;
- DS1302_CLK = 1;
- DS1302_CLK = 0;
- }
- return(ACC);
- }
- //**************************************
- //ucAddr: DS1302地址, ucData: 要写的数据
- //**************************************
- void Write1302(uchar ucAddr, uchar ucDa)
- {
- DS1302_RST = 0;
- DS1302_CLK = 0;
- DS1302_RST = 1;
- DS1302InputByte(ucAddr); // 地址,命令
- DS1302InputByte(ucDa); // 写1Byte数据
- DS1302_CLK = 1;
- DS1302_RST = 0;
- }
- //**************************************
- //读取DS1302某地址的数据
- //**************************************
- uchar Read1302(uchar ucAddr)
- {
- uchar 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_GetTime(SYSTEMTIME *Time)
- {
- uchar ReadValue;
- ReadValue = Read1302(DS1302_SECOND);
- Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);//转换为相应的10进制数
- ReadValue = Read1302(DS1302_MINUTE);
- Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- ReadValue = Read1302(DS1302_HOUR);
- Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- ReadValue = Read1302(DS1302_DAY);
- Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- ReadValue = Read1302(DS1302_WEEK);
- Time->Week = ((ReadValue&0x10)>>4)*10 + (ReadValue&0x0F);
- ReadValue = Read1302(DS1302_MONTH);
- Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
- ReadValue = Read1302(DS1302_YEAR);
- Time->Year = ((ReadValue&0xf0)>>4)*10 + (ReadValue&0x0F);
- }
- //******************************************
- //将时间年,月,日,星期数据转换成液
- //晶显示字符串,放到数组里DateString[]
- //******************************************
- void DateToStr(SYSTEMTIME *Time)
- {
- uchar tab[ ]={0XD2,0XBB,0XB6,0XFE,0XC8,0XFD,0XCB,0XC4,0XCE,0XE5,0XC1,0XF9,0XC8,0XD5};
- if(hide_year<2) //这里的if,else语句都是判断位闪烁,<2显示数据,>2就不显示,输出字符串为 2007/07/22
- {
- Time->DateString[0] = '2';
- Time->DateString[1] = '0';
- Time->DateString[2] = Time->Year/10 + '0';
- Time->DateString[3] = Time->Year%10 + '0';
- }
- else
- {
- Time->DateString[0] = ' ';
- Time->DateString[1] = ' ';
- Time->DateString[2] = ' ';
- Time->DateString[3] = ' ';
- }
- Time->DateString[4]='-';
- if(hide_month<2)
- {
- Time->DateString[5] = Time->Month/10 + '0';
- Time->DateString[6] = Time->Month%10 + '0';
- }
- else
- {
- Time->DateString[5] = ' ';
- Time->DateString[6] = ' ';
- }
- Time->DateString[7]='-';
- if(hide_day<2)
- {
- Time->DateString[8] = Time->Day/10 + '0';
- Time->DateString[9] = Time->Day%10 + '0';
- }
- else
- {
- Time->DateString[8] = ' ';
- Time->DateString[9] = ' ';
- }
- if(hide_week<2)
- {
- week_value[0] =tab[2*(Time->Week%10)-2]; //星期的数据另外放到 week_value[]数组里,跟年,月,日的分开存放,因为等一下要在最后显示
- week_value[1] =tab[2*(Time->Week%10)-1];
- }
- else
- {
- week_value[0] = ' ';
- week_value[1]=' ';
- }
- week_value[2] = '\0';
- Time->DateString[10] = '\0'; //字符串末尾加 '\0' ,判断结束字符
- }
- //******************************************
- //将时,分,秒数据转换成液晶
- //显示字符放到数组 TimeString[]
- //*****************************************
- void TimeToStr(SYSTEMTIME *Time)
- { if(hide_hour<2)
- {
- Time->TimeString[0] = Time->Hour/10 + '0';
- Time->TimeString[1] = Time->Hour%10 + '0';
- }
- else
- {
- Time->TimeString[0] = ' ';
- Time->TimeString[1] = ' ';
- }
- Time->TimeString[2] = ':';
- if(hide_min<2)
- {
- Time->TimeString[3] = Time->Minute/10 + '0';
- Time->TimeString[4] = Time->Minute%10 + '0';
- }
- else
- {
- Time->TimeString[3] = ' ';
- Time->TimeString[4] = ' ';
- }
- Time->TimeString[5] = ':';
- if(hide_sec<2)
- {
- Time->TimeString[6] = Time->Second/10 + '0';
- Time->TimeString[7] = Time->Second%10 + '0';
- }
- else
- {
- Time->TimeString[6] = ' ';
- Time->TimeString[7] = ' ';
- }
- Time->TimeString[8] = '\0';
- }
- //******************************
- //时钟芯片初始化
- //******************************
- void Initial_DS1302(void)
- {
- uchar Second=Read1302(DS1302_SECOND);
- if(Second&0x80) //判断时钟芯片是否关闭
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x8c,0x07); //以下写入初始化时间 日期:07/07/25.星期: 3. 时间: 23:59:55
- Write1302(0x88,0x07);
- Write1302(0x86,0x25);
- Write1302(0x8a,0x07);
- Write1302(0x84,0x23);
- Write1302(0x82,0x59);
- Write1302(0x80,0x55);
- Write1302(0x8e,0x80); //禁止写入
- }
- }
复制代码 (3)DS18B20驱动程序
(4) 按键驱动程序
- //************************************
- //跳出调整模式,返回默认显示
- //************************************
- void outkey()
- { uchar Second;
- if(out==0)
- { mdelay(5);
- count=0;
- hide_sec=0,hide_min=0,hide_hour=0,hide_day=0,hide_week=0,hide_month=0,hide_year=0;
- Second=Read1302(DS1302_SECOND);
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x80,Second&0x7f);
- Write1302(0x8E,0x80); //禁止写入
- done=0;
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //*************************
- //升序按键
- //*************************
- void Upkey()
- {
- Up=1;
- if(Up==0)
- {
- mdelay(5);
- switch(count)
- {case 1:
- temp=Read1302(DS1302_SECOND); //读取秒数
- temp=((temp&0x70)>>4)*10 + (temp&0x0F);
- temp=temp+1; //秒数加1
- up_flag=1; //数据调整后更新标志
- if((temp)>59) //超过59秒,清零
- temp=0;
- temp=temp/10*16+temp%10;
- break;
- case 2:
- temp=Read1302(DS1302_MINUTE); //读取分数
- temp=((temp&0x70)>>4)*10 + (temp&0x0F);
- temp=temp+1; //分数加1
- up_flag=1;
- if(temp>59) //超过59分,清零
- temp=0;
- temp=temp/10*16+temp%10;
- break;
- case 3:
- temp=Read1302(DS1302_HOUR); //读取小时数
- temp=((temp&0x70)>>4)*10 + (temp&0x0F);
- temp=temp+1; //小时数加1
- up_flag=1;
- if(temp>23) //超过23小时,清零
- temp=0;
- temp=temp/10*16+temp%10;
- break;
- case 4:
- temp=Read1302(DS1302_WEEK); //读取星期数
- temp=((temp&0x70)>>4)*10 + (temp&0x0F);
- temp=temp+1; //星期数加1
- up_flag=1;
- if(temp>7)
- temp=1;
- temp=temp/10*16+temp%10;
- break;
- case 5:
- temp=Read1302(DS1302_DAY); //读取日数
- temp=((temp&0x70)>>4)*10 + (temp&0x0F);
- temp=temp+1; //日数加1
- up_flag=1;
- if(temp>31)
- temp=1;
- temp=temp/10*16+temp%10;
- break;
- case 6:
- temp=Read1302(DS1302_MONTH); //读取月数
- temp=((temp&0x70)>>4)*10 + (temp&0x0F);
- temp=temp+1; //月数加1
- up_flag=1;
- if(temp>12)
- temp=1;
- temp=temp/10*16+temp%10;
- break;
- case 7:
- temp=Read1302(DS1302_YEAR); //读取年数
- temp=((temp&0xf0)>>4)*10 + (temp&0x0F);
- temp=temp+1; //年数加1
- up_flag=1;
- if(temp>99)
- temp=0;
- temp=temp/10*16+temp%10;
- break;
- default:break;
- }
-
- // while(Up==0);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //************************
- //降序按键
- //************************
- void Downkey()
- {
- Down=1;
- if(Down==0)
- {
- mdelay(5);
- switch(count)
- {case 1:
- temp=Read1302(DS1302_SECOND); //读取秒数
- temp=((temp&0x70)>>4)*10 + (temp&0x0F);
- temp=temp-1; //秒数减1
- down_flag=1; //数据调整后更新标志
- if(temp==-1) //小于0秒,返回59秒
- temp=59;
- temp=temp/10*16+temp%10;
- break;
- case 2:
- temp=Read1302(DS1302_MINUTE); //读取分数
- temp=((temp&0x70)>>4)*10 + (temp&0x0F);
- temp=temp-1; //分数减1
- down_flag=1;
- if(temp==-1)
- temp=59; //小于0秒,返回59秒
- temp=temp/10*16+temp%10;
- break;
- case 3:
- temp=Read1302(DS1302_HOUR); //读取小时数
- temp=((temp&0x70)>>4)*10 + (temp&0x0F);
- temp=temp-1; //小时数减1
- down_flag=1;
- if(temp==-1)
- temp=23;
- temp=temp/10*16+temp%10;
- break;
- case 4:
- temp=Read1302(DS1302_WEEK); //读取星期数;
- temp=((temp&0x70)>>4)*10 + (temp&0x0F);
- temp=temp-1; //星期数减1
- down_flag=1;
- if(temp==0)
- temp=7;
- temp=temp/10*16+temp%10;
- break;
- case 5:
- temp=Read1302(DS1302_DAY); //读取日数
- temp=((temp&0x70)>>4)*10 + (temp&0x0F);
- temp=temp-1; //日数减1
- down_flag=1;
- if(temp==0)
- temp=31;
- temp=temp/10*16+temp%10;
- break;
- case 6:
- temp=Read1302(DS1302_MONTH); //读取月数
- temp=((temp&0x70)>>4)*10 + (temp&0x0F);
- temp=temp-1; //月数减1
- down_flag=1;
- if(temp==0)
- temp=12;
- temp=temp/10*16+temp%10;
- break;
- case 7:
- temp=Read1302(DS1302_YEAR); //读取年数
- temp=((temp&0xf0)>>4)*10 + (temp&0x0F);
- temp=temp-1; //年数减1
- down_flag=1;
- if(temp==-1)
- temp=99;
- temp=temp/10*16+temp%10;
- break;
- default:break;
- }
-
- // while(Down==0);
- }
- }
- //**************************
- //模式选择按键
- //**************************
- void Setkey()
- {
- Set=1;
- if(Set==0)
- {
- mdelay(5);
- count=count+1; //Setkey按一次,count就加1
- done=1; //进入调整模式
- while(Set==0);
- }
- }
- //*************************
- //按键功能执行
- //*************************
- void keydone()
- {
- uchar Second;
- /* if(flag==0) //关闭时钟,停止计时
- { Write1302(0x8e,0x00); //写入允许
- temp=Read1302(0x80);
- Write1302(0x80,temp|0x80);
- Write1302(0x8e,0x80); //禁止写入
- flag=1;
- }*/
- Setkey(); //扫描模式切换按键
- switch(count)
- {
- case 1:do //count=1,调整秒
- {
- outkey(); //扫描跳出按钮
- Upkey(); //扫描加按钮
- Downkey(); //扫描减按钮
- if(up_flag==1||down_flag==1) //数据更新,重新写入新的数据
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x80,temp); //写入新的秒数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- if(Down!=0&&Up!=0)
- {
- hide_sec++;
- if(hide_sec>3)
- hide_sec=0;
- }
- else hide_sec=0;
- show_time(); //液晶显示数据
- }while(count==2);break;
- case 2:do //count=2,调整分
- {
- hide_sec=0;
- outkey();
- Upkey();
- Downkey();
- if(temp>0x60)
- temp=0;
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x82,temp); //写入新的分数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- if(Down!=0&&Up!=0)
- {
- hide_min++;
- if(hide_min>3)
- hide_min=0;
- }
- else hide_min=0;
- show_time();
- }while(count==3);break;
- case 3:do //count=3,调整小时
- {
- hide_min=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x84,temp); //写入新的小时数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- if(Down!=0&&Up!=0)
- {
- hide_hour++;
- if(hide_hour>3)
- hide_hour=0;
- }
- else hide_hour=0;
- show_time();
- }while(count==4);break;
- case 4:do //count=4,调整星期
- {
- hide_hour=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x8a,temp); //写入新的星期数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- if(Down!=0&&Up!=0)
- {
- hide_week++;
- if(hide_week>3)
- hide_week=0;
- }
- else hide_week=0;
- show_time();
- }while(count==5);break;
- case 5:do //count=5,调整日
- {
- hide_week=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x86,temp); //写入新的日数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- if(Down!=0&&Up!=0)
- {
- hide_day++;
- if(hide_day>3)
- hide_day=0;
- }
- else hide_day=0;
- show_time();
- }while(count==6);break;
- case 6:do //count=6,调整月
- {
- hide_day=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x88,temp); //写入新的月数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- if(Down!=0&&Up!=0)
- {
- hide_month++;
- if(hide_month>3)
- hide_month=0;
- }
- else hide_month=0;
- show_time();
- }while(count==7);break;
- case 7:do //count=7,调整年
- {
- hide_month=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x8c,temp); //写入新的年数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- if(Down!=0&&Up!=0)
- {
- hide_year++;
- if(hide_year>3)
- hide_year=0;
- }
- else hide_year=0;
- show_time();
- }while(count==8);break;
- case 8: count=0;hide_year=0; //count8, 跳出调整模式,返回默认显示状态
- Second=Read1302(DS1302_SECOND);
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x80,Second&0x7f);
- Write1302(0x8E,0x80); //禁止写入
- done=0;
- break; //count=7,开启中断,标志位置0并退出
- default:break;
- }
- }
复制代码 (5) 主程序
- main()
- {
- // flag=1; //时钟停止标志
- init_lcd();
- clrram_lcd();
- Init_DS18B20( ) ; //DS18B20初始化
- Initial_DS1302(); //时钟芯片初始化
- up_flag=0;
- down_flag=0;
- done=0; //进入默认液晶显示
- while(1)
- {
- while(done==1)
- keydone(); //进入调整模式
- while(done==0)
- {
- show_time(); //液晶显示数据
- // flag=0;
- Setkey(); //扫描各功能键
- }
- }
- }
复制代码
|
评分
-
查看全部评分
|