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.
DS18S20 thermometer 0.1C resolution
-
- Posts: 6
- Joined: 14 Oct 2011 18:21
- Location: Lithuania
Re: DS18S20 thermometer 0.1C resolution
You seem to be assigning a float value to a char with extended_temperature.
I am a little rusty with my coding though.
I am a little rusty with my coding though.
Re: DS18S20 thermometer 0.1C resolution
Hi,
Have you tried the DS1820 example supplied with the compiler ?
Regards,
Filip.
Have you tried the DS1820 example supplied with the compiler ?
Regards,
Filip.
-
- Posts: 6
- Joined: 14 Oct 2011 18:21
- Location: Lithuania
Re: DS18S20 thermometer 0.1C resolution
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.
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.
Re: DS18S20 thermometer 0.1C resolution
Hi Aleksandr,
I also had problems with calculating the fraction part properly.
Your code is:
My code in mEPascal is:
What I don't see in your code is the removal of the last bit of the raw temperature. See spec:
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
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;
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;
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.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 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.
Re: DS18S20 thermometer 0.1C resolution
Why don't you use DS18B20. It is selectable 9 - 12 bit resolution.
Nick
[url]http://www.pic_examples.byethost3.com/[/url]
[url]http://www.pic_examples.byethost3.com/[/url]