PIC master and slave

Discussion in 'Embedded Systems and Microcontrollers' started by RG23, Apr 12, 2012.

  1. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    I am using two pic controllers

    I want to know how to use the I2C to send data from master to slave

    I want to send a count value from the master PIC, the slave should read that count value and accordingly set or reset one of its port bits for example PORTA,5 depending on the count value

    If anyone has an idea please let me know

    Thanks
     
  2. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    Any ideas to send the data from master PIC to the slave PIC using i2C??
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Good questions draw good answers. As most us in this forum are not clairvoyant. So a good start would be to post imperative information like any code and schematics you have.
     
  4. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    this is I2C, if you use the datasheet, you need to set up the address for your i2c slave, i.e.: 01010101 therefore everytime your masters wants to communicated, you use the standard I2C functions to communicate with address 01010101.. sending your data across..?:)
    make sense?
    like t06afre says though, schematics etc will help, dont even know what pic models youre using!
     
  5. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    I implemented the following subroutine which works perfectly fine when I send data from the master PIC to a digital potentiometer AD5245

    But AD5245 has a specific device ID

    Pot_0:
    ;{
    call I2C_STRT////Start I2C communication subroutine
    movlw 0x58 //////Device ID (address)for digital pot
    movwf I2C_Value
    call I2C_SEND
    movlw 0x00
    movwf I2C_Value
    call I2C_SEND
    movlw 0x79
    movwf I2C_Value
    call I2C_SEND/////Send data
    call I2C_STP/////Stop subroutine
    return
    Now I want to implement something similar but use PIC16f887 as slave

    The Master PIC is PIC18f67J90

    If anyone has an idea please let me know

    Thanks
     
  6. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    PIC16F887 Datasheet page 187. chapter "13.4.1 Slave Mode"
    like i said, you need to set the MSSP up so you can use I2C slave, 7 or 10 bit address..
    then you need to give it the address so 0101010 or whatever isnt being used in your system.
    then you need to communicate. as normal.
    yes?
     
  7. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    and please use code tags.
     
  8. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    I did try setting the MSSP I2C slave, 7 or 10 bit address but it didn't work

    Do i need to set it for the master PIC also?
     
  9. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    no, you do need to set the address though?
    and then write to that address from the master?
    SSPADD i think is the register you need to put the address in. again check the datasheet, it has the method of communication for it:)
    from the master side -
    standard I2C comms, use the address of your slave, and transmit the data you want as standard
     
  10. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    Now I know how to transfer the data from master to slave

    But I am not able to read the data from the slave

    For master to slave I used the following code:

    Master PIC subroutine:

    call I2C_STRT
    movlw 0x40
    movwf I2C_Value
    call I2C_SEND
    movlw 0x00
    movwf I2C_Value
    call I2C_SEND
    movlw d'10'///////data to be sent to slave PIC
    movwf I2C_Value
    call I2C_SEND
    call I2C_STP
    return

    I implemented the Slave PIC subroutine in the interrupt Service routine:

    btfss PIR1,3
    goto $
    bcf PIR1,3

    btfsc SSPSTAT,5
    call label_data

    retfie

    label_data:
    movf SSPBUF,0
    movwf TEMP////register to store data received from master pic
    return

    Above code worked perfectly for master to slave

    But now I am having issues in reading the data from slave to master
     
  11. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    so to clarify, you are transmitting the data from master to slave? but you cannot read the data? try pausing the program and having a look at the SSPBUF make sure the data youve sent is actually there.. then you know 100% its being transmited and the addressing is right..
    ALSO please remember to use code tags. try editing your post if you can and using them (# button at top of reply window) it makes things so much easier to understand
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    chrisw, I believe he has two PICs communicating via I2C, one master (which he has working) and a slave (which is not working).

    rg23: I recently found a code example, I2C module in PIC18F4685 is configured as slave by Microchip. I have an upcoming need to make a slave unit so I'm re-researching it; I did it once but I am no longer with the company I left that code with.

    I've yet to delve into this project.
     
  13. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    i know, im trying to establish whether he actually has the communication correct at the moment.. though its triggering the interrupt for reception, and it wouldnt be if it wasnt.. obviously..
     
Loading...