?? ds18b20.c
字號:
#include<AT89X52.H>
#include<INTRINS.H>
unsigned char code displaybit_p[]={0xf1,0xf2,0xf4,0xf8}; //正溫度位選
//unsigned char code displaybit_close={0xf0
unsigned char code displaybit_n[]={0xd1,0xd2,0xd4,0xd8}; //負溫度位選
unsigned char code displaycode[]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e,0xff,0xbf};
unsigned char dotcode[32]={0,3,6,9,12,16,19,22,
25,28,31,34,38,41,44,48,
50,53,56,59,63,66,69,72,
75,78,81,84,88,91,94,97};
unsigned char displaycount;
unsigned char read_sign;
unsigned displaybuf[4]={16,16,16,16};
unsigned displaybuf_1[4]={16,16,16,16};
unsigned char timecount;
unsigned char readdata[8];
sbit DQ=P3^7;
bit sflag;
void show(void)
{
if(displaycount==2)
{
P1=displaycode[displaybuf[displaycount]]&0x7f;
}
else
{
P1=displaycode[displaybuf[displaycount]];
}
if(sflag==0 )
P2=displaybit_p[displaycount];
else
P2=displaybit_n[displaycount];
displaycount++;
if(displaycount==4)
{
displaycount=0;
}
}
//復位處理
bit resetpulse(void)
{
unsigned char i;
DQ=0;
for(i=255;i>0;i--);
DQ=1;
for(i=60;i>0;i--);
return(DQ) ;
for(i=200;i>0;i--);
show();
}
//寫數據到DS18B20
void writecommandtods18b20(unsigned char command)
{
unsigned char i;
unsigned char j;
for(i=0;i<8;i++)
{
if((command&0x01)==0)
{
DQ=0;
for(j=35;j>0;j--);
DQ=1;
}
else
{
DQ=0;
for(j=2;j>0;j--);
DQ=1;
for(j=33;j>0;j--);
}
command=_cror_(command,1);
show();
}
}
//主機讀數據
unsigned char readdatafromds18b20(void)
{
unsigned char i;
unsigned char j;
unsigned char temp;
temp=0;
for(i=0;i<8;i++)
{
temp=_cror_(temp,1);
DQ=0;
_nop_();
_nop_();
DQ=1;
for(j=10;j>0;j--);
if(DQ==1)
{
temp=temp|0x80;
}
else
{
temp=temp|0x00;
}
for(j=200;j>0;j--);
show();
}
return(temp);
}
void main(void)
{
unsigned int i;
TMOD=0x01;
TH0=(65536-4000)/256;
TL0=(65536-4000)%256;
ET0=1;
EA=1;
while(resetpulse());
writecommandtods18b20(0xcc);
writecommandtods18b20(0x44);
TR0=1;
while(1)
{
i++;
if(i==100)
{
// show();
i=0;
}
}
}
void t0(void) interrupt 1
{
unsigned char x;
unsigned int result;
TH0=(65536-4000)/256;
TL0=(65536-4000)%256;
if(displaycount==2)
{
P1=displaycode[displaybuf[displaycount]]&0x7f;
}
else
{
P1=displaycode[displaybuf[displaycount]];
}
if(sflag==0 )
P2=displaybit_p[displaycount];
else
P2=displaybit_n[displaycount];
displaycount++;
if(displaycount==4)
{
displaycount=0;
}
timecount++;
if(timecount==188)
{
TR0=0;
timecount=0;
while(resetpulse());
show();
writecommandtods18b20(0xcc);
show();
writecommandtods18b20(0xbe);
show();
readdata[0]=readdatafromds18b20();
show();
readdata[1]=readdatafromds18b20();
show();
for(x=0;x<4;x++)
{
displaybuf[x]=16;
}
sflag=0;
if((readdata[1]&0xf8)!=0x00)
{
sflag=1;
readdata[1]=~readdata[1];
readdata[0]=~readdata[0];
result=readdata[0]+1;
readdata[0]=result;
if(result>255)
{
readdata[1]++;
}
}
readdata[1]=readdata[1]<<4;
readdata[1]=readdata[1]&0x70;
x=readdata[0];
x=x>>4;
x=x&0x0f;
readdata[1]=readdata[1]|x;
x=2;
result=readdata[1];
while(result/10)
{
displaybuf[x]=result%10;
result=result/10;
x++;
}
displaybuf[x]=result;
x=readdata[0]&0x0f;
x=x<<1;
displaybuf[0]=(dotcode[x])%10;
displaybuf[1]=(dotcode[x]/10);
show();
while(resetpulse());
writecommandtods18b20(0xcc);
show();
writecommandtods18b20(0x44);
show();
TR0=1;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -