I2C Communication between two PIC16F877

Home Forums Microcontrollers PIC Microcontroller I2C Communication between two PIC16F877

Tagged: , ,

This topic contains 1 reply, has 2 voices, and was last updated by  Ligo George 10 months, 3 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #12298

    alek
    Participant

    Hi everyone;

    I am trying to accomplish I2C communication between two PIC16F877. But I did not succeed. My codes in below. Can any body help me

    Master PIC code :

    #include <xc.h>
    
    #pragma config FOSC = XT
    #pragma config WDTE = OFF
    #pragma config PWRTE = OFF
    #pragma config CP = OFF
    #pragma config BOREN = ON
    #pragma config LVP = ON
    #pragma config CPD = OFF
    #pragma config WRT = ON
    
    #define _XTAL_FREQ 4000000
    
    void WaitC( void )
    {
      while ( ( SSPCON2 & 0x1F ) || ( SSPSTATbits.R_W ) )
      continue;
    }
    
    void StartC( void )
    {
      SSPCON2bits.SEN = 1;
      while(SEN);
    }
    
    void StopC( void )
    {
      SSPCON2bits.PEN = 1;
      while(PEN);
    }
    
    signed char WriteC( unsigned char veri )
    {
      WaitC();
      SSPBUF = veri;
      return(ACKSTAT);
    }
    
    void main(void)
    {
      TRISC   = 0XFF;
      SSPCON  = 0X38;
      SSPADD  = 9;
      SSPSTAT = 0X80;
      SSPCON2 = 0X00;
      TRISB = 0;
      PORTB = 0;
      SSPSTATbits.R_W = 0;
    
      while(1)
      {
        StartC();
        WriteC(0X22);
        WriteC(0X40);
        StopC();
        __delay_ms(100);
      }
    }
    
    

    Slave PIC code :

    #include <xc.h>
    #pragma config FOSC = XT
    #pragma config WDTE = OFF
    #pragma config PWRTE = OFF
    #pragma config CP = OFF
    #pragma config BOREN = ON
    #pragma config LVP = ON
    #pragma config CPD = OFF
    #pragma config WRT = ON
    
    #define _XTAL_FREQ 4000000
    
    void interrupt KESME (void)
    {
      if(SSPIF == 1)
      {
        PORTB = 255;
      }
      SSPIF    = 0;
    }
    
    void main(void)
    {
      TRISB   = 0X00;
      PORTB   = 0X00;
      TRISC   = 0XFF;
      SSPIE = 1;
      PEIE = 1;
      GIE = 1;
      SSPCON  = 0X36;
      SSPADD  = 0X22;
      SSPSTAT = 0X00;
      SSPCON2 = 0X00;
    
      while(1)
      {
      }
    }
    
    Attachments:
    You must be logged in to view attached files.
    #12352

    Ligo George
    Keymaster

    Hi,
    Sorry for the delay in reply. We were about to publish an article regarding this. So now, try the following link.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.