完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
13 1602液晶显示的DS1302实时时钟
/*************** writer:shopping.w ******************/ #include #include #include #define uint unsigned int #define uchar unsigned char ***it IO = P1^0; //DS1302数据线 ***it SCLK = P1^1; //DS1302时钟线 ***it RST = P1^2;//DS1302复位线 ***it RS = P2^0; //LCD寄存器选择 ***it RW = P2^1; //LCD读写控制器 ***it EN = P2^2; //LCD启用 //0,2,3,4,5,6,7分别对应周日,周一至周六 uchar *WEEK[]= { "SUN","***","MON","TUS","WEN","THU","FRI","SAT" }; uchar LCD_DSY_BUFFER1[]={"DATE 00-00-00 "}; //LCD显示缓冲 uchar LCD_DSY_BUFFER2[]={"tiME 00:00:00 "}; uchar DateTime[7];//所读取的日期时间 //延时 void DelayMS(uint ms) { uchar i; while(ms--) { for(i=0;i<120;i++); } } //向DS1302写入一字节 void Write_A_Byte_TO_DS1302(uchar x) { uchar i; for(i=0;i<8;i++) { IO=x&0x01;SCLK=1;SCLK=0;x>>=1; } } //向DS1302读取一字节 uchar Get_A_Byte_FROM_DS1302() { uchar i,b=0x00; for(i=0;i<8;i++) { b |= _crol_((uchar)IO,i); SCLK=1;SCLK=0; } return b/16*10+b%16; 、、//返回BCD码 } //向DS1302指定位置读数据 uchar Read_Data(uchar addr) { uchar dat; RST = 0;SCLK=0;RST=1; Write_A_Byte_TO_DS1302(addr); dat = Get_A_Byte_FROM_DS1302(); SCLK=1;RST=0; return dat; } //读取当前日期时间 void GetTime() { uchar i,addr = 0x81; for(i=0;i<7;i++) { DateTime[i]=Read_Data(addr);addr+=2; } } //读LCD状态 uchar Read_LCD_State() { uchar state; RS=0;RW=1;EN=1;DelayMS(1); state=P0; EN = 0;DelayMS(1); return state; } //忙等待 void LCD_Busy_Wait() { while((Read_LCD_State()&0x80)==0x80); DelayMS(5); } //向LCD写数据 void Write_LCD_Data(uchar dat) { LCD_Busy_Wait(); RS=1;RW=0;EN=0;P0=dat;EN=1;DelayMS(1);EN=0; } //写LCD指令 void Write_LCD_Command(uchar cmd) { LCD_Busy_Wait(); RS=0;RW=0;EN=0;P0=cmd;EN=1;DelayMS(1);EN=0; } //LCD初始化 void Init_LCD() { Write_LCD_Command(0x38); DelayMS(1); Write_LCD_Command(0x01); DelayMS(1); Write_LCD_Command(0x06); DelayMS(1); Write_LCD_Command(0x0c); DelayMS(1); } //设置液晶显示位置 void Set_LCD_POS(uchar p) { Write_LCD_Command(p|0x80); } //在LCD是显示字符串 void Display_LCD_String(uchar p,uchar *s) { uchar i; Set_LCD_POS(p); for(i=0;i<16;i++) { Write_LCD_Data(s[i]); DelayMS(1); } } //日期与时间值转换为数字字符 void Format_DateTime(uchar d,uchar *a) { a[0]=d/10+'0'; a[1]=d%10+'0'; } //主程序 void main() { Init_LCD(); while(1) { GetTime(); //年月日 Format_DateTime(DateTime[6],LCD_DSY_BUFFER1+5); Format_DateTime(DateTime[4],LCD_DSY_BUFFER1+8); Format_DateTime(DateTime[3],LCD_DSY_BUFFER1+11); //星期 strcpy(LCD_DSY_BUFFER1+13,WEEK[DateTime[5]]); //时分秒 Format_DateTime(DateTime[2],LCD_DSY_BUFFER1+5); Format_DateTime(DateTime[1],LCD_DSY_BUFFER1+8); Format_DateTime(DateTime[0],LCD_DSY_BUFFER1+11); //显示年月日时分秒 Display_LCD_String(0x00,LCD_DSY_BUFFER1); Display_LCD_String(0x40,LCD_DSY_BUFFER2); } } 谁能帮我分析一下!!!感谢! |
|
相关推荐
1 个讨论
|
|
只有小组成员才能发言,加入小组>>
828浏览 0评论
193浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-21 07:53 , Processed in 1.186941 second(s), Total 44, Slave 35 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号