时钟芯片DS1302调整时间的思路是怎么样的?最好能给一个调整时间的程序,谢谢

调整时间需要用得到定时器吗?
2024年12月05日 02:12
有2个网友回答
网友(1):

我级别太低不能穿电路图!!

#ifndef _1602_yejing_
#define _1602_yejing_
#include
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P3^4;
sbit lcdrs=P3^5;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_lcd(uchar x,char *cha)
{ uchar length,i=0;
write_com(x);
for(length=0;cha[length]!=0;length++);
for(i=0;i {
write_data(cha[i]);
delay(5);
}
}
void write_bcd(uchar cha)
{
uchar ch1,ch2;
ch1=(cha&0x0f)+'0';
ch2=((cha>>4)&0x0f)+'0';
write_data(ch2);
write_data(ch1);
}
void write_fd(float t)//显示float型函数
{ uchar s1,s2,s3,s4;
uint tt;
tt=t*100;
s1=tt/1000;s1+=0x30;
s2=tt%1000/100;s2+=0x30;
s3=tt%1000%100/10;s3+=0x30;
s4=tt%10;s4+=0x30;
write_data(s1);
write_data(s2);
write_data(46);
write_data(s3);
write_data(s4);
}
void init()
{
lcden=0;
write_com(0x38);//设置16*2显示
write_com(0x0c);//设置开显示,不显示光标
write_com(0x06);//写一个字符后地址指针加1
write_com(0x01);//显示清0,数据指针清0
}
#endif
/**************************************************************************

THE REAL TIMER DS1302 DRIVER LIB

COPYRIGHT (c) 2010 BY ZYK.
-- ALL RIGHTS RESERVED --

File Name: DS1302.h
Author: ZHANG YUAN KE
Created: 2010/06/21
Modified: NO
Revision: 1.0

***************************************************************************/
#ifndef _DS1302_2010_06_21_
#define _DS1302_2010_06_21_
sbit SCLK = P1^6; //实时时钟时钟线引脚
sbit DIO = P1^7; //实时时钟数据线引脚
sbit CE = P1^5; //实时时钟复位线引脚
sbit ACC0 = ACC^0;
sbit ACC7 = ACC^7;
char sec,min,hour,day,mon,week,year;
char *tab[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat",};
void write_1302(uchar add,uchar dat)
{
uchar i;
ACC=add;
CE=0;
SCLK=0;
CE=1;
for(i=0;i<8;i++)
{
DIO=ACC0;
SCLK=1;
SCLK=0;
ACC>>=1;
}
ACC=dat;
for(i=0;i<8;i++)
{
DIO=ACC0;
SCLK=1;
SCLK=0;
ACC>>=1;
}

CE=0;
}
uchar read_1302(uchar add)
{
uchar i;
ACC=add;
CE=0;
SCLK=0;
CE=1;
for(i=8;i>0;i--) // 为什么不能for(i=0;i<8;i++) 又为什么用我写的函数不能改时间!!!!!!!!!!!!
{
DIO=ACC0;
SCLK=1;
ACC>>=1;
SCLK=0;

}

for(i=8;i>0;i--)
{ ACC>>=1;
ACC7=DIO;
SCLK=1;
SCLK=0;

}

return(ACC);
CE=0;
}
void inputbyte(uchar d) //实时时钟写入一字节(内部函数)
{
uchar i;
ACC=d;
for(i=8;i>0;i--)
{
DIO=ACC0; //相当于汇编中的 RRC
SCLK=1;
SCLK=0;
ACC>>=1;
}
}

uchar outputbyte() //实时时钟读取一字节(内部函数)
{
uchar i;
for(i=8; i>0; i--)
{
ACC=ACC>>1; //相当于汇编中的 RRC
ACC7=DIO;
SCLK=1;
SCLK=0;
}
return(ACC);
}

void write_1302(uchar add,uchar dat) //ucAddr: DS1302地址, ucData: 要写的数据
{
CE=0;
SCLK=0;
CE=1;
inputbyte(add); // 地址,命令
inputbyte(dat); // 写1Byte数据
CE=0;
}

uchar read_1302(uchar add) //读取DS1302某地址的数据
{
uchar dat;
CE=0;
SCLK=0;
CE=1;
inputbyte(add); // 地址,命令
dat=outputbyte(); // 读1Byte数据
CE=0;
return(dat);
}

void setprotect(bit flag)
{
if(flag)
write_1302(0x8e,0x80);
else
write_1302(0x8e,0x00);
}
void gettime_1302()
{
sec=read_1302(0x81);
min=read_1302(0x83);
hour=read_1302(0x85);
day=read_1302(0x87);
mon=read_1302(0x89);
week=read_1302(0x8b);
year=read_1302(0x8d);

}
void stop_time(bit flag) // 是否将时钟停止
{
unsigned char dat;
dat=read_1302(0x81);
setprotect(0);
if(flag)
write_1302(0x80, dat|0x80);
else
write_1302(0x80, dat&0x7F);
}

void init_1302()
{
uchar second=read_1302(0x81);

if(second&0x80)
write_1302(0x80,0);

}

/********************************************************************************
void BurstWrite1302(unsigned char *pWClock) //往DS1302写入时钟数据(多字节方式)
{
unsigned char i;
Write1302(0x8e,0x00); // 控制命令,WP=0,写操作?
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(0xbe); // 0xbe:时钟多字节写命令
for (i = 8; i>0; i--) //8Byte = 7Byte 时钟数据 + 1Byte 控制
{
DS1302InputByte(*pWClock); // 写1Byte数据
pWClock++;
}
DS1302_CLK = 1;
DS1302_RST = 0;
}

void BurstRead1302(unsigned char *pRClock) //读取DS1302时钟数据(时钟多字节方式)
{
unsigned char i;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(0xbf); // 0xbf:时钟多字节读命令
for (i=8; i>0; i--)
{
*pRClock = DS1302OutputByte(); // 读1Byte数据
pRClock++;
}
DS1302_CLK = 1;
DS1302_RST = 0;
}

********************************************************************************/
#endif
#include "1602.h"
#include "DS1302.h"
sbit set=P3^2;
sbit jia1=P3^0;
sbit jian1=P3^1;
uchar slect=0;

uchar bcdtoasc(uchar cha)
{
return ((cha/16)*10+(cha%16));
}
uchar asctobcd(uchar cha)
{
return ((cha/10)*16+(cha%10));
}
void disp_week()
{ switch(week)
{

case 1:{write_lcd(0xcd,tab[0]); break;}
case 2:{write_lcd(0xcd,tab[1]); break;}
case 3:{write_lcd(0xcd,tab[2]); break;}
case 4:{write_lcd(0xcd,tab[3]); break;}
case 5:{write_lcd(0xcd,tab[4]); break;}
case 6:{write_lcd(0xcd,tab[5]); break;}
case 7:{write_lcd(0xcd,tab[6]); break;}

}
}
void disp_time()
{
write_com(0x87);
write_bcd(year);
write_com(0x8a);
write_bcd(mon);
write_com(0x8d);
write_bcd(day);
write_com(0xc5);
write_bcd(hour);
write_com(0xc8);
write_bcd(min);
write_com(0xcb);
write_bcd(sec);
disp_week();
}

void keyscan()
{
set=1;
jia1=1;
jian1=1;

stop_time(1);

if(set==0)
{delay(10);
if(set==0)
{
delay(200);
slect++;
}
if(slect==8)
{slect=0; stop_time(0);write_com(0x0c);}
}

if(slect)
{

switch(slect)

{ case 1:{
write_com(0xcb);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
sec=read_1302(0x81);
sec=sec&0x7f;
sec=bcdtoasc(sec);
sec+=1;
if(sec==60)
sec=0;

sec=asctobcd(sec);
write_1302(0x80,sec);
write_com(0x0c);
write_bcd(sec);

}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);

sec=read_1302(0x81);
sec=sec&0x7f;
sec=bcdtoasc(sec);
sec-=1;
if(sec<0)sec=59;
sec=asctobcd(sec);
write_1302(0x80,sec);
write_com(0x0c);
write_bcd(sec);

}

}
break;
}

case 2:{write_com(0xc8);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
min=read_1302(0x83);
min=bcdtoasc(min);
min+=1;
if(min==60)
min=0;

min=asctobcd(min);
write_1302(0x82,min);
write_com(0x0c);
write_bcd(min);

}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);

min=read_1302(0x83);
min=bcdtoasc(min);
min-=1;
if(min<0)min=59;
min=asctobcd(min);
write_1302(0x82,min);
write_com(0x0c);
write_bcd(min);

}

}

break;}
case 3:{write_com(0xc5);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
hour=read_1302(0x85);
hour=bcdtoasc(hour);
hour+=1;
if(hour==24)
hour=0;

hour=asctobcd(hour);
write_1302(0x84,hour);
write_com(0x0c);
write_bcd(hour);

}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);

hour=read_1302(0x85);
hour=bcdtoasc(hour);
hour-=1;
if(hour<0)hour=23;
hour=asctobcd(hour);
write_1302(0x84,hour);
write_com(0x0c);
write_bcd(hour);

}

}
break;}
case 4:{write_com(0x8d);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
mon=read_1302(0x89);
year=read_1302(0x8d);
day=read_1302(0x87);
day=bcdtoasc(day);
day+=1;
year=bcdtoasc(year);
if(year%4==0)
{
if(mon==2)
{if(day==29)day=1;}
else if(day==30)day=1;
}
else{ switch(mon)
{case 1:{if(day==32)day=1;break;}
case 2:{if(day==30)day=1;break;}
case 3:{if(day==32)day=1;break;}
case 4:{if(day==31)day=1;break;}
case 5:{if(day==32)day=1;break;}
case 6:{if(day==31)day=1;break;}
case 7:{if(day==32)day=1;break;}
case 8:{if(day==32)day=1;break;}
case 9:{if(day==31)day=1;break;}
case 0x10:{if(day==32)day=1;break;}
case 0x11:{if(day==31)day=1;break;}
case 0x12:{if(day==32)day=1;break;}

}
if(mon==1|mon==3|mon==5|mon==7|mon==8|mon==0x10|mon==0x12)
if(day==32)day=0; */
if(mon==4|mon==6|mon==9|mon==0x11)
{if(day==31)day=1;}
else if(day==32)day=1;

}

day=asctobcd(day);
write_1302(0x86,day);
write_com(0x0c);
write_bcd(day);

}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);

day=read_1302(0x87);
day=bcdtoasc(day);

day-=1;
if(day<1)
{ mon=read_1302(0x89);
year=read_1302(0x8d);
switch(mon)
{ case 0x01: {day=31; break;}
case 0x02: {day=29; break;}
case 3: {day=31; break;}
case 4: {day=30; break;}
case 5: {day=31; break;}
case 6: {day=30; break;}
case 7: {day=31; break;}
case 8: {day=31; break;}
case 9: {day=30; break;}
case 0x10: {day=31; break;}
case 0x11: {day=30; break;}
case 0x12: {day=31; break;}
}
year=bcdtoasc(year);
if(year%4==0)if(mon==2)day=28;
}
day=asctobcd(day);
write_1302(0x86,day);
write_com(0x0c);
write_bcd(day);

}

}
break;}
case 5:{write_com(0x8a);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
mon=read_1302(0x89);
mon=bcdtoasc(mon);
mon+=1;
if(mon==13)
mon=1;

mon=asctobcd(mon);
write_1302(0x88,mon);
write_com(0x0c);
write_bcd(mon);

}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);

mon=read_1302(0x89);
mon=bcdtoasc(mon);
mon-=1;
if(mon<1)mon=12;
mon=asctobcd(mon);
write_1302(0x88,mon);
write_com(0x0c);
write_bcd(mon);

}

}
break;}
case 6:{write_com(0x87);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
year=read_1302(0x8d);
year=bcdtoasc(year);
year+=1;
if(year==100)
year=0;

year=asctobcd(year);
write_1302(0x8c,year);
write_com(0x0c);
write_bcd(year);

}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);

year=read_1302(0x8d);
year=bcdtoasc(year);
year-=1;
if(year<0)year=99;
year=asctobcd(year);
write_1302(0x8c,year);
write_com(0x0c);
write_bcd(year);

}

}
break;}
case 7:{write_com(0xcd);
write_com(0x0d);
if(jia1==0)
{delay(10);
if(jia1==0)
{
delay(200);
week=read_1302(0x8b);
week=bcdtoasc(week);
week+=1;
if(week==8)
week=1;

week=asctobcd(week);
write_1302(0x8a,week);
write_com(0x0c);
disp_week();
/* switch(week)
{

case 1:{write_lcd(0xcd,tab[0]); break;}
case 2:{write_lcd(0xcd,tab[1]); break;}
case 3:{write_lcd(0xcd,tab[2]); break;}
case 4:{write_lcd(0xcd,tab[3]); break;}
case 5:{write_lcd(0xcd,tab[4]); break;}
case 6:{write_lcd(0xcd,tab[5]); break;}
case 7:{write_lcd(0xcd,tab[6]); break;}

} */
}
}
if(jian1==0)
{
delay(10);
if(jian1==0)
{ delay(200);

week=read_1302(0x8b);
week=bcdtoasc(week);
week-=1;
if(week<1)week=7;
week=asctobcd(week);
write_1302(0x8a,week);
write_com(0x0c);
disp_week();
/* switch(week)
{

case 1:{write_lcd(0xcd,tab[0]); break;}
case 2:{write_lcd(0xcd,tab[1]); break;}
case 3:{write_lcd(0xcd,tab[2]); break;}
case 4:{write_lcd(0xcd,tab[3]); break;}
case 5:{write_lcd(0xcd,tab[4]); break;}
case 6:{write_lcd(0xcd,tab[5]); break;}
case 7:{write_lcd(0xcd,tab[6]); break;}

} */

}

}
break; }

}

}

}

main()
{
init();
init_1302();

write_lcd(0x80,"DATE:20 - -");
write_lcd(0x80+0x40,"TIME: : :");

while(1)
{
gettime_1302();
set=1;
if(set==0)
{delay(10);
if(set==0){delay(200);slect++;}
}
while(slect)
keyscan();
disp_time();
delay(30);
}
}

网友(2):

需要