单片机读取DS18B20温度失败,显示“0001”。
图片如下:
2.5ms的定时时标,2.5ms调用一次以下代码及刷新一次显示,代码如下:
tWord v_Ds18b20ReadTemp_f( void )
{
tWord TempH, TempL ;
tWord ReturnTemp ;
tByte Temp_G=0;
if (++Temp_G == 6)
{
Temp_G = 0;
}
switch (Temp_G)
{
case 0:
{
v_Ds18b20Init_f() ;
break;
}
case 1:
{
_nop_();
v_Ds18b20Write_f( 0xcc ) ; //????ROM
break;
}
case 2:
{
v_Ds18b20Write_f( 0x44 ) ;
break;
}
case 3:
{
v_Ds18b20Init_f() ;
break;
}
case 4:
{
_nop_();
v_Ds18b20Write_f( 0xcc ) ;
break;
}
case 5:
{
v_Ds18b20Write_f( 0xbe ) ;
}
}
TempL = v_Ds18b20Read_f() ;
TempH = v_Ds18b20Read_f() ;
ReturnTemp = TempH ;
ReturnTemp <<= 8 ;
ReturnTemp |= TempL ;
return ReturnTemp ;
}
复制代码
void v_TemperatureUpdate_f( void )
{
tWord Tflag = 0 ;
tWord TempDat ,a,b,c;
float Temp ;
TempDat = v_Ds18b20ReadTemp_f() ;
if( TempDat & 0xf000 )
{
Tflag = 1 ;
TempDat = ~TempDat + 1 ;
}
Temp = TempDat *0.0625;
TempDat = Temp * 10+0.5 ;
a=TempDat / 100;
b=TempDat % 100 ;
b=b/ 10 ;
c=TempDat% 10 ;
Temperature[ 3 ] = 0x3F ;
Temperature[ 2 ] = LED_Table_G[a] ;
Temperature[ 1 ] = LED_Table_G[b] ;
Temperature[ 0 ] = LED_Table_G[c] ;
}
复制代码
转载
|