完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include #include #define uchar unsigned char #define uint unsigned int ***it dula=P2^6; //数码管段选线 ***it wela=P2^7; //数码管位选线 uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x07, 0x39,0x5e,0x79,0x71 }; ***it IO=P2^2; ***it SCK=P2^3; ***it RST=P2^1; uint i,te,g; unsigned char time_buf1[8] = {20,10,6,5,12,55,00,6};//空年月日时分秒周 unsigned char time_buf[8] ; //空年月日时分秒周 /*-------------------------------------- 向DS1302写入一字节数据 ------------------------------------------------*/ void Ds1302_Write_Byte(unsigned char addr, unsigned char d) { unsigned char i; RST=1; //写入目标地址:addr addr = addr & 0xFE; //最低位置零 for (i = 0; i < 8; i ++) { if (addr & 0x01) { IO=1; } else { IO=0; } SCK=1; SCK=0; addr = addr >> 1; } //写入数据:d for (i = 0; i < 8; i ++) { if (d & 0x01) { IO=1; } else { IO=0; } SCK=1; SCK=0; d = d >> 1; } RST=0; //停止DS1302总线 } /*------------------------------------------------ 从DS1302读出一字节数据 ------------------------------------------------*/ unsigned char Ds1302_Read_Byte(unsigned char addr) { unsigned char i; unsigned char temp; RST=1; //写入目标地址:addr addr = addr | 0x01;//最低位置高 for (i = 0; i < 8; i ++) { if (addr & 0x01) { IO=1; } else { IO=0; } SCK=1; SCK=0; addr = addr >> 1; } //输出数据:temp for (i = 0; i < 8; i ++) { temp = temp >> 1; if (IO) { temp |= 0x80; } else { temp &= 0x7F; } SCK=1; SCK=0; } RST=0; //停止DS1302总线 return temp; } /*------------------------------------------------ 向DS1302写入时钟数据 ------------------------------------------------*/ void Ds1302_Write_Time(void) { unsigned char i,tmp; for(i=0;i<8;i++) { //BCD处理变为十六进制 tmp=time_buf1[i]/10; time_buf[i]=time_buf1[i]%10; time_buf[i]=time_buf[i]+tmp*16; } Ds1302_Write_Byte(0x8e,0x00); //关闭写保护 Ds1302_Write_Byte(0x80,0x80); //暂停 //Ds1302_Write_Byte(ds1302_charger_add,0xa9); //涓流充电 Ds1302_Write_Byte(0x8c,time_buf[1]); //年 Ds1302_Write_Byte(0x88,time_buf[2]); //月 Ds1302_Write_Byte(0x86,time_buf[3]); //日 Ds1302_Write_Byte(0x8a,time_buf[7]); //周 Ds1302_Write_Byte(0x84,time_buf[4]); //时 Ds1302_Write_Byte(0x82,time_buf[5]); //分 Ds1302_Write_Byte(0x80,time_buf[6]); //秒 Ds1302_Write_Byte(0x8e,0x80); //打开写保护 } /*------------------------------------------------ 从DS1302读出时钟数据 ------------------------------------------------*/ void Ds1302_Read_Time(void) { unsigned char i,tmp; time_buf[1]=Ds1302_Read_Byte(0x8d); //年 time_buf[2]=Ds1302_Read_Byte(0x89); //月 time_buf[3]=Ds1302_Read_Byte(0x87); //日 time_buf[4]=Ds1302_Read_Byte(0x85); //时 time_buf[5]=Ds1302_Read_Byte(0x83); //分 time_buf[6]=(Ds1302_Read_Byte(0x81))&0x7F;//秒 time_buf[7]=Ds1302_Read_Byte(0x8b); //周 for(i=0;i<8;i++) { //BCD处理变为十进制 tmp=time_buf[i]/16; time_buf1[i]=time_buf[i]%16; time_buf1[i]=time_buf1[i]+tmp*10; } } /*------------------------------------------------ DS1302初始化 ------------------------------------------------*/ void Ds1302_Init(void) { RST=0; //RST脚置低 SCK=0; //SCK脚置低 Ds1302_Write_Byte(0x80,0x00); } void dis_temp(void) { te=time_buf1[4]/10; g=time_buf1[4]%10; dula=0; wela=1; P0=0xfe; dula=1; wela=0; P0=table[te]; dula=0; wela=1; P0=0xfd; dula=1; wela=0; P0=table[g]; dula=0; wela=1; P0=0xfb; dula=1; wela=0; P0=0x4f; te=time_buf1[5]/10; g=time_buf1[5]%10; dula=0; wela=1; P0=0xf7; dula=1; wela=0; P0=table[te]; dula=0; wela=1; P0=0x7f; dula=1; wela=0; P0=table[g]; dula=0; wela=1; P0=0xbf; dula=1; wela=0; P0=0x4f; te=time_buf1[6]/10; g=time_buf1[6]%10; dula=0; wela=1; P0=0xdf; dula=1; wela=0; P0=table[te]; dula=0; wela=1; P0=0xef; dula=1; wela=0; P0=table[g]; } void main() { Ds1302_Init(); Ds1302_Write_Time(); while(1) { Ds1302_Read_Time(); dis_temp(); } } |
|
相关推荐
1个回答
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
求助一下关于51系列单片机的Timer0的计时问题,TH0、TL0+1的时间是怎么算的?
195 浏览 0 评论
【RA-Eco-RA4E2-64PIN-V1.0开发板试用】开箱+Keil环境搭建+点灯+点亮OLED
172 浏览 0 评论
【敏矽微ME32G070开发板免费体验】使用coremark测试敏矽微ME32G070 跑分
453 浏览 0 评论
【敏矽微ME32G070开发板免费体验】开箱+点灯+点亮OLED
697 浏览 2 评论
602 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11985 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-21 08:54 , Processed in 0.496985 second(s), Total 72, Slave 55 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号