ds1302读数据偶数值出错 数据年月日时分秒只要是偶数都不正确,奇数就正常
答案:2 悬赏:30 手机版
解决时间 2021-03-24 05:12
- 提问者网友:我没有何以琛的痴心不悔
- 2021-03-23 11:52
最后发现只要接上显示器测CLK时钟引脚所有读写都正常,不接就不正常·
最佳答案
- 五星知识达人网友:封刀令
- 2021-03-23 12:08
你好!把你程序和原理图发一下看看,找找原因
全部回答
- 1楼网友:慢性怪人
- 2021-03-23 13:06
void ds1302inputbyte(unsigned char d) //实时时钟写入一字节(内部函数)
{
unsigned char i;
acc = d;
for(i=8; i>0; i--)
{
ds1302_io = acc0; //相当于汇编中的 rrc
ds1302_clk = 1;
ds1302_clk = 0;
acc = acc >> 1;
}
}
unsigned char ds1302outputbyte(void) //实时时钟读取一字节(内部函数)
{
unsigned char i;
for(i=8; i>0; i--)
{
acc = acc >>1; //相当于汇编中的 rrc
acc7 = ds1302_io;
ds1302_clk = 1;
ds1302_clk = 0;
}
return(acc);
}
void write1302(unsigned char ucaddr, unsigned char ucda) //ucaddr: ds1302地址, ucdata: 要写的数据
{
ds1302_rst = 0;
ds1302_clk = 0;
ds1302_rst = 1;
ds1302inputbyte(ucaddr); // 地址,命令
ds1302inputbyte(ucda); // 写1byte数据
ds1302_clk = 1;
ds1302_rst = 0;
}
unsigned char read1302(unsigned char ucaddr) //读取ds1302某地址的数据
{
unsigned char ucdata;
ds1302_rst = 0;
ds1302_clk = 0;
ds1302_rst = 1;
ds1302inputbyte(ucaddr|0x01); // 地址,命令
ucdata = ds1302outputbyte(); // 读1byte数据
ds1302_clk = 1;
ds1302_rst = 0;
return(ucdata);
}
void ds1302_setprotect(bit flag) //是否写保护
{
if(flag)
write1302(0x8e,0x10);
else
write1302(0x8e,0x00);
}
void ds1302_settime(unsigned char address, unsigned char value) // 设置时间函数
{
ds1302_setprotect(0);
write1302(address, ((value/10)<<4 | (value%10)));
}
void ds1302_gettime(systemtime *time)
{
unsigned char readvalue;
readvalue = read1302(ds1302_second);
time->second = ((readvalue&0x70)>>4)*10 + (readvalue&0x0f);
readvalue = read1302(ds1302_minute);
time->minute = ((readvalue&0x70)>>4)*10 + (readvalue&0x0f);
readvalue = read1302(ds1302_hour);
time->hour = ((readvalue&0x70)>>4)*10 + (readvalue&0x0f);
readvalue = read1302(ds1302_day);
time->day = ((readvalue&0x70)>>4)*10 + (readvalue&0x0f);
readvalue = read1302(ds1302_week);
time->week = ((readvalue&0x70)>>4)*10 + (readvalue&0x0f);
readvalue = read1302(ds1302_month);
time->month = ((readvalue&0x70)>>4)*10 + (readvalue&0x0f);
readvalue = read1302(ds1302_year);
time->year = ((readvalue&0x70)>>4)*10 + (readvalue&0x0f);
}
void datetostr(systemtime *time)
{
time->datestring[0] = time->year/10 + '0';
time->datestring[1] = time->year%10 + '0';
time->datestring[2] = '-';
time->datestring[3] = time->month/10 + '0';
time->datestring[4] = time->month%10 + '0';
time->datestring[5] = '-';
time->datestring[6] = time->day/10 + '0';
time->datestring[7] = time->day%10 + '0';
time->datestring[8] = '\0';
}
void timetostr(systemtime *time)
{
time->timestring[0] = time->hour/10 + '0';
time->timestring[1] = time->hour%10 + '0';
time->timestring[2] = ':';
time->timestring[3] = time->minute/10 + '0';
time->timestring[4] = time->minute%10 + '0';
time->timestring[5] = ':';
time->timestring[6] = time->second/10 + '0';
time->timestring[7] = time->second%10 + '0';
time->datestring[8] = '\0';
}
void initial_ds1302(void)
{
unsigned char second=read1302(ds1302_second);
if(second&0x80)
ds1302_settime(ds1302_second,0);
}
参考这个
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯