| Author |
Message |
serjts
Joined: 24 Jul 2009 Posts: 6
|
02 Oct 2009 11:33 DS1820 Temp Sensor giving odd but coherant values |
|
|
|
|
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)
| 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;
} |
|
|
| Back to top |
|
 |
FvM
Joined: 22 Jan 2008 Posts: 5160 Helped: 767 Location: Bochum, Germany
|
02 Oct 2009 13:22 Re: DS1820 Temp Sensor giving odd but coherant values |
|
|
|
|
Long post, simple explanation.
The results don't show absurdly high values. You just ignored the fact, that the sensor has 12 Bit output data and you must consider the MSB value as well. I guess, the measured temperature is actually increasing from 24.5 to 30 °C (with a truncated value of 1 in the MSB). Read the datasheet.
As a good news, you apparently succeded in managing the one-wire protocol, in principle.
|
|
| Back to top |
|
 |
serjts
Joined: 24 Jul 2009 Posts: 6
|
02 Oct 2009 15:44 DS1820 Temp Sensor giving odd but coherant values |
|
|
|
|
Datasheet Here
The sensor has a default 9-bit resolution, a higher resolution is possible using the counter register as indicated in page 3.
Considering the 9-bit resolution, the datasheet specifies that the LSByte holds the temperature value from 2^-1 to 2^6 (lsbit to msbit), and the MSByte holds only the sign in all of its beats, so it is in two-s complement format.
I am sorry but i still don't understand were I am doing wrong.
|
|
| Back to top |
|
 |
Google AdSense

|
02 Oct 2009 15:44 Ads |
|
|
|
|
|
|
| Back to top |
|
 |
ChrisC
Joined: 30 Sep 2009 Posts: 34 Helped: 8 Location: London, UK
|
02 Oct 2009 16:13 DS1820 Temp Sensor giving odd but coherant values |
|
|
|
|
Your code only waits 500us (assuming the Delay_100us(5) call is as self-explanatory as it seems) after issuing the 0x44 command byte, but the datasheet lists Tconv as being up to 750ms, so you're not giving the sensor anything like sufficient time to complete the conversion before you try reading the value back.
I presume you're not using parasitic power mode on the sensor?
|
|
| Back to top |
|
 |
serjts
Joined: 24 Jul 2009 Posts: 6
|
02 Oct 2009 17:09 DS1820 Temp Sensor giving odd but coherant values |
|
|
|
|
| Good observation, but unfortunately even waiting a milisecond doesn't change the result. Unfortunately the local hardware store doens't have this part because I am seriously thinking it's damaged..
|
|
| Back to top |
|
 |
ChrisC
Joined: 30 Sep 2009 Posts: 34 Helped: 8 Location: London, UK
|
02 Oct 2009 20:14 Re: DS1820 Temp Sensor giving odd but coherant values |
|
|
|
|
| serjts wrote: |
| Good observation, but unfortunately even waiting a milisecond doesn't change the result. |
If that was a typo and you meant to write "waiting a second", then fair enough. On the other hand, if you really did just wait 1ms, then it's still nowhere near long enough - note the units for Tconv...
And again, are you using parasitic power or not?
|
|
| Back to top |
|
 |