PDA

View Full Version : Writing to 1-wire EEPROM



crhomberg
- 1st October 2008, 20:32
I need to write to and then read back from a 1-wire EEPROM.
I have an iButton DS1973 which has the same insides as a
standard 1-wire DS2433 4Kb EEPROM.

I have read the datasheet, tried without success, searched the PBP and other sites but no answer.
I just need an example on how to write 1 byte to an address on the EEPROM and then read it back again. This code I tried but doesn't work:

ADCON1=7 ' Set all Ports to Digital I/O
OPTION_REG.7=0 ' Enable weak PullUps
TRISB.0=0

MEM VAR BYTE
RESULT VAR BYTE[4]

iButton var PortD.2


START:

SEROUT2 PORTB.0,16416,["START",10,13] 'just to test the comms

'WRITE TO EEPROM

OWOUT ibutton,1,[$CC,$0F,$00]
OWOUT IBUTTON,1,[$54,$65,$73,$74] 'write characters "test"
OWOUT IBUTTON,1,[$CC,$FF,$A5]
PAUSE 25

'READ FROM EEPROM

OWOUT ibutton,0,[$CC,$F0,$00]
FOR MEM=1 TO 32
OWIN IBUTTON,0,[RESULT]
SEROUT2 PORTB.0,16416,[RESULT]
PAUSE 100
NEXT MEM

END

Can anyone tell me what I have done wrong?

Regards

Chris

mackrackit
- 1st October 2008, 22:38
Maybe
http://www.picbasic.co.uk/forum/showthread.php?t=1736&highlight=DS1973

crhomberg
- 1st October 2008, 23:27
I am only going to load data on the iButton EEPROM once, is it nessesary to use the "Cyclic Redundancy Checks" to write to the device or is it just an option like parity is on other serial systems. Can I not just write directly to it without vast CRC calculations and then read it out to check? I only am going to need about 12 bytes to be written in total for button identification. (I am using the code on the button to change a pic's options, plug button 1 in it does one thing, plug button 2 in it does another)

It must be simpler?

Regards

Chris

Darrel Taylor
- 1st October 2008, 23:43
The Write Scratchpad command requires a 2-byte address. You're only giving it 1.
OWOUT ibutton,1,[$CC,$0F,$00,$00]

After that command, you can't "RESET" before the data
OWOUT IBUTTON,0,[$54,$65,$73,$74] 'write characters "test"

Not sure what this was supposed to be. There isn't a $FF command. Due to the delay afterwards, it looks like that was supposed to do the Write.
OWOUT IBUTTON,1,[$CC,$FF,$A5]
PAUSE 25

But instead, you need to READ the scratchpad memory to get the correct authorization code, so that you can do a "Copy Scratchpad" that actually writes it to EEPROM.

After that, you can do the "Read Memory" command. (With 2-byte address again)

Look at the COPY SCRATCHPAD [55h] section in the datasheet.

hth,

crhomberg
- 2nd October 2008, 00:49
Thanks Darrel,

Does this mean that I don't have to use the CRC calculation system?

Regards

Chris

Darrel Taylor
- 2nd October 2008, 00:51
Does this mean that I don't have to use the CRC calculation system?
Unless you want a program that's not susceptible to spurious noise Errors, NO, you don't have to use CRC's.

crhomberg
- 2nd October 2008, 14:35
Hi,

I modified my program using the authorisation code but I still seem not to get my data back.
This is my latest version (for a 16F877A)

'************************************************* ***************
'* Name : eeprom.BAS *
'* ************************************************** ************
@ device hs_OSC, wdt_on, pwrt_on, protect_off
DEFINE OSC 20

ADCON1=7 ' Set all Ports to Digital I/O
OPTION_REG.7=0 ' Enable weak PullUps


TRISC.0=1
TRISC.1=1
TRISC.2=1
TRISB.0=0

iButton var PortD.2

MEM VAR BYTE
RESULT VAR BYTE[4]
Taddr var byte[2] 'target address 2 bytes
ES var byte 'ending offset data status

START:
SEROUT2 PORTB.0,16416,["START",10,13]

'WRITE TO EEPROM
OWOUT ibutton,1,[$CC,$0F,$00,$00] 'Write to scratchpad
OWOUT IBUTTON,0,[$54,$65,$73,$74] 'Write characters "test"
OWout IBUTTON,0,[$CC,$AA]
owin ibutton,0,[Taddr[0],Taddr[1],ES] 'read back the scratchpad to get the Authorisation code
owout ibutton,0,[$CC,$55] 'Write back the contents of the target address & E/S register
owin ibutton,0,[Taddr[0],Taddr[1],ES] 'to allow copy scratchpad to take place.

PAUSE 25

'READ FROM EEPROM
OWOUT ibutton,0,[$CC,$F0,$00,$00]
FOR MEM=1 TO 32
OWIN IBUTTON,0,[RESULT]
SEROUT2 PORTB.0,16416,[RESULT]
PAUSE 100
NEXT MEM

END

Where have I gone wrong?

Regards

Chris

crhomberg
- 2nd October 2008, 14:43
Thanks Darrel,

I got it right, it is now send back the letters test with this code...

'WRITE TO EEPROM
OWOUT ibutton,1,[$CC,$0F,$00,$00] 'Write to scratchpad
OWOUT IBUTTON,0,[$54,$65,$73,$74] 'Write characters "test"
OWout IBUTTON,1,[$CC,$AA] 'get ready to read scratchpad
owin ibutton,0,[Taddr[0],Taddr[1],ES] 'read back the scratchpad to get the Authorisation code
owout ibutton,1,[$CC,$55] 'Write back the contents of the target address & E/S register
owout ibutton,0,[Taddr[0],Taddr[1],ES] 'to allow copy scratchpad to take place.

PAUSE 25

'READ FROM EEPROM
OWOUT ibutton,1,[$CC,$F0,$00,$00]
FOR MEM=1 TO 4
OWIN IBUTTON,0,[RESULT]
SEROUT2 PORTB.0,16416,[RESULT]
PAUSE 100
NEXT MEM

END

Thanks Chris

Darrel Taylor
- 2nd October 2008, 22:36
WooHoo! There ya go.
That looks much better.

Tricky little 1's and 0's in the mode.

Nice work!
<br>

crhomberg
- 4th October 2008, 00:34
I am so proud of the code I wrote for the DS1994 REAL TIME CLOCK & DS1973 EEPROM I think I must post both as they work so well and finally are relatively simple and there seems to be no sample code on the net.
Where on the site is the place for Code Samples?

Regards

Chris

Darrel Taylor
- 4th October 2008, 01:05
Careful, that feeling of "Elation" is addictive. :D

Code examples always welcome.
http://www.picbasic.co.uk/forum/forumdisplay.php?f=11