serjts
Newbie level 4
I am interfacing a DS1820 one-wire temperature sensor with a at89s8253 uC. My problem is that the DS1820 sensor give absurdly high temperature values. I know i am reading the write register because when i hold the sensor the display (lcd) value increases correspondigly.
This is an example of the contents of the LSB of the temperature value:
DEC BIN
136 1000 1XXX <- This is when i hold the sensor with fingers. The X's are 0's
144 1001 0XXX
152 1001 1XXX
160 1010 0XXX
168 1010 1XXX
176 1011 0XXX
184 1011 1XXX
192 1100 0XXX
200 1100 1XXX
208 1101 0XXX
216 1101 1XXX
224 1110 0XXX
It seams the 3lsbits are always 0. I´ve tried writing to the TL and TH registers and reading them back and the values come out ok. I've also read the COUNT_PER_C register and as expected it reads out correct with the value 0x10.
Maybe the sensor is damaged?
This is the code (sorry i've completely re-wrote the routines in a desperate try ;P)
This is an example of the contents of the LSB of the temperature value:
DEC BIN
136 1000 1XXX <- This is when i hold the sensor with fingers. The X's are 0's
144 1001 0XXX
152 1001 1XXX
160 1010 0XXX
168 1010 1XXX
176 1011 0XXX
184 1011 1XXX
192 1100 0XXX
200 1100 1XXX
208 1101 0XXX
216 1101 1XXX
224 1110 0XXX
It seams the 3lsbits are always 0. I´ve tried writing to the TL and TH registers and reading them back and the values come out ok. I've also read the COUNT_PER_C register and as expected it reads out correct with the value 0x10.
Maybe the sensor is damaged?
This is the code (sorry i've completely re-wrote the routines in a desperate try ;P)
Code:
/** D E C L A R A T I O N S **************************************************/
unsigned char ow_reset(void)
{
unsigned char presence;
presence = 1;
DQ = 0;
Delay_100us(6); // Leave low for atleast 480us
DQ = 1;
Delay_10us(); // Wait for presence
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
if (DQ == 1)
{
presence = 0;
}
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
if (DQ == 1)
{
presence = 0;
}
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
if (DQ == 1)
{
presence = 0;
}
Delay_100us(5); // wait for end of timeslot
DQ = 1;
return presence; // 0==presence
}
unsigned char ow_readbit(void)
{
unsigned char read_val;
read_val = 1;
DQ = 0; // Read time-slot
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
DQ = 1;
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
if (DQ == 0)
{
read_val = 0;
}
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
if (DQ == 0)
{
read_val = 0;
}
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
return read_val;
}
void ow_writebit(unsigned char ow_bit)
{
DQ = 0; // Write time-slot
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
if (ow_bit)
{
DQ = 1;
}
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
DQ = 1;
return;
}
unsigned char ow_readbyte(void)
{
unsigned char i;
unsigned char value;
value = 0;
for (i=0;i<8;i++)
{
if (ow_readbit())
{
value |= (1 << i);
}
Delay_10us();
}
return value;
}
void ow_writebyte(unsigned char ow_byte)
{
unsigned char i, temp;
for (i=0;i<8;i++)
{
temp = ow_byte >> i;
temp &= 0x01;
ow_writebit(temp);
Delay_10us();
}
return;
}
unsigned char ow_readtemp(void)
{
unsigned char temp_lsb, temp_msb, count_rem, count_c;
// if (ow_reset())
// {
// return 1;//OW_NOT_PRESENT;
// }
//
// ow_writebyte(0xCC); // SKIP ROM
//
// ow_writebyte(0x4E); // WRITE SCHPAD
//
// ow_writebyte(0x70);
//
// ow_writebyte(0x00);
// if (ow_reset())
// {
// return 1;//OW_NOT_PRESENT;
// }
//
// ow_writebyte(0xCC); // Skip ROM
// Delay_10us();
// ow_writebyte(0x48); // Copy SCHPAD
// Delay_10us();
if (ow_reset())
{
return 1;//OW_NOT_PRESENT;
}
ow_writebyte(0xCC); // SKIP ROM
Delay_10us();
ow_writebyte(0x44); // CONVERT T
Delay_100us(5);
// while(!ow_readbit());
if (ow_reset())
{
return 1;//OW_NOT_PRESENT;
}
ow_writebyte(0xCC); // SKIP ROM
Delay_10us();
ow_writebyte(0xBE); // Read Scratchpad
Delay_10us();
temp_lsb = ow_readbyte();
temp_msb = ow_readbyte();
ow_readbyte();
ow_readbyte();
ow_readbyte();
ow_readbyte();
count_rem = ow_readbyte();
count_c = ow_readbyte();
// if ((temp_lsb & 0x01) != 0x00)
// {
// temperatura = (temp_lsb >> 1) + 1;
// }
// else
// {
// temperatura = (temp_lsb >> 1);
// }
temperatura = temp_lsb;
return 0;
}