I2c

Status
Not open for further replies.

MrJammin

New Member
Does anyone have any reliable C source code for implementing an I2C bus between a PIC and another IC? I tried looking online to no avail.
 
Here you go, this should get you started.

Code:
void InitI2C();
void WaitSSP();
void SendStart();
void SendStop();
void SendRestart();
char SendByte(char);
char ReceiveByte();
void SendNack();
void SendAck();
char WriteByte(int,char);
char ReadByte(int);

//initialise the MSSP module
void InitI2C(){
    TRISBbits.TRISB0=1;
    TRISBbits.TRISB1=1;
    SSPCON1=0b00101000;
    SSPCON2=0;
    SSPSTAT=0b10000000;
    SSPADD=50;  //20000/(4*100);   //Fosc/(4*baud)
    PIR1bits.SSPIF=0;
    PIR2bits.BCLIF=0;
}

//Wait for the module to finish it's last action.
void WaitSSP(){
    while(PIR1bits.SSPIF==0);
    PIR1bits.SSPIF=0;
}

//send start bit
void SendStart(){
    SSPCON2bits.SEN=1;
    WaitSSP();
}

//Send stop bit.
void SendStop(){
    SSPCON2bits.PEN=1;
    WaitSSP();
}

//Send restart bit
void SendRestart(){
    SSPCON2bits.RSEN=1;
    WaitSSP();
}

//Send byte and return ack bit - 1=Ack  0=NAck
char SendByte(char Byte){
    SSPBUF=Byte;
    WaitSSP();
    return(!SSPCON2bits.ACKSTAT);
}

//Get a byte from the slave
char ReceiveByte(){
    SSPCON2bits.RCEN=1;
    WaitSSP();
    return(SSPBUF);
}

//Send a Not Acknowledge (NAck) to the slave
void SendNack(){
    SSPCON2bits.ACKDT=1;
    SSPCON2bits.ACKEN=1;
    WaitSSP();
}

//Send an Acknowledge (Ack) to the slave
void SendAck(){
    SSPCON2bits.ACKDT=0;
    SSPCON2bits.ACKEN=1;
    WaitSSP();
}

//Sends the start and device address.
//If the device is busy then it resends until accepted.
void SendID(char DeviceID){
    SendStart();
    if(SendByte(DeviceID)==1)
        return;
    do{
        SendRestart();
    }while(SendByte(DeviceID)==0);
}

//Write a byte to 24LC256
char WriteByte(int Address, char Byte){
    SendID(0b10100000);
    if(SendByte(Address>>8)==0)
        return(0);
    if(SendByte(Address&0xff)==0)
        return(0);
    if(SendByte(Byte)==0)
        return(0);
    SendStop();
    return(1);
}

//Read a byte from a 24LC256
char ReadByte(int Address){
char Byte;
    SendID(0b10100000);
    if(SendByte(Address>>8)==0)
        return(0);
    if(SendByte(Address&0xff)==0)
        return(0);
    SendRestart();
    if(SendByte(0b10100001)==0)
        return(0);
    Byte=ReceiveByte();
    SendNack();
    SendStop();
    return(Byte);
}
This was for a 18F4550.

Mike.
 
Does anyone have any reliable C source code for implementing an I2C bus between a PIC and another IC? I tried looking online to no avail.

do you want software or hardware i2c? do you need slave side or the master side?

check out the: https://www.electro-tech-online.com/threads/i2c-lcd-controller-using-pic16f690.40338/#post323873

You have there example where 2 pic's communicate over i2c protocol both hw and sw implementation. It is in C .. MikroC and PICC C are used, but you can easily port to another C compiler.

hope that helps
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…