我写了一个1602显示时钟,用的是DS12C887,神奇的是写年的时候,01年,02年,都可以,写为03年或者以后就所有数据出错,?????高手解释一下,那里的问题???,而且,写成2002年12月31日24:59:59让他自己跑,很顺利就进入03年了 我一头雾水啊……
新建 Microsoft Word 文档.doc
(38 KB, 下载次数: 11)
2011-11-1 18:49 上传
点击文件名下载附件
并且,还有一个问题,就是掉点之后时钟芯片似乎就不走了,比如我在20:21:30关电,过一会打开,现实的将是20:21:31,也就是说,每次重新供电是,时间都是以上次掉电时的时间为基础开始计时。这又是为什么??
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit dsrw=P1^6;
sbit dsds=P1^7;
sbit dscs=P1^4;
sbit dsas=P1^5;
sbit dsirq=P3^3;
sbit rs=P3^5;
sbit lcden=P3^4;
sbit dula=P2^6;
sbit wela=P2^7;
sbit line=P2^2; //DS18B20数据线
uint temp;
bit symbol;
uint temperature;
uchar code time1[]="20 - - ";
uchar code time2[]=" : : C";
uchar code number[]="0123456789";
uchar year,month,day,date,hour,minute,second;
void delay(uint count)
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
void dsreset() //初始化函数
{
uint i;
i=103;
line=0;
while(i>0) i--;
line=1;
i=4;
while(i>0) i--;
}
/*void tmpwritebyte(uchar a) //写字节
{
uint i,j;
bit temp1;
temp1=a;
for(j=8;j>0;j--)
{
if(temp1==0)
{
line=0;
i=8;
while(j>0) j--;
line=1;
i++;i++;
}
else
{
line=0;
i++;i++;
line=1;
i=8;
while(j>0) i--;
}
a=a>>1;
temp1=a;
}
}*/
void tmpwritebyte(uchar dat) //write a byte to ds18b20
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
line=0;
i++;i++;
line=1;
i=8;while(i>0)i--;
}
else
{
line=0; //write 0
i=8;
while(i>0)
i--;
line=1;
i++;i++;
}
}
}
bit tmpreadbit() //读一位
{
uint i;
bit a;
//line=1;
line=0;
i++;
line=1;
i++;i++;i++;i++;
a=line;
i=8;
while(i>0) i--;
return a;
}
uchar tmpread()
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
return(dat);
}
void tmpchange()
{
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0x44);
}
uint tmp() //get the temperature
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8; //two byte compose a int variable
temp=temp|a;
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
}
void write_com(uchar com) //1602
{
P0=com;
rs=0;
lcden=0;
delay(2);
lcden=1;
delay(2);
lcden=0;
}
void write_date(uchar date) //1602
{
P0=date;
rs=1;
lcden=0;
delay(2);
lcden=1;
delay(2);
lcden=0;
}
void dswrite(uchar a, uchar b)
{
dscs=0;
dsds=1;
dsrw=1;
dsas=1;
P0=a;
dsas=0;
dsrw=0;
P0=b;
dsrw=1;
dsas=1;
dscs=1;
}
uchar dsread(uchar a)
{
uchar b;
dscs=0;
dsds=1;
dsrw=1;
dsas=1;
P0=a;
dsas=0;
dsds=0;
P0=0xff;
b=P0;
dsds=1;
dsas=1;
dscs=1;
return b;
}
void dsfirst()
{
dswrite(0x0a,0x20);
dswrite(0x0b,0x26);
}
void init1602()
{
uchar i;
dula=0;
wela=0;
write_com(0x38);
delay(1);
write_com(0x0c);
delay(1);
write_com(0x06);
delay(1);
write_com(0x01);
delay(1);
write_com(0x80+0);
for(i=0;i<10;i++)
{
write_date(time1
);
}
write_com(0xc0+3);
for(i=0;i<12;i++)
{
write_date(time2);
}
}
void init_time()
{
dswrite(0,30);
dswrite(1,0);
dswrite(2,00);
dswrite(3,0);
dswrite(4,22);
dswrite(5,0);
dswrite(6,1);
dswrite(7,31);
dswrite(8,10);
dswrite(9,0); //????,写0,1,2都可以,写3以上就全乱了(包括别的位),但自己跑到3就正常
}
void display(uchar a,uchar b,uchar c,uchar d,uchar e,uchar f,uchar g,uint h)
{
uchar ge,shi;
shi=a/10;
ge=a%10;
write_com(0x80+2);
write_date(number[shi]);
write_date(number[ge]);
shi=b/10;
ge=b%10;
write_com(0x80+5);
write_date(number[shi]);
write_date(number[ge]);
shi=c/10;
ge=c%10;
write_com(0x80+8);
write_date(number[shi]);
write_date(number[ge]);
write_com(0x80+11);
switch(d)
{
case 1:write_date('M');write_date('O');write_date('N');break;
case 2:write_date('T');write_date('U');write_date('E');break;
case 3:write_date('W');write_date('E');write_date('N');break;
case 4:write_date('T');write_date('H');write_date('U');break;
case 5:write_date('F');write_date('R');write_date('I');break;
case 6:write_date('S');write_date('A');write_date('T');break;
case 7:write_date('S');write_date('U');write_date('N');break;
}
write_com(0xc0+3);
shi=e/10;
ge=e%10;
write_date(number[shi]);
write_date(number[ge]);
write_com(0xc0+6);
shi=f/10;
ge=f%10;
write_date(number[shi]);
write_date(number[ge]);
write_com(0xc0+9);
shi=g/10;
ge=g%10;
write_date(number[shi]);
write_date(number[ge]);
write_com(0xc0+12);
shi=h/100;
ge=h/10-10*shi;
write_date(number[shi]);
write_date(number[ge]);
}
void main()
{
//dsfirst();
dsreset();
init_time();
init1602();
while(1)
{
tmpchange();
delay(100);
temperature=tmp();
year=dsread(9);
month=dsread(8);
day=dsread(7);
date=dsread(6);
hour=dsread(4);
minute=dsread(2);
second=dsread(0);
display(year,month,day,date,hour,minute,second,temperature);
}
}
[ 本帖最后由 sphshine 于 2011-11-2 18:11 编辑 ]
此帖出自小平头技术问答
一周热门 更多>