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 }
評論