I2C with External Eeprom


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Code:
    DEFINE OSC 8
    INCLUDE "modedefs.bas" 
    @ DEVICE INTRC_OSC
    @ DEVICE MCLR_OFF
    @ DEVICE PROTECT_OFF
    @ DEVICE CPD_OFF
    @ DEVICE LVP_OFF
    @ DEVICE BOD_OFF
    @ DEVICE PWRT_OFF
    @ DEVICE WDT_OFF
    DEFINE DEBUG_REG PORTB  
    DEFINE DEBUG_BIT 7
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_MODE 1
    osccon=$7e:cmcon=7:adcon1=$80:ansel=1:adcon0=$c1:porta=0:portb=0:trisa=1
    trisb=7:sda var portb.0:scl var portb.1:addr var word:dat var byte
    dat2 var byte:ctw con $a0
    start:     dat=dat+1:addr=addr+1
    i2cwrite sda,scl,ctw,addr.highbyte,addr.lowbyte,[dat]:pause 10
    debug "Wrote: ",dec addr," with ",dec dat,13,10
    i2cread sda,scl,ctw,addr.highbyte,addr.lowbyte,[dat2]
    debug " Read: ",dec addr," val: ",dec dat2 , 13 , 10
    if dat = dat2 then
    debug "Write/Read Successful at ",dec5 addr," with ",dec3 dat,13,10
    else
    debug "Write/Read Error at ",dec5 addr,"-Wrote:",dec3 dat," - Read:",dec dat2,13,10
    endif
    pause 500:goto start
    END
    OR un-COLONized...

    Code:
    DEFINE OSC 8
    INCLUDE "modedefs.bas" 
    @ DEVICE INTRC_OSC
    @ DEVICE MCLR_OFF
    @ DEVICE PROTECT_OFF
    @ DEVICE CPD_OFF
    @ DEVICE LVP_OFF
    @ DEVICE BOD_OFF
    @ DEVICE PWRT_OFF
    @ DEVICE WDT_OFF
    DEFINE DEBUG_REG PORTB  
    DEFINE DEBUG_BIT 7
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_MODE 1
    osccon = $7e
    cmcon = 7
    adcon1 = $80
    ansel = 1
    adcon0 = $c1
    porta = 0
    portb = 0
    trisa = 1
    trisb = 7
    sda var portb.0
    scl var portb.1
    addr var word
    dat var byte
    dat2 var byte
    ctw con $a0
    
    start:
    dat = dat + 1
    addr = addr + 1
    i2cwrite sda , scl , ctw , addr.highbyte , addr.lowbyte , [ dat ]
    pause 10
    debug "Wrote: " , dec5 addr , " with " , dec3 dat , 13 , 10
    i2cread sda , scl , ctw , addr.highbyte , addr.lowbyte , [ dat2 ]
    debug " Read: " , dec5 addr , " val: " , dec3 dat2 , 13 , 10
    if dat = dat2 then
    debug "Write/Read Successful at " , dec5 addr , " with " , dec3 dat , 13 , 10
    else
    debug "Write/Read Error at " , dec5 addr , "-Wrote:" , dec3 dat , " - Read:" , dec dat2 , 13 , 10
    endif
    pause 500
    goto start
    END
    Last edited by skimask; - 20th February 2008 at 23:42.

  2. #2


    Did you find this post helpful? Yes | No

    Default

    Hi skimask,

    i'm pleased to say that your code worked. Thank you very much for your help

    Will re-write my entire code from scratch.

    I have some notes here and i will see what happens.

    .

  3. #3
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Cool

    Quote Originally Posted by ruijc View Post
    Hi skimask,
    i'm pleased to say that your code worked. Thank you very much for your help
    Will re-write my entire code from scratch.
    I have some notes here and i will see what happens.
    .
    Really?
    That's amazing...because I did a quick search a couple days ago for 24LC512 here on THESE forums and, except for COLON-izing the one listing, practically did a cut-and-paste.
    Search engine must've been broken that day...huh?

  4. #4


    Did you find this post helpful? Yes | No

    Default

    I guess i missed that one, sorry.

    But thanks anyway for your help

    .

  5. #5


    Did you find this post helpful? Yes | No

    Default

    Greetings,

    While reviewing my code i changed the I2C lines from :

    I2Cread SDA,SCL,CTW,mem,[x]

    to

    I2Cread SDA,SCL,CTW,mem.highbyte,mem.lowbyte,[x]

    to make it work.

    Sorry if i'm a bit stubborn ( just trying to learn ), but i re-made ( yes... re-made ) the search and as before almost every one mentions the I2C lines as follows :


    I2cwrite SDA,SCL,CONTROL,ADDR,[DATA]

    Posts like:
    http://www.picbasic.co.uk/forum/show...hlight=24lc512

    http://www.picbasic.co.uk/forum/show...hlight=24lc512

    http://www.picbasic.co.uk/forum/show...hlight=24lc512

    Including melanie's post

    http://www.picbasic.co.uk/forum/showthread.php?t=587

    The only one that mentions to separate the address in high and low somehow did not worked in that case

    http://www.picbasic.co.uk/forum/show...hlight=24lc512


    My question is just to try to understand

    Why is that the i2c line ( I2cwrite SDA,SCL,CONTROL,ADDR,[DATA] ) works with other pics ( yes...all my programs with 12F675 work ) and with the 16F88 does not ? Why with this pic i need to have the address separated with high and low byte ?

    Also ( question for skimask )
    In post # 7 you mention :

    Code:
    'cause I see a few flaws in your logic in the code written a few posts back.
    Can you please point where the flaws are so i can take a look at them ?

    Thanks

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ruijc View Post
    Why is that the i2c line ( I2cwrite SDA,SCL,CONTROL,ADDR,[DATA] ) works with other pics ( yes...all my programs with 12F675 work ) and with the 16F88 does not ? Why with this pic i need to have the address separated with high and low byte ?
    Quite frankly, if it works, who cares?
    Actually, I don't remember the answer to that at the moment. I'll remember eventually...

    Also ( question for skimask )
    In post # 7 you mention :
    Code:
    'cause I see a few flaws in your logic in the code written a few posts back.
    Can you please point where the flaws are so i can take a look at them ?
    Thanks
    You'll figure it out... Suffice to say there's just MUCH better ways to do some things, more reliable, faster, longer, stronger, and so on...

  7. #7


    Did you find this post helpful? Yes | No

    Default

    I've had to make some changes in my logic to work with the new I2C lines ( low/high address ) but still maintained the generic structure.

    This was my priority #1

    After checking if it works will study on those flaws .

    I undestand your point of "if it works, who cares", but if we leave things like that we will never get to improve the codes to be more reliable, faster and shorter

    Dont you agree ?

    In the mean time, if you remember please let me know

    Thanks again
    .

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. Can't read sequential addresses in external EEPROM
    By tjkelly in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th February 2010, 14:46
  3. Replies: 1
    Last Post: - 28th January 2010, 22:15
  4. Problem with I2C EEPROM addressing
    By Atom058 in forum General
    Replies: 14
    Last Post: - 3rd November 2009, 03:17
  5. HARDWARE I2C SAMPLE CODE question
    By Michael Wakileh in forum Code Examples
    Replies: 2
    Last Post: - 16th June 2009, 21:07

Members who have read this thread : 0

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