I need help with i2c memory


Closed Thread
Results 1 to 18 of 18
  1. #1
    Join Date
    Jun 2005
    Posts
    7

    Default I need help with i2c memory

    For my senior project we are using a pic16f84. A large part of the project is storing data in a serial memory chip usint i2c. ive spent 5ish hours trying to figure this out. went through data sheets and searched online, still no success. i am confident my circuit is wired correctly. any help would be greatly appriciated.

    the memory used currently is microchip's 24AA512, we also have a 24c01 laying around to play with as well.
    i have 2 pull up resistors (5.1k) on scl and sda
    write protect is grounded

    here is my code,

    addr var byte
    sda var byte
    scl var byte
    contR con %10100001
    contW con %10100000
    x var byte 'test data'
    sda=PORTB.0
    scl=PORTB.1

    addr=%00000000
    x=3 'test data is the number 3'
    pause 100 'pause just for fun'

    I2Cwrite sda,scl,contW,addr,[x] 'should write 3 to address 0, right?'
    pause 10
    I2Cread sda,scl,contR,addr,[x] 'should read the data'
    pause 10
    serout Portb.2,4,[x] 'displays data on an LCD'


    end

    Thanks!
    Last edited by -c3-; - 16th June 2005 at 00:04.

  2. #2
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Talking

    Make your addr a word variable, not byte.

    Also make sure you assign a value to your addr before the write command.

    Example:

    Addr=3
    I2Cwrite sda,scl,contW,addr,[x] 'should write 3 to address 0, right?'
    pause 10

    Finally you may want to add DEFINE I2C_SLOW 1 if things are still not working.

    Basically if your wired like the manual says and you do the above it works great.
    Last edited by DynamoBen; - 16th June 2005 at 00:07.

  3. #3
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Try this:
    Code:
    addr var word
    sda var PORTB.0
    scl var PORTB.1
    cont con %10100000
    x var byte 'test data'
    addr=%00000000
    x=3 'test data is the number 3'
    
    I2Cwrite sda,scl,cont,addr,[x] 'should write 3 to address 0, right?'
    pause 10
    I2Cread sda,scl,cont,addr,[x] 'should read the data'
    pause 10
    serout Portb.2,4,[DEC x] 'displays data on an LCD'
    
    
    end
    Last edited by NavMicroSystems; - 16th June 2005 at 01:03.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  4. #4
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    Also keep in mind if your hoping to see the number three on the LCD you will need to modify your code a bit.

    serout Portb.2,4,[DEC x] 'displays ASCII version of data on an LCD'

  5. #5
    Join Date
    Jun 2005
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Tried that, still nothing. it makes me wonder if there is something with my circuit i am missing...(i think it is right...)

    if any one wants to check the wiring (its not messy)
    chip pinouts included

    http://www.siue.edu/~bluttin/circuit.htm

    thanks

    the middle chip is the uln2803, its just driving the leds, ive tried it connected, and unconnected.
    Last edited by -c3-; - 16th June 2005 at 01:33.

  6. #6
    Join Date
    Jun 2005
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by NavMicroSystems
    Try this:
    Code:
    addr var word
    sda var PORTB.0
    scl var PORTB.1
    cont con %10100000
    x var byte 'test data'
    addr=%00000000
    x=3 'test data is the number 3'
    
    I2Cwrite sda,scl,cont,addr,[x] 'should write 3 to address 0, right?'
    pause 10
    I2Cread sda,scl,cont,addr,[x] 'should read the data'
    pause 10
    serout Portb.2,4,[DEC x] 'displays data on an LCD'
    
    
    end

    when i go to compile i get "bad expression" on serout Portb.2,4,[DEC x]
    i am using microcode studio.

  7. #7
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    You are missing the two pull-up resistors that are required.

    Try wiring it identical to the Melabs user manual. This is your best shot at getting it to work. I have included the drawing in this post.
    Attached Images Attached Images  

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by -c3-
    when i go to compile i get "bad expression" on serout Portb.2,4,[DEC x]
    i am using microcode studio.
    what about serout Portb.2,4,[#x]???
    Steve

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

  9. #9
    Join Date
    Jun 2005
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e
    what about serout Portb.2,4,[#x]???

    that compiles, only problem now is i killed my lcd...... needless to say, from now on i will be a little more careful of which voltage supply i am using.... little puffs of smoke are bad....
    Last edited by -c3-; - 16th June 2005 at 06:54.

  10. #10
    Join Date
    Jun 2005
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by DynamoBen
    You are missing the two pull-up resistors that are required.

    Try wiring it identical to the Melabs user manual. This is your best shot at getting it to work. I have included the drawing in this post.
    you are talking about the 2 pull up resistors on scl and sda correct? they are there. as far as i can tell the only difference between what i have and your picture is the cap from vdd to ground on pin 14. ill put that in and see what happens..
    Last edited by -c3-; - 16th June 2005 at 06:54.

  11. #11
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    You may want to wire it exactly like the drawing for testing purposes.

  12. #12
    Join Date
    Jun 2005
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    alright, ive spent some more time on this, with my new lcd. redid the circuit to the schematic posted, still not working for me, it seems it is not writing at all,

    do i need to set trisb to something? thats my current line of thought,
    thx

  13. #13
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    Whats not working the LCD or the EEProm?

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


    Did you find this post helpful? Yes | No

    Default

    Usually you don't need to set TRIS for those built-in command like SERIN/SEROUT/LCDOUT and of course for I2CREAD/I2CWRITE.

    For the last, TRIS will change depending if you read ar write from an external I2C device.

    What about your whole stuff??? schematic, Parts, Code???
    Steve

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

  15. #15
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I think you should go back up and take another look at Ralph's post.

    I know you had an LCD problem at the time, so you may have overlooked it, but it does have 2 important points.

    Most important is the declarations of (sda and scl). Originally you had ...

    sda var byte
    scl var byte
    . . .
    sda=PORTB.0
    scl=PORTB.1

    While it may seem that this is assigning PORTB.0 to the sda variable, it doesn't work that way. What it really does is Read PORTB.0 (which is 1 due to the Pull-up resistor) then place the value in sda. Then when you try to use sda, it get's interpreted as a Pin Number (0-15). A 1 as a Pin Number points to PORTB.1

    The same goes for scl, so both sda and scl will end up pointing to PORTB.1

    The second big point was mentioned by Ben. The address of a 64Kx8 EEPROM has to be a WORD sized variable, not BYTE sized.

    A third point that's not as critical, is shown in ralph's example. You only need to have 1 Control Byte. PBP automatically adjusts the value of the control byte depending on whether iit's a Read or a Write operation. The manual specifically says to leave bit 0 "Clear".

    Oh yeah, a small typo, it should have been Serout2, that's why you got the compiler error previously.

    HTH,
       Darrel

  16. #16
    Join Date
    Jun 2005
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    hey! its working,
    the problem i was having was in declaring tris when reading/writing to the memory. ive got it working now and am very happy. thankyou all for the help, heres my current code,

    trisb=%00000010 'read i comment one out
    'trisb=%00000001 'write
    addr var byte
    sda var PORTB.0
    scl var PORTB.1
    cont con %10100000
    x var byte 'test data
    y var byte 'read data


    'x=%00000001 'test data is the number 0'

    addr=0
    loop:

    'I2Cwrite sda,scl,cont,(addr),[x] 'comment out when reading
    'pause 20
    i2Cread sda,scl,cont,addr,[y] 'should read the data
    pause 20
    serout portb.2,4,[254,1]

    serout Portb.2,4,["y=",#y," x=",#x," a=",#addr] 'visual verification
    addr=addr+1
    'x=x+%00000001

    if addr=255 then end

    goto loop

    end

  17. #17
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    I have used external I2C EEProm on a number of projects and have NEVER had to change the TRIS statement to get things to work. Also you STILL have you address variable as a byte, it needs to be a word.

    There is still something horribly wrong with either the software or hardware.
    Last edited by DynamoBen; - 23rd June 2005 at 18:45.

  18. #18
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    In your first message, you mentioned 2 different EEPROMs.

    24AA512 and 24c01

    Which one are you currently having success with?

    The 24AA512 is a 64Kx8 device and needs a WORD sized address.
    The 24c01 is a 128x8 and only needs a BYTE sized address.

    My guess is that you're using the 24c01.

    If that's the case, then your test routine loops through 255 address locations, which is double the actual size of the EEPROM. Anything above 127 will loop around and display the first 128 again. Of course, it's just a test routine, but just wanted to let you know.

    Best regards,
       Darrel

Similar Threads

  1. I2C Master/Slave 16F88/16F767 working code
    By DanPBP in forum Code Examples
    Replies: 2
    Last Post: - 23rd October 2012, 22:31
  2. HARDWARE I2C SAMPLE CODE question
    By Michael Wakileh in forum Code Examples
    Replies: 2
    Last Post: - 16th June 2009, 21:07
  3. I2C Master Slave issues.
    By cpayne in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 29th March 2008, 19:33
  4. A help with Write/Read a I2C memory
    By Eugeniu in forum mel PIC BASIC Pro
    Replies: 25
    Last Post: - 28th March 2008, 13:53
  5. Please help with i2cslave i2c slave
    By cycle_girl in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 1st December 2005, 13:55

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