Shop OBEX P1 Docs P2 Docs Learn Events
I2C Code for the BS2, BS2e, and BS2sx - Page 2 — Parallax Forums

I2C Code for the BS2, BS2e, and BS2sx

2»

Comments

  • basejumper9basejumper9 Posts: 4
    edited 2007-03-20 19:11
    So I think I'm close now here is what I have for just reading the eeprom:
    ' -----[noparse][[/noparse] I/O Definitions ]-------------------------------------------------
    
    SDA             PIN     5                       ' I2C serial data line
    SCL             PIN     4                       ' I2C serial clock line
    
    
    ' -----[noparse][[/noparse] Constants ]-------------------------------------------------------
    
    Ack             CON     0                       ' acknowledge bit
    Nak             CON     1                       ' no ack bit
    
    ' -----[noparse][[/noparse] Variables ]-------------------------------------------------------
    
    slvAddr         VAR     Byte                    ' slave address
    devNum          VAR     Nib                     ' device number (0 - 7)
    addrLen         VAR     Nib                     ' 0, 1 or 2
    devAddr         VAR     Word                    ' address in device
    
    i2cData         VAR     Byte                    ' data to/from device
    i2cWork         VAR     Byte                    ' work byte for TX routine
    i2cAck          VAR     Bit                     ' Ack bit from device
    
    location        VAR     Byte
    
    
    
    ' -----[noparse][[/noparse] EEPROM Data ]-----------------------------------------------------
    
    
    ' -----[noparse][[/noparse] Initialization ]--------------------------------------------------
    
    Reset:
      #IF ($stamp >= BS2P) #THEN
        #ERROR "Use I2COUT and I2CIN!"
      #ENDIF
    
      devNum = 0                                    ' device address %000
      slvAddr = (location) << 1
      addrLen = 0                                   ' no internal addresses
    
      DEBUG CLS,
            "24C11 Demo", CR                         ' setup output screen
    
    
    ' -----[noparse][[/noparse] Program Code ]----------------------------------------------------
    
    Main:
    
        FOR location = 0 TO 127
          slvAddr = (location) << 1
          GOSUB Read_Byte
          DEBUG BIN8 slvAddr, " "
          DEBUG IHEX i2cData,CR
          'PAUSE 100
        NEXT
    
    
      END
    
    
    ' -----[noparse][[/noparse] Subroutines ]-----------------------------------------------------
    
    
    ' -----[noparse][[/noparse] High Level I2C Subroutines]---------------------------------------
    ' Random location read
    ' -- pass device slave address in "slvAddr"
    ' -- pass address bytes (0, 1 or 2) in "addrLen"
    ' -- register address passed in "devAddr"
    ' -- data byte read is returned in "i2cData"
    
    Read_Byte:
      GOSUB I2C_Start                               ' send Start
      i2cWork = slvAddr | %00000001                 ' send slave ID (read)
      GOSUB I2C_TX_Byte
      GOSUB I2C_RX_Byte_Nak
      GOSUB I2C_Stop
      i2cData = i2cWork
      RETURN
    
    
    ' -----[noparse][[/noparse] Low Level I2C Subroutines]----------------------------------------
    
    ' *** Start Sequence ***
    
    I2C_Start:                                      ' I2C start bit sequence
      INPUT SDA
      INPUT SCL
      LOW SDA
    
    Clock_Hold:
      DO : LOOP UNTIL (SCL = 1)                     ' wait for clock release
      RETURN
    
    
    ' *** Transmit Byte ***
    
    I2C_TX_Byte:
      SHIFTOUT SDA, SCL, MSBFIRST, [noparse][[/noparse]i2cWork\8]      ' send byte to device
      SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cAck\1]          ' get acknowledge bit
      RETURN
    
    
    ' *** Receive Byte ***
    
    I2C_RX_Byte_Nak:
      i2cAck = Nak                                  ' no Ack = high
      GOTO I2C_RX
    
    I2C_RX_Byte:
      i2cAck = Ack                                  ' Ack = low
    
    I2C_RX:
      SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]i2cWork\8]         ' get byte from device
      SHIFTOUT SDA, SCL, LSBFIRST, [noparse][[/noparse]i2cAck\1]       ' send ack or nak
      RETURN
    
    
    ' *** Stop Sequence ***
    
    I2C_Stop:                                       ' I2C stop bit sequence
      LOW SDA
      INPUT SCL
      INPUT SDA
      RETURN
    
    



    Everything looks right to me unless I missed something, any ideas?

    Post Edited (basejumper9) : 3/20/2007 7:16:55 PM GMT
  • basejumper9basejumper9 Posts: 4
    edited 2007-03-25 18:14
    For some reason the eeprom does seem to respond right, it seems like no matter what I do I only get an $ff/ 11111111 back unless I'm missing something major in the literature it should go like this for a read cycle:
    1. I2C start
    2. Master sends a 7 bit address (0000000 through 1111111) with the 8th bit being read/write which would be a 1 cause we are reading
    3. Slave sends an Ack which is pulling the line low
    4. Slave sends a byte back
    5. Master keeps the line high to signal no Ack
    6. Master sends a stop

    Questions:
    Is all that above right? Am I addressing the eeprom right? an ideas on where my code is wrong?
  • edited 2009-01-03 00:42
    I like to communicate with an ADC MAX127 over I2C.

    I need to write an Slave_Byte and a Control_Byte to program it and I need to read it Like Slave_byte, Data_byte1, Data_byte2.

    How should I write this program???


    I2COUT SDA, Slave_Byte, [noparse][[/noparse]Control_Byte]

    I2CIN SDA, Slave_Byte, [noparse][[/noparse]result1, result2] ??????


    Need help!!!!
  • $WMc%$WMc% Posts: 1,884
    edited 2009-01-03 03:09
    Mr.Jon Williams

    Thank You for the EEPROM I2C Applications. This is A great help to Me. I was gonna post a "new topic" on this, And Boom their it was on the forum.

    Its Your "I'll show You" approach, That I like most. From this I learn the most!!!...Your REM statements tell Me whats going on in the code.I learn from this and advance to higher levels of programing code with the BS2 series. Thanks Again

    __________________$WMc%_________________Happy New Year

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Truth is out there·········································· E=$WMc%2

    Post Edited ($WMc%) : 1/3/2009 6:38:39 AM GMT
Sign In or Register to comment.