用80C51+DS1302做电子钟要加入DS1302的程序吗如何写入

用80C51+DS1302做电子钟要加入DS1302的程序吗如何写入,第1张

恩,你可以先参考别人的程序。
#include<reg52h>
#define uint unsigned int
#define uchar unsigned char
#define DS1302_SECOND 0x80
#define DS1302_MINUTE 0x82
#define DS1302_HOUR 0x84
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
sbit beep=P1^0;
sbit clk=P1^3;
sbit io=P1^2;
sbit rst=P1^1;
sbit k1=P1^4;
sbit k2=P1^5;
sbit k3=P1^6;
sbit k4=P1^7;
sbit w1=P2^7;
sbit w2=P2^6;
sbit w3=P2^5;
sbit w4=P2^4;
sbit w5=P2^3;
sbit w6=P2^2;
sbit w7=P2^1;
sbit w8=P2^0;
sbit ds=P3^7; //DS18B20的端口
char shi,fen,miao;
char month,week,date;
int year;
uchar k1num;
uchar fc,count,flag;
uchar fik,jt;
uint temp;
float tt;
uint p;
uchar code sec_7[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x46,0xbf,0xff};
void key();
//void Set_time(unsigned char k1num);
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/
DS1820相关
/
void dsinit()
{
uint i;
ds=0;
i=103;
while(i>0)i--;
ds=1;
i=4;
while(i>0)i--;
}
bit readbit()
{
uint i;
bit datas;
ds=0;i++;
ds=1;i++;i++;
datas=ds;
i=8;while(i>0)i--;
return(datas);
}
uchar read()
{
uchar i,j,datas;
datas=0;
for(i=1;i<=8;i++)
{
j=readbit();
datas=(j<<7)|(datas>>1);
}
return(datas);
}
void writbyte(uchar datas)
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=datas&0x01;
datas=datas>>1;
if(testb)
{
ds=0;
i++;i++;
ds=1;
i=8;while(i>0)i--;
}
else
{
ds=0;
i=8;while(i>0)i--;
ds=1;
i++;i++;
}
}
}
void tempconvert()
{
dsinit();
delay(1);
writbyte(0xcc);
writbyte(0x44);
}
uint readtemp()
{
uchar a,b;
dsinit();
delay(1);
writbyte(0xcc);
writbyte(0xbe);
a=read();
b=read();
temp=b;
temp<<=8;
temp=temp|a;
tt=temp00625;
temp=tt10;
return temp;
}
/
函 数 名:WriteByte()
功 能:实时时钟写入一字节
说 明:往DS1302写入1Byte数据 (内部函数)
入口参数:d 写入的数据
返 回 值:无
/

void WriteByte(uchar d)
{
uchar h;
ACC = d;
for(h=8; h>0; h--)
{
io = ACC0; /相当于汇编中的 RRC /
clk = 1;
clk = 0;
ACC = ACC >> 1;
}
}
/
函 数 名:RTOutputByte()
功 能:实时时钟读取一字节
说 明:从DS1302读取1Byte数据 (内部函数)
入口参数:无
返 回 值:ACC
/
uchar RTOutputByte(void)
{
uchar h;
for(h=8; h>0; h--)
{
ACC = ACC >>1; /相当于汇编中的 RRC /
ACC7 =io;
clk = 1;
clk = 0;
}
return ACC;
}
/
函 数 名:W1302()
功 能:往DS1302写入数据
说 明:先写地址,后写命令/数据 (内部函数)
调 用:RTInputByte() , RTOutputByte()
入口参数:ucAddr: DS1302地址, ucData: 要写的数据
返 回 值:无
/
void W1302(uchar ucAddr, uchar ucDa)
{
rst = 0;
clk = 0;
rst = 1;
WriteByte(ucAddr); // 地址,命令
WriteByte(ucDa); //写1Byte数据
clk = 1;
rst = 0;
}
/
函 数 名:R1302()
功 能:读取DS1302某地址的数据
说 明:先写地址,后读命令/数据 (内部函数)
调 用:RTInputByte() , RTOutputByte()
入口参数:ucAddr: DS1302地址
返 回 值:ucData :读取的数据
/
uchar R1302(uchar ucAddr)
{
uchar ucData;
rst = 0;
clk = 0;
rst = 1;
WriteByte(ucAddr); / 地址,命令 /
ucData = RTOutputByte(); / 读1Byte数据 /
clk = 1;
rst = 0;
return ucData;
}
void Init_Time(void)
{//设置初始时间
// AMPM(0); //0 为24小时制,1为12小时制
W1302(DS1302_SECOND,0x08);
W1302(DS1302_MINUTE,0x08);
W1302(DS1302_HOUR,0x08);
}
/void Init_DS1302(void)
{//DS1302初始化
unsigned char Second=Read1302(DS1302_SECOND);
if(Second&0x80)
DS1302_SetTime(DS1302_SECOND,0);
}
/

/读取DS1302中的时间/
void du1302()
{
// uchar yy;
miao=R1302(0x81); //读取 秒 分 时 日 月 星期 年
fen=R1302(0x83);
shi=R1302(0x85);
/ shi=shi/1610+shi%16;
fen=fen/1610+fen%16;
miao=miao/1610+miao%16;/
date=R1302(0x87);
month=R1302(0x89);
week=R1302(0x8b);
year=R1302(0x8d);
}
void display()
{
uchar ms,mg ,fs,fg,ss,sg;
uchar mons,mong,dats,datg;
uchar wks,wkg,years,yearg;
static uchar hh=0, ff=0;
ms=miao/16;
mg=miao%16;
fs=fen/16;
fg=fen%16;
ss=shi/16;
sg=shi%16;
mons=month/16;
mong=month%16;
dats=date/16;
datg=date%16;
wks=week/16;
wkg=week%16;
years=year/16;
yearg=year%16;
if(k1num==1&&ff==0)
{
years=12;yearg=12;hh++;
}
if(hh>30||ff==1) //闪烁 ,hh值越大 闪烁越慢
{
years=year/16;
yearg=year%16;
hh--;
if(hh==0)
ff=0;
else
ff=1;
}
if(k1num==2&&ff==0)
{
mons=12;mong=12;hh++;
}
if(hh>30||ff==1) //闪烁 hh值越大 闪烁越慢
{
mons=month/16;
mong=month%16;
hh--;
if(hh==0)
ff=0;
else
ff=1;
}
if(k1num==3&&ff==0)
{
dats=12;datg=12;hh++;
}
if(hh>30||ff==1)
{
dats=date/16;
datg=date%16;
hh--;
if(hh==0)
ff=0;
else
ff=1;
}
if(k1num==4&&ff==0)
{
wks=12;wkg=12;hh++;
}
if(hh>30||ff==1) //闪烁 ,hh值越大 闪烁越慢
{
wks=week/16;
wkg=week%16;
hh--;
if(hh==0)
ff=0;
else
ff=1;
}
if(k1num==5&&ff==0)
{
ss=12;sg=12;hh++;
}
if(hh>30||ff==1) //闪烁 hh值越大 闪烁越慢
{
ss=shi/16;
sg=shi%16;
hh--;
if(hh==0)
ff=0;
else
ff=1;
}
if(k1num==6&&ff==0)
{
fs=12;fg=12;hh++;
}
if(hh>30||ff==1) //闪烁 hh值越大 闪烁越慢
{
fs=fen/16;
fg=fen%16;
hh--;
if(hh==0)
ff=0;
else
ff=1;
}
if(k1num==7&&ff==0)
{
ms=12;mg=12;hh++;
}
if(hh>30||ff==1) //闪烁 hh值越大 闪烁越慢
{
ms=miao/16;
mg=miao%16;
hh--;
if(hh==0)
ff=0;
else
ff=1;
}
/显示时分秒/
if((k1num==0||k1num==5||k1num==6||k1num==7)&&(fik==0)&&jt==0)
{
P0=sec_7[mg];
w8=0;
delay(2);
w8=1;
P0=sec_7[ms];
w7=0;
delay(2);
w7=1;
P0=sec_7[fg];
w5=0;
delay(2);
w5=1;
P0=sec_7[fs];
w4=0;
delay(2);
w4=1;
P0=sec_7[sg];
w2=0;
delay(2);
w2=1;

if(ss==0&&sg!=0)ss=12;//消除小时前端的0
P0=sec_7[ss];
w1=0;
delay(2);
w1=1;
P0=sec_7[11];
w6=0;
delay(2);
w6=1;

P0=sec_7[11];
w3=0;
delay(2);
w3=1;
}
/显示日期+星期///
if((fik==1||k1num==2||k1num==3||k1num==4)||jt==2)
{
P0=sec_7[mong];
w2=0;
delay(2);
w2=1;
if(mons==0)mons=12;
P0=sec_7[mons];
w1=0;
delay(2);
w1=1;
P0=sec_7[datg];
w5=0;
delay(2);
w5=1;

if(dats==0)dats=12;
P0=sec_7[dats];
w4=0;
delay(2);
w4=1;
P0=0xf7;
w3=0;
delay(2);
w3=1;
P0=0xf7;
w6=0;
delay(2);
w6=1;
P0=0xf7;
w7=0;
delay(2);
w7=1;
if(wkg==7)(wkg=8);
P0=sec_7[wkg];
w8=0;
delay(2);
w8=1;
P0=0x7f;
w8=0;
delay(2);
w8=1;
}
/显示年 /
if((fik==2||k1num==1)||(jt==1))
{
P0=sec_7[2];
w1=0;
delay(2);
w1=1;
P0=sec_7[0];
w2=0;
delay(2);
w2=1;
P0=sec_7[years];
w3=0;
delay(2);
w3=1;
P0=sec_7[yearg];
w4=0;
delay(2);
w4=1;
P0=sec_7[11];
w5=0;
delay(2);
w5=1;
P0=sec_7[11];
w6=0;
delay(2);
w6=1;
P0=sec_7[11];
w7=0;
delay(2);
w7=1;
P0=sec_7[11];
w8=0;
delay(2);
w8=1;
}
/显示温度/
if(fik==3||jt==3)
{
P0=sec_7[p%10];
w3=0;
delay(2);
w3=1;
P0=sec_7[p%100/10];
w2=0;
delay(2);
w2=1;
P0=0x7f; //显示小数点
w2=0;
delay(2);
w2=1;
P0=sec_7[p/100];
w1=0;
delay(2);
w1=1;
P0=0x46; //显示℃ 的符号
w4=0;
delay(2);
w4=1;

}
}
void key()
{
uchar xx;
if(k1==0) //k1是调整键,按下后调整的位会闪烁。
{
delay(15);
if(k1==0)
{
beep=0;
for(xx=4;xx>0;xx--)
{
display();
}
beep=1;
fik=0; jt=0;
count=0;fc=0;
k1num++;
if(k1num==8)k1num=0;
}
while(!k1)display(); //防止按下按键盘时数码管息灭(2010年元月10号修改)
}
if(k2==0) //按K2是加时间
{
delay(15);
if(k2==0)
{
beep=0;
for(xx=4;xx>0;xx--)
{
display();
}beep=1;
count=0; fc=0; jt=0;
switch(k1num)
{
case 7:
{
miao=0;
W1302(0x8e,0x00);
W1302(0x80,0);
W1302(0x8e,0x80);
}break;
case 6:
{

fen=fen/1610+fen%16;
fen++;
if(fen==60)fen=0;
fen=fen/1016+fen%10;
W1302(0x8e,0x00);
W1302(0x82,fen);
W1302(0x8e,0x80);
}break;
case 5 :
{
shi=shi/1610+shi%16;
shi++;
if(shi==24)shi=0;
shi=shi/1016+shi%10;
W1302(0x8e,0x00);
W1302(0x84,shi);
W1302(0x8e,0x80);
}break;
case 2:
{ month=month/1610+month%16;
month++;
if(month==13)month=1;
month=month/1016+month%10;
W1302(0x8e,0x00);
W1302(0x88,month);
W1302(0x8e,0x80); }
break;
case 3:
{ date=date/1610+date%16;
date++;if(date>31)date=1;

date=date/1016+date%10;
W1302(0x8e,0x00);
W1302(0x86,date);
W1302(0x8e,0x80); }
break;
case 4:
{ week=week/1610+week%16;
week++;if(week==8)week=1;

week=week/1016+week%10;
W1302(0x8e,0x00);
W1302(0x8a,week);
W1302(0x8e,0x80); }
break;
case 1:
{ year=year/1610+year%16;
year++;if(year>99)year=0;

year=year/1016+year%10;
W1302(0x8e,0x00);
W1302(0x8c,year);
W1302(0x8e,0x80); }
break;
/ case 8:
{
shi=shi/1610+shi%16;
if(shi>12)
shi=shi-12;
if(shi==0)shi=12; /12/24小时制转换
shi=shi/1016+shi%10;
} /
case 8:
k1num=0;
break;
default:
break;
}
while(!k2)display();
}
}
if(k3==0) //K3是减 时间
{
delay(15);
if(k3==0)
{
beep=0;
for(xx=4;xx>0;xx--)
{
display();
}beep=1;
count=0; fc=0; jt=0;
if(k1num==5)
{
shi=shi/1610+shi%16;
shi--;if(shi<0)shi=23;
shi=shi/1016+shi%10;
W1302(0x8e,0x00);
W1302(0x84,shi);
W1302(0x8e,0x80);
}
if(k1num==6)
{
fen=fen/1610+fen%16;
fen--;if(fen<0)fen=59;
fen=fen/1016+fen%10;
W1302(0x8e,0x00);
W1302(0x82,fen);
W1302(0x8e,0x80);
}
if(k1num==7)
{
miao=0;
W1302(0x8e,0x00);
W1302(0x80,miao);
W1302(0x8e,0x80);
}
if(k1num==2)
{
month=month/1610+month%16;
month--;if(month==0)month=12;
month=month/1016+month%10;
W1302(0x8e,0x00);
W1302(0x88,month);
W1302(0x8e,0x80);
}
if(k1num==3)
{
date=date/1610+date%16;
date--;if(date==0)date=31;
date=date/1016+date%10;
W1302(0x8e,0x00);
W1302(0x86,date);
W1302(0x8e,0x80);
}
if(k1num==4)
{
week=week/1610+week%16;
week--;if(week==0)week=7;
week=week/1016+week%10;
W1302(0x8e,0x00);
W1302(0x8a,week);
W1302(0x8e,0x80);
}
if(k1num==1)
{
year=year/1610+year%16;
year--;if(year<0)year=99;
year=year/1016+year%10;
W1302(0x8e,0x00);
W1302(0x8c,year);
W1302(0x8e,0x80);
}
}while(!k3)display();
}
}
void main()
{
Init_Time();//设置DS1302初始时间
TMOD=0x01; //16位定时
TH0=0xa6; //25ms晶振为110592M
TL0=0x00;
EA=1;
TR0=1;
ET0=1;
P2=0xff;
beep=0;
delay(300); //开机滴一声
beep=1;
W1302(0x90,0xaa); //打开涓流充电
while(1)
{

if(k4==0)
{ delay(15);
if(k4==0)
{

fik++; if(fik==4)fik=0;
count=0;k1num=0;
fc=0;
beep=0;
delay(100);
beep=1;
}while(!k4)display();
}
tempconvert();//温度转换
display();
key();
du1302(); //获取时间
display();
//Set_time(k1num);

p=readtemp(); //读取温度
miao=miao/1610+miao%16;
// if(miao==30&&k1num==0){jt=1;fik=0;} //交替显示年3S
if(miao==30&&k1num==0){jt=2;fik=0;} //交替显示月日星期5S
if(miao==33&&k1num==0){fik=0;jt=3;} //交替显示温度
if(miao<30||miao>35)jt=0; //正常显示时间 时分秒
miao=miao/1016+miao%10;
display();
}
}
void timer_1(void) interrupt 1 //中断入口,闪烁
{
TH0=0xa6; //25ms定时 晶振24M
TL0=0x00;
if(count++==40) //1000ms
{ count=0;

if(fc++==10) //12秒后进入正常走时。
{k1num=0;fc=0;fik=0;}// flag=0;
}
}

以上就是关于用80C51+DS1302做电子钟要加入DS1302的程序吗如何写入全部的内容,包括:用80C51+DS1302做电子钟要加入DS1302的程序吗如何写入、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10174040.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存