electronics forum

Rules | Recent posts | topic RSS | Search | Register  | Log in

DS1820 Temp Sensor giving odd but coherant values


Post new topic  Reply to topic    EDAboard.com Forum Index -> Microcontrollers -> DS1820 Temp Sensor giving odd but coherant values
Author Message
serjts



Joined: 24 Jul 2009
Posts: 6


Post02 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


Post02 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


Post02 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
Google Adsense




Post02 Oct 2009 15:44   

Ads




Back to top
ChrisC



Joined: 30 Sep 2009
Posts: 34
Helped: 8
Location: London, UK


Post02 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


Post02 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


Post02 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
Arabic versionBulgarian versionCatalan versionCzech versionDanish versionGerman versionGreek versionEnglish versionSpanish versionFinnish versionFrench versionHindi versionCroatian versionIndonesian versionItalian versionHebrew versionJapanese versionKorean versionLithuanian versionLatvian versionDutch versionNorwegian versionPolish versionPortuguese versionRomanian versionRussian versionSlovak versionSlovenian versionSerbian versionSwedish versionTagalog versionUkrainian versionVietnamese versionChinese version
Post new topic  Reply to topic    EDAboard.com Forum Index -> Microcontrollers -> DS1820 Temp Sensor giving odd but coherant values
Page 1 of 1 All times are GMT + 1 Hour
Similar topics:
ds1820 temp sensor without microcontroller (2)
Very simple but odd battery charger. Need assistance. (3)
atmega8L avr with temp sensor (1)
Read temp with DS18S20 temperature sensor (6)
is it possible to interface TMP100 temp sensor to LPC2129??? (2)
Dallas DS1821 TEmp Sensor interface with 89C51 (3)
the LM35 temp. sensor - package gets extremely heated (3)
Pressure measurement values difference - electronic sensor (3)
DS1820 (1)
Power from power source - average values or the RMS values? (2)


Abuse || Administrator || Moderators || Support us || sitemap
topic RSS