DS1302实时时钟模块,1602显示

上传人:索**** 文档编号:141104107 上传时间:2020-08-04 格式:PDF 页数:8 大小:11.04KB
返回 下载 相关 举报
DS1302实时时钟模块,1602显示_第1页
第1页 / 共8页
DS1302实时时钟模块,1602显示_第2页
第2页 / 共8页
亲,该文档总共8页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《DS1302实时时钟模块,1602显示》由会员分享,可在线阅读,更多相关《DS1302实时时钟模块,1602显示(8页珍藏版)》请在金锄头文库上搜索。

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

2、P11; /DS1302 地址定义 #define ds1302_sec_add 0 x80 / 秒数据地址 #define ds1302_min_add 0 x82 / 分数据地址 #define ds1302_hr_add 0 x84 / 时数据地址 #define ds1302_date_add 0 x86 / 日数据地址 #define ds1302_month_add 0 x88 / 月数据地址 #define ds1302_day_add 0 x8a / 星期数据地址 #define ds1302_year_add 0 x8c / 年数据地址 #define ds1302_cont

3、rol_add 0 x8e / 控制数据地址 #define ds1302_charger_add 0 x90 #define ds1302_clkburst_add 0 xbe / 初始时间定义 uchar time_buf8 = 0 x20,0 x10,0 x06,0 x01,0 x23,0 x59,0 x55,0 x02;/ 初始时间2010 年 6 月 1 号 23 点 59 分 55 秒 星期二 / 功能 :延时 1 毫秒 / 入口参数 :x / 出口参数 :无 / 说明 :当晶振为 12M 时, j112;当晶振为11.0592M 时, j122 void Delay_xms(ui

4、nt 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; Delay_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

5、=Wdata; LCD_en_write();/ 写入数据 / 字符显示初始地址设置 void LCD_SET_XY(uchar X,uchar Y) uchar address; if(Y=0) address=0 x80+X;/Y=0,表示在第一行显示,地址基数为0 x80 else address=0 xc0+X;/Y 非 0 时,表时在第二行显示,地址基数为0 xC0 Write_Instruction(address);/写指令,设置显示初始地址 / 在第 X行 Y列开始显示Wdata 所对应的单个字符 void LCD_write_char(uchar X,uchar Y,ucha

6、r Wdata) LCD_SET_XY(X,Y);/ 写地址 Write_Data(Wdata);/ 写入当前字符并显示 / 清屏函数 void LCD_clear(void) Write_Instruction(0 x01); Delay_xms(5); / 显示屏初始化函数 void LCD_init(void) Write_Instruction(0 x38); /8bit interface,2line,5*7dots Delay_xms(5); Write_Instruction(0 x38); Delay_xms(5); Write_Instruction(0 x38); Writ

7、e_Instruction(0 x08); /关显示,不显光标,光标不闪烁 Write_Instruction(0 x01); /清屏 Delay_xms(5); Write_Instruction(0 x04); /写一字符,整屏显示不移动 /Write_Instruction(0 x05); / 写一字符,整屏右移 /Write_Instruction(0 x06); / 写一字符,整屏显示不移动 /Write_Instruction(0 x07); / 写一字符,整屏左移 Delay_xms(5); /Write_Instruction(0 x0B); / 关闭显示(不显示字符,只有背光

8、亮) Write_Instruction(0 x0C); / 开显示,光标、闪烁都关闭 /Write_Instruction(0 x0D); / 开显示,不显示光标,但光标闪烁 /Write_Instruction(0 x0E); / 开显示,显示光标,但光标不闪烁 /Write_Instruction(0 x0F); / 开显示,光标、闪烁均显示 /DS1302 初始化函数 void ds1302_init(void) RST=0; /RST 脚置低 SCK=0; /SCK 脚置低 / 向 DS1302写入一字节数据 void ds1302_write_byte(uchar addr, uc

9、har d) uchar i; RST=1; / 启动 DS1302总线 / 写入目标地址:addr addr = addr / 最低位置零,寄存器0 位为 0 时写,为 1 时读 for (i = 0; i 1; / 写入数据: d for (i = 0; i 1; RST=0; / 停止 DS1302总线 / 从 DS1302读出一字节数据 uchar ds1302_read_byte(uchar addr) uchar i,temp; RST=1; / 启动 DS1302总线 / 写入目标地址:addr addr = addr | 0 x01; / 最低位置高,寄存器0 位为 0 时写,

10、为1 时读 for (i = 0; i 1; / 输出数据: temp for (i = 0; i 1; if (IO) temp |= 0 x80; else temp SCK=1; SCK=0; RST=0; / 停止 DS1302总线 return temp; / 向 DS302写入时钟数据 void ds1302_write_time(void) ds1302_write_byte(ds1302_control_add,0 x00); / 关闭写保护 ds1302_write_byte(ds1302_sec_add,0 x80); / 暂停时钟 /ds1302_write_byte(d

11、s1302_charger_add,0 xa9); / 涓流充电 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_min_add,time_buf5); / 分 ds1302_write_byte(ds

12、1302_sec_add,time_buf6); / 秒 ds1302_write_byte(ds1302_day_add,time_buf7); / 周 ds1302_write_byte(ds1302_control_add,0 x80); / 打开写保护 / 从 DS302读出时钟数据 void ds1302_read_time(void) time_buf1=ds1302_read_byte(ds1302_year_add); / 年 time_buf2=ds1302_read_byte(ds1302_month_add); / 月 time_buf3=ds1302_read_byte

13、(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)/秒,屏蔽秒的第7 位,避免超 出 59 time_buf7=ds1302_read_byte(ds1302_day_add); / 周 void Display(void) LCD_write_char(3,0,dis_time_buf0+0); LCD_write_char

14、(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,0,dis_time_buf7+0); LCD_wr

15、ite_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_buf12+0); LCD_write_char(10,1,dis_time_buf13+0); / 定时器中断函数 void Timer2() interrupt 5 / 定时器 2 是 5 号中断 static uch

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号