部分程序如下:
uchar code sn1[]={"0x28,0x9D,0x7A,0x04,0x00,0x00,0x80,0xB7"};//序列号
uchar code sn2[]={"0x28,0xFF,0xBF,0xEA,0xA4,0x15,0x03,0xA3"};
uchar code sn3[]={"0x28,0xFF,0xE1,0xD0,0x89,0x16,0x03,0x16"};
void match_b20rom(uchar a) //匹配ROM
{
char j;
Ds18b20_WriteByte(0x55);
if(a==1)
{
for(j=0;j<8;j++)
Ds18b20_WriteByte(sn1[j]);
}
if(a==2)
{
for(j=0;j<8;j++)
Ds18b20_WriteByte(sn2[j]);
}
if(a==3)
{
for(j=0;j<8;j++)
Ds18b20_WriteByte(sn3[j]);
}
}
int Ds18b20_ReadTemp(uchar z)//读取温度
{
int temp0 = 0;int j;
uchar tmh, tml;
if(Ds18b20_Init()==1)
{
ds_flag=1;
return ds_flag;
}
else
{
ds_flag=0;
Ds18b20_Init() ;
if(z==1) match_b20rom(1); //选择匹配的B20
if(z==2) match_b20rom(2);
if(z==3) match_b20rom(3);
//Ds18b20_WriteByte(0xcc); //先写入转换命令跳过ROM操作命令
Ds18b20_WriteByte(0x44); //温度转换命令
j++;
j++;
Ds18b20_Init() ;
if(z==1) match_b20rom(1); //选择匹配的B20
if(z==2) match_b20rom(2);
if(z==3) match_b20rom(3);
//Ds18b20_WriteByte(0xcc); //跳过ROM操作命令
Ds18b20_WriteByte(0xbe); //发送读取温度命令
tml = Ds18b20_ReadByte(); //读取温度值共16位,先读低字节
tmh = Ds18b20_ReadByte(); //再读高字节
temp0 = tmh;
temp0 <<= 8;
temp0 |= tml;
return temp0;
}
}
void main(void)
{uint tk=0;
int i;
int tk1=0;
lcd_init();
delay1ms(50);
while(1)
{tk1=Ds18b20_ReadTemp(2);
tk=Temperature(tk1);
// read_b20sn();
if(ds_flag==0)
{
for(i=0;i<9;i++)
{write_dat(dis1[i]);}
b20display(tk) ;//显示温度
}
else
{
lcd_pos(2,0);
for(i=0;i<6;i++)
{write_dat(dis1[i]);}
}
}
}
为什么多点测温的时候就是就是出不来???
0