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

Thread Starter

sairyuva

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

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

Art

Joined Sep 10, 2007
806
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.
 

retched

Joined Dec 5, 2009
5,207
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?
 

Thread Starter

sairyuva

Joined Mar 9, 2010
11
i m just starting
Please see this code and state my mistake
Rich (BB code):
/*master.c*/
#include<htc.h>


__CONFIG(HS & WDTDIS & PWRTEN & LVPDIS & BORDIS);

unsigned char _data, i=0x00;

void Init_I2C()
{
    TRISC3=0;    //SCL
    TRISC4=0;    //SDA
    SSPCON=0x38;
    SSPCON2=0x00;
    SMP=1;
    SSPEN=1;
    CKE=0;
    SSPIF=0;
    BCLIF=0;    //bus collision flag
    SSPADD=0x63;    
    SEN=1;
    TRISB=0x00;
    PORTB=0x00;
    TRISD7=0;
    RD7=0;
}









void main()
{
    Init_I2C();
   
    while(1)
    {
        
        SEN=1;
        ACKSTAT=1;
        i=0;
       
    
        RCEN=1;
        ACKEN=1;
        
        while(SSPIF==0);
        SSPIF=0;
    
        SSPBUF=i;        
        i++;
      
        while(SSPIF==0);
        SSPIF=0;
        SSPBUF=i;
        i++;
      
        while(SSPIF==0);
        SSPIF=0;
        PEN=1;
        while(SSPIF==0);
        SSPIF=0;
        delay_Nms(100);
    }
}
Rich (BB code):
/*slave.c*/


#include<htc.h>




__CONFIG(HS & WDTDIS & PWRTEN & LVPDIS & BORDIS);

unsigned char _data;







void Init_I2C()
{
    TRISC3=1;    //SCL
    TRISC4=1;    //SDA
    SSPCON=0x3E;    //slave mode
    SSPCON2=0x00;
    SMP=1;
    ei();    
    
    CKE=0;
    CKP=1;
    SSPEN=1;
    SSPIF=0;
    SSPADD=0x69;
    BCLIF=0;    
    TRISB=0x00;
    PORTB=0x00;
    TRISD7=0;
    RD7=0;
    SEN=1;
    SSPOV=0;
}






void main()
{
    Init_I2C();
    while(1)
    {
        PORTB=_data;
        
    }
}

void interrupt ISR(void)
{
    if(SSPIF)
    {
        if(RW==1)
        {
           
            RD7=1;
            _data=SSPBUF;
            CKP=1;
        }
        SSPIF=0;    
                
    }
}
 

retched

Joined Dec 5, 2009
5,207
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?
 
Top