Pic to pic communication with i2c

Discussion in 'Embedded Systems and Microcontrollers' started by franklin75, Jun 30, 2016.

  1. franklin75

    Thread Starter New Member

    Jun 11, 2016
    14
    1
    Hi everyone! I have just studied at i2c so i write some codes to pic to pic communication use ccsc .my project is soo basic and it is about master pic communicate the another slave pic and the led Which is contact with slave pic blink,but led does not blink.i didn't solve this problem.I am a new in electronic,the forum,and english.I need some suggestion about i2c communication.thanks for helps.
    My codes:
    MASTER
    Code (C):
    1.  
    2. #include <I2_C_MASTER.h>
    3. #fuses HS,NOWDT,PROTECT,NOLVP,BROWNOUT,PUT
    4. #use delay(clock=20Mhz)
    5. #use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)
    6. void main(){
    7.  while(1){
    8.   int data=1;
    9.   i2c_start();
    10.   delay_ms(50);
    11.   i2c_write(0b00001111);//slave adresi
    12.   delay_ms(50);
    13.   i2c_write(data);//veri
    14.  delay_ms(50);
    15.  i2c_stop();
    16.  }//while
    17. }
    18.  
    SLAVE
    Code (C):
    1.  
    2. #include <I2C_SLAVE.h>
    3. #fuses HS,NOWDT,PROTECT,NOLVP,BROWNOUT,PUT
    4. #use delay(clock=20Mhz)
    5. #use i2c(slave, sda=pin_c4, scl=pin_c3,address=0x15)
    6. int data;
    7. void main(){
    8. set_tris_a(0x00);
    9. while(TRUE)
    10.    {
    11.     if(i2c_poll()) {
    12.       data=i2c_read();
    13.       output_toggle(pin_a0);
    14.       delay_ms(50);
    15.       }
    16.    }
    17. }
    Mod edit: code tags
     
    Last edited by a moderator: Jun 30, 2016
  2. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,908
    379
    You do have pull-ups on the clock and data lines?
    I don't know that compiler so can't really comment on the code, except that I am not keen on all those 50mS delays. Put one delay in the master while loop, say 200mS, and no delays in the slave.
     
  3. franklin75

    Thread Starter New Member

    Jun 11, 2016
    14
    1
    Thanks for your suggestion.ı use pullup.
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Hello franklyn, welcome to the forums.

    A long time ago I got pic to pic I2C working where read data was sent back and fourth. Getting the master to work was fairly straightforward, but the slave end was difficult working with two pic18f4550 devices and the C18 compiler. I spent an enTire Saturday with a debugger and a scope working out what the slave was doing. My slave code was mostly interrupt code working each part of a transaction.

    Without knowing what compiler and library you are using I cannot comment on your method. Also knowing what pic device you are using will help us help you.

    Good luck.

    (I will be on vacation tomorrow and will not have any access to my previous work or informant as I am leaving my PC home. I might have some suggestions posting thru my tablet.)
     
  5. franklin75

    Thread Starter New Member

    Jun 11, 2016
    14
    1
    thanks for your answer.please send me a some algorithms or codes about i2c communication,also i use interrupt to slave but it didn't work.i find same examples in youtube and i used the same codes which are works properly on proteus but in my pc they did not work.please help me.I interested in communication so i study at electronic and communication engineering in Yıldız technical university in İstanbul.
     
  6. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,908
    379
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Hey Franklin, can you answer what PIC you are using and what compiler?

    My flight leaves in 23 hours so I don't have any time to search and post my previous work (which was really just a hack job anyway, not something even I could follow today. But it did work.)
     
  8. franklin75

    Thread Starter New Member

    Jun 11, 2016
    14
    1
    I am using pic16f877 and ccsc complier.have a nice journey.thank you for your help
     
Loading...