I2c write&read


Closed Thread
Results 1 to 16 of 16

Thread: I2c write&read

  1. #1
    Join Date
    Apr 2008
    Posts
    85

    Default I2c write&read

    Hi every one,
    i ahce a problem with writing and reading using I2C WRITE...

    thansk for any help,
    -----------------

    here is the connection:

    pic16f628: porta.6 and 7 are SDA & SCL

    24lc256 (8 bit EEPROM): A0,1,2,3,4 and WP: ground
    SDA &SCL connected 10k to 5V...
    ------------------------
    ;the program:


    CMCON = 1
    TRISA = %11000000 ; set port A6,7 to input (SDA & SLC)
    TRISB = 0
    DPin var PORTA.6 ' I2C data pin
    Cpin var PORTA.7 ' I2C clock pin

    I var byte
    S VAR BYTE
    b1 var byte
    k var byte

    B0 var byte

    i = 0
    b0 = 0

    while b0 != $ff
    lookup I, [$3F,$48,$48,$48,$3F,$3F,$00,$ff],B0 ;put a value into
    ;B0
    I2CWRITE DPIN,CPIN,$A0,i,[B0] ' Write B0 to address I

    Pause 10 ' Delay 10ms after each write
    i = i+1 ' increase the address by 1
    wend
    loop:
    ;reading process
    b1 = 0 ; clear b1
    while b1 !=$ff
    I2CREAD DPIN,CPIN,$A1,i,[B1]

    FOR K = 0 TO 63 ; scan for 64 times
    CALL RESET
    PORTB = b1
    CALL DELAY

    NEXT K
    i = i +1
    wend
    goto loop
    end
    RESET: PORTA.1 = 1 ;RESET 4017
    @NOP
    PORTA.1 = 0
    RETURN

    DELAY: pause 10
    portb = 0 ; reset portb
    porta.0 = 1 ; clock the 4017
    @nop
    porta.0 = 0
    return

  2. #2
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post Faq

    Read this thread: I2CRead & I2CWrite not working as expected

    BR,
    -Gusse-

  3. #3
    Join Date
    Apr 2008
    Posts
    85


    Did you find this post helpful? Yes | No

    Default

    thanks for your words,

    i checked but it is ok, the device is 8 bit size. and the address is variable..
    all are defined by BYTE.
    i.e i = 0 to 10 ;(i : location in the EEPROM)


    thanks agains
    MInhle

  4. #4
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post

    Hi MInhle,

    Check once more the thread and do like Melanie advices!

    Define I2CDevice as a BYTE, not a constant.
    Define I2CAddress as a WORD (your EEPROM is >16k and therefore variable must be a word)

    Example:
    I2CWrite SDA,SCL,I2CDevice,I2CAddress,[DataA]

    BR,
    -Gusse-
    Last edited by Gusse; - 4th January 2010 at 08:25.

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    10K is also a little high... drop it by at least half to say 4K7.

  6. #6
    Join Date
    Apr 2008
    Posts
    85


    Did you find this post helpful? Yes | No

    Default

    Hello All,

    the deviec is 16f628. the OSC 20Mhz (internal OSC, so R is ~10k-12K)
    and the 24lc256 is BYTE SIZE. (32K X8 BIT)
    the address is variable.

    i will change to 4.7k and redefine eeprom address.

    however the fuse defined: (work well )

    OSC: INTRO CLOCKOUT
    MCLR: INPUT PIN
    (all other defined in the fuse window: marain unchange)

    thank for all

    regards,

  7. #7
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post Rtfm ;)

    Hi MInhle,

    Check the link and see the table.
    I2CRead/I2CWrite part of PicBasic Pro manual

    What is address size for over 24LC16 (2K bytes) devices?


    BR,
    -Gusse-

  8. #8
    Join Date
    Apr 2008
    Posts
    85


    Did you find this post helpful? Yes | No

    Default

    thank Gusse,

    i replaced data address (I: in this case) by WORD . . and/or change R from 10K to 6K. ..it doesnot work.

    (in the data sheet of 24lc256 dooesnot mention anything data address.

    VOLTAGE drop across: R_SCL (10K or 6K) : ~5V (unchanged)
    R_SDA (10K or 6K): ~2.5V

    !! is it possible the EEPROM: DEATH??

  9. #9
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post

    Hi MInhle,

    If you are running with internal oscillator then speed is 4MHz, not 20MHz. Also you should tell that to compiler.
    14.2.6 INTERNAL 4 MHZ OSCILLATOR
    The internal RC oscillator provides a fixed 4 MHz
    (nominal) system clock at VDD = 5V and 25°C, see
    “Electrical Specifications” section for information on
    variation over voltage and temperature.
    Code:
    DEFINE OSC 4
    Quote Originally Posted by MINHLE View Post
    OSC: INTRO CLOCKOUT
    14.2.7 CLKOUT
    The PIC16F62X can be configured to provide a clock
    out signal by programming the configuration word. The
    oscillator frequency, divided by 4 can be used for test
    purposes or to synchronize other logic.
    CLKOUT is RA6, which is also your I2C data pin. Disable CLKOUT or change I2C data pin to other IO.

    If you are not using comparator then disable those.
    Code:
    CMCON = 7
    I hope these modifications will bring up some life to your EEPROM interface.

    BR,
    -Gusse-

  10. #10
    Join Date
    Apr 2008
    Posts
    85


    Did you find this post helpful? Yes | No

    Default

    hello GUSSE,

    i got a BONUS now. the led is on (pin2 only) and flash

    i lookk for error in teh program and let u knoow later

    thanks for your help

  11. #11
    Join Date
    Apr 2008
    Posts
    85


    Did you find this post helpful? Yes | No

    Default

    HI Gusse,

    the modifications were made.
    as indicated.

    defined OSC 4 ;no effect: ( INTCLOUT is set in the fuse window)
    SDA PORTA.3
    SCL PORRT.4
    ......

    R_SDA & R_SCL: 4.5k
    a led on portb flashed is due to error that i cannot see where it comes from


    not write nor read. i assume EEPROM???

  12. #12
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post Test script

    Hi MInhle,

    Below is very simple I2C write and read test script.

    Code:
    <code><font color="#000000">CMCON = 7
    TRISA = %11000000
    TRISB = %00000000
    
    SDA                 <b>VAR </b>PortA.3
    SCL                 <b>VAR </b>PortA.4     
    LED                 <b>VAR </b>PortB.0     <font color="#000080"><i>'Indicator LED (change if needed)    
    
    </i></font>I2CDeviceEEprom     <b>VAR BYTE    
    </b>I2CAddressEEprom    <b>VAR WORD
    </b>TEMP1               <b>VAR BYTE
    </b>TEMP2               <b>VAR BYTE
    
    </b>I2CDeviceEEprom=$A0     <font color="#000080"><i>'DeviceID 10100000 = $A0 = %1010 A2=0, A1=0, A0=0 R/W=0
    
    </i></font><b>DEFINE </b>OSC 4
        
    Main:
        I2CAddressEEprom = 0
        TEMP1 = 1
        <b>GOSUB </b>Write_I2C
        <b>PAUSE </b>10
        <b>GOSUB </b>Read_I2C
        <b>IF </b>TEMP1 = TEMP2 <b>THEN
                HIGH </b>LED        <font color="#000080"><i>'If OK then 0.5s LED blink
                </i></font><b>PAUSE </b>500
                <b>LOW </b>LED
            <b>ELSE
                HIGH </b>LED        <font color="#000080"><i>'If FAIL then 2s LED blink
                </i></font><b>PAUSE </b>2000
                <b>LOW </b>LED
        <b>ENDIF
        GOTO </b>Main   
        
    Write_I2C:
        <b>I2CWRITE </b>SDA, SCL, I2CDeviceEEprom, I2CAddressEEprom,[ TEMP1 ]
        <b>RETURN
    
    </b>Read_I2C:
        <b>I2CREAD </b>SDA, SCL, I2CDeviceEEprom, I2CAddressEEprom,[ TEMP2 ]
        <b>RETURN
        
    END
    </b></code>
    If write and read are successfully executed then LED will blink for 0.5s.
    2s LED blink will be an indication of fail.

    BR,
    -Gusse-

  13. #13
    Join Date
    Apr 2008
    Posts
    85


    Did you find this post helpful? Yes | No

    Default

    hello Gusse,

    I copy you program. run ---> the light is solid.,



    i tr to put direcly data into EEProm, the out put is whatever,,,

    i.e.

    i = 0 ; data address
    b0 = $2a ; data to be stored

    i2cwrite .......... i, [b0]

    b0 = 0
    i2cread .........i,[b0]

    run ----> whatever... output (BONUS ONLY)

    i have to replace the EEPROm
    thanks for your time

    regards,

  14. #14
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post Small bug

    Hi,

    Sorry, I had one bug there. One pause missing.

    Code:
    <code><font color="#000000">CMCON = 7
    TRISA = %11000000
    TRISB = %00000000
    
    SDA                 <b>VAR </b>PortA.3
    SCL                 <b>VAR </b>PortA.4     
    LED                 <b>VAR </b>PortB.0     <font color="#000080"><i>'Indicator LED (change if needed)    
    
    </i></font>I2CDeviceEEprom     <b>VAR BYTE    
    </b>I2CAddressEEprom    <b>VAR WORD
    </b>TEMP1               <b>VAR BYTE
    </b>TEMP2               <b>VAR BYTE
    
    </b>I2CDeviceEEprom=$A0     <font color="#000080"><i>'DeviceID 10100000 = $A0 = %1010 A2=0, A1=0, A0=0 R/W=0
    
    </i></font><b>DEFINE </b>OSC 4
        
    Main:
        I2CAddressEEprom = 0
        TEMP1 = 1
        <b>GOSUB </b>Write_I2C
        <b>PAUSE </b>10
        <b>GOSUB </b>Read_I2C
        <b>IF </b>TEMP1 = TEMP2 <b>THEN
                HIGH </b>LED        <font color="#000080"><i>'If OK then 0.5s LED blink
                </i></font><b>PAUSE </b>500
                <b>LOW </b>LED
            <b>ELSE
                HIGH </b>LED        <font color="#000080"><i>'If FAIL then 2s LED blink
                </i></font><b>PAUSE </b>2000
                <b>LOW </b>LED
        <b>ENDIF</b>
        <b>PAUSE </b>1000        <font color="#000080"><i>'PAUSE ADDED</i></font>
        <b>GOTO </b>Main   
        
    Write_I2C:
        <b>I2CWRITE </b>SDA, SCL, I2CDeviceEEprom, I2CAddressEEprom,[ TEMP1 ]
        <b>RETURN
    
    </b>Read_I2C:
        <b>I2CREAD </b>SDA, SCL, I2CDeviceEEprom, I2CAddressEEprom,[ TEMP2 ]
        <b>RETURN
        
    END
    </b></code>
    Now it will blink.

    BR,
    -Gusse-

  15. #15
    Join Date
    Apr 2008
    Posts
    85


    Did you find this post helpful? Yes | No

    Default

    Hello Gusse,

    thanks for your help,

    the eeprom is wrong. I replace a another one. it is ok now.

    and the R_SCL and R_SDA are either 13K or 3,3 k the light intensity and frequency does not change (by naked eyes)

    thanks again
    Minh le

  16. #16
    Join Date
    Apr 2008
    Posts
    85


    Did you find this post helpful? Yes | No

    Default

    Hello Gusse,
    actually it is not necessary to " define OSC 4" it is defined in fuse ("INTCLOUT.."

    the 24lc256 it takes only 5ms to write

    thanks
    regards

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. PIC16F877 I2C programme
    By cooqo in forum mel PIC BASIC
    Replies: 3
    Last Post: - 21st April 2008, 10:02
  4. I2C Master Slave issues.
    By cpayne in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 29th March 2008, 19:33
  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 : 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