DS18S20 Where is the mistake?

General discussion on mikroPascal.
Post Reply
Author
Message
digitstudios
Posts: 26
Joined: 31 May 2005 13:32

DS18S20 Where is the mistake?

#1 Post by digitstudios » 19 May 2009 11:28

Hello guys.
I'm trying to read the temperature with a DS18S20 every two seconds, printing the result on a RS232 printer.
The problem is that some times (everi 15/20 readings) the result goes from 28,00 to -0,00 or 60,00 then returns to 28,00 degrees (currente temperature in my office)

Below there is the code

Code: Select all


//--------------------------------------------
       Delay_ms(500);
       Ow_Reset(PORTE,2);            // onewire reset signal
       Ow_Write(PORTE,2, $CC);       // issue SKIP ROM command to DS1820
       Ow_Write(PORTE,2, $44);       // issue CONVERT T command to DS1820
       Delay_us(120);
       i  := Ow_Reset(PORTE,2);
       Ow_Write(PORTE,2, $CC);        // issue SKIP ROM command to DS1820
       Ow_Write(PORTE,2, $BE);        // issue READ SCRATCHPAD command to DS1820
       j1 := Ow_Read(PORTE, 2);       // get result
       j2 := Ow_Read(PORTE, 2);       // get result

       if j2 = $FF then
        begin
         tmp_sign := '-';              // temperature sign
         j1 := j1 or $FF;              // complement of two
         j1 := j1 + $01;
        end
       else
         tmp_sign := ' ';

       j2 := (j1 and $01)*5;          // get decimal value
       j1 := j1 shr 1;                // get temp value

       BEETEXT:='Tmp2    0.0000';

       ByteToStr(j1, text);           // whole number
       BEETEXT[7]:= tmp_sign;
       BEETEXT[8]:= text[1];
       BEETEXT[9]:= text[2];
       BEETEXT[10]:= '.';
       ByteToStr(j2, text);           // decimal
       BEETEXT[11]:=text[2];

       Usart_Write_Text(BEETEXT);
       USART_Write(13); USART_Write(10);Delay_500us;

//--------------------------------------------

Any ideas?
Thanks in advance
Gianluca Colombo

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#2 Post by yo2lio » 19 May 2009 13:10

Hello Gianluca,

Code: Select all

//--------------------------------------------
       Delay_ms(250);
       Ow_Reset(PORTE,2);            // onewire reset signal
       Ow_Write(PORTE,2, $CC);       // issue SKIP ROM command to DS1820
       Ow_Write(PORTE,2, $44);       // issue CONVERT T command to DS1820
       Delay_ms(750);           // wait for conversion complete
       i  := Ow_Reset(PORTE,2);
       Ow_Write(PORTE,2, $CC);        // issue SKIP ROM command to DS1820
       Ow_Write(PORTE,2, $BE);        // issue READ SCRATCHPAD command to DS1820
       Delay_us(120);
       j1 := Ow_Read(PORTE, 2);       // get result
       j2 := Ow_Read(PORTE, 2);       // get result

       if j2 = $FF then
        begin
         tmp_sign := '-';              // temperature sign
         j1 := j1 or $FF;              // complement of two
         j1 := j1 + $01;
        end
       else
         tmp_sign := ' ';

       j2 := (j1 and $01)*5;          // get decimal value
       j1 := j1 shr 1;                // get temp value

       BEETEXT:='Tmp2    0.0000';

       ByteToStr(j1, text);           // whole number
       BEETEXT[7]:= tmp_sign;
       BEETEXT[8]:= text[1];
       BEETEXT[9]:= text[2];
       BEETEXT[10]:= '.';
       ByteToStr(j2, text);           // decimal
       BEETEXT[11]:=text[2];

       Usart_Write_Text(BEETEXT);
       USART_Write(13); USART_Write(10);
//-------------------------------------------- 
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

digitstudios
Posts: 26
Joined: 31 May 2005 13:32

#3 Post by digitstudios » 19 May 2009 13:53

Hi Florin, with your sample, the over temperature was disappeared, but unfortunately sometime the -0.00 still appears :(
Gianluca Colombo

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#4 Post by Dany » 19 May 2009 15:59

Seems that sometimes the DS1820 is read wrongly.
Are you using long non shielded wires between PIC and DS?
Another possibility is perhaps reading the whole scrathpad (not only 2 bytes), calculate if the checksum is correct, and only accept the result if the checksum is correct (see http://users.edpnet.be/rosseel01/DRO/PI ... 0_CRC.ppas).
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

rainer
Posts: 320
Joined: 07 Dec 2006 11:00
Location: Vienna, Austria
Contact:

#5 Post by rainer » 20 May 2009 07:55

Hi!
Instead of calculating CRCs and so forth, I used always this simple method:

I accept only changes in readings of +/-2°C. If the change is bigger, I ignore it. This works fine and filters out some accidental mis-readings. But reading checksum is surely more reliable.

digitstudios
Posts: 26
Joined: 31 May 2005 13:32

#6 Post by digitstudios » 20 May 2009 08:03

rainer wrote:Hi!
Instead of calculating CRCs and so forth, I used always this simple method:

I accept only changes in readings of +/-2°C. If the change is bigger, I ignore it. This works fine and filters out some accidental mis-readings. But reading checksum is surely more reliable.
I agree with you, yesterday I tryed with a smaller tollerance than yours (+/-1°C) and it seems work fine at the moment
Gianluca Colombo

rainer
Posts: 320
Joined: 07 Dec 2006 11:00
Location: Vienna, Austria
Contact:

#7 Post by rainer » 20 May 2009 08:08

How long is the cable to the sensor?
What is the value of the pull-up resistor?
Do you use phantom powering or a 3-wires connection with separate power?
You hopefully do not use a 2-wires shielded cable and use the shield as ground? No current shall flow over a shield; if you do, you transfer a lot of noise into the signal wires.

It also can help to put a capacitor at the DS18S20 over the power pins. This removes some voltage spikes.

digitstudios
Posts: 26
Joined: 31 May 2005 13:32

#8 Post by digitstudios » 20 May 2009 08:56

rainer wrote:How long is the cable to the sensor?
What is the value of the pull-up resistor?
Do you use phantom powering or a 3-wires connection with separate power?
You hopefully do not use a 2-wires shielded cable and use the shield as ground? No current shall flow over a shield; if you do, you transfer a lot of noise into the signal wires.

It also can help to put a capacitor at the DS18S20 over the power pins. This removes some voltage spikes.
Unfortunately i'm using EasyPIC5 board for the test ,so the DS1820 is fixed on board directly :(
Gianluca Colombo

Post Reply

Return to “mikroPascal General”