urgent help with I2C communication between two PIC16f877a, one master one slave

Discussion in 'Embedded Systems and Microcontrollers' started by sairyuva, May 6, 2010.

  1. sairyuva

    Thread Starter New Member

    Mar 9, 2010
    11
    0
    Can any one help me with I2C?
    What should I do at salve side of PIC16f877a

    MPLAB v8.33
    HITECH C compiler 9.7
    Ω°Ω°Ω°Ω°
     
  2. kingdano

    Member

    Apr 14, 2010
    377
    19
    *sigh*

    no one can help you with such minimal information.

    please post more detail or delete your post
     
  3. Art

    Distinguished Member

    Sep 10, 2007
    785
    61
    Why not use ordinary serial comms?
    I2C was implemented to talk to third party chips.
    I'm not sure you'd find existing routines for communications on the slave side.
     
  4. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    I had a burn and the doctor put salve on it.

    But I wouldnt but salve on a PIC16f877a, unless it had dry skin. ;)

    As for you other problem, What are you using as the MASTER, and why are you using I2C?
     
  5. sairyuva

    Thread Starter New Member

    Mar 9, 2010
    11
    0
    i m just starting
    Please see this code and state my mistake
    Code ( (Unknown Language)):
    1.  
    2. /*master.c*/
    3. #include<htc.h>
    4.  
    5.  
    6. __CONFIG(HS & WDTDIS & PWRTEN & LVPDIS & BORDIS);
    7.  
    8. unsigned char _data, i=0x00;
    9.  
    10. void Init_I2C()
    11. {
    12.     TRISC3=0;    //SCL
    13.     TRISC4=0;    //SDA
    14.     SSPCON=0x38;
    15.     SSPCON2=0x00;
    16.     SMP=1;
    17.     SSPEN=1;
    18.     CKE=0;
    19.     SSPIF=0;
    20.     BCLIF=0;    //bus collision flag
    21.     SSPADD=0x63;    
    22.     SEN=1;
    23.     TRISB=0x00;
    24.     PORTB=0x00;
    25.     TRISD7=0;
    26.     RD7=0;
    27. }
    28.  
    29.  
    30.  
    31.  
    32.  
    33.  
    34.  
    35.  
    36.  
    37. void main()
    38. {
    39.     Init_I2C();
    40.    
    41.     while(1)
    42.     {
    43.        
    44.         SEN=1;
    45.         ACKSTAT=1;
    46.         i=0;
    47.        
    48.    
    49.         RCEN=1;
    50.         ACKEN=1;
    51.        
    52.         while(SSPIF==0);
    53.         SSPIF=0;
    54.    
    55.         SSPBUF=i;        
    56.         i++;
    57.      
    58.         while(SSPIF==0);
    59.         SSPIF=0;
    60.         SSPBUF=i;
    61.         i++;
    62.      
    63.         while(SSPIF==0);
    64.         SSPIF=0;
    65.         PEN=1;
    66.         while(SSPIF==0);
    67.         SSPIF=0;
    68.         delay_Nms(100);
    69.     }
    70. }
    71.  
    Code ( (Unknown Language)):
    1.  
    2. /*slave.c*/
    3.  
    4.  
    5. #include<htc.h>
    6.  
    7.  
    8.  
    9.  
    10. __CONFIG(HS & WDTDIS & PWRTEN & LVPDIS & BORDIS);
    11.  
    12. unsigned char _data;
    13.  
    14.  
    15.  
    16.  
    17.  
    18.  
    19.  
    20. void Init_I2C()
    21. {
    22.     TRISC3=1;    //SCL
    23.     TRISC4=1;    //SDA
    24.     SSPCON=0x3E;    //slave mode
    25.     SSPCON2=0x00;
    26.     SMP=1;
    27.     ei();    
    28.    
    29.     CKE=0;
    30.     CKP=1;
    31.     SSPEN=1;
    32.     SSPIF=0;
    33.     SSPADD=0x69;
    34.     BCLIF=0;    
    35.     TRISB=0x00;
    36.     PORTB=0x00;
    37.     TRISD7=0;
    38.     RD7=0;
    39.     SEN=1;
    40.     SSPOV=0;
    41. }
    42.  
    43.  
    44.  
    45.  
    46.  
    47.  
    48. void main()
    49. {
    50.     Init_I2C();
    51.     while(1)
    52.     {
    53.         PORTB=_data;
    54.        
    55.     }
    56. }
    57.  
    58. void interrupt ISR(void)
    59. {
    60.     if(SSPIF)
    61.     {
    62.         if(RW==1)
    63.         {
    64.            
    65.             RD7=1;
    66.             _data=SSPBUF;
    67.             CKP=1;
    68.         }
    69.         SSPIF=0;    
    70.                
    71.     }
    72. }
    73.  
     
  6. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    What are you talking to with the I2C bus? A PIC on one side, but what is on the other?
     
  7. sairyuva

    Thread Starter New Member

    Mar 9, 2010
    11
    0
    its PIC16f877a both
     
  8. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    Ok, so you want 2 PIC 16f877a to talk to each other.

    Is this an assignment or can you choose to use a different communications protocol?
     
  9. sairyuva

    Thread Starter New Member

    Mar 9, 2010
    11
    0
    Assisgnment
     
  10. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    ok. What are you using as a clock for the master? I see nothing in you code.
     
  11. sairyuva

    Thread Starter New Member

    Mar 9, 2010
    11
    0
    20Mhz XTAL
     
  12. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    Can you post a schematic of your master and slave setup?
     
  13. sairyuva

    Thread Starter New Member

    Mar 9, 2010
    11
    0
    I used Proteus Vsm to test it
    [​IMG]
     
Loading...