DS1302芯片C语言程序

上传人:工**** 文档编号:506732650 上传时间:2023-07-11 格式:DOC 页数:6 大小:42.50KB
返回 下载 相关 举报
DS1302芯片C语言程序_第1页
第1页 / 共6页
DS1302芯片C语言程序_第2页
第2页 / 共6页
DS1302芯片C语言程序_第3页
第3页 / 共6页
DS1302芯片C语言程序_第4页
第4页 / 共6页
DS1302芯片C语言程序_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《DS1302芯片C语言程序》由会员分享,可在线阅读,更多相关《DS1302芯片C语言程序(6页珍藏版)》请在金锄头文库上搜索。

1、#include /包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义#include sbit SCK=P36;/时钟sbit SDA=P34;/数据sbit RST = P35;/ DS1302复位sbit LS138A=P22; sbit LS138B=P23;sbit LS138C=P24; bit ReadRTC_Flag;/定义读DS1302标志unsigned char l_tmpdate7=0,0,12,15,5,3,8;/秒分时日月周年08-05-15 12:00:00unsigned char l_tmpdisplay8;code unsigned char w

2、rite_rtc_address7=0x80,0x82,0x84,0x86,0x88,0x8a,0x8c; /秒分时日月周年 最低位读写位code unsigned char read_rtc_address7=0x81,0x83,0x85,0x87,0x89,0x8b,0x8d; code unsigned char table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40; /共阴数码管 0-9 - 熄灭表/*/* 函数声明 */*/void Write_Ds1302_byte(unsigned char temp); voi

3、d Write_Ds1302( unsigned char address,unsigned char dat );unsigned char Read_Ds1302 ( unsigned char address );void Read_RTC(void);/read RTC void Set_RTC(void); /set RTC void InitTIMER0(void);/inital timer0/*/* 主函数 */*/void main(void) InitTIMER0(); /初始化定时器0 Set_RTC(); /写入时钟值,如果使用备用电池时候,不需要没每次上电写入,此程序

4、应该屏蔽while(1) if(ReadRTC_Flag) ReadRTC_Flag=0;Read_RTC(); l_tmpdisplay0=l_tmpdate2/16;/数据的转换,因我们采用数码管09的显示,将数据分开l_tmpdisplay1=l_tmpdate2&0x0f;l_tmpdisplay2=10; /加入-l_tmpdisplay3=l_tmpdate1/16;l_tmpdisplay4=l_tmpdate1&0x0f;l_tmpdisplay5=10;l_tmpdisplay6=l_tmpdate0/16;l_tmpdisplay7=l_tmpdate0&0x0f; /*/

5、* 定时器0初始化 */*/void InitTIMER0(void) TMOD|=0x01;/定时器设置 16位 TH0=0xef;/初始化值 TL0=0xf0; ET0=1; TR0=1; EA=1;/*/* 写一个字节 */*/void Write_Ds1302_Byte(unsigned char temp) unsigned char i; for (i=0;i=1; /右移一位 SCK=1; /*/* 写入DS1302 */*/void Write_Ds1302( unsigned char address,unsigned char dat ) RST=0;_nop_(); SC

6、K=0;_nop_(); RST=1; _nop_(); /启动 Write_Ds1302_Byte(address);/发送地址 Write_Ds1302_Byte(dat);/发送数据 RST=0; /恢复/*/* 读出DS1302数据 */*/unsigned char Read_Ds1302 ( unsigned char address ) unsigned char i,temp=0x00; RST=0;_nop_();_nop_(); SCK=0;_nop_();_nop_(); RST=1;_nop_();_nop_(); Write_Ds1302_Byte(address);

7、 for (i=0;i=1;/右移一位_nop_(); _nop_(); _nop_(); SCK=1; RST=0;_nop_(); /以下为DS1302复位的稳定时间 _nop_();RST=0;SCK=0;_nop_();_nop_();_nop_();_nop_();SCK=1;_nop_();_nop_();SDA=0;_nop_();_nop_();SDA=1;_nop_();_nop_();return (temp);/返回/*/* 读时钟数据 */*/void Read_RTC(void) /读取 日历 unsigned char i,*p; p=read_rtc_addres

8、s; /地址传递 for(i=0;i7;i+) /分7次读取 秒分时日月周年 l_tmpdatei=Read_Ds1302(*p); p+; /*/* 设定时钟数据 */*/void Set_RTC(void) /设定 日历unsigned char i,*p,tmp;for(i=0;i7;i+) /BCD处理tmp=l_tmpdatei/10;l_tmpdatei=l_tmpdatei%10;l_tmpdatei=l_tmpdatei+tmp*16; Write_Ds1302(0x8E,0X00); p=write_rtc_address;/传地址 for(i=0;i7;i+)/7次写入 秒分时日月周年 Write_Ds1302(*p,l_tmpdatei); p+; Write_Ds1302(0x8E,0x80);/*/*

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 幼儿/小学教育 > 小学课件

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