embeddedpicpic181wire

One Wire Problem


I need your qualified help! I'm programing in C++, using a PIC 18F87J50 and trying to connect DS18B20 at my H0 Port!

I think my underlying programing is correct so.... the problem I have (think I have), is when performing a ROM Command, I'm searching for the 64-bit ROM CODE.

The first byte should tell me what family the component belongs to (28h). The next 48 bits should give me a uniq serial for just that component. The last one is used for a CRC.

Am I thinking right when doing like this:

void Device_ID( uint8_t command ){
    uint8_t ROM_CODE[8]; // 1 byte CRC, 6 bytes SERIAL, 1 byte Family code
    uint8_t loop;
    static char container[8];

    OW_reset_pulse();
    OW_write_byte( command );

    for(loop = 0; loop < 8; loop++)     // 1 byte in per time = 64-bits
    {
        ROM_CODE[loop] = OW_read_byte(); 
    }

    HexToStrWithZeros(ROM_CODE[0], container);
    Display_Cls();
    Display_StringAt ("Family Code: ",5,6);
    Display_Buffer (container);
}

If I ask for the code in ROM_CODE[1-6] I should get the uniq number?? should'nt I??

Kind Regards!


Solution

  • Well, the best way to access the serial number is probably to copy it into a separate buffer using strncpy.

    #include <string.h>
    
    ...
    
    char family;
    char serial[7]; // Extra byte for null terminator
    char checksum;
    
    ...
    
    family = ROM_CODE[0];
    
    strncpy(serial, &ROM_CODE[1], 6);
    serial[6] = '\0';
    
    checksum = ROM_CODE[7];
    
    ...
    

    The &ROM_CODE[1] is there to fetch the address of the 2nd element in ROM_CODE. ROM_CODE+1 may also work, but my C is a touch rusty.

    The null ('\0') is added at the end as C uses null-terminated strings. This will make sure it's compatible with C library routines and commonly used C idioms.

    You could also access it directly from the array. But that'll be harder to work with and unlikely to be worth it unless you really need that 6 bytes of memory.

    Depending on how sophisticated your app is, you may want to wrap this in a class. Pass the 8 character buffer to the constructor and then use methods such as getFamily() / getSerial() to retrieve the information you want.

    For a very simple app though, that's a whole lot of extra code to simplify something that's already very manageable.