#include"reg52.h"
#include"intrins.h"
#define uchar unsigned char
***it DIO=P3^0;
***it B7=B^7;
***it A0=ACC^0;
uchar tL,tH;
void delay(uchar us)
{
while(us--);
}
bit init18b20()
{
bit w;
DIO=0; //pull low
delay(145); //pull low 880us
DIO=1; //pull High
delay(15); //pull High 90us
w=DIO; //after read state
delay(90); //Not less than 480 us
return w; //w=0,success,w=1,fail
}
uchar r18b20(void)
{
uchar i;
B=0;
for(i=0;i<8;i++)
{
B=B>>1;
DIO=0;
_nop_(); //pull low 2us
_nop_(); //12M nop 2 times,6M 1 time.
DIO=1; //pull high read
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
B7=DIO; //after 15us ,read bit to B
delay(10); // 60us
}
return B;
}
void w18b20(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
DIO=0;
_nop_();
_nop_();
DIO=dat & 0x01;
delay(5);
DIO=1;
_nop_();
dat=dat>>1;
}
}
bit converT(void)
{
if(init18b20()==0) //0 sucess
{
w18b20(0xcc);
w18b20(0x44);
return 1;
}
else //1 fail
return 0;
}
void readdata(void)
{
uchar L***,M***;
if(init18b20()==0) //sucess
{
w18b20(0xcc);
w18b20(0xbe);
L***=r18b20();
M***=r18b20();
tL=L***;
tH=M***;
}
else
{
tL=0;
tH=0;
}
}
void main()
{
uchar temp;
while(1)
{
if(convert()==1)
{
readdata();
temp=(temp1<<4) | (temp0>>4);
if(temp1 & 0x08 ) //-
{
P3=0x01;
P2=0x3F & (~temp)+1;
}
else //+
{
P3=0xff;
P2=temp;
}
}
}
}
下面程序为分屏显示
#include"reg52.h"
#include"intrins.h"
#define uchar unsigned char
***it rs=P2^0;
***it rw=P2^1;
***it en=P2^2;
***it rst =P2^3;
***it sclk=P2^4;
***it DI =P2^5;
***it ds18b20=P2^6;
***it A7=ACC^7;
***it A0=ACC^0;
***it B0=B^0;
uchar tt0[]="Time:";
uchar tt1[]="Data:";
uchar tt2[]="week:";
uchar tt3[]="temp:";
static char ch[7][3]={"Sun","Mon","Tue","Wed","Thu","Tri","Sat"};
unsigned char TT,temp1;
unsigned char temp2;
void delay(uchar us)
{
while(us--);
}
bit Reset(void)
{
bit k;
ds18b20=0;
delay(145);
ds18b20=1;
delay(10);
k=ds18b20;
delay(90);
return k;
}
//---------------------------
unsigned char ReadByte(void)
{
unsigned char j,buf=0;
for(j=0;j<8;j++)
{
buf=buf>>1;
ds18b20=0;
_nop_();
_nop_();
ds18b20=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
if(ds18b20==1)
buf|=0x80;
delay(8);
}
return buf;
}
//-------------------------------
void WriteByte(unsigned char dat)
{
unsigned char j;
for(j=0;j<8;j++)
{
if(dat&0x01)
{
ds18b20=0;
_nop_();
_nop_();
ds18b20=1;
delay(10);
}
else
{
ds18b20=0;
delay(10);
ds18b20=1;
_nop_();
_nop_();
}
dat=dat>>1;
}
}
//------------------
bit Convert(void)
{
if(Reset()==0)
{
WriteByte(0xcc);
WriteByte(0x44);
return 1;
}
else
{
return 0;
}
//---------------------------------------
void ReadFlash(void)
{
unsigned char L***,M***;
if(Reset()==0)
{
WriteByte(0xcc);
WriteByte(0xbe);
L***=ReadByte();
M***=ReadByte();
temp1=L***;
temp2=M***;
}
else
{
temp1=0;
temp2=0;
}
}
void w1602(uchar dat,bit r)
{
rs=r;
rw=0;
P3=dat;
en=0;
delay(2);
en=1;
delay(4);
en=0;
}
void init1602()
{
w1602(0x38,0);
w1602(0x06,0);
w1602(0x0c,0);
w1602(0x01,0);
}
uchar r1302(uchar addr)
{
uchar n;
rst=0;
sclk=0;
_nop_();
rst=1;
B=addr;
for(n=0;n<8;n++)
{
sclk=0;
DI=B0;
sclk=1;
B=B>>1;
}
for(n=0;n<8;n++)
{
A7=DI;
sclk=1;
ACC=ACC>>1;
sclk=0;
}
rst=0;
return(ACC);
}
void main()
{
uchar i,n;
unsigned int m;
init1602();
while(1)
{
m=1500;
while(m--)
{
w1602(0x80,0);
for(i=0;i<5;i++)
w1602(tt0,1);
w1602(0x30+(r1302(0x85)>>4 & 0x0f),1);
w1602(0x30+(r1302(0x85) & 0x0f),1);
w1602(0x2d,1);
w1602(0x30+(r1302(0x83)>>4 & 0x0f),1);
w1602(0x30+(r1302(0x83) & 0x0f),1);
w1602(0x2d,1);
w1602(0x30+(r1302(0x81)>>4 & 0x0f),1);
w1602(0x30+(r1302(0x81) & 0x0f),1);
w1602(0xc0,0);
for(i=0;i<5;i++)
w1602(tt1,1);
w1602(0x30+(r1302(0x87)>>4 & 0x0f),1);
w1602(0x30+(r1302(0x87) & 0x0f),1);
w1602(0x2d,1);
w1602(0x30+(r1302(0x89)>>4 & 0x0f),1);
w1602(0x30+(r1302(0x89) & 0x0f),1);
w1602(0x2d,1);
w1602(0x30+(r1302(0x8d)>>4 & 0x0f),1);
w1602(0x30+(r1302(0x8d) & 0x0f),1);
}
w1602(0x01,0);
m=500;
while(m--)
{
if(Convert()==1)
{
ReadFlash();
TT=(temp2<<4)|(temp1>>4);
}
w1602(0x80,0);
for(i=0;i<5;i++)
w1602(tt2,1);
for(n=0;n<3;n++)
w1602(ch[(r1302(0x8B) & 0x07)-1][n],1);
w1602(0xc0,0);
for(i=0;i<5;i++)
w1602(tt3,1);
if(temp2 & 0x80)
{
w1602(0x2d,1);
w1602(0x30+((~TT & 0x3f) +1)/10,1);
w1602(0x30+((~TT & 0x3f) +1)%10,1);
}
else
{
if(TT & 0x7F)
w1602(0x2b,1);
else
w1602(0x20,1);
w1602(0x30+TT/10,1);
w1602(0x30+TT%10,1);
}
}
w1602(0x01,0);
}
}
|