Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problems with 18b20
01-11-2013, 03:40 AM,
#1
problems with 18b20
Hi:

I built a 20 Mhz 4550 pinguino that looks that works properly, as I can upload, and run blink and fading examples.

I am using IDE Rev 910, bootloader v4.12_4550_20Mhz, in a ubuntu 12.04 comunicating with minicom ( -o -D /dev/ttyACM0)

I am trying examples/sensors/d18B20/thermometer, but I am getting these readings in minicom screen

23.12 C
104.37 C
104.37 C
23.62 C
23.18 C
0.6 C
23.18 C
0.6 C
23.18 C
23.43 C
23.18 C
0.6 C
23.18 C

those around 23 ºC are the goods ones, they even increase if I put my finger on the sensor, but as you can see, there are some incorrect readings

I am conecting the 18b20 properly, with a 4,7k resistor between dq and 5v, and even tryed adding a 0.1 uf capacitor between ground and vcc in the 18b20. I also tryed with a 1k resistor, with the same results

I have tested it in a protoboard with about 10 cm long wires, and also with a specifically made 20 cm long wire, getting always the same results

Any one knows what is happening?
Anyone is using the 18b20?

bye, ricardo
Reply
01-11-2013, 11:13 AM,
#2
RE: problems with 18b20
Worked OK on PIC32 (OTG).

John
Reply
16-03-2014, 10:48 PM,
#3
RE: problems with 18b20
Hello,
I have same problem on my homemade Pinguino-2550.
I tried to debug a little and found that for some reason when -0.6 is showed, the reading of the 8byte+CRC are
FF FF FF FF FF FF FF FF FF.
I also noted that there are many reading errors causing wrong temperature display (eg. 104.37, 54.50 ecc..) while ambient temp is around 23°C.
Initially I suspected timing issue on "OneWireRead(ONEWIREBUS)" routine but after many attempt I gave up.
Then I decided to calculate CRC to be compared with 9th byte.
I found over the internet this great function:
https://www.mikroe.com/forum/viewtopic.p...w=previous
(I post only the link because I don't know if this slice of code is protected by copyright or not)
Results: I put all code into a while statement and now I always get the right temperature.
Dirty but working Smile
Hope that my experience can be helpful to anyone is facing this kind of problem
Luca
Reply
17-03-2014, 07:29 PM,
#4
RE: problems with 18b20
Here are some routines placed in the public domain : http://www.barrgroup.com/Embedded-System...ion-C-Code
Here are some others : https://www.ccsinfo.com/forum/viewtopic.php?t=37015 with some statistics and among them the one Pinguino uses (here called crc2 and far faster than the others).
You might want to test all of them and report which work Cool
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
19-03-2014, 06:28 PM,
#5
RE: problems with 18b20
The first thing to do is to understand if is the sensor that send erratic values or if is the pic that decode the protocol used by the sensor in a wrong manner.

So, use a protocol analyzer to check what data the sensor is sending and compare it to what pic decode.
If you find differences the problem is related to pic decoding routine. If data are equals the sensor send wrong data and you have to check electric noise or other potentially disturbing effect that can affect the measures.

Hope this help.
Enjoy, Fabio.



(01-11-2013, 03:40 AM)el_ric Wrote: Hi:

I built a 20 Mhz 4550 pinguino that looks that works properly, as I can upload, and run blink and fading examples.

I am using IDE Rev 910, bootloader v4.12_4550_20Mhz, in a ubuntu 12.04 comunicating with minicom ( -o -D /dev/ttyACM0)

I am trying examples/sensors/d18B20/thermometer, but I am getting these readings in minicom screen

23.12 C
104.37 C
104.37 C
23.62 C
23.18 C
0.6 C
23.18 C
0.6 C
23.18 C
23.43 C
23.18 C
0.6 C
23.18 C

those around 23 ºC are the goods ones, they even increase if I put my finger on the sensor, but as you can see, there are some incorrect readings

I am conecting the 18b20 properly, with a 4,7k resistor between dq and 5v, and even tryed adding a 0.1 uf capacitor between ground and vcc in the 18b20. I also tryed with a 1k resistor, with the same results

I have tested it in a protoboard with about 10 cm long wires, and also with a specifically made 20 cm long wire, getting always the same results

Any one knows what is happening?
Anyone is using the 18b20?

bye, ricardo
Reply
20-03-2014, 12:15 AM,
#6
RE: problems with 18b20
Thanks regis for your suggestions.
I have just tested crc2 function which works very well for pinguino-2550.
Here is the code I used:
Code:
#include <1wire.c>
#define ONEWIREBUS    10
u8 count = 0;
u8 led_handle = 0;
u8 ow_set_TH = 0x7D;              // TH 125°C
u8 ow_set_TL = 0xB7;              // TL -55°C
u8 ow_set_RES = 0x7F;             // RES_12BIT=0x7F, RES_11BIT=0x5F,RES_10BIT=0x3F,RES_9BIT=0x1F

byte DS18B20_crc_calc1(byte buff[], byte num_vals)
{
   byte shift_reg=0, data_bit, sr_lsb, fb_bit, i, j;

   for (i=0; i<num_vals; i++)       // for each byte
   {
      for(j=0; j<8; j++)      
      {
         data_bit = (buff[i]>>j)&0x01;
         sr_lsb = shift_reg & 0x01;
         fb_bit = (data_bit ^ sr_lsb) & 0x01;
         shift_reg = shift_reg >> 1;
         if (fb_bit)
         {
            shift_reg = shift_reg ^ 0x8C;
         }
      }
   }
   return(shift_reg);
}

byte DS18B20_crc_calc2(byte buff[], byte num_vals)
{
   byte crc_reg=0, i, val;
   for (i=0; i<num_vals; i++)       // for each byte
   {
       val = (buff[i] ^ crc_reg) & 0xff;
       crc_reg = 0;
       if(val & 1)
          crc_reg ^= 0x5e;
       if(val & 2)
          crc_reg ^= 0xbc;
       if(val & 4)
          crc_reg ^= 0x61;
       if(val & 8)
          crc_reg ^= 0xc2;
       if(val & 0x10)
          crc_reg ^= 0x9d;
       if(val & 0x20)
          crc_reg ^= 0x23;
       if(val & 0x40)
          crc_reg ^= 0x46;
       if(val & 0x80)
          crc_reg ^= 0x8c;
   }
   return(crc_reg);
}

void DS18B20_configure(u8 ow_TH, u8 ow_TL, u8 ow_RES)
{
    OneWireReset(ONEWIREBUS);
    OneWireWrite(ONEWIREBUS,0xCC);              // SKIPROM
    OneWireWrite(ONEWIREBUS,0x4E);              // WRITE SCRATCHPAD
    OneWireWrite(ONEWIREBUS,ow_set_TH);         // TH 125
    OneWireWrite(ONEWIREBUS,ow_set_TL);         // TL -55
    OneWireWrite(ONEWIREBUS,ow_set_RES);        // Config RESOLUTION 12bit
}

void DS18B20_read_temperature()
{
    byte busy = 0;
    int ow_temp_sign, ow_temp_integer;
    u16 tt0, ow_temp_fraction;
    byte ow_temp_buff[];
    byte ow_receive_byte_count;
    byte ow_crc_result1 = 0;
    byte ow_crc_result2 = 0;
    byte ow_crc_flag = 0;
    
    while(ow_crc_flag == 0)
    {
        OneWireReset(ONEWIREBUS);
        OneWireWrite(ONEWIREBUS,0xCC);
        OneWireWrite(ONEWIREBUS,0x44);
        while(busy == 0)                          //Wait while busy (bus is low)
        {
            busy = OneWireReadBit(ONEWIREBUS);
        }
        OneWireReset(ONEWIREBUS);
        OneWireWrite(ONEWIREBUS,0xCC);            //Skip ROM, address all devices    
        OneWireWrite(ONEWIREBUS,0xBE);            //Read scratchpad
        
        for (ow_receive_byte_count = 0; ow_receive_byte_count <= 8; ow_receive_byte_count++)
        {
            ow_temp_buff[ow_receive_byte_count] = OneWireRead(ONEWIREBUS);  // Receive 9 bytes from DS18B20
        }

        ow_crc_result1 = DS18B20_crc_calc1(ow_temp_buff,8);                   // Calculate CRC
        ow_crc_result2 = DS18B20_crc_calc2(ow_temp_buff,8);                   // Calculate CRC
        
        if (ow_crc_result2 == ow_temp_buff[8])
        {
            tt0 = ow_temp_buff[1];
            tt0 = (tt0 << 8) + ow_temp_buff[0];
            if (ow_temp_buff[1] & 0b11111000)            // negative temperature
            {
                ow_temp_sign = 45;                      // ascii "-"
                tt0 = (tt0 ^ 0xFFFF) + 1;               // 2's complement conversion
            }
            else                                        // positive temperature
            {
                ow_temp_sign = 43;                      // ascii "+"
            }
            ow_temp_integer = (tt0 >> 4) & 0x7F;         // fractional part is removed, leaving only integer part
            ow_temp_fraction = (tt0 & 0x0F) * 625;
            ow_temp_fraction /= 100;                    // two digits after decimal
            ow_crc_flag = 1;                            // Flag for good CRC: this will exit the while statement
            if ( ow_temp_buff[2] != ow_set_TH || ow_temp_buff[3] != ow_set_TL || ow_temp_buff[4] != ow_set_RES)
            {
                DS18B20_configure(ow_set_TH, ow_set_TL, ow_set_RES);    // Check if configuration was set on DS18B20
            }
        }
    }
    CDC.printf("%02X %02X %02X %02X %02X %02X %02X %02X %02X (%02X) (%02X) = %c%d.%02d %cC\r\n", ow_temp_buff[0], ow_temp_buff[1], ow_temp_buff[2], ow_temp_buff[3], ow_temp_buff[4], ow_temp_buff[5], ow_temp_buff[6], ow_temp_buff[7], ow_temp_buff[8], ow_crc_result1, ow_crc_result2, ow_temp_sign, ow_temp_integer, ow_temp_fraction, 248);
}

void DS18B20_read_rom()
{
    byte ow_temp_buff[];
    byte ow_receive_byte_count;
    byte ow_crc_result = 0;
    byte ow_crc_flag = 0;
    
    while(ow_crc_flag == 0)
    {    
        OneWireReset(ONEWIREBUS);
        OneWireWrite(ONEWIREBUS,0x33);
        for (ow_receive_byte_count = 0; ow_receive_byte_count <= 7; ow_receive_byte_count++)
        {
            ow_temp_buff[ow_receive_byte_count] = OneWireRead(ONEWIREBUS);  // Receive 8 bytes
        }
        ow_crc_result = DS18B20_crc_calc2(ow_temp_buff,7);                   // Calculate CRC
        if (ow_crc_result == ow_temp_buff[7])
        {
            ow_crc_flag = 1;                                                // Flag for good CRC: this will exit the while statement
        }
    }
    CDC.printf("%02X %02X %02X %02X %02X %02X %02X %02X \r\n", ow_temp_buff[0], ow_temp_buff[1], ow_temp_buff[2], ow_temp_buff[3], ow_temp_buff[4], ow_temp_buff[5], ow_temp_buff[6], ow_temp_buff[7]);
}

void setup()
{
    pinMode(ONEWIREBUS, INPUT);
    pinMode(USERLED, OUTPUT);
}

void loop()
{
    DS18B20_read_rom();                 // Read and Print (CDC) DS18B20 ID ROM
    for (count=0; count<=10; count++)
    {
    DS18B20_read_temperature();         // Read and Print (CDC) DS18B20 Scratchpad      
    led_handle = (led_handle ^ 1);
    digitalWrite(USERLED,led_handle);
    delay(1000);
    }
}

which outputs the following strings:
28 43 1C C7 05 00 00 E5
6A 01 7D B7 7F FF 06 10 6B (6B) (6B) = +22.62 øC
6A 01 7D B7 7F FF 06 10 6B (6B) (6B) = +22.62 øC
6B 01 7D B7 7F FF 05 10 7D (7D) (7D) = +22.68 øC
6A 01 7D B7 7F FF 06 10 6B (6B) (6B) = +22.62 øC
6A 01 7D B7 7F FF 06 10 6B (6B) (6B) = +22.62 øC

inside brackets is the CRC calculation from routine#1 and routine#2 which is equal to the 9th byte.
Routine#2 is far faster then #1.
Form the link that you posted I really liked the idea to calculate directly the CRC bit by bit on 1wire receceiving fuction (let's say while waiting for time slot).
I think this last solution will be the best.
I will try to work on it!
Luca
Reply
20-03-2014, 10:27 AM,
#7
RE: problems with 18b20
Hi Luca,
Can you check the DS18B20.c library to see where the problem comes from ?
Thank you.
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
28-03-2014, 12:50 AM,
#8
RE: problems with 18b20
OK, I got something working.
I added the already working CRC calculation at the DS18B20Read function, and a cycle to repeat the reading until CRC is found valid.
Next days I will try to use it with multiple sensors on the bus.
Luca


Attached Files
.c   18b20.c (Size: 17.66 KB / Downloads: 21)
Reply
30-03-2014, 04:36 PM,
#9
RE: problems with 18b20
Tell me once you've done all your tests and I will update the file.
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
18-04-2014, 11:34 PM,
#10
RE: problems with 18b20
just to inform that in the setup that I informed in my first post (the first of this thread) I replaced the original file '18b20.c' (on x.4/p8/pinguino/libraries) with the one provided by brikker on post #8 and problem looks solved. Readings looks ok. I have only one 18b20 so I can not do further testing.

bye and thenk you all
ricardo
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)