Need help to get I2CRead woriking


Closed Thread
Results 1 to 4 of 4
  1. #1
    w7ami's Avatar
    w7ami Guest

    Angry Need help to get I2CRead woriking

    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
    Attached Images Attached Images  
    Last edited by w7ami; - 29th November 2007 at 00:19. Reason: Correct typo and add file

  2. #2
    Join Date
    Jul 2007
    Posts
    53


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by GrandPa; - 29th November 2007 at 05:50.

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    You should also check if the EEPROM WP pin is tied to GND, and your pull-up on I2C Bus are <4.7K
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    w7ami's Avatar
    w7ami Guest


    Did you find this post helpful? Yes | No

    Smile Need help to get I2CRead woriking

    "-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

Similar Threads

  1. I2CRead and USART interrupt
    By HenrikBuhl in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 10th January 2007, 14:04
  2. I2cwrite - I2cread
    By savnik in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 5th July 2006, 02:12
  3. Problem with I2Cread and I2CWRITE function
    By Tony85 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th June 2006, 21:03
  4. Problem with I2Cread and I2CWRITE function
    By Tony85 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 6th June 2006, 19:32
  5. PFC8583 Connection and I2Cwrite I2cread!
    By uludere72 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 27th February 2006, 14:32

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts