Problems with 1-Wire DS2438 and PIC16F88


Closed Thread
Results 1 to 13 of 13
  1. #1
    Join Date
    May 2009
    Location
    Philadelphia, PA
    Posts
    3

    Default Problems with 1-Wire DS2438 and PIC16F88

    I've hit a road block while attempting to gather data off a Dallas DS2438 1-Wire Battery Monitor using a PIC16F88. Initially the script and wiring seemed correct. I've used a similar setup for a Dallas DS18B20 Temp Sensor and it worked like a charm. I assumed the sensor was damaged, but after giving 5 more a try, I figured I would post to see if anyone else had a suggestion.

    The raw return is a bunch of 1's on the LCD. I've tried many different options, but with the same result.

    Below is the PICBasic code I've been working with (although messy, but I'll fix later after I gain communication with the sensor) and a schematic. Any and all feedback is appreciated.


    DS2438 Datasheet: http://datasheets.maxim-ic.com/en/ds/DS2438.pdf

    PIC16F88 Datasheet: http://ww1.microchip.com/downloads/e...doc/30487c.pdf




    DEFINE OSC 20 ' We're using a 20MHz oscillator
    DQ_Pin VAR PORTB.2 ' One-wire Data-Pin "DQ" on PortB.2
    Busy VAR BIT ' Busy Status-Bit
    ANSEL = %00000000 ' set the AN's to digital

    voltage VAR word

    DEFINE DEBUG_REG PORTB ' Debug Port = PortB
    DEFINE DEBUG_BIT 1 ' Debug.bit = 1
    DEFINE DEBUG_BAUD 9600 ' Default baud rate = 9600
    DEFINE DEBUG_MODE 1 ' Send Inverted serial data with debug

    high PORTB.0 ' Proof of life LED
    pause 500 'Let the LCD warm up


    Start_Convert:
    OWOUT DQ_Pin,1,[$CC, $B4] ' send voltage conversion command


    Wait_Up:
    OWIN DQ_Pin, 4, [Busy] ' Read busy-bit
    IF Busy = 0 THEN Wait_Up ' Still busy..?, Wait_Up..!


    PAUSE 15 ' Give it some time
    OWOUT DQ_Pin,1,[$CC, $BE, $00] ' Skip ROM search, send Convert V, Read page 00h
    OWIN DQ_Pin,2,[voltage.lowbyte, voltage.highbyte] ' Read two bytes

    GOSUB Convert_Volt
    GOTO Start_Convert

    Convert_Volt:
    DEBUG $FE,$01 ' Clear the LCD
    DEBUG "Voltage:" ,Dec voltage.Highbyte/100,".", DEC1 voltage.Lowbyte/10,"v",10,13 ' needs to be modified
    DEBUG "Raw:", IBIN16 voltage, 10,13

    pause 1000 ' Wait 1 second then loop
    RETURN

    END




  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Code:
    OWOUT DQ_Pin,1,[$CC, $BE, $00] ' Skip ROM search, send Convert V, Read page 00h
    OWIN DQ_Pin,2,[voltage.lowbyte, voltage.highbyte] ' Read two bytes
    $BE is the Read Scratchpad command. (just a comment prob)

    The Read Scratchpad command always starts reading at address 0 of the selected page.

    The first 2 bytes of page 0 are STATUS/CONFIGURATION and TEMP LSB.
    The STATUS/CONFIGURATION defaults to 1's, which is probably what you're seeing.

    The Voltage value is at addresses 3-4, so you'll need to read at least 5 bytes to get to the Voltage.

    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=3415&stc=1&d=124285359 2">
    Attached Images Attached Images  
    DT

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Here's an easy way to read the whole page ...

    Using a technique shown here (Typcasting Variables inside of Arrays)

    Code:
    <font color="#000000"><b>DSbuffer  </b><font color="#008000"><b>VAR BYTE</b></font>[<font color="#800000"><b>9</b></font>] <b>BANK0</b>
    <font color="#008000"><b>ASM
    </b></font><font color="#000080">DSstat    = _DSbuffer      </font><font color="#0000FF"><b><i>; byte
    </i></b></font><font color="#000080">DStemp    = _DSbuffer + 1  </font><font color="#0000FF"><b><i>; word
    </i></b></font><font color="#000080">DSvolts   = _DSbuffer + 3  </font><font color="#0000FF"><b><i>; word
    </i></b></font><font color="#000080">DScurrent = _DSbuffer + 5  </font><font color="#0000FF"><b><i>; word
    </i></b></font><font color="#000080">DSthres   = _DSbuffer + 7  </font><font color="#0000FF"><b><i>; byte
    </i></b></font><font color="#000080">DScrc     = _DSbuffer + 8  </font><font color="#0000FF"><b><i>; byte
    </i></b></font><font color="#008000"><b>ENDASM
    
    </b></font><b>DSstat    </b><font color="#008000"><b>VAR  BYTE  </b></font><b>EXT </b><font color="#0000FF"><b><i>; Status/Configuration
    </i></b></font><b>DStemp    </b><font color="#008000"><b>VAR  WORD  </b></font><b>EXT </b><font color="#0000FF"><b><i>; Temperature
    </i></b></font><b>DSvolts   </b><font color="#008000"><b>VAR  WORD  </b></font><b>EXT </b><font color="#0000FF"><b><i>; Voltage
    </i></b></font><b>DScurrent </b><font color="#008000"><b>VAR  WORD  </b></font><b>EXT </b><font color="#0000FF"><b><i>; Current
    </i></b></font><b>DSthres   </b><font color="#008000"><b>VAR  BYTE  </b></font><b>EXT </b><font color="#0000FF"><b><i>; Threshold
    </i></b></font><b>DScrc     </b><font color="#008000"><b>VAR  BYTE  </b></font><b>EXT </b><font color="#0000FF"><b><i>; CRC</i></b></font>
    Then you can read page 0 like this ...<table><tr><td>
    Code:
    <font color="#008000"><b>OWOUT </b></font><b>DQ_Pin</b>,<font color="#800000"><b>1</b></font>,[<font color="#800000"><b>$CC</b></font>, <font color="#800000"><b>$B8</b></font>, <font color="#800000"><b>$00</b></font>] <font color="#0000FF"><b><i>' Skip ROM search, Recall Memory page 0
    </i></b></font><font color="#008000"><b>OWOUT </b></font><b>DQ_Pin</b>,<font color="#800000"><b>1</b></font>,[<font color="#800000"><b>$CC</b></font>, <font color="#800000"><b>$BE</b></font>, <font color="#800000"><b>$00</b></font>] <font color="#0000FF"><b><i>' Skip ROM search, Read page 0
    </i></b></font><font color="#008000"><b>OWIN  </b></font><b>DQ_Pin</b>,<font color="#800000"><b>2</b></font>,[<font color="#008000"><b>STR </b></font><b>DSbuffer</b>\<font color="#800000"><b>9</b></font>]<font color="#0000FF"><b><i>' Read 9 bytes</i></b></font>
    </td><td valign=top><br><br>Recall Memory added: see next few posts.</td></tr></table>
    After reading, all the values will be in the correct variables, with the correct size (word/byte), ready to be used.

    hth,
    Last edited by Darrel Taylor; - 22nd May 2009 at 01:22. Reason: Recall

  4. #4
    Join Date
    May 2009
    Location
    Philadelphia, PA
    Posts
    3


    Did you find this post helpful? Yes | No

    Default

    Thanks for your help, Darrel! Your EXT methods will be a great help with other projects.

    After adding in your code, I noticed that the raw data was still a series of 1's. I switched to a DS18B20 using your suggested code (with slight modification to the one-wire commands) and it worked perfectly.

    For kicks, I removed all of the 1-Wire devices and the raw data is still showing 1's. Seems like the PIC is reading the pin's state (pulled up with a resistor) rather than the data coming over the DQ wire. While the wiring and code seem correct and appear to conform with the datasheets, perhaps I'm missing something?

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Try adding this before the Read command. (after the conversion)

    Code:
    OWOUT DQ_Pin,1,[$CC, $B8, $00] ' Recall Memory page 0
    From the datasheet ...
    Recall Memory [B8hxxh]
    This command recalls the stored values in EEPROM / SRAM page xxh to the scratchpad page xxh.
    This command must proceed a Read SPxx command in order to read any page of memory on the DS2438.
    hth,
    DT

  6. #6
    Join Date
    May 2009
    Location
    Philadelphia, PA
    Posts
    3


    Did you find this post helpful? Yes | No

    Default

    That worked perfectly. While I figured the recall function was a bit redundant, after some more studying of the datasheet, it now makes sense. I'll post the complete code and schematic once I've completed the project.

    Thanks for your help, Darrel.

  7. #7
    Join Date
    Jan 2009
    Location
    Alabama,USA
    Posts
    232


    Did you find this post helpful? Yes | No

    Default DS2438 Scratchpad ?

    I am working on a project for the farm and I want to use a 1-wire DS2438 initialized to use the Vad voltage input. I am building on Darrel Taylor’s May 2009 post. I have been unable to write to the scratchpad. The scratchpad always reads defalt values. Temperature seems to be working as expected. The voltage is always equal to Vdd and not Vad. I am not sure if OWOUT will handle the necessary condition needed which is:
    Reset, SkipRom, WriteScratchpad, page#, 1 data byte , Reset

    I’ve tried:
    OWOUT DQ_pin, 3 ,[ SkipRom, WriteScratchpad, $00, $00]
    I’ve also tried:
    OWOUT DQ_pin, 1 ,[ SkipRom, WriteScratchpad, $00]
    OWOUT DQ_pin, 2 ,[ $00]
    And:
    OWOUT DQ_pin, 1 ,[ SkipRom, WriteScratchpad, $00, $00]
    OWOUT DQ_pin, 1 ,[ SkipRom, ConvesrtT]
    Nothing seems to work.

    I am using an X1 board with 16F877 running at 16Mhz.
    Has anyone had success at using PBP to write to the DS2438 scratchpad? Everything else worked as expected, what am I overlooking?
    Code:
        define  osc 16
    
    ' -----[ Constants ]------------------------------------------------------------
    ' 1-Wire ROM Function Commands
    ReadROM        CON $33    ;read ID, serial num, CRC
    MatchROM       CON $55    ;look for specific device
    SkipROM        CON $CC    ; skip rom (one device)
    SearchROM      CON $F0    ;
    ' DS2438 Memory Command functions
    ConvertV       CON $B4   ; convert the voltage
    ConvertT       CON $44   ; convert the temperature
    RecallMemory   CON $B8   ; set page to be read
    ReadScratchpad CON $BE   ; set address to be read
    CopyScratchpad CON $48
    WriteScratchpad CON $4E
    ' configuratioin register status bits
    ADVdd   CON %1000       ;Voltage A/D input slect Bit 1=Vdd 
    ADVad   CON %0000       ;Voltage A/D input slect Bit 0=Vad  
    TB      CON %10000      ;Temp conversion flag
    ADB     CON %1000000    ;A/D conversion flag
    ' -----[ Variables ]------------------------------------------------------------
    DSbuffer  VAR BYTE[9] BANK0
    ASM          ;Typcasting Variables inside of Arrays
    DSstat    = _DSbuffer      ; byte
    DStemp    = _DSbuffer + 1  ; word
    DSvolts   = _DSbuffer + 3  ; word
    DScurrent = _DSbuffer + 5  ; word
    DSthres   = _DSbuffer + 7  ; byte
    DScrc     = _DSbuffer + 8  ; byte
    ENDASM
    DSstat      VAR  BYTE  EXT ; Status/Configuration
    DStemp      VAR  WORD  EXT ; Temperature
    DSvolts     VAR  WORD  EXT ; Voltage
    DScurrent   VAR  WORD  EXT ; Current
    DSthres     VAR  BYTE  EXT ; Threshold
    DScrc       VAR  BYTE  EXT ; CRC
    
    GB1net      VAR  PORTC.7  ' 1-Wire net work for Bin #1                            
    sd_tran     VAR  PORTC.6  ' Transmit pin
    sd_receive  VAR  PORTC.7  ' Receive pin
    
    ' -----[ Main Code ]------------------------------------------------------------
    ;Initialize:  ;set configuration to A/D convesion on Vad pin 4 of DS2438
    Main:
        OWOUT GB1net,1,[SkipRom,WriteScratchpad,$00,$00]  
    GetData:
        OWOUT GB1net,1,[SkipRom,ConvertT]
        pause 10 
        OWOUT GB1net,1,[SkipRom,ConvertV]
        pause 10
        OWOUT GB1net,1,[SkipRom,RecallMemory, $00]   ' Skip ROM search, Recall Memory page 0
        OWOUT GB1net,1,[SkipRom,ReadScratchpad, $00] ' Skip ROM search, Read SP 
        OWIN  GB1net,0,[STR DSbuffer\9]              ' Read 9 bytes                                                                                      
        Serout2 sd_tran, 84,["   Scratchpad ",bin DSstat,",  temp ",bin DStemp,",  DSvolts "_
          , bin DSvolts, ", DSthres ", bin DSthres, ",  DScrc ",bin DScrc,13,10,13,10  ]
        pause 5000
        goto GetData
        END

  8. #8
    Join Date
    Jan 2009
    Location
    Alabama,USA
    Posts
    232


    Did you find this post helpful? Yes | No

    Default DS2438 not clearing scratchpad

    I’ve pulled out the logic analyzer to make sure the WriteScratchpad command was actually being sent. It is, USBee results after sending:

    OWOUT GB1net,1,[SkipROM, WriteScratchpad,$00,$00]
    OWOUT GB1net,1,[SkipROM, RecallMemory, $00]
    OWOUT GB1net,1,[SkipROM, ReadScratchpad, $00]
    OWIN GB1net,2,[STR DSbuffer\9]

    Presences Pulse
    CC < SkipROM
    4E < WriteScratchpad
    00 < Page 0
    00 < write on byte of data “$00”
    Reset Pulse
    Presences Pulse
    CC < SkipROM
    B8 < RecallMemory
    00 < Page 0
    Reset Pulse
    Presences Pulse
    CC < SkipROM
    BE < ReadScratchpad
    00 < Page 0
    0F < page 0, byte 0 status/configuration register was never set to $00
    E8
    15
    28
    03
    00
    00
    00
    4A <crc

    Reset Pulse
    Presences Pulse

    I’ve changed DS2438 and get the same results?

    Now what, most of the hair has already been sacrificed!

  9. #9
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Try changing the mode to 2 before reading the data (no reset pulse).

    Code:
        OWIN  GB1net,2,[STR DSbuffer\9]              ' Read 9 bytes

  10. #10
    Join Date
    Jan 2009
    Location
    Alabama,USA
    Posts
    232


    Did you find this post helpful? Yes | No

    Default The world is not always as it appears!

    OK, so you shouldn’t always jump to conclusions even if you have reason to do so. As stated in my previous post the scratchpad/config file is reading back $OF after being set to $00. I assumed the Vad was not working. I was wrong. It is working. It now appears the configuration bit Page $00 byte $00 bit.3 is clearing to allow Vad to function however ReadScratchpad config register always reads default values (?) The Vad is working fine. My only gripe is that I have to set the Vad bit on every loop before a convert. All other registers are changing as they should and read values as expected. I’ve ask Maxim if this is normal, no reply as yet. It acts as if the config register changes to default when read, weird.
    Darrel, thanks for the reply, your right about the mode. I am still going to play around with the ReadScratchpad command to see if I can find some trick to make the DS2438 function as I think it should. Grain Moisture Content project is underway.

  11. #11
    Join Date
    Feb 2011
    Posts
    3


    Did you find this post helpful? Yes | No

    Default

    I have the feeling that there is some problem with his sensor. Most probably it is damaged to a considerable extent and that is the reason why he is unable to gather data. I would request him to replace it with a new one. I am pretty sure that it will solve the issue.

  12. #12
    Join Date
    Jan 2009
    Location
    Alabama,USA
    Posts
    232


    Did you find this post helpful? Yes | No

    Default

    Hi mosajii,
    The problem was that the scratchpad would not keep the control bits, which had been set, once the conversions were been made. On the DS2438 you must clear bit.3 of the scratchpad to enable the Vad. This bit should stay clear until the user sets it to enable Vss as A/D. Instead the scratchpad would reset to $0F after all conversions and read back of the scratchpad. This means I must set the control bit on each loop rather than only once as it should be. The devise is collecting data as it should. It is just irritating to have to reset the scratchpad on each loop. Maxim never responded to my inquire about this behavior.

  13. #13
    Join Date
    Jan 2009
    Location
    Alabama,USA
    Posts
    232


    Did you find this post helpful? Yes | No

    Default

    correction: until the user sets it to enable Vdd as A/D

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts