Menu

Master I2C write to PicSlave Example

kent_twt4
2007-08-18
2013-05-30
  • kent_twt4

    kent_twt4 - 2007-08-18

    Here is an example of using a master Pic to control a slave Pic.  The code uses the I2C MasterI2C.h and SSP_I2C_Picslave.h library's.  As a short term solution, the following subroutine code would need to be copied and pasted into the MasterI2C.h file:

    sub I2CWriteTemp(WriteID,command) ;#NR
      Start
      TxI2C(WriteID)  'Device ID write byte
      TxI2C(command)  'Send byte size data
      Stop
    end sub

    ;-----------------------------------------------------------------------------------------;The Master I2C code example:
    ;-----------------------------------------------------------------------------------------
    'Chip model
    #chip 16f877A,20
    #include <MasterI2C.h>

    'Setup 4 bit LCD
    #define LCD_IO 4
    #define LCD_DB4 PORTD.4
    #define LCD_DB5 PORTD.5
    #define LCD_DB6 PORTD.6
    #define LCD_DB7 PORTD.7
    #define LCD_RS PORTD.1
    #define LCD_RW PORTD.2
    #define LCD_Enable PORTD.3
    Dir PortD out

    'Setup Master I2C
    #define PicSlaveWrite b'00110010'
    #define PicSlaveRead  b'00110011'
    #define BufferLen 8
    #define ClockValue b'00110001'  ;ClockValue or SSPADD = (FOSC / (4 * Baud)) - 1   
    Dir PortC.3 In  'SCL Port Pin
    Dir PortC.4 In  'SDA Port Pin
    InitI2C

    Main:
    cls
    wait 25 10ms  ;Blip screen to verify refreshed data
    For index = 1 to BufferLen
    I2CData = index
    I2CWriteTemp(PicSlaveWrite,I2CData)  ;Write data to slave
    LCDInt(I2CData):Print " "  ;Show data being sent to slave
    wait 2 s
    Next
    goto Main

    ;-----------------------------------------------------------------------------------------
    ;The slave I2C example:
    ;-----------------------------------------------------------------------------------------
    'Chip model
    #chip 16f88,20

    ;Setup SSP module
    #include <SSP_I2C_PicSlave.h>
    #define PicSlave b'00110010'  ;Set Pic Slave I2C address
    #define BufferLen 1  ;How many bytes to receive
    Dim RxBuffer(RxBufferSize)
    InitSSP  ;Setup SSP registers and enable interrupts
    Dir PortB.1 in  ;SDA pin for 16f88
    Dir PortB.4 in  ;SCL pin for 16f88

    ;Receive test Byte from master
    ;And send out lower nibble on Slave Led's
    Dir PortB.0 out
    Dir PortB.2 out
    Dir PortB.3 out
    Dir PortB.5 out

    Main:
    TestLed = RxBuffer(1)  ;Receive Byte from Master
    Rotate TestLed Right
    PortB.0 = Status.C 
    Rotate TestLed Right
    PortB.2 = Status.C 
    Rotate TestLed Right
    PortB.3 = Status.C 
    Rotate TestLed Right
    PortB.5 = Status.C 
    goto Main

     
    • kent_twt4

      kent_twt4 - 2008-05-05

      Most unfortunately, the call for the interrupt subroutine did not make it to the (pic) slave I2C example.  Insert the interrupt call as below.  Also, the pic slave BufferLen is only working currently with one byte transfers (i.e. #define BufferLen 1), per example.  Due not confuse the BufferLen in the master code, that was an inappropriate/confusing variable designation.

      'Insert missing interrupt call subroutine for pic slave
      Main:
      Interrupt
      TestLed = RxBuffer(1) ;Receive Byte from Master
      Rotate TestLed Right
      PortB.0 = Status.C 
      Rotate TestLed Right
      PortB.2 = Status.C 
      Rotate TestLed Right
      PortB.3 = Status.C 
      Rotate TestLed Right
      PortB.5 = Status.C 
      goto Main

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.