DS1302实时时钟模块-1602显示.docx

上传人:rrsccc 文档编号:9350396 上传时间:2021-02-21 格式:DOCX 页数:9 大小:42.49KB
返回 下载 相关 举报
DS1302实时时钟模块-1602显示.docx_第1页
第1页 / 共9页
DS1302实时时钟模块-1602显示.docx_第2页
第2页 / 共9页
DS1302实时时钟模块-1602显示.docx_第3页
第3页 / 共9页
DS1302实时时钟模块-1602显示.docx_第4页
第4页 / 共9页
DS1302实时时钟模块-1602显示.docx_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《DS1302实时时钟模块-1602显示.docx》由会员分享,可在线阅读,更多相关《DS1302实时时钟模块-1602显示.docx(9页珍藏版)》请在三一文库上搜索。

1、/*说明:*将实时时钟数据通过LCD1602显示-基于51单片机*/#include #include #define uchar unsigned char#define uint unsigned intuchar dis_time_buf16=0;/LCD1602引脚定义/采用8位并行方式,DB0DB7连接至LCDDATA0LCDDATA7sbit RS=P20;sbit RW=P21;sbit CS=P22;#define LCDDATA P0/DS1302引脚定义sbit RST=P13;sbit IO=P12;sbit SCK=P11;/DS1302地址定义#define ds13

2、02_sec_add0x80/秒数据地址#define ds1302_min_add0x82/分数据地址#define ds1302_hr_add0x84/时数据地址#define ds1302_date_add0x86/日数据地址#define ds1302_month_add0x88/月数据地址#define ds1302_day_add0x8a/星期数据地址#define ds1302_year_add0x8c/年数据地址#define ds1302_control_add0x8e/控制数据地址#define ds1302_charger_add0x90 #define ds1302_c

3、lkburst_add0xbe/初始时间定义uchar time_buf8 = 0x20,0x10,0x06,0x01,0x23,0x59,0x55,0x02;/初始时间2010年6月1号23点59分55秒 星期二/功能:延时1毫秒/入口参数:x/出口参数:无/说明:当晶振为12M时,j112;当晶振为11.0592M时,j122void Delay_xms(uint x) uint i,j; for(i=0;ix;i+) for(j=0;j0;t-) _nop_(); /控制LCD写时序void LCD_en_write(void) CS=1; Delay_xus(20); CS=0; De

4、lay_xus(20); /写指令函数void Write_Instruction(uchar command) RS=0; RW=0; CS=1; LCDDATA=command; LCD_en_write();/写入指令数据/写数据函数void Write_Data(uchar Wdata) RS=1; RW=0; CS=1; LCDDATA=Wdata; LCD_en_write();/写入数据/字符显示初始地址设置void LCD_SET_XY(uchar X,uchar Y) uchar address; if(Y=0) address=0x80+X;/Y=0,表示在第一行显示,地址

5、基数为0x80请预览后下载! else address=0xc0+X;/Y非0时,表时在第二行显示,地址基数为0xC0 Write_Instruction(address);/写指令,设置显示初始地址/在第X行Y列开始显示Wdata所对应的单个字符void LCD_write_char(uchar X,uchar Y,uchar Wdata) LCD_SET_XY(X,Y);/写地址 Write_Data(Wdata);/写入当前字符并显示/清屏函数void LCD_clear(void) Write_Instruction(0x01); Delay_xms(5);/显示屏初始化函数void

6、LCD_init(void) Write_Instruction(0x38);/8bit interface,2line,5*7dotsDelay_xms(5);Write_Instruction(0x38);Delay_xms(5);Write_Instruction(0x38);Write_Instruction(0x08);/关显示,不显光标,光标不闪烁Write_Instruction(0x01);/清屏Delay_xms(5);Write_Instruction(0x04);/写一字符,整屏显示不移动/Write_Instruction(0x05);/写一字符,整屏右移/Write_

7、Instruction(0x06);/写一字符,整屏显示不移动/Write_Instruction(0x07);/写一字符,整屏左移Delay_xms(5);/Write_Instruction(0x0B);/关闭显示(不显示字符,只有背光亮)Write_Instruction(0x0C);/开显示,光标、闪烁都关闭/Write_Instruction(0x0D);/开显示,不显示光标,但光标闪烁/Write_Instruction(0x0E);/开显示,显示光标,但光标不闪烁/Write_Instruction(0x0F);/开显示,光标、闪烁均显示/DS1302初始化函数void ds13

8、02_init(void) 请预览后下载!RST=0;/RST脚置低SCK=0;/SCK脚置低/向DS1302写入一字节数据void ds1302_write_byte(uchar addr, uchar d) uchar i;RST=1;/启动DS1302总线/写入目标地址:addraddr = addr & 0xFE; /最低位置零,寄存器0位为0时写,为1时读for (i = 0; i 1;/写入数据:dfor (i = 0; i 1;RST=0;/停止DS1302总线/从DS1302读出一字节数据uchar ds1302_read_byte(uchar addr) uchar i,te

9、mp;RST=1;/启动DS1302总线/写入目标地址:addraddr = addr | 0x01; /最低位置高,寄存器0位为0时写,为1时读for (i = 0; i 1;/输出数据:tempfor (i = 0; i 1;if (IO) temp |= 0x80;else temp &= 0x7F;SCK=1;SCK=0;RST=0;/停止DS1302总线return temp;/向DS302写入时钟数据void ds1302_write_time(void) ds1302_write_byte(ds1302_control_add,0x00);/关闭写保护 ds1302_write_

10、byte(ds1302_sec_add,0x80);/暂停时钟 /ds1302_write_byte(ds1302_charger_add,0xa9); /涓流充电 ds1302_write_byte(ds1302_year_add,time_buf1);/年 ds1302_write_byte(ds1302_month_add,time_buf2);/月 ds1302_write_byte(ds1302_date_add,time_buf3);/日 ds1302_write_byte(ds1302_hr_add,time_buf4);/时 ds1302_write_byte(ds1302_m

11、in_add,time_buf5);/分ds1302_write_byte(ds1302_sec_add,time_buf6);/秒ds1302_write_byte(ds1302_day_add,time_buf7);/周 ds1302_write_byte(ds1302_control_add,0x80);/打开写保护 /从DS302读出时钟数据void ds1302_read_time(void) time_buf1=ds1302_read_byte(ds1302_year_add);/年 请预览后下载!time_buf2=ds1302_read_byte(ds1302_month_ad

12、d);/月 time_buf3=ds1302_read_byte(ds1302_date_add);/日 time_buf4=ds1302_read_byte(ds1302_hr_add);/时 time_buf5=ds1302_read_byte(ds1302_min_add);/分 time_buf6=(ds1302_read_byte(ds1302_sec_add)&0x7f;/秒,屏蔽秒的第7位,避免超出59time_buf7=ds1302_read_byte(ds1302_day_add);/周 void Display(void) LCD_write_char(3,0,dis_ti

13、me_buf0+0); LCD_write_char(4,0,dis_time_buf1+0); LCD_write_char(5,0,dis_time_buf2+0); LCD_write_char(6,0,dis_time_buf3+0); LCD_write_char(7,0,/); LCD_write_char(8,0,dis_time_buf4+0); LCD_write_char(9,0,dis_time_buf5+0); LCD_write_char(10,0,/); LCD_write_char(11,0,dis_time_buf6+0); LCD_write_char(12,

14、0,dis_time_buf7+0); LCD_write_char(15,0,dis_time_buf14+0); /第2行显示 LCD_write_char(3,1,dis_time_buf8+0); LCD_write_char(4,1,dis_time_buf9+0); LCD_write_char(5,1,:); 请预览后下载! LCD_write_char(6,1,dis_time_buf10+0); LCD_write_char(7,1,dis_time_buf11+0); LCD_write_char(8,1,:); LCD_write_char(9,1,dis_time_bu

15、f12+0); LCD_write_char(10,1,dis_time_buf13+0);/定时器中断函数void Timer2() interrupt 5 /定时器2是5号中断 static uchar t; TF2=0; t+; if(t=4) /间隔200ms(50ms*4)读取一次时间 t=0; ds1302_read_time(); /读取时间 dis_time_buf0=(time_buf04); /年 dis_time_buf1=(time_buf0&0x0f); dis_time_buf2=(time_buf14); dis_time_buf3=(time_buf1&0x0f

16、); dis_time_buf4=(time_buf24); /月 dis_time_buf5=(time_buf2&0x0f); dis_time_buf6=(time_buf34); /日 dis_time_buf7=(time_buf3&0x0f); dis_time_buf14=(time_buf7&0x07); /星期 /第2行显示 dis_time_buf8=(time_buf44); /时 dis_time_buf9=(time_buf4&0x0f); 请预览后下载! dis_time_buf10=(time_buf54); /分 dis_time_buf11=(time_buf

17、5&0x0f); dis_time_buf12=(time_buf64); /秒 dis_time_buf13=(time_buf6&0x0f); /定时器2初始化void Init_timer2(void) RCAP2H=0x3c;/赋T2初始值0x3cb0,溢出20次为1秒,每次溢出时间为50ms RCAP2L=0xb0; TR2=1; /启动定时器2 ET2=1; /打开定时器2中断 EA=1; /打开总中断/主函数void main(void) Delay_xms(50);/等待系统稳定 LCD_init(); /LCD初始化 LCD_clear(); /清屏 ds1302_init(); /DS1302初始化 Delay_xms(10); ds1302_write_time(); /写入初始值 Init_timer2(); /定时器2初始化 while(1) Display(); (注:可编辑下载,若有不当之处,请指正,谢谢!) 请预览后下载!

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1