利用六位数码管显示、DS1302读取,实现基本时钟功能,请教大伙如何用代码实现设置闹钟
2条回答 默认 最新
- ☞黑心萝卜三条杠☜ 2023-01-14 19:11关注
六位数码管可以给 时 分 秒每个 分配两个数码管显示,我这有个基于蓝桥杯单片机写的ds1302代码,可以参考参考
/*DS1302是一个时钟芯片*/ #include <STC15F2K60S2.h> #include <intrins.h> #include "ds1302.h" sbit SCK=P1^7; sbit SDA=P2^3; sbit RST = P1^3; // DS1302复位 unsigned char hour = 1,minute = 1,second = 1; void Write_Ds1302(unsigned char temp) //写入时钟数据函数 { unsigned char i; for (i=0;i<8;i++) { SCK=0; SDA=temp&0x01; temp>>=1; SCK=1; } } void Write_Ds1302_Byte( unsigned char address,unsigned char dat )//在时钟特定地址写入特定数值的函数 { RST=0; _nop_(); SCK=0; _nop_(); RST=1; _nop_(); Write_Ds1302(address); Write_Ds1302(dat); RST=0; } unsigned char Read_Ds1302_Byte ( unsigned char address )//读取特定地址的时钟数据函数 { unsigned char i,temp=0x00; RST=0; _nop_(); SCK=0; _nop_(); RST=1; _nop_(); Write_Ds1302(address); for (i=0;i<8;i++) { SCK=0; temp>>=1; if(SDA) temp|=0x80; SCK=1; } RST=0; _nop_(); SCK=0; _nop_(); SCK=1; _nop_(); SDA=0; _nop_(); SDA=1; _nop_(); return (temp); } void ds1302_init(unsigned char h,unsigned char m,unsigned char s){//给时钟设定初始值 Write_Ds1302_Byte(0x8e,0); Write_Ds1302_Byte(0x84,h);//设置 时 Write_Ds1302_Byte(0x82,m);//设置 分 Write_Ds1302_Byte(0x80,s);//设置 秒 Write_Ds1302_Byte(0x8e,0x80); } void ds1302_read(){//读取时钟的数据 hour = Read_Ds1302_Byte(0x85);//读取 时 minute = Read_Ds1302_Byte(0x83);//读取 分 second = Read_Ds1302_Byte(0x81);//读取 秒 }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报