Hi,
Code is working, but now when itøs getting hotter, i have a feeling it's not gererating correct.
Some fresh eyes will be good.
as write , the code working, but not sure when hot >22 degree
Code is working, but now when itøs getting hotter, i have a feeling it's not gererating correct.
Some fresh eyes will be good.
Code:
unsigned char reset(void)
{
// return ONEWIRE_ABSENT; // check
Tx_18B20;
Port_18B20 = 0;
__delay_us(DS18B20_RESET_PULSE);
Rx_18B20;
__delay_us(DS18B20_WAIT_TIME);
if (Port_18B20 == 0)
{
__delay_us(DS18B20_RESET_PULSE);
return ONEWIRE_PRESENT;
}
else
{__delay_us(DS18B20_RESET_PULSE);
return ONEWIRE_ABSENT;
}
}
void write(char WRT)
{
char i,Cmd;
Cmd = WRT;
Rx_18B20;
for(i = 0; i < 8; i++)
{
if((Cmd & (1<<i))!= 0)
{
Tx_18B20;
Port_18B20 = 0;
__delay_us(DS18B20_PULLUP_TIME);
Rx_18B20;
__delay_us(DS18B20_WAIT_TIME);
}
else
{
Tx_18B20;
Port_18B20 = 0;
__delay_us(DS18B20_WAIT_TIME);
Rx_18B20;
}
}
}
unsigned char read()
{
char i =0;
result = 0;
Rx_18B20;
for(i = 0; i < 8; i++)
{
Tx_18B20;
Port_18B20 = 0;
__delay_us(1);//DS18B20_PULLUP_TIME);
Rx_18B20;
__delay_us(2);
if(Port_18B20 != 0)
{
result |= 1<<i;
}
__delay_us(DS18B20_WAIT_TIME);
}
return result;
}
void laestemp (void)
{
oldtemp=now;
if(reset()==0)
{
write(Skip_ROM);
__delay_us(10);
write(Convert_T);
__delay_ms(DS18B20_CONV_TIME);
reset();
write(Skip_ROM);
__delay_us(10);
write(Read_scratchpad);
//reset();
lsb = read();
msb = read();
TReading = (msb<< 8) + lsb;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
Whole = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;
if (SignBit) // If its negative
{
// do things negativ ADD 5000 to avoid negative
now= 5000-((Whole*100)+Fract);
}
else
{ now=5000+((Whole*100)+Fract);
}
} // end temp read.
}