新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > msp430單片機(jī)的ds18b20測(cè)溫度c程序

msp430單片機(jī)的ds18b20測(cè)溫度c程序

作者: 時(shí)間:2016-11-27 來源:網(wǎng)絡(luò) 收藏


void DS18B20_ReadTemp(void)
{unsigned char tempH,tempL,tempH_2,tempL_2;
DS18B20_Init();
DS18B20_Init_2();
DS18B20_WriteByte(SkipRom);
_NOP();
DS18B20_WriteByte_2(SkipRom);
_NOP();
//There is just one DS1820 on the bus;
DS18B20_WriteByte(ConvertTemperature);
DelayX10us(10);
DS18B20_WriteByte_2(ConvertTemperature);
DelayX10us(10);
//Start to convert temperature;
DS18B20_Init();
DS18B20_Init_2();
DS18B20_WriteByte(SkipRom);
_NOP();
DS18B20_WriteByte_2(SkipRom);
_NOP();
DS18B20_WriteByte(ReadScratchpad);
DS18B20_WriteByte_2(ReadScratchpad);
GetScratchpad[0]=DS18B20_ReadByte();
//Master samples the LSB temperature from the scratchpad;
GetScratchpad[1]=DS18B20_ReadByte();
//Master samples the MSB temperature from the scratchpad;
GetScratchpad[2]=DS18B20_ReadByte();
//Master samples the Th register or userbyte1 from the scratchpad;
GetScratchpad[3]=DS18B20_ReadByte();
//Master samples the Tl register or userbyte0 from the scratchpad;
GetScratchpad[4]=DS18B20_ReadByte();
//Master samples the configuration register from the scratchpad;
GetScratchpad[5]=DS18B20_ReadByte();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad[6]=DS18B20_ReadByte();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad[7]=DS18B20_ReadByte();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad[8]=DS18B20_ReadByte();
//Master samples the CRC from the scratchpad;
tempH=(GetScratchpad[1] < 4) | (GetScratchpad[0] >> 4);
tempL=(GetScratchpad[0] & 0x0f);
DS18B20_Init();

GetScratchpad_2[0]=DS18B20_ReadByte_2();
//Master samples the LSB temperature from the scratchpad;
GetScratchpad_2[1]=DS18B20_ReadByte_2();
//Master samples the MSB temperature from the scratchpad;
GetScratchpad_2[2]=DS18B20_ReadByte_2();
//Master samples the Th register or userbyte1 from the scratchpad;
GetScratchpad_2[3]=DS18B20_ReadByte_2();
//Master samples the Tl register or userbyte0 from the scratchpad;
GetScratchpad_2[4]=DS18B20_ReadByte_2();
//Master samples the configuration register from the scratchpad;
GetScratchpad_2[5]=DS18B20_ReadByte_2();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad_2[6]=DS18B20_ReadByte_2();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad_2[7]=DS18B20_ReadByte_2();
//Master samples the reservedbyte from the scratchpad;
GetScratchpad_2[8]=DS18B20_ReadByte_2();
//Master samples the CRC from the scratchpad;
tempH_2=(GetScratchpad_2[1] < 4) | (GetScratchpad_2[0] >> 4);
tempL_2=(GetScratchpad_2[0] & 0x0f);
DS18B20_Init_2();
//Issue a reset to terminate left parts;
//if(tempH & 0x80)
//{ tempH=~tempH;
//tempL=~tempL+1;
//ResultSignal=1;
////Negative temperature;
//}
ResultTemperatureH=tempH;
ResultTemperatureLL=decimalL[tempL];
ResultTemperatureLH=decimalH[tempL];

ResultTemperatureH_2=tempH_2;
ResultTemperatureLL_2=decimalL[tempL_2];
ResultTemperatureLH_2=decimalH[tempL_2];
//Result of temperature;
}//Read the byte0 and byte1 from scratchpad;



void Delay10us(void){
unsigned char i;
for (i=0; i<(SMCLK/500-3); i++);
}

//Time is accurately !!
void DelayX10us(unsigned char x10us){
unsigned int i;
for (i=0; i
Delay10us
}

上一頁(yè) 1 2 3 下一頁(yè)

關(guān)鍵詞: msp430單片機(jī)ds18b2

評(píng)論


技術(shù)專區(qū)

關(guān)閉