I2C.BSP & BS2P - Can't write, but reads already writen EPROM properly.
Hello.
I have BS2P & stamps in class board and wanted to work with I2C. I've copied the program I2C.BSP from reference manual, and attached EPROM according to schematics. One difference is that I have 24C32 eprom, while program says 24LC16. The code is below:
It stops, because does not reads what expected. But, if I comment the code: IF (result(check) <> value) THEN Error
The read operation continiues successfully, and I can see the contents of eeprom, it being read properly.
What can be the cause?
I have BS2P & stamps in class board and wanted to work with I2C. I've copied the program I2C.BSP from reference manual, and attached EPROM according to schematics. One difference is that I have 24C32 eprom, while program says 24LC16. The code is below:
' I2C.bsp
' This program demonstrates writing and reading every location in a 24LC16B
' EEPROM using the BS2p/BS2pe's I2C commands. Connect the BS2p, BS2pe, or
' BS2px to the 24LC16B DIP EEPROM as shown in the diagram in the I2CIN or
' I2COUT command description.
' {$STAMP BS2p}
' {$PBASIC 2.5}
#IF ($STAMP < BS2P) #THEN
#ERROR "Program requires BS2p, BS2pe, or BS2px."
#ENDIF
SDA PIN 1 ' I2C SDA pin
SCL PIN 0
addr VAR Word ' internal address
block VAR Nib ' block address in 24LC16
value VAR Byte ' value to write
check VAR Nib ' for checking retuned values
result VAR Byte(16) ' array for returned value
Write_To_EEPROM:
DEBUG "Writing...", CR
PAUSE 2000
FOR addr = 0 TO 2047 STEP 16 ' loop through all addresses
block = addr.NIB2 << 1 ' calculate block address
value = addr >> 4 ' create value from upper 8 bits
' write 16 bytes
I2COUT SDA, $A0 | block, addr, [REP value\16]
PAUSE 5
DEBUG "Addr: ", DEC4 addr, "-", DEC4 addr + 15, " ",
"Value: ", DEC3 value, CR
NEXT
PAUSE 2000
Read_From_EEPROM:
DEBUG CR, "Reading...", CR
PAUSE 2000
FOR addr = 0 TO 2047 STEP 16
block = addr.NIB2 << 1
value = addr >> 4
I2CIN SDA, $A1 | block, addr, [STR result\16]
FOR check = 0 TO 15
IF (result(check) <> value) THEN Error
NEXT
DEBUG "Addr: ", DEC4 addr, "-", DEC4 addr + 15, " ",
"Value: ", DEC3 result, CR
NEXT
PAUSE 100
DEBUG CR, "All locations passed"
END
Error:
DEBUG "Error at location: ", DEC4 addr + check, CR,
"Found: ", DEC3 result(check), ", Expected: ", DEC3 value
END
It stops, because does not reads what expected. But, if I comment the code: IF (result(check) <> value) THEN Error
The read operation continiues successfully, and I can see the contents of eeprom, it being read properly.
What can be the cause?
Comments
Tried to play with write protect pin - no changes
http://forums.parallax.com/showthread.php/82908-I2C-On-A-BASIC-Stamp
If not, this is a good place to start:
http://www.nxp.com/documents/user_manual/UM10204.pdf
This is straight from the horse's mouth (aka "the people that created I2C"). :thumb:
- SDA must be pin 0 or pin 8. Doesn't matter if you define SCL, it is always the "next pin up" from SDA. So if SDA is pin0, then SCL must be pin1. If SDA is pin8, SCL must be pin9. On the BS2p40 you can use either MAIN or AUX pins, btw.
- I believe you are assigning your address wrong, unless you have multiple EEPROMs on the I2C bus. The first byte in the I2C stream is the chip address, which is controlled by the address pins on the EEPROM. The last bit of this first byte address is the read/write bit; on the BS2p you do NOT need to set this bit, as I2COUT/I2CIN set it automatically (but it never hurts to set it if you want code clarity). The next byte(s) are the address within the chip to write to. You are ORing the "block" variable onto your address which I don't think is doing what you intend.
Post your most recent code if you are still having trouble.
It might help if you post the most recent code you tried and even a photo of your wiring (for example, are you sure that the address select pins on the EEPROM are wired properly, etc.).
Then revisit your code and look at my earlier comments.
Also, the data sheets specify a maximum page write time of 5 ms for the 24LC16B and 10 ms for the 24C32. You might want to try changing the PAUSE 5 statement that follows I2COUT... to PAUSE 10.
And make sure you have changed the pin assignments in the program so that SDA is Pin 0 and SCL is pin 1.
If the EEPROM still won't write then you probably have a faulty device.