Shop OBEX P1 Docs P2 Docs Learn Events
24c16 — Parallax Forums

24c16

ArchiverArchiver Posts: 46,084
edited 2000-03-31 03:39 in General Discussion
this might help you may need to change how you address using a word rather than
a byte



' FM24C16.BS2
'
' Illustrates how to write and read to and from the
' RAMTRON FM24C16 16K EEPROM
'
' BS2 FM24C16
'
' Pin15
SCL
To Other
' Pin14
SDA
I2C Devices
'
' Note that 10K pullup resistors to +5VDC are required
' on both signal leads
'
' Note that the slave address, determined by A2, A1 and A0,
' Is used as the page address in this device. Other FM24C16
' devices on the bus are NOT supported.
'
' Debug and Pause statements were included only to see what is going on.
'
' Orginal work 24LC32.BS2 is
' copyright Peter H. Anderson, MSU, March 1, '97
' modified by
' Mike Wingstrom, Adaptive Systems, Ltd. 4/26/99
'

address var word ' ppp aaaa aaaa
' 000 0000 0000 - 111 1111 1111

page var address.highbyte ' ppp
pageMsk con %00000111
addr var address.lowbyte ' aaaa aaaa

dta var byte ' data to program

o_byte var byte ' byte to send to memory
i_byte var byte ' byte fetched from memory
old_val var byte ' Old value

n var byte ' index
b var bit ' bit
ack_bit var bit

SDA_PIN con 14
SCL_PIN con 15
SDA_OUT var out14
SCL_OUT var out15
SDA_IN var in14
SDA_DIR var dir14

OUT con 1
IN con 0

' Start of program

dirs = $0000

main: 'Read a location, incrememt it, and write it back
' then read it again to verify write
address = $341 ' use address $341

' debug "Read Data", CR
gosub read_random_data ' read from location
old_val = i_byte

' debug "Write Data", CR
dta = old_val + 3 ' Make new data
gosub write_random_data ' write dta to address $341

' debug "Read Data", CR
gosub read_random_data ' read from same location

debug hex ?old_val, hex ?dta, hex ?i_byte, CR

goto main
stop

write_random_data: 'writes specified data to specified address
' debug "Enter WRD",CR

gosub start

page = page & pageMsk
o_byte = $a0 | (page << 1) ' 1 0 1 0 a2 a1 a0 0
' debug "Slave Addr >"
gosub out_byte
gosub nack

o_byte = addr ' low byte of address
' debug " Word Addr >"
gosub out_byte
gosub nack

o_byte = dta ' data
' debug " Data >"
gosub out_byte
gosub nack

gosub sstop

return

read_random_data: ' reads data from specified address
' returns in variable in_byte
gosub start
page = page & pageMsk
o_byte = $a0 | (page << 1) ' 1 0 1 0 a2 a1 a0 0
' debug "Slave Addr >"
gosub out_byte
gosub nack

o_byte = addr ' low byte of address
' debug " Word Addr >"
gosub out_byte
gosub nack

gosub start
o_byte = $a0 | (page << 1) | $01 ' 1010 a2 a1 a0 1
' debug "Slave Addr >"
gosub out_byte
gosub nack

' debug " Read Data >"
gosub get_byte
gosub nack

gosub sstop
return

out_byte:

low SDA_PIN

for n = 0 to 7
b = (o_byte >> 7) & 1
if (b = 1) then out_one
SDA_DIR = OUT
' debug "o"
_clk:
high SCL_PIN
' pause 100
low SCL_PIN
' pause 100
o_byte = o_byte << 1
next
SDA_DIR = IN
return

out_one:
SDA_DIR = IN
' debug "i"
goto _clk


get_byte:
SDA_DIR = IN 'input

i_byte = 0
for n = 0 to 7
' pause 200
high SCL_PIN
' pause 200
i_byte = (i_byte << 1) | SDA_IN
' debug dec SDA_IN
low SCL_PIN

next

SDA_DIR = OUT 'output
return

nack:
SDA_DIR = IN ' input just in case
ack_bit = 1
high SCL_PIN
ack_bit = SDA_IN
' debug " A", dec ack_bit, $0d
low SCL_PIN
SDA_DIR = OUT ' output
return

ack:
' debug "POLL", CR
gosub start
o_byte = $a0 | (page << 1)
gosub out_byte
gosub nack

return

start:
high SDA_PIN
high SCL_PIN
' debug "START", $0d
low SDA_PIN 'bring SDA low while clock is high
low SCL_PIN
return

sstop:
low SDA_PIN
high SCL_PIN
' pause 10
high SDA_PIN 'bring SDA high while clock is high
' debug "STOP", $0d
return
Sign In or Register to comment.