PDA

View Full Version : Problems with 1-Wire DS2438 and PIC16F88



HeadCase
- 20th May 2009, 20:42
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/en/devicedoc/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



http://146.145.203.135/images/DS2438-Schematic.jpg

Darrel Taylor
- 20th May 2009, 22:13
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=1242853592">

Darrel Taylor
- 20th May 2009, 23:42
Here's an easy way to read the whole page ...

Using a technique shown here (http://www.picbasic.co.uk/forum/showthread.php?t=3891) (Typcasting Variables inside of Arrays)


<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>
<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,

HeadCase
- 21st May 2009, 16:21
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?

Darrel Taylor
- 21st May 2009, 21:06
Try adding this before the Read command. (after the conversion)


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,

HeadCase
- 22nd May 2009, 14:33
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.

MOUNTAIN747
- 8th January 2011, 15:46
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?

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

MOUNTAIN747
- 9th January 2011, 20:41
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!

Darrel Taylor
- 10th January 2011, 21:12
Try changing the mode to 2 before reading the data (no reset pulse).


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

MOUNTAIN747
- 12th January 2011, 00:09
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.

mosajii
- 4th February 2011, 05:12
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.:(:confused:

MOUNTAIN747
- 4th February 2011, 17:30
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.

MOUNTAIN747
- 4th February 2011, 18:51
correction: until the user sets it to enable Vdd as A/D