PDA

View Full Version : Need help to get I2CRead woriking



w7ami
- 28th November 2007, 21:26
I need help getting I2CRead to work. I am using an 18F4620 running at 4 MHz and a 24LC65 serial eeprom. I can write data to the eeprom but I can not read the data back out. I have an oscilloscope connected to the clock and data lines of the eeprom and I can see that it is sending data back to the pic. The data bus is two way and I can see both the data sent to and from the eeprom, see the attached file. The returned value of Data1 is always 0. What the hey?

; PIC18F4620
DEFINE OSC 4
;**************************************
; *** LCD SET UP ***
;
;Set up LCD display ports
DEFINE LCD_DREG PORTC ; Set LCD Data Port to port C
DEFINE LCD_DBIT 0 ; Fisrt data port is C.0
DEFINE LCD_RSREG PORTC ; Register Select Port is C
DEFINE LCD_RSBIT 4 ; Register Select Bit is 4
DEFINE LCD_EREG PORTC ; Set LCD Enable Port is C;
DEFINE LCD_EBIT 5 ; Set LCD Enable Bit is 5
DEFINE LCD_BITS 4 ; 4 bus lines
DEFINE LCD_LINES 4 ; 4 display lines
DEFINE LCD_COMMANDUS 2000 ; command delay in uS
DEFINE LCD_DATUS 50 ; Set data delay in uS
;
; ** Define LCD Control Constants **
I CON $fe ; Instruction Prefix
CLR CON $1 ; Clear the display
L1 CON $80 ; Line 1
L2 CON $C0 ; line 2
L3 CON $94 ; Line 3
L4 CON $D4 ; Line 4
;
;**************************************
;
; I2C Setup
;
SDA VAR PORTE.1 ; I2C data pin
SLC VAR PORTE.0 ; I2C clock pin
I2CAddress VAR WORD
I2CDevice VAR BYTE
Data1 VAR BYTE
;
I2CDevice = $A0
;
;**************************************

PAUSE 1000 ; Wait for LCD to initialize
LCDOUT I, CLR ; Clear LCD
PAUSE 1000
LCDOUT i, l4, "Ready"

; Write Pattern To eeprom
For I2CAddress = 0 To 20 STEP 2 ; Loop 10 times
I2CWRITE SDA,SLC,I2CDevice,I2CAddress,[%10101010, %01010101]
LCDOUT I, L1, DEC I2CAddress
Pause 10 ; Delay 10 mS after each write
Next

Loop:
For I2CAddress = 0 To 20 ; Loop 20 times
I2CREAD SDA,SLC,I2CDevice,I2CAddress,[Data1], BadRead
LCDOUT I, L2 ,DEC I2CAddress, " ", DEC Data1
PAUSE 500
NEXT
GOTO Loop
BadRead:
LCDOUT I, L3 , "Bad Read"
GOTO Loop
end

GrandPa
- 28th November 2007, 23:50
If you read carefully page 48 of the manual, you will find that you should first declare a variable for the values you want to write. Either a byte or word variable. Then you use this variable within the I2CWrite command.

Most EEPROM have page write and byte write capability. Your should either try to write a full page, or go one byte/word after the other (that's a single 'value' at a time). Anything else will result in 'strange' problems. See your EEPROM datasheet to make sure it support page write first, you will also find the size of the write buffer for your chip. But to stay on the safe side I would suggest you to go one byte or word at a time.


This: I2CWRITE SDA,SLC,I2CDevice,I2CAddress,[%10101010, %01010101]

should then be:

WordVar VAR WORD
...
WordVar = put any value here
I2CWRITE SDA,SLC,I2CDevice,I2CAddress,[WordVar]

OR

ByteVar VAR BYTE
...
ByteVar = put any value here
I2CWRITE SDA,SLC,I2CDevice,I2CAddress,[ByteVar]



Also, without the schematic I can only point some area for you to check:

-Since I2CDevice = $A0 = %10100000, you should make sure the A2,A1 and A0 pins of your EEPROM are at Vss (Ground)

-Are you using pull-up resistors on SCL and SDA line ?

-Did you make sure you turned off analog mode for SDA an SCL pins you're using? (if applicable)


Hope this will help.

J-P

mister_e
- 29th November 2007, 13:44
You should also check if the EEPROM WP pin is tied to GND, and your pull-up on I2C Bus are <4.7K

w7ami
- 29th November 2007, 16:22
"-Did you make sure you turned off analog mode for SDA an SCL pins you're using? (if applicable)"

Oh gee, that was bright. Thanks guys, I added ADCON1 = %1010 to make PortE a digital port, its analog by default, and it is working. I can't tell you how long I looked at that without seeing the light.

Terry