I2C Read/Write problems


Closed Thread
Results 1 to 24 of 24

Hybrid View

  1. #1

    Default I2C Read/Write problems

    Hello all,

    I did a search but didn't find anything that quite explains my problem, so here it goes:

    I'm attempting to do I2C communications with another (slaved) microcontroller. The other microcontroller is a purchased product so I have little control over the code loaded on it. The device ID (device address?) is $10, I'm attempting to read a 16bit word on the microcontroller that is stored at $22 and $23 on the microcontroller. The microcontroller uses I2C communications at 400Khz, but for some reason my clock is at 122Khz (PIC16f676, 20MHz crystal). I'm not 100% sure I'm using the I2C read command correctly :P. Here is my code thus far:

    define OSC 20

    'lines to use i2cread
    SDA var PORTA.0
    SCK var PortA.1
    'end lines to use i2cread

    'LCDOUT DEFINES
    define LCD_DREG PORTC
    define LCD_DBIT 0
    define LCD_RSREG PORTC
    define LCD_RSBIT 4
    define LCD_EREG PORTC
    DEFINE LCD_EBIT 5 'Register enable bit on portc.5
    define lcd_lines 4
    'END LCDOUT DEFINES


    Scntrl var byte
    sadd var byte
    Scntrl = $10
    sadd = $22
    myvar var word
    pause 2000
    main:
    lcdout $fe,1,"Dubugging..."
    i2cread PortA.0, PortA.1, Scntrl,sadd,[myvar.lowbyte, myvar.highbyte],fail

    'last:
    lcdout $fe,$c0,"Success!!!"
    lcdout $fe,$94, "myvar = ", #myvar
    goto done
    fail:
    LCDOUT $fe,$c0,"READ FAILED!!!"
    done:
    end

    NOTE: THE LCD Works fine.

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Dispersion123 View Post
    uses I2C communications at 400Khz
    Probably not 400Khz all the time, but a maximum of 400Khz...

    (PIC16f676, 20MHz crystal).
    Look carefully at the datasheet for the 'F676, specifically the RA0, RA1, and section 6 of the datasheet...

  3. #3
    Join Date
    Mar 2008
    Location
    Texas, USA
    Posts
    114


    Did you find this post helpful? Yes | No

    Default

    I don't think you can use the myval.Highbyte etc. in the I2C instruction.

    From the manual:

    "If a word-sized Var is specified, 2 bytes are read and stored into the Var high byte first, followed by the low byte. This order is different than the way variables are normally stored, low byte first."

    Looks like you just use the variable (WORD) as is. Pay attention to the loading order.
    No, I'm not Superman, but I did stay at a Holiday Inn Express last night!

  4. #4


    Did you find this post helpful? Yes | No

    Default

    I have tried just putting myvar instead of myvar.lowbyte and myvar.highbyte in there. So, I'm guessing I need to turn the comparators off?

    ANSEL = %00000000
    CMCON = %00000111

    ???

    Any suggestions on how to get the correct device address sent ($10), and read the address $22?

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


    Did you find this post helpful? Yes | No

    Default

    You already have a LCD attached to it... why not monitor the HEX value of the WORD variable using HEX4

    Not sure if the optional label jump work with I2CREAD and your external device...
    Last edited by mister_e; - 2nd April 2008 at 20:03.
    Steve

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

  6. #6
    Join Date
    Mar 2008
    Location
    Texas, USA
    Posts
    114


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Dispersion123 View Post
    Any suggestions on how to get the correct device address sent ($10), and read the address $22?
    If the device address is $10 then doesn't the address have to be left shifted one place?

    $10 = %00010000

    PBP handles the R/W bit (bit.0), so your sending out address %0001000R (Read or Write bit) and the address "seen" by the receiving device is %00001000

    Remember that I2C device addresses are shifted left one place to allow for the read/write bit. You'll never (edit: never say never - there are 10 bit addressing in some devices, though this is not part of the original I2C design data) see an I2C device address over $7F, or %01111111 or DEC127.

    So, I think you should be using the DEVICE ADDRESS of $20
    Last edited by JD123; - 2nd April 2008 at 23:10.
    No, I'm not Superman, but I did stay at a Holiday Inn Express last night!

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


    Did you find this post helpful? Yes | No

    Default

    Doesn't sounds right to me... how about most I2C EEPROM with $A0?
    Steve

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

  8. #8
    Join Date
    Mar 2008
    Location
    Texas, USA
    Posts
    114


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    Doesn't sounds right to me... how about most I2C EEPROM with $A0?

    Sorry, I overlooked your post. Are you seeing what I'm saying?
    No, I'm not Superman, but I did stay at a Holiday Inn Express last night!

  9. #9
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by JD123 View Post
    If the device address is $10 then doesn't the address have to be left shifted one place?
    I thought PBP handled that part for you. 'cause with a generic serial eeprom, the address is something like $a0, and that's how you address it...$a0, without the left shift, PBP handles the R/W bit.
    But then again, we don't know exactly what kind of secret magic device is being addressed here...

  10. #10
    Join Date
    Mar 2008
    Location
    Texas, USA
    Posts
    114


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    I thought PBP handled that part for you. 'cause with a generic serial eeprom, the address is something like $a0, and that's how you address it...$a0, without the left shift, PBP handles the R/W bit.
    But then again, we don't know exactly what kind of secret magic device is being addressed here...
    $A0 is the LOWER 7 bit's of the address, shifted left one place. The DEVICE address being sent out and 'seen' by the reciever is %01010000, or $50. Check the specs of I2C, not just EEPROMS. The device address is specified as a 7 bit address, not 8. But since we are adding the R/W bit to the 8 bit address byte, we have to left shift the DEVICE address one place.

    The confusion comes in dealing with 2 different data "specs" or "types" combined into one 8 bit Byte being send. Then suming the two values to "use" one value. That "one" value is not the device address.

    You ever do this: (I know I have)
    Gang 8 64KBit EEproms and use the device address (bit 4:1) by just rolling the long address over directly into the device address. It won't work. Trust me, just shift the address left once to embed the address into the device address byte.
    Last edited by JD123; - 2nd April 2008 at 21:52.
    No, I'm not Superman, but I did stay at a Holiday Inn Express last night!

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. I2C slave
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th March 2008, 03:46
  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