題目:用ds1302制作一個精密的電子時鐘感想:我看視頻的時候講的是DS12CR887這塊芯片,兩塊畢竟是不一樣的,所以,我只是看了他講怎樣看時序,之后的就沒再看了。我就拿著自己下載打印的DS1302數(shù)據(jù)手冊,研習(xí),研習(xí),再研習(xí)。還查了書上的,網(wǎng)上的,源程序。但是,你別指望著誰會把所有的東西都給你寫上去。只能作為參考。后來,我終于寫出了一個程序,可是一編譯,就漏洞百出。最主要的有兩點:for循環(huán)和BCD碼。不過還好,都得到了很好的解決。
本文引用地址:http://butianyuan.cn/article/201611/320167.htm解決方案:
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar data table[]="2009-01-01 Mon.";
uchar data table1[]=" 00:00:00";
uchar code xingqi[]="Mon. Tue. Wed. Thu. Fri. Sat. Sun.";
uchar num;
uchar hour,min,sec,week,day,month,year;
sbit ds1302_sclk=P1^4;
sbit ds1302_io=P1^5;
sbit ds1302_ce=P2^2;
sbit acc0=ACC^0;
sbit acc7=ACC^7;
sbit lcdrs=P1^7; //端口定義
sbit lcdrw=P1^6;
sbit lcde=P2^4;
sbit le=P3^6;
sbit leden=P2^5;
sbit dkle=P3^7;
void delay(uint a);
void write_byte(uchar dat)//寫入一個字節(jié)
{
uchar i;
ACC=dat;
for(i=8;i>0;i--)
{
ds1302_io=acc0;
ds1302_sclk=1;
ds1302_sclk=0;
ACC=ACC>>1;
}
}
uchar read_byte() //讀出一個字節(jié)
{
uchar i;
for(i=8;i>0;i--)
{
ACC=ACC>>1;
acc7=ds1302_io;
ds1302_sclk=1;
ds1302_sclk=0;
}
return(ACC);
}
void s_write(uchar add,uchar dat)//單字節(jié)寫入子函數(shù)
{
ds1302_ce=0;
ds1302_sclk=0;
ds1302_ce=1;
write_byte(add);
write_byte(dat);
ds1302_sclk=1;
ds1302_ce=0;
}
uchar s_read(uchar add) //單字節(jié)讀出子函數(shù)
{
uchar temp;
ds1302_ce=0;
ds1302_sclk=0;
ds1302_ce=1;
write_byte(add);
temp=read_byte();
ds1302_sclk=1;
ds1302_ce=0;
temp=(temp/0x0a)*10+temp%0x0a;
return(temp);
}
void set_ds1302(uchar *pClock)//設(shè)置ds1302的時間
{
uchar i;
uchar add=0x80;
EA=0;
s_write(0x8e,0x00);
for(i=7;i>0;i--)
{
s_write(add,*pClock);
pClock++;
add+=2;
}
s_write(0x8e,0x80);
EA=1;
}
void read_ds1302(uchar Curtime[]) //讀取ds1302的時間
{
uchar i;
uchar add=0x81;
EA=0;
for(i=7;i>0;i--)
{
Curtime[i]=s_read(add);
add+=2;
}
EA=1;
}
void write_com(uchar com) //寫命令子函數(shù)
{
lcde=0;
lcdrw=0;
lcdrs=0;
delay(1);
lcde=1;
delay(1);
P0=com;
delay(1);
lcde=0;
}
void write_data(uchar dat)//寫數(shù)據(jù)子函數(shù)
{
lcde=0;
lcdrw=0;
lcdrs=1;
delay(1);
lcde=1;
delay(1);
P0=dat;
delay(1);
lcde=0;
}
void init()
{
month=1;
day=1;
year=9;
lcde=0; //LCD1602初始化
le=0;
leden=0;
write_com(0x38);
delay(100);
write_com(0x38);
delay(50);
write_com(0x38);
delay(10);
write_com(0x08);
write_com(0x01);
write_com(0x0c);
write_com(0x80);
for(num=0;num<15;num++) //在第一行顯示“ 2000-00-00 Mon.”
{
write_data(table[num]);
}
write_com(0x80+0x40);
for(num=0;num<10;num++) //在第二行末尾顯示“ 00:00:00”
{
write_data(table1[num]);
}
}
void write_time(uchar add,uchar dat) //寫入時間子函數(shù)
{
uchar shi,ge;
shi=dat/16;
ge=dat%16;
write_com(0x80+0x40+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_riqi(uchar add,uchar dat) //寫入日期子函數(shù)
{
uchar shi,ge;
shi=dat/16;
ge=dat%16;
write_com(0x80+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_xingqi(uchar dat) //寫入星期子函數(shù)
{
write_com(0x80+0x0b);
switch(dat)
{
case 1: for(num=0;num<4;num++)
{
write_data(xingqi[num]);
};break;
case 2: for(num=5;num<9;num++)
{
write_data(xingqi[num]);
};break;
case 3: for(num=10;num<14;num++)
{
write_data(xingqi[num]);
};break;
case 4: for(num=15;num<19;num++)
{
write_data(xingqi[num]);
};break;
case 5: for(num=20;num<24;num++)
{
write_data(xingqi[num]);
};break;
case 6: for(num=25;num<29;num++)
{
write_data(xingqi[num]);
};break;
case 7: for(num=30;num<34;num++)
{
write_data(xingqi[num]);
};break;
}
}
void main()
{
init();
delay(5);
s_write(0x8e,0x00); //控制寫入WP=0
s_write(0x90,0xa5);
s_write(0x80,0x00); //秒
s_write(0x82,0x20); //分
s_write(0x84,0x09); //時24時制
s_write(0x86,0x27); //日
s_write(0x88,0x08); //月
s_write(0x8a,0x04); //星期
s_write(0x8c,0x09); //年 */
s_write(0x8e,0x80);//控制寫入WP=1;
P0=0xff;
while(1)
{
sec=s_read(0x81);
write_time(8,sec);
write_com(0x80+0x40+9);
min=s_read(0x83);
write_time(5,min);
write_com(0x80+0x40+6);
hour=s_read(0x85);
write_time(2,hour);
write_com(0x80+0x40+3);
week=s_read(0x8b);
write_xingqi(week);
write_com(0x80+0x0d);
day=s_read(0x87);
write_riqi(8,day);
write_com(0x80+9);
month=s_read(0x89);
write_riqi(5,month);
write_com(0x80+6);
year=s_read(0x8d);
write_riqi(2,year);
write_com(0x80+3);
}
}
void delay(uint a) { //延時子函數(shù)
uint i,j;
for(j=a;j>0;j--)
for(i=250;i>0;i--) ;
}
評論