Cant read temperature from DS18B20

General discussion on mikroC PRO for PIC.
Author
Message
xpolhecx
Posts: 110
Joined: 24 Feb 2011 16:09

Cant read temperature from DS18B20

#1 Post by xpolhecx » 20 Mar 2011 20:09

Hi,

I know there is a lot of examples on how to connect and read temperature from DS18B20 sensor. I'm using code from mikroc example library and i always get "000.5000" as result on my GLCD. Can someone please look at my code because i have no idea whats wrong :? I am using PIC18F4550, my sensor is connected to PORTC.F0

Image

Code: Select all

char GLCD_DataPort at PORTD;
sbit GLCD_CS1 at RB1_bit;
sbit GLCD_CS2 at RB0_bit;
sbit GLCD_RS at RB2_bit;
sbit GLCD_RW at RB3_bit;
sbit GLCD_EN at RB4_bit;
sbit GLCD_RST at RB5_bit;

sbit GLCD_CS1_Direction at TRISB1_bit;
sbit GLCD_CS2_Direction at TRISB0_bit;
sbit GLCD_RS_Direction at TRISB2_bit;
sbit GLCD_RW_Direction at TRISB3_bit;
sbit GLCD_EN_Direction at TRISB4_bit;
sbit GLCD_RST_Direction at TRISB5_bit;

const unsigned short TEMP_RESOLUTION = 9;
char *text = "000.0000";
unsigned temp;

void Display_Temperature(unsigned int temp2write) {
  const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
  char temp_whole;
  unsigned int temp_fraction;

  // check if temperature is negative
  if (temp2write & 0x8000) {
     text[0] = '-';
     temp2write = ~temp2write + 1;
     }

  // extract temp_whole
  temp_whole = temp2write >> RES_SHIFT;

  // convert temp_whole to characters
  if (temp_whole/100)
     text[0] = temp_whole/100  + 48;
  else
     text[0] = '0';

  text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit
  text[2] =  temp_whole%10     + 48;             // Extract ones digit

  // extract temp_fraction and convert it to unsigned int
  temp_fraction  = temp2write << (4-RES_SHIFT);
  temp_fraction &= 0x000F;
  temp_fraction *= 625;

  // convert temp_fraction to characters
  text[4] =  temp_fraction/1000    + 48;         // Extract thousands digit
  text[5] = (temp_fraction/100)%10 + 48;         // Extract hundreds digit
  text[6] = (temp_fraction/10)%10  + 48;         // Extract tens digit
  text[7] =  temp_fraction%10      + 48;         // Extract ones digit

  // print temperature on LCD
  //Lcd_Out(2, 5, text);
	Glcd_Write_Text(text, 0, 0, 1);
}

void main() {
  ADCON1 = 0x0F;
  CMCON  = 7;
  TRISA = 0x00; 				// PORTA as output (one LED on PORTA.F0)
  //TRISC = 0xFF;
  PORTA.F0 = 1;
  Glcd_Init();
  Glcd_Fill(0x00);

	while(1) {

    //--- perform temperature reading
    Ow_Reset(&PORTC, 0);                         // Onewire reset signal
    Ow_Write(&PORTC, 0, 0xCC);                   // Issue command SKIP_ROM
    Ow_Write(&PORTC, 0, 0x44);                   // Issue command CONVERT_T
    Delay_us(120);

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

    temp =  Ow_Read(&PORTC, 0);
    temp = (Ow_Read(&PORTC, 0) << 8) + temp;

    //--- Format and display result on Lcd
    Display_Temperature(temp);
    Delay_ms(500);
	}
}

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

Re: Cant read temperature from DS18B20

#2 Post by drdoug » 21 Mar 2011 01:37

Didn't look through all the code but the "B" has 12 bit resolution and the "S" has 9 bit resolution. You have 9 bit defined but note you are using an 18B20.

xpolhecx
Posts: 110
Joined: 24 Feb 2011 16:09

Re: Cant read temperature from DS18B20

#3 Post by xpolhecx » 21 Mar 2011 05:25

Yes i know that. 18B20 can use either 9, 10, 11 or 12 bit resolution (user defined)

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

Re: Cant read temperature from DS18B20

#4 Post by drdoug » 21 Mar 2011 06:43

Yes i know that. 18B20 can use either 9, 10, 11 or 12 bit resolution (user defined)
It is user defined but the default setting is 12 bit and I did not see in your code where you set it up . I think you need something like this. UNTESTED

Code: Select all

void main() {
  ADCON1 = 0x0F;
  CMCON  = 7;
  TRISA = 0x00;             // PORTA as output (one LED on PORTA.F0)
  //TRISC = 0xFF;
  PORTA.F0 = 1;
  Glcd_Init();
  Glcd_Fill(0x00);
      //--- configure for 9 bit reading
    Ow_Reset(&PORTC, 0);                         // Onewire reset signal
    Ow_Write(&PORTC, 0, 0xCC);                   // Issue command SKIP_ROM
    Ow_Write(&PORTC, 0, 0x4E);                   // write scratchpad
    Ow_Write(&PORTC, 0, 0x00);                   // write Th
    Ow_Write(&PORTC, 0, 0x00);                   // write Tl
    Ow_Write(&PORTC, 0, 0x00);                  // clear bits R0 and R1
    Delay_us(120);


   while(1) {

    //--- perform temperature reading
    Ow_Reset(&PORTC, 0);                         // Onewire reset signal
    Ow_Write(&PORTC, 0, 0xCC);                   // Issue command SKIP_ROM
   ...
It looks like a straight copy of the example. I would suspect a hardware problem if the code is from the example.
How long is the signal line? Some users have reported problems with longer lines and lower voltages requiring a smaller resistor.

You may also want to flash your LED to make sure the timing is correct as well since the 4550 can sometimes be tricky to set up.

xpolhecx
Posts: 110
Joined: 24 Feb 2011 16:09

Re: Cant read temperature from DS18B20

#5 Post by xpolhecx » 21 Mar 2011 12:44

Yes this is a copy of mikroc example. I dont think there is a problem with hardware connection. My first pin of DS18B20 is wired to GND, second pin is wired to PIC PORTC.F0 and third pin is wired to VCC. Your code doesn't make any difference :/ If the sensor is connected, the result on LCD is "000.5000" but if i disconnect the sensor, result is "000.0000" :D

I had no problems with my 18F4550 until now. Everything works perfect. This is my current project (my device is connected to PC over USB and displays data from rFactor on my LCD). Do you think USB interrupts can be causing this problem?

http://www.youtube.com/watch?v=Dx7-kkp1xqc
Last edited by xpolhecx on 24 Mar 2011 01:31, edited 1 time in total.

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

Re: Cant read temperature from DS18B20

#6 Post by Dany » 21 Mar 2011 13:05

xpolhecx wrote:Do you think USB interrupts can be causing this problem?
The one-wire protocol is very timing sensitive. It is better to disable all interrupts while doing something with the ow-bus.
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)

xpolhecx
Posts: 110
Joined: 24 Feb 2011 16:09

Re: Cant read temperature from DS18B20

#7 Post by xpolhecx » 21 Mar 2011 13:16

I'll try to make new circuit later when i get new protoboard and see if there is any changes :D
The default resolution at power-up is 12-bit. The DS18B20 powers-up in a low-power idle state. To initiate a temperature measurement and A-to-D conversion, the
master must issue a Convert T [44h] command.
You were right :D But anyway... I need 12 bit resolution because my sensor needs to be very accurate. So i guess i have to change Display_Temperature function in example? I dont understand how to read and converrt bytes from sensor in this function :/

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

Re: Cant read temperature from DS18B20

#8 Post by drdoug » 21 Mar 2011 13:32

Here is how I use my DS18B20. I never got around to using the CRC but it is working to +/- 1 degree. The calculations are from my conversion from 18S20. Even with 12 bits, you are still only going to get within +/- 1 degree accuracy. You can get the extra decimal places but it may or may not be accurate. Resolution != Accuracy.

Code: Select all

float TempFloat = 0; //CalcFloat = 0;
signed int Temp1Avg, Temp2Avg, Temp3Avg, Temp0Avg;
signed int temp =0, templ= 0, temph =0;
unsigned char Trash, CR, CPC, CRC;


signed int GetTemp (unsigned short j)
{
    Ow_Reset(&PORTB, j);                         // Onewire reset signal
    Ow_Write(&PORTB, j, 0xCC);                   // Issue command SKIP_ROM
    Ow_Write(&PORTB, j, 0x44);                   // Issue command CONVERT_T
    Delay_us(120);
 //    Delay_ms(850);
    Ow_Reset(&PORTB, j);
    Ow_Write(&PORTB, j, 0xCC);                   // Issue command SKIP_ROM
    Ow_Write(&PORTB, j, 0xBE);                   // Issue command READ_SCRATCHPAD

    templ =  Ow_Read(&PORTB, j);      //  Read Temp LSB
    temph =  Ow_Read(&PORTB, j);      //  Read Temp MSB
    Trash =  Ow_Read(&PORTB, j);      //  Read TH  (unused)
    Trash =  Ow_Read(&PORTB, j);      //  Read TL  (unused)
    Trash =  Ow_Read(&PORTB, j);      //  (unused)  Configuration Register
    Trash =  Ow_Read(&PORTB, j);      //  (unused)   Reserved
    Trash =  Ow_Read(&PORTB, j);      //  (unused)   Reserved
    Trash =  Ow_Read(&PORTB, j);      //  (unused)   Reserved
    CRC =  Ow_Read(&PORTB, j);        // unused
    Ow_Reset(&PORTB, j);
    temp = (templ >> 4) + (temph << 4);
//    TempFloat = ((CPC- CR)/CPC) + 0.25;
//    TempFloat = temp - TempFloat;
    TempFloat = temp*1.8 + 32;
    temp = TempFloat;
    return temp;
}
Back in my main code I use an IntToStr conversion and display the result on LCD.

You can modify the mE code with this

Code: Select all

//  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
//  18S20: 9  (default setting; can be 9,10,11,or 12)
//  18B20: 12
const unsigned short TEMP_RESOLUTION = 12;


xpolhecx
Posts: 110
Joined: 24 Feb 2011 16:09

Re: Cant read temperature from DS18B20

#9 Post by xpolhecx » 21 Mar 2011 14:39

Quote from datasheet:
The core functionality of the DS18B20 is its direct-to-digital temperature sensor. The resolution of the temperature sensor is user-configurable to 9, 10, 11, or 12 bits, corresponding to increments of 0.5°C, 0.25°C, 0.125°C, and 0.0625°C, respectively.
What does it mean? If i can't get +/- 0.1°C accuracy i really have to buy more accurate sensor :D

xpolhecx
Posts: 110
Joined: 24 Feb 2011 16:09

Re: Cant read temperature from DS18B20

#10 Post by xpolhecx » 21 Mar 2011 15:08

I realized my sensor was dead (overheated). If i connect third pin (vcc) to +5V my sensor is getting very hot so i guess i overheated it. I bought new one but i cant connect third pin to VCC again :) Can my sensor be powered by PIC over second (data) pin and do i have to use pull-up resistor (4.7k)?

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

Re: Cant read temperature from DS18B20

#11 Post by drdoug » 21 Mar 2011 15:12

Quote from the datasheet
±0.5°C Accuracy from -10°C to +85°C
I think if you want a higher degree of accuracy, you are going to have to make some careful calibration adjustments on each sensor over a range of temperatures.
Are other sensors really more accurate?
How do you verify?
What are you measuring?

Also, I forgot to mention it but you need a longer conversion time for the higher resolution but I was not able to discern a difference with shorter times???

xpolhecx
Posts: 110
Joined: 24 Feb 2011 16:09

Re: Cant read temperature from DS18B20

#12 Post by xpolhecx » 21 Mar 2011 15:21

Yes i know the higher resolution it is, more time is needed for convertion. Sensor has to be really accurate because i'm measuring air temperatures in 4 different places on airport (airfield) and i have to calculate a deviation (difference) between these sensors. The data is transfered over RF module to my device. And i have to fly with my glider over that area where the air temperature is the highest.

I'm really sorry for my bad english :D

EDIT: I cant determine why my sensor is getting hot if i connect third pin to VCC?

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

Re: Cant read temperature from DS18B20

#13 Post by drdoug » 21 Mar 2011 15:27

Did you release the magic smoke. It won't work if you let it out of the device.
My favorite was not putting a diode on a relay and melted my solderless breadboard.
Good luck.
I think you can power it from any 5v source but the PIC is limited to 25mA output but that shouldn't be a problem.

xpolhecx
Posts: 110
Joined: 24 Feb 2011 16:09

Re: Cant read temperature from DS18B20

#14 Post by xpolhecx » 21 Mar 2011 15:34

Magic smoke? What do you mean by that :D

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

Re: Cant read temperature from DS18B20

#15 Post by drdoug » 21 Mar 2011 15:37

The temperature conditions are going to vary so greatly that a higher resolution will not make a difference. You would also have to measure at different heights as well. I think it would be better to have the temp sensor on the glider.
Of course the easiest guess on the heating problem is the device is in backwards.

Post Reply

Return to “mikroC PRO for PIC General”