智能电子钟LCD显示_第1页
智能电子钟LCD显示_第2页
智能电子钟LCD显示_第3页
智能电子钟LCD显示_第4页
智能电子钟LCD显示_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include <reg52.h>#include <intrins.h>#define uint unsigned int#define uchar unsigned char#define Date P3 /数码管数据口#define com P1sbit DS1302_CLK = P17; /实时时钟时钟线引脚 sbit DS1302_IO = P26; /实时时钟数据线引脚 sbit DS1302_RST = P16; /实时时钟复位线引脚sbit ACC0 = ACC0;sbit ACC7 = ACC7;uchar code table=0x3f,0x06,

2、0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year; /秒,分,时到日,月,年位闪的计数sbit Set = P20; /模式切换键sbit Up = P21; /加法按钮sbit Down = P22; /减法按钮sbit out = P23; /立刻跳出调整模式按钮sbit di=P24; /蜂鸣器char done,count,temp,flag,up_flag,down_flag;uchar TempBuffer5

3、,week_value2;void show_time(); /显示程序/* 功能 : 延时,延时时间大概为ms。*/void delayms(int x)int i,j;for(i=110; i>0; i-) for(j=x; j>0; j-);void beep() di=1;delayms(400); di=0;/*名称: 数码管显示*/xianshi(uchar shi1,uchar shi2,uchar fen1,uchar fen2,uchar miao1,uchar miao2)Date=tableshi1;com=0xfe;delayms(10);Date=tabl

4、eshi2;com=0xfd;delayms(10);Date=tablefen1;com=0xfb;delayms(10);Date=tablefen2;com=0xf7;delayms(10);Date=tablemiao1;com=0xef;delayms(10);Date=tablemiao2;com=0xdf;delayms(10);/*1602液晶显示部分子程序*/sbit LcdRs= P25;sbit LcdRw= P26;sbit LcdEn = P27;sfr DBPort= 0x80;/内部等待函数*unsigned char LCD_Wait(void) LcdRs=0

5、; LcdRw=1; _nop_(); LcdEn=1; _nop_();LcdEn=0;return DBPort;/向LCD写入命令或数据*#define LCD_COMMAND0 / Command#define LCD_DATA1 / Data#define LCD_CLEAR_SCREEN0x01 / 清屏#define LCD_HOMING 0x02 / 光标返回原点void LCD_Write(bit style, unsigned char input)LcdEn=0;LcdRs=style;LcdRw=0;_nop_();DBPort=input;_nop_();/注意顺序L

6、cdEn=1;_nop_();/注意顺序LcdEn=0;_nop_();LCD_Wait();/设置显示模式*#define LCD_SHOW0x04 /显示开#define LCD_HIDE0x00 /显示关 #define LCD_CURSOR0x02 /显示光标#define LCD_NO_CURSOR0x00 /无光标 #define LCD_FLASH0x01 /光标闪动#define LCD_NO_FLASH0x00 /光标不闪动void LCD_SetDisplay(unsigned char DisplayMode)LCD_Write(LCD_COMMAND, 0x08|Dis

7、playMode);/设置输入模式*#define LCD_AC_UP0x02#define LCD_AC_DOWN0x00 / default#define LCD_MOVE0x01 / 画面可平移#define LCD_NO_MOVE0x00 /defaultvoid LCD_SetInput(unsigned char InputMode)LCD_Write(LCD_COMMAND, 0x04|InputMode);/初始化LCD*void LCD_Initial() LcdEn=0;LCD_Write(LCD_COMMAND,0x38); /8位数据端口,2行显示,5*7点阵LCD_W

8、rite(LCD_COMMAND,0x38);LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); /开启显示, 无光标LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); /清屏LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); /AC递增, 画面不动 /液晶字符输入的位置*void GotoXY(unsigned char x, unsigned char y)if(y=0)LCD_Write(LCD_COMMAND,0x80|x);if(y=1)LCD_Write(LCD_COMMAND,0x80|(x-0x40

9、); /将字符输出到液晶显示void Print(unsigned char *str)while(*str!='0')LCD_Write(LCD_DATA,*str);str+;/*DS1302时钟部分子程序*/typedef struct _SYSTEMTIME_unsigned char Second;unsigned char Minute;unsigned char Hour;unsigned char Week;unsigned char Day;unsigned char Month;unsigned char Year;unsigned char DateStr

10、ing11;unsigned char TimeString9;SYSTEMTIME;/定义的时间类型SYSTEMTIME CurrentTime;#define AM(X)X#define PM(X)(X+12) / 转成24小时制#define DS1302_SECOND0x80 /时钟芯片的寄存器位置,存放时间#define DS1302_MINUTE0x82#define DS1302_HOUR0x84 #define DS1302_WEEK0x8A#define DS1302_DAY0x86#define DS1302_MONTH0x88#define DS1302_YEAR0x8C

11、 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; /

12、相当于汇编中的 RRC ACC7 = DS1302_IO; DS1302_CLK = 1;DS1302_CLK = 0; return(ACC); void Write1302(unsigned char ucAddr, unsigned char ucDa) DS1302_RST = 0; DS1302_CLK = 0; DS1302_RST = 1; DS1302InputByte(ucAddr); / 地址,命令 DS1302InputByte(ucDa); / 写1Byte数据 DS1302_CLK = 1; DS1302_RST = 0; unsigned char Read1302

13、(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_GetTime(SYSTEMTIME *Time) /获取时钟芯片的时钟数据到自定义的结构型数组 unsign

14、ed char ReadValue; ReadValue = Read1302(DS1302_SECOND);Time->Second = (ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_MINUTE);Time->Minute = (ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_HOUR);Time->Hour = (ReadValu

15、e&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_DAY);Time->Day = (ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_WEEK);Time->Week = (ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_MONTH);Time-

16、>Month = (ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_YEAR);Time->Year = (ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);void DateToStr(SYSTEMTIME *Time) /将时间年,月,日,星期数据转换成液晶显示字符串,放到数组里DateStringif(hide_year<2) Time->DateString0 = '2

17、9; Time->DateString1 = '0' Time->DateString2 = Time->Year/10 + '0' Time->DateString3 = Time->Year%10 + '0' else Time->DateString0 = ' ' Time->DateString1 = ' ' Time->DateString2 = ' ' Time->DateString3 = ' 'Time->D

18、ateString4 = '/'if(hide_month<2)Time->DateString5 = Time->Month/10 + '0' Time->DateString6 = Time->Month%10 + '0' else Time->DateString5 = ' ' Time->DateString6 = ' 'Time->DateString7 = '/'if(hide_day<2)Time->DateString8 =

19、 Time->Day/10 + '0' Time->DateString9 = Time->Day%10 + '0' elseTime->DateString8 = ' 'Time->DateString9 = ' ' if(hide_week<2) /星期的数据另外放到 week_value数组里,因为等一下要在最后显视 week_value0 = Time->Week%10 + '0' else week_value0 = ' ' week_value1

20、 = '0' Time->DateString10 = '0' /字符串末尾加 '0' ,判断结束字符 void TimeToStr(SYSTEMTIME *Time) if(hide_hour<2) Time->TimeString0 = Time->Hour/10 + '0' Time->TimeString1 = Time->Hour%10 + '0' else Time->TimeString0 = ' 'Time->TimeString1 =

21、 ' 'Time->TimeString2 = ':'if(hide_min<2) Time->TimeString3 = Time->Minute/10 + '0' Time->TimeString4 = Time->Minute%10 + '0' else Time->TimeString3 = ' 'Time->TimeString4 = ' ' Time->TimeString5 = ':' if(hide_sec<

22、2) Time->TimeString6 = Time->Second/10 + '0' Time->TimeString7 = Time->Second%10 + '0' elseTime->TimeString6 = ' 'Time->TimeString7 = ' ' Time->DateString8 = '0'void Initial_DS1302(void) /时钟芯片初始化 unsigned char Second=Read1302(DS1302_SECOND

23、);if(Second&0x80) /判断时钟芯片是否关闭 Write1302(0x8e,0x00); /写入允许Write1302(0x8c,0x07); /Write1302(0x88,0x07);Write1302(0x86,0x25);Write1302(0x8a,0x07);Write1302(0x84,0x23);Write1302(0x82,0x59);Write1302(0x80,0x55);Write1302(0x8e,0x80); /禁止写入 void Delay1ms(unsigned int count)unsigned int i,j;for(i=0;i<

24、;count;i+) for(j=0;j<20;j+);/*延时子程序*/void mdelay(uint delay)uint i;for(;delay>0;delay-)for(i=0;i<62;i+) /1ms延时.;void outkey() /跳出调整模式,返回默认显示 uchar Second;if(out=0) mdelay(8); 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_SEC

25、OND); Write1302(0x8e,0x00); /写入允许Write1302(0x80,Second&0x7f);Write1302(0x8E,0x80); /禁止写入done=0; while(out=0); /void Upkey()/升序按键 Up=1;if(Up=0) mdelay(8);switch(count)case 1: temp=Read1302(DS1302_SECOND); /读取秒数 temp=temp+1; /秒数加1 up_flag=1; /数据调整后更新标志 if(temp&0x7f)>0x59) /超过59秒,清零 temp=0;

26、break; case 2:temp=Read1302(DS1302_MINUTE); /读取分数 temp=temp+1; /分数加1up_flag=1;if(temp>0x59) /超过59分,清零temp=0;break;case 3: temp=Read1302(DS1302_HOUR); /读取小时数temp=temp+1; /小时数加1up_flag=1;if(temp>0x23) /超过23小时,清零 temp=0; break; case 4: temp=Read1302(DS1302_WEEK); /读取星期数 temp=temp+1; /星期数加1 up_fla

27、g=1; if(temp>0x7) temp=1; break; case 5: temp=Read1302(DS1302_DAY); /读取日数 temp=temp+1; /日数加1 up_flag=1; if(temp>0x31) temp=1; break; case 6: temp=Read1302(DS1302_MONTH); /读取月数 temp=temp+1; /月数加1 up_flag=1; if(temp>0x12) temp=1; break; case 7: temp=Read1302(DS1302_YEAR); /读取年数 temp=temp+1; /

28、年数加1 up_flag=1; if(temp>0x85) temp=0; break; default:break;while(Up=0); /void Downkey()/降序按键 Down=1;if(Down=0) mdelay(8); switch(count) case 1: temp=Read1302(DS1302_SECOND); /读取秒数 temp=temp-1; /秒数减1 down_flag=1; /数据调整后更新标志 if(temp=0x7f) /小于0秒,返回59秒 temp=0x59; break; case 2: temp=Read1302(DS1302_M

29、INUTE); /读取分数 temp=temp-1; /分数减1 down_flag=1; if(temp=-1) temp=0x59; /小于0秒,返回59秒 break; case 3: temp=Read1302(DS1302_HOUR); /读取小时数 temp=temp-1; /小时数减1 down_flag=1; if(temp=-1) temp=0x23; break; case 4: temp=Read1302(DS1302_WEEK); /读取星期数 temp=temp-1; /星期数减1 down_flag=1; if(temp=0) temp=0x7; break; ca

30、se 5: temp=Read1302(DS1302_DAY); /读取日数 temp=temp-1; /日数减1 down_flag=1; if(temp=0) temp=31; break; case 6: temp=Read1302(DS1302_MONTH); /读取月数 temp=temp-1; /月数减1 down_flag=1; if(temp=0) temp=12; break; case 7: temp=Read1302(DS1302_YEAR); /读取年数 temp=temp-1; /年数减1 down_flag=1; if(temp=-1) temp=0x85; bre

31、ak; default:break;while(Down=0); void Setkey()/模式选择按键Set=1;if(Set=0) mdelay(8); count=count+1; /Setkey按一次,count就加1 done=1; /进入调整模式 while(Set=0); void bell()if (Set=1);while(Set=1); void keydone()/按键功能执行 uchar Second; if(flag=0) /关闭时钟,停止计时 Write1302(0x8e,0x00); /写入允许 temp=Read1302(0x80); Write1302(0x

32、80,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|0x80); /写入新的秒数 Write1302(0x8e,0x80); /禁止写入 up_flag=

33、0; down_flag=0; hide_sec+; /位闪计数 if(hide_sec>3) 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)

34、; /禁止写入 up_flag=0; down_flag=0; hide_min+; if(hide_min>3) 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; do

35、wn_flag=0; hide_hour+; if(hide_hour>3) 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; hide_w

36、eek+; if(hide_week>3) 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; hide_day+; if(hide_day>

37、;3) 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; hide_month+; if(hide_month>3) 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_fl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论