I2CRead / I2CWrite Problem?


Closed Thread
Results 1 to 3 of 3
  1. #1

    Default I2CRead / I2CWrite Problem?

    Good day

    I'm using a PIC16F877 with a 24LC512 EEPROM.
    All I do is write from 0 to 23 to the EEPROM and Read it back and then send it through RS232 to the PC.

    Here is my PB Code
    <hr>
    <pre>
    Include "modedefs.bas"

    DEFINE OSC 4 ' Set the Xtal frequency to 20mHz
    DEFINE LOADER_USED 1 ' bootloader
    DEFINE HSER_RCSTA 90h ' enable serial port,
    ' enable continuous receive
    define HSER_TXSTA 24h ' enable transmit,
    ' BRGH=1
    define HSER_SPBRG 103 ' set baudrate to 2400
    DEFINE HSER_CLOERR 1 ' automatic clear overrun error

    RCIF VAR PIR1.5 ' Receive interrupt flag (1=full , 0=empty)
    TXIF VAR PIR1.4 ' Transmit interrupt flag (1=empty, 0=full)

    SerialData var byte
    SWITCHPIN VAR PORTD.1

    SCL var PORTC.3 ' Clock pin
    SDA var PORTC.4 ' Data pin
    B0 var word ' Address
    B1 var byte ' Data 1
    B2 var byte ' Data 1
    i var word

    Main:
    'TRISC = %10000000 ' PORTC.7 is the RX input, PORTC.6 is the TX output
    TRISC.7 = 1
    TRISC.6 = 0
    pause 10 'safe start-up delay
    high SWITCHPIN
    ADCON1 = 7 ' Set PORTA and PORTE to digital
    for i = 0 to 23
    B2 = i
    I2CWRITE SDA,SCL,$A0,i,[B2] ' Write each location
    Pause 10 ' Delay 10ms after each write
    hserout ["Write=",#B2,"|"]
    next i
    loop:
    for i = 0 to 23
    I2CREAD SDA,SCL,$A0,i,[B1] ' Read 2 locations in a row
    hserout ["Read=",#B1,"|"]
    next i
    End
    </pre>
    <hr>


    Somehow the last item "23" is red back from the EEPROM as "2"
    Here is that data I received back from the Pic?
    <hr>
    Write=0|Write=1|Write=2|Write=3|Write=4|Write=5|Wr ite=6|Write=7|Write=8|Write=9|Write=10|Write=11|Wr ite=12|Write=13|Write=14|Write=15|Write=16|Write=1 7|Write=18|Write=19|Write=20|Write=21|Write=22|Wri te=23|Read=0|Read=1|Read=2|Read=3|Read=4|Read=5|Re ad=6|Read=7|Read=8|Read=9|Read=10|Read=11|Read=12| Read=13|Read=14|Read=15|Read=16|Read=17|Read=18|Re ad=19|Read=20|Read=21|Read=22|Read=2
    <hr>

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


    Did you find this post helpful? Yes | No

    Default

    Looks like your HSEROUT is where the problem is... flush the Transmit Buffer with a CR/LF... tell us what happens...

    either...
    Code:
    loop:
         for i = 0 to 23
           I2CREAD SDA,SCL,$A0,i,[B1] ' Read 2 locations in a row
           hserout ["Read=",#B1,13,10]        
         next i
    or this...

    Code:
    loop:
         for i = 0 to 23
           I2CREAD SDA,SCL,$A0,i,[B1] ' Read 2 locations in a row
           hserout ["Read=",#B1,"|"]        
         next i
         hserout [13,10]

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Thank you Melanie, Both methods are working perfect!!!!

Similar Threads

  1. I2CRead & I2CWrite not working as expected
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 30
    Last Post: - 27th October 2021, 19:36
  2. Problem with I2Cread and I2CWRITE function
    By Tony85 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th June 2006, 21:03
  3. Problem with I2Cread and I2CWRITE function
    By Tony85 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 6th June 2006, 19:32
  4. PFC8583 Connection and I2Cwrite I2cread!
    By uludere72 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 27th February 2006, 14:32
  5. I2CWRITE and I2CREAD
    By Tomas in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 23rd April 2004, 03:30

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