BCSCTL1 &= "XT2OFF;//XT2 = HF XTAL
do {
IFG1 &= "OFIFG;//Clear OSCFault flag
for (i=0xFF;i>0;i--);//Time for flag to set
}while((IFG1&OFIFG));//OSCFault flag still set?
BCSCTL2 |= (SELM1 + SELS);//MCLK = SMCLK = XT2
// ow output
DelayX10us(48); // End of timeslot total 480us;
return(result); // any 1 wire device ?result:=1 no devide; ?result:=0 have device;
}//Intialization the 1-wire devices;
unsigned char DS18B20_ReadBit(void){
unsigned char result;
DS18B20_DIR |= DS18B20_DQ; // ow output
DS18B20_OUT &= "DS18B20_DQ; // DS18B20_DQ=0;
_NOP(); // Start of timeslot;
DS18B20_OUT |= DS18B20_DQ; // DS18B20_DQ=1;
_NOP();_NOP();_NOP();_NOP();
// Wait from the start;
DS18B20_DIR &= "DS18B20_DQ; // ow input
result = DS18B20_IN & DS18B20_DQ;
DS18B20_DIR |= DS18B20_DQ; // ow output
return(result); // return the result of the 1-wire devide;
}//Read a bit on the 1-wire bus;
void DS18B20_WriteBit(unsigned char oww_dat){
DS18B20_DIR |= DS18B20_DQ; // ow output
DS18B20_OUT &= "DS18B20_DQ; // DS18B20_DQ=0;
if (1 == oww_dat)
DS18B20_OUT |= DS18B20_DQ; // DS18B20_DQ=1;
DelayX10us(10);
// Remain the state for 100us;
DS18B20_OUT |= DS18B20_DQ; // DS18B20_DQ=1;
}//Write a bit to the 1-wire bus;
unsigned char DS18B20_ReadByte(void){
unsigned char i;
unsigned char result="0";
for(i = 0; i [/td][/tr][/table]ult); // return the result of the 1-wire device;
}//Read a byte from the 1-wire bus;
void DS18B20_WriteByte(unsigned char oww_dat){
unsigned char i,temp;
for(i = 0; i > i;
temp &= 0x01;
DS18B20_WriteBit(temp);
}
DelayX10us(7); // ??
}//Write a byte to the 1-wire bus;
void DS18B20_ReadTemp(void)
{unsigned char tempH,tempL;
DS18B20_Init();
DS18B20_WriteByte(SkipRom);
_NOP();
//There is just one DS1820 on the bus;
DS18B20_WriteByte(ConvertTemperature);
DelayX10us(10);
//Start to convert temperature;
DS18B20_Init();
DS18B20_WriteByte(SkipRom);
_NOP();
DS18B20_WriteByte(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 sa
mples 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);
tempL=(GetScratchpad[0] & 0x0f);
DS18B20_Init();
&nbs
p;//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];
//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<X10US; i++)
Delay10us();
}