




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/*说明 :*将实时时钟数据通过 LCD1602 显示 基于 51 单片机*/#include #include #define uchar unsigned char#define uint unsigned int uchar dis_time_buf16=0;/LCD1602 引脚定义/ 采用 8 位并行方式 ,DB0DB7 连接至 LCDDATA0LCDDATA7sbit RS=P2A0;sbit RW=P2A1;sbit CS=P2A2;#define LCDDATA P0/DS1302 引脚定义sbit RST=P1A3;sbit IO=P1A2;sbit SCK=P1A1;/DS
2、1302 地址定义#define ds1302_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 ds13
3、02_charger_add0x90#define ds1302_clkburst_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 L
4、CD_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=Wdata;LCD_en_write();/ 写入数据/ 字符显示初始地址设置void LCD_SET_XY(uchar X,uchar Y)uchar address;if(Y
5、=0) address=0x80+X;/Y=0, 表示在第一行显示,地址基数为0x80elseaddress=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(0x0
6、1);Delay_xms(5);/ 显示屏初始化函数void 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_I
7、nstruction(0x05);/Write_Instruction(0x06);/Write_Instruction(0x07);Delay_xms(5);/ 写一字符,整屏右移/ 写一字符,整屏显示不移动/ 写一字符,整屏左移/ 关闭显示(不显示字符,只有背光亮)/ 开显示,光标、闪烁都关闭/ 开显示,不显示光标,但光标闪烁/ 开显示,显示光标,但光标不闪烁/ 开显示,光标、闪烁均显示/Write_Instruction(0x0B);Write_Instruction(0x0C);/Write_Instruction(0x0D);/Write_Instruction(0x0E);/Wri
8、te_Instruction(0x0F); /DS1302 初始化函数void ds1302_init(void)RST=0;SCK=0;/RST 脚置低/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=O;停止 DS1302 总线/从DS130
9、2读出一字节数据uchar ds1302_read_byte(uchar addr) uchar i,temp;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;/停止 DS1302 总线RST=0;return temp;/向DS302写入时钟数据 void ds1302_write_tim
10、e(void) ds1302_write_byte(ds1302_control_add,0x00);/ 关闭写保护ds1302_write_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);/ 日ds
11、1302_write_byte(ds1302_hr_add,time_buf4);/ 时ds1302_write_byte(ds1302_min_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_byt
12、e(ds1302_year_add);/ 年time_buf2=ds1302_read_byte(ds1302_month_add);/ 月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
13、_byte(ds1302_day_add); / 周void Display(void)LCD_write_char(3,0,dis_time_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,
14、0,/);LCD_write_char(11,0,dis_time_buf6+0);LCD_write_char(12,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_w
15、rite_char(8,1,:);LCD_write_char(9,1,dis_time_buf12+0);LCD_write_char(10,1,dis_time_buf13+0);/ 定时器 2 是 5 号中断/ 定时器中断函数void Timer2() interrupt 5static 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_tim
16、e_buf2=(time_buf14);dis_time_buf3=(time_buf1&0x0f);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_buf5&0x0f);dis_time_buf12=(time_buf64); / 秒 dis_time_buf13=(time_buf6&0x0f);/ 定时器 2 初始化void Init_timer2(void)50msRCAP2H=0x3c;赋T2初始值0x3cb0,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030中国心血管CT影像AI分析软件临床采纳障碍研究报告
- 项目执行规定
- 知识产权保护
- 零售业市场营销细则
- 维护酒店饮食安全制度
- 企业活动策划方案及预算控制表
- 项目检验批管理标准操作方案
- 2019天津市中考化学真题解析与备考方案
- 2025年健身教练职业技能考核试卷:健身教练健身设备选购与应用试题
- 建筑工地开放式管理活动策划方案
- 水泥标准培训课件
- 2025秋二年级上册语文上课课件 5 去外婆家
- 《植物组织培养》课件 项目3 无菌操作技术
- 2025届广东省广州市高三4月二模生物试题(原卷版+解析版)
- 装修装饰工程技术施工方案
- 《白银投资深度解析》课件
- 道德与法治课件《我们神圣的国土》课件(34张)
- 计算与人工智能概论(湖南大学)知到智慧树章节答案
- GB/T 44625-2024动态响应同步调相机技术要求
- 25《王戎不取道旁李》 教学设计
- 2024年咨询工程师继续教育城市轨道交通工程可行性研究报告编制方法考试答案
评论
0/150
提交评论