DS18S20 thermometer 0.1C resolution

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
soloman_5000
Posts: 6
Joined: 14 Oct 2011 18:21
Location: Lithuania

DS18S20 thermometer 0.1C resolution

#1 Post by soloman_5000 » 16 Feb 2012 15:22

Hey there,

I've been trying to resolve the problem of increasing the resolution of DS18S20 digital thermometer. Everything seems to be done according to the datasheet, but the fraction part still doesn't seem to be right... I'll post the code, so if anyone has managed to succeed in this or spotted something wrong, please comment. I omitted the void main and settings, assuming that this works (and it does :) ) So here are the ReadTemperature and Display_Temperature functions:

const unsigned short TEMP_RESOLUTION = 9;
unsigned TEMP_LSB, TEMP_MSB, T_H, T_L, RESERVED1, RESERVED2, COUNT_REMAIN, COUNT_PER_C, CRC;
char calculated_temperature, extended_temperature, temperature_integer, temperature_fraction;
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;


void ReadTemperature() {
Ow_Reset(&PORTC, 2); // Reset
Ow_Write(&PORTC, 2, 0xCC); // SKIP_ROM
Ow_Write(&PORTC, 2, 0x44); // CONVERT_T
Delay_us(120);

Ow_Reset(&PORTC, 2);
Ow_Write(&PORTC, 2, 0xCC); // SKIP_ROM
Ow_Write(&PORTC, 2, 0xBE); // READ_SCRATCHPAD

TEMP_LSB = Ow_Read(&PORTC, 2);
TEMP_MSB = Ow_Read(&PORTC, 2);
T_H = Ow_Read(&PORTC, 2);
T_L = Ow_Read(&PORTC, 2);
RESERVED1 = Ow_Read(&PORTC, 2);
RESERVED2 = Ow_Read(&PORTC, 2);
COUNT_REMAIN = Ow_Read(&PORTC, 2); // as said in the datasheet, these ones are crutial
COUNT_PER_C = Ow_Read(&PORTC, 2);
CRC = Ow_Read(&PORTC, 2);
// after reading all 9 bytes, I just use the ones I need...
}

void Display_Temperature() { // here I look for positive or negative temperature signs
if (TEMP_MSB == 0xFF) {
temperature_text[0] = '-';
calculated_temperature = ~TEMP_LSB + 1;
}
else{ calculated_temperature = TEMP_LSB;}

// for example the real temerature is 24.3C

temperature_integer = calculated_temperature >> RES_SHIFT; // here we have 24C. This indeed works

extended_temperature = temperature_integer - 0.25 + (COUNT_PER_C - COUNT_REMAIN)/COUNT_PER_C;
// this should be 24.3C

temperature_fraction = (extended_temperature - temperature_integer) * 10;
// here we have (24.3 - 24)*10 = 0.3*10 = 3
// the reason I added the *10 part is to get an integer number to then display it. I explained it further when I try to
// display it in GLCD...

if (extended_temperature/100) // as in MikroC example, but with a different argument
temperature_text[0] = extended_temperature/100 + 48;
else
temperature_text[0] = ' ';

temperature_text[1] = (temperature_integer/10)%10 + 48; // This part is correct
temperature_text[2] = temperature_integer%10 + 48; // this - as well

temperature_text[4] = temperature_fraction%10 + 48;
// and here I sometimes get random symbols like & or <... I understood, that if I have temperature_fraction = 0
// I get " " (space) on my GLCD... Then if I get temperature_fraction = 1, 1 is shown on my glcd... And so on...
// I assume that here so called "tables" are used to display charaters... That's why I added the *10 part when calculating // the temperature_fraction

Glcd_Write_text(temperature_text, 18, 2, 1);
}

Aleksandr.

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: DS18S20 thermometer 0.1C resolution

#2 Post by drdoug » 17 Feb 2012 05:53

You seem to be assigning a float value to a char with extended_temperature.
I am a little rusty with my coding though.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: DS18S20 thermometer 0.1C resolution

#3 Post by filip » 17 Feb 2012 12:25

Hi,

Have you tried the DS1820 example supplied with the compiler ?

Regards,
Filip.

soloman_5000
Posts: 6
Joined: 14 Oct 2011 18:21
Location: Lithuania

Re: DS18S20 thermometer 0.1C resolution

#4 Post by soloman_5000 » 17 Feb 2012 14:20

Hello, Filip.

I've used the uC compiler example and it works quite well. The only downside to it, is that it uses only 0.5C resolution. My code shoul theoretically work with 0.1C resolution... But it kind of doesn't. The integer part of the temperature is correct... But the fraction part isn't. I'll try to calculate the extended temperature and assign it to a float value and then extrude the fraction part, as drdoug mentioned.

Regards,
Aleksandr.

Andreas
Posts: 139
Joined: 05 Feb 2005 12:43
Location: Germany

Re: DS18S20 thermometer 0.1C resolution

#5 Post by Andreas » 22 Apr 2012 12:21

Hi Aleksandr,
I also had problems with calculating the fraction part properly.

Your code is:

Code: Select all

extended_temperature = temperature_integer - 0.25 + (COUNT_PER_C - COUNT_REMAIN)/COUNT_PER_C;
My code in mEPascal is:

Code: Select all

LO(RawTempINTEGER) := Ow_Read(PORTC, 1);
HI(RawTempINTEGER) := Ow_Read(PORTC, 1);
...
TrueTempREAL := (RawTempINTEGER AND %1111111111111110) / 2;    //Remove last bit and make it °C
TrueTempREAL := TrueTempREAL - 0.25 + ((16.0 - CountRemainBYTE) / 16.0); //Increase resolution
TempINTEGER := TrueTempREAL * 10;
What I don't see in your code is the removal of the last bit of the raw temperature. See spec:
Resolutions greater than 9 bits can be calculated using the data from the temperature, COUNT REMAIN and COUNT PER °C registers in the scratchpad. Note that the COUNT PER °C register is hard-wired to 16 (10h). After reading the scratchpad, the TEMP_READ value is obtained by truncating the 0.5°C bit (bit 0) from the temperature data (see Figure 2). The extended resolution temperature can then be calculated using the following equation:
I had the problem that the part "((16.0 - CountRemainBYTE) / 16.0)" resulted in an integer value, although it needs to be floating point, so I added the ".0" to make the 16.0 a float.

I also have a problem with the DS18S20. Maybe you could take a look at this thread http://www.mikroe.com/forum/viewtopic.php?f=93&t=48141

Hope it helps
Andreas
Last edited by Andreas on 22 Apr 2012 18:35, edited 1 time in total.

Nick101
Posts: 339
Joined: 06 Aug 2006 15:32
Location: Texas
Contact:

Re: DS18S20 thermometer 0.1C resolution

#6 Post by Nick101 » 22 Apr 2012 18:20

Why don't you use DS18B20. It is selectable 9 - 12 bit resolution.
Nick
[url]http://www.pic_examples.byethost3.com/[/url]

Post Reply

Return to “mikroC PRO for PIC General”