I2C Device discovery - GCB code to discover devices.

Anobium
2013-07-02
2013-07-02
  • Anobium

    Anobium - 2013-07-02

    Sample code to discovery I2c devices. This requires the i2C libaries dated after Jan 2014.

     
    Last edit: Anobium 2014-03-28
  • Anobium

    Anobium - 2013-07-02

    The code

    ~~~~~
    ' change the processor
    #chip 16F1937, 4
    #config Osc = intOSC, MCLRE_OFF, PLLEN_ON, VCAPEN_OFF

    ' Define I2C settings - CHANGE PORTS
    #define I2C_MODE Master
    #define I2C_DATA portc.4
    #define I2C_CLOCK portc.3

    ' Define an LED port - this is not working at the moment... I want to flash the LED to count out the Device ID
    ' CHANGE PORTS
    #define D1 LATD1
    Dir PORTD.1 Out

    ' CHANGE PORTS AND/OR CHANGE TO SOFTWARE RS232
    ' Define the USART port
    #define USART_BAUD_RATE 9600
    #define USART_BLOCKING
    #define SerInPort PORTc.7
    #define SerOutPort PORTc.6
    'Set pin directions
    Dir SerOutPort Out
    Dir SerInPort In

    initserial:
    wait 15 ms
    HSerSend 27
    HSerPrint "[2J"
    HSerSend 27
    HSerPrint "[0"
    HSerSend 59
    HSerPrint "0H"
    HSerPrint "Great Cow Basic - Discovery I2C Device": crlf ( 1 )
    HSerPrint "Searching I2C address space": crlf (2)

      for deviceID = 0 to 255
        I2CStart
        I2CSend deviceID
    
        if I2CDetectSend then
          HSerPrint "ID : "
          HSerPrint deviceID
          HSerPrint " (0x"
          HSerPrint hex(deviceID)
          HSerPrint ") - "
          HSerPrint "I2C Write": crlf ( 2 )
          FlashLED 1, @PORTD, 1
        end if
    
        I2CStart
        I2CSend deviceID
        I2CReceive device_result
    
        if device_result <> 255 then
          HSerPrint "ID : "
          HSerPrint deviceID
          HSerPrint " (0x"
          HSerPrint hex(deviceID)
          HSerPrint ") - "
          HSerPrint "I2C Receive": crlf ( 2 )
          FlashLED 1, @PORTD, 1
        end if
    
      next
      HSerPrint "End of Device Search": crlf ( 2 )
      I2CStop
    

    END

    Function I2CDetectSend ()
    dim I2CByte as byte
    I2CByte = 0: 'this is test byte to send
    dim I2CGetAck
    I2CGetAck = true
    Dim function_result
    function_result = false

    #if I2C_MODE = Master
      I2C_CLOCK_LOW
      Wait I2C_END_DELAY
    
      'Send highest bit first
      For I2CCurrByte = 1 to 8
        If I2CByte.7 = On Then
          I2C_DATA_HIGH
        Else
          I2C_DATA_LOW
        End If
        Rotate I2CByte Left
    
        Wait I2C_END_DELAY
        I2C_CLOCK_HIGH
        Wait While I2C_CLOCK = Off
        Wait I2C_CLOCK_DELAY
        I2C_CLOCK_LOW
        Wait I2C_END_DELAY
      Next
    
      If I2CGetAck Then
        I2C_DATA_HIGH
        Wait I2C_END_DELAY
    
        I2C_CLOCK_HIGH
        Wait While I2C_CLOCK = Off
        Wait I2C_CLOCK_DELAY
        For I2CCurrByte = 0 to 255
          If I2C_DATA = 0 Then
            I2CGetAck = False
            'If data line goes low, ack has been received
            function_result = true
            Goto I2CAckEndlocal
          End If
          Wait I2C_END_DELAY
        Next
        'No ack received
        I2CAckEndlocal:
        I2C_CLOCK_LOW
        Wait I2C_END_DELAY
        I2C_DATA_LOW
        Wait I2C_END_DELAY
      End If
    
      'Wait one bit length
      Wait I2C_BIT_DELAY
    #endif
    I2CDetectSend = function_result
    

    end sub

    ' 1.01 added USART SUPPORT and PORT

    function crlf (Optional num As byte = 1, Optional nport As byte = 1 )

    #ifdef USART_BAUD_RATE
      for loopcrlf = 1 to num
        HSerSend 13
        HSerSend 10
      next
    #endif
    
    #ifndef USART_BAUD_RATE
      for loopcrlf = 1 to num
        SerSend nport,13
        SerSend nport,10
      next
    #endif
    

    end function

    Sub FlashLED ( In loopcount, In Destvar as word, In Destbit)

     for loopx = 0 to 10
            Poke  Destvar, Peek(Destvar) or Destbit
            wait 30 ms
            Poke  Destvar, Peek(Destvar) AND NOT Destbit
            wait 30 ms
    next
    wait 1 s
    

    End Sub

     
    Last edit: Anobium 2013-09-28

Log in to post a comment.