PDA

View Full Version : Writing & Reading to iButton EEPROM



crhomberg
- 6th October 2008, 13:32
'************************************************* ***************
'* How to write and then read back from an iButton EEPROM *
'* DS1973 or the equivalent 1-wire device, the DS2433 EEPROM. *
'* : All Rights Reserved *
'************************************************* ***************
@ device hs_OSC, wdt_on, pwrt_on, protect_off

DEFINE OSC 20
iButton var PortD.2 'Connected iButton on 16F877 at this port.

RESULT VAR BYTE[19] 'Byte array to put string of characters into.
IN VAR BYTE 'Byte to collect the 1 byte you want to read.
Taddr var byte[2] 'Target address 2 bytes (TA1 & TA2)
ES var byte 'Ending offset data status (E/S)

IN=0
RESULT=0

'(Just connect your serout to to the serial communicator @ 19200)

START:

'------------------------------------------------
'WRITE TO EEPROM
'------------------------------------------------
OWOUT ibutton,1,[$CC,$0F,$00,$00] 'Write to scratchpad

OWOUT IBUTTON,0,["Testing this EEPROM",10,13] 'Write a few characters or values into EEPROM
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
'to allow copy scratchpad to take place.
owout ibutton,0,[Taddr[0],Taddr[1],ES] 'Enter Authorisation code as TA1,TA2,E/S

PAUSE 25 'Give EEPROM time to write


'-----------------------------------------------
'READ STRING OF BYTES FROM EEPROM
'-----------------------------------------------
OWOUT ibutton,1,[$CC,$F0,$00,$00] 'Read scratchpad
OWIN IBUTTON,0,[STR RESULT\7] '
PAUSE 1 'Not really nessesary
SEROUT2 PORTD.7,16416,[RESULT],10,13] 'You should get the full string you put in.


'------------------------------------------------
'READ 1 BYTE FROM SPECIFIC ADDRESS
'------------------------------------------------
OWOUT ibutton,1,[$CC,$F0,$010,$00] 'Read from address $10, first byte of
'target address is the Least Significant.

OWIN IBUTTON,0,[IN]
SEROUT2 PORTD.7,16416,[IN] 'You should get the letter "T"

END

Archangel
- 6th October 2008, 18:14
Thanks Chris !

mackrackit
- 6th October 2008, 20:40
Nice job! Will be useful.