DS18B20程序有点错误,求指教
时间:10-02
整理:3721RD
点击:
#include<REG51.H>
#include<intrins.h>
#define Disdata p0
#define Discan p2
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P2^3;
sbit DIN=P0^7;
uint h;
uchar flag;
uchar code ditab[16]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
uchar code dis_7[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
uchar code scan_con[4]={0x7f,0xbf,0xdf,0xef};
uchar data temp_data[2]={0x00,0x00};
uchar data display[5]={0x00,0x00,0x00,0x00};
void delay(uint t)
{
for(;t>0,t--);
}
scan()
{
char k;
for(k=0;k<4;k++)
{
disdata=0xff;
disdata=dia_7[display[k]];
if(k==1){DIN=0;}
discan=scan_con[k];delay(90);
discan=0xff;
}
}
ow_rest(void)
{
char presence=1;
while(presence)
{
while(presence)
{
DQ=1;_nop_();_nop_();
DQ=0;
delay(50);
DQ=1;
delay(6);
presence=DQ;
}
delay(45);
presence = ~DQ;
}
DQ=1;
}
Void write_byte(uchar val)
{
uchar i;
for (i=8,i>0,i--)
{
DQ=1;_nop_();_nop_();
DQ=0;_nop_();_nop_(); _nop_();_nop_();_nop_();
DQ=val&0x01;
delay(6);
val=val/2;
}
DQ=1;
delay(1);
}
uchar read_byte(void)
{
uchar i;
uchar value=0;
for (i=8,i>0,i--)
{
DQ=1;_nop_();_nop_();
value>>=1;
DQ=0;_nop_();_nop_(); _nop_();_nop_();
DQ=1;_nop_();_nop_(); _nop_();_nop_();
if(DQ)value=ox80;
delay(6);
}
DQ=1;
return(value);
}
read_temp()
{
ow_reset();
write_byte(0xCC);
write_byte(0xBE);
temp_data[0]=read_byte();
temp_data[1]=read_byte();
ow_reste();
write_bute(0xCC);
write_bute(0x44);
}
Void work_temp()
{
uchar n=0;
uchar doth,datl;
uchar flag3=1,flag2=1;
if((temp_data[1]&oxf8)!=0x00)
{
temp_data[1]= ~(temp_data[1]);
temp_data[0]= ~(temp_data[0])+1;
n=1;
flag=1;
}
if(temp_data[0]>255)
{
temp_data[1]++;
}
display[4]=temp_data[0]&0x0f;
display[0]=ditab[display[4]];
doth=display[0]/10;
dotl=display[0]%10;
display[4]=((temp_data[0]&0xf0)>>4)|((temp_data[1]&0x07)<<4);
display[3]=display[4]/100;
display[2]=display[4]/10%10;
display[1]=display[4]%10;
if(!display[3])
{
display[3]=0x0a;
flag3=0 ;
if(!display[2])
{
display[2]=0x0a;
flag2=0;
}
}
if(n)
{
display[3]=0x0b;
flag3=0;
}
}
main()
{
disdata=0xff;
discan=0xff;
for(h=o;h<4;h++){display[h]=8;}
ow_reset();
write_byte(0xcc);
write_byte(0x44);
for(h=0;h<500;h++)
{scan();}
while(1)
{
read_temp();
work_temp();
scan();
}
}
采用4位LED显示的DS18B20温度测量!帮忙找找错误
#include<intrins.h>
#define Disdata p0
#define Discan p2
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P2^3;
sbit DIN=P0^7;
uint h;
uchar flag;
uchar code ditab[16]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
uchar code dis_7[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
uchar code scan_con[4]={0x7f,0xbf,0xdf,0xef};
uchar data temp_data[2]={0x00,0x00};
uchar data display[5]={0x00,0x00,0x00,0x00};
void delay(uint t)
{
for(;t>0,t--);
}
scan()
{
char k;
for(k=0;k<4;k++)
{
disdata=0xff;
disdata=dia_7[display[k]];
if(k==1){DIN=0;}
discan=scan_con[k];delay(90);
discan=0xff;
}
}
ow_rest(void)
{
char presence=1;
while(presence)
{
while(presence)
{
DQ=1;_nop_();_nop_();
DQ=0;
delay(50);
DQ=1;
delay(6);
presence=DQ;
}
delay(45);
presence = ~DQ;
}
DQ=1;
}
Void write_byte(uchar val)
{
uchar i;
for (i=8,i>0,i--)
{
DQ=1;_nop_();_nop_();
DQ=0;_nop_();_nop_(); _nop_();_nop_();_nop_();
DQ=val&0x01;
delay(6);
val=val/2;
}
DQ=1;
delay(1);
}
uchar read_byte(void)
{
uchar i;
uchar value=0;
for (i=8,i>0,i--)
{
DQ=1;_nop_();_nop_();
value>>=1;
DQ=0;_nop_();_nop_(); _nop_();_nop_();
DQ=1;_nop_();_nop_(); _nop_();_nop_();
if(DQ)value=ox80;
delay(6);
}
DQ=1;
return(value);
}
read_temp()
{
ow_reset();
write_byte(0xCC);
write_byte(0xBE);
temp_data[0]=read_byte();
temp_data[1]=read_byte();
ow_reste();
write_bute(0xCC);
write_bute(0x44);
}
Void work_temp()
{
uchar n=0;
uchar doth,datl;
uchar flag3=1,flag2=1;
if((temp_data[1]&oxf8)!=0x00)
{
temp_data[1]= ~(temp_data[1]);
temp_data[0]= ~(temp_data[0])+1;
n=1;
flag=1;
}
if(temp_data[0]>255)
{
temp_data[1]++;
}
display[4]=temp_data[0]&0x0f;
display[0]=ditab[display[4]];
doth=display[0]/10;
dotl=display[0]%10;
display[4]=((temp_data[0]&0xf0)>>4)|((temp_data[1]&0x07)<<4);
display[3]=display[4]/100;
display[2]=display[4]/10%10;
display[1]=display[4]%10;
if(!display[3])
{
display[3]=0x0a;
flag3=0 ;
if(!display[2])
{
display[2]=0x0a;
flag2=0;
}
}
if(n)
{
display[3]=0x0b;
flag3=0;
}
}
main()
{
disdata=0xff;
discan=0xff;
for(h=o;h<4;h++){display[h]=8;}
ow_reset();
write_byte(0xcc);
write_byte(0x44);
for(h=0;h<500;h++)
{scan();}
while(1)
{
read_temp();
work_temp();
scan();
}
}
采用4位LED显示的DS18B20温度测量!帮忙找找错误
代码好长啊!
不长了,编译出现discan错误
