I2C Slave with a PIC


Closed Thread
Results 1 to 40 of 130

Hybrid View

  1. #1
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    I need this because i build it from the status of four bits of Port A

    Any idea?

    Ralf, you can try this:

    Code:
    
    
    Select Case PortA
    
    Case 1
    i2caddress CON $01
    Case 2
    i2caddress CON $02
    case 3
    i2caddress CON $03
    .
    .
    .
    .
    Case x
    i2caddress CON $x
    End Select
    It should work.

    Edited:

    No! it doesn't work sorry.

    Al.
    Last edited by aratti; - 25th March 2010 at 13:19.
    All progress began with an idea

  2. #2
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    ralfmayr, Here is what I have been using for years. It uses the base address for the first of 3 PCF8574's. I then calculate the address depending on the one I need to gather or send data to.

    UNIT0 CON %01000000 'U5's CONTROL BYTE (PCF8574) I/O EXPANDER

    '**************** I2C I/O ROUTINE (PCF8574A) I/O EXPANDER ************
    READ_WRITE VAR BIT 'READ DATA or WRITE DATA FLAG
    EXPAND_UNIT VAR BYTE 'CONTROL BYTE FOR I2C
    DEVICE VAR BYTE 'SELECTED I/O EXPANDER DEVICE
    OUTPUTS VAR BYTE[3] 'RELAY OUTPUT BYTES
    SWITCHES VAR BYTE[3] 'SWITCH INPUTS BYTES

    '************************************************* *******************
    R_WI2CS:'READ DIGITAL INPUTS FROM or WRITE DIGITAL OUTPUTS TO PCF8574'S
    '************************************************* *******************
    EXPAND_UNIT = UNIT0 'COPY BASE ADDRESS OF PCF8574'S
    EXPAND_UNIT = (EXPAND_UNIT | (DEVICE << 1)) 'SELECT WHICH DEVICE
    IF READ_WRITE = 0 THEN 'READ INPUT STATUS FROM PCF8574'S
    I2CREAD SDA,SCL,EXPAND_UNIT,[SWITCHES(DEVICE)]
    ELSE 'SET OUTPUT STATES TO PCF8574'S
    I2CWRITE SDA,SCL,EXPAND_UNIT,[OUTPUTS(DEVICE)]
    ENDIF
    RETURN


    Then to use it first set the device index.
    Next set the read_write flag.
    Then finally call the routine.....

    Dave Purola,
    N8NTA

  3. #3
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Ralf, you can try the case select using the proper addresses (not my example). You must correct your code as per the line in black bold and comment out the line in red bold. The code compile so it should work. Longing to hear from you on the final test.

    Al.


    Code:
    ' Initialize I2C slave mode
    
    Select case PortA
    
    Case 1
    SSPADD = 2
    case 2 
    SSPADD = 3
    case 3
    SSPADD = 4
    
    end select
    
    SCLDIR = 1  ' SCL must be made an input before enabling interrupts
    SDADIR = 1   
    'SSPADD = I2Caddress ' Set our address
    SSPCON2.7 = 0 ' General call address disabled
    SSPCON = $36 ' Set to I2C slave with 7-bit address
    SSPSTAT = 0
    SSPIE = 1 
    SSPIF = 0 
    RxBufferIndex = 0
    TxBufferIndex = 0
    
    'Initialization Done!
    
     
    GoTo main
    
    i2cslave: ' I2C slave subroutine
    SSPIF = 0 ' Clear interrupt flag
    IF R_W = 1 Then i2crd ' Read data from us
    IF BF = 0 Then i2cexit ' Nothing in buffer so exit
    IF D_A = 1 Then i2cwr ' Data for us (not address)
    IF SSPBUF != SSPADD Then i2cexit ' Clear the address from the buffer
    readcnt = 0 ' Mark as first read
    GoTo i2cexit
    All progress began with an idea

  4. #4
    Join Date
    Nov 2008
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    Hi Al,
    my mistake, it works now.
    I made a mistake in calculating the address from four bits of a port :-(

    This works:

    main_loop:
    ...
    i2caddress = (addr0 * 16) + (addr1 * 8) + (addr2 * 4) + (addr3 * 2)
    SSP1ADD = I2Caddress
    ...
    goto main_loop

    ...in the moment i try to implement the slave routine into a 18LF14K22...
    It does not work in the moment, it is the same like on a18F6722, there it
    works great.

    Regards,
    Ralf

  5. #5
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Code:
    i2caddress = (addr0 * 16) + (addr1 * 8) + (addr2 * 4) + (addr3 * 2)
    SSP1ADD = I2Caddress
    Very good Ralf !

    Al.
    All progress began with an idea

  6. #6
    Join Date
    Nov 2008
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    Hi Al,
    success!
    The slave routine runs in a 18LF14K22.
    I read back via i2c 20 values (a/d values and digital i/o) and i can set
    several parameters with two bytes, 1st is array index, 2nd is value.
    The handler runs via interrupt, in the moment i am cleaning a little bit,
    but i am very happy with it.
    Regards,
    Ralf

  7. #7
    Join Date
    Nov 2008
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    Some Remarks:

    1. It is necessary to set the "DEFINE I2C_HOLD 1" in the master to get a
    better behaviour of the timing. I checkrd it with the logic analyzer and it is
    exactly like in the original "Phillips-Spec"

    2. In the section
    ...
    while STAT_BF = 1
    i = SSPBUF ' Dummy Read inserted => clears BF Flag
    wend
    ...
    it is necessary to inlude the dummy read to get it work on
    a 18F6722 and a 18LF14K22 (refer to MCHIP App Note 734, page 17)

    3. Address range ist tested from 00h to FFh, all work well.

    4. New address setting works (in the main loop) by only setting SSPADD
    register with new value without reseting the complete handler.

    Regards,
    Ralf

Similar Threads

  1. Problem with PICto PIC I2C MASTER-SLAVE
    By juanen19 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th June 2013, 02:58
  2. PIC as I2C Slave
    By Mainul in forum General
    Replies: 4
    Last Post: - 5th January 2013, 13:23
  3. I2C Slave, config Vref - + in pic with ADC
    By sebapostigo in forum PBP Wish List
    Replies: 4
    Last Post: - 5th March 2007, 03:21
  4. Pic as an i2c slave
    By Robert Soubie in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 25th January 2007, 21:11
  5. Use pic as slave in I2C
    By robert0 in forum General
    Replies: 2
    Last Post: - 3rd February 2006, 19:26

Members who have read this thread : 2

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