Author Topic: PIC16F1829 i2c Problem  (Read 2712 times)

0 Members and 1 Guest are viewing this topic.

Offline Trabbo93

  • Contributor
  • Posts: 11
PIC16F1829 i2c Problem
« on: August 19, 2013, 02:29:08 pm »
Hi, I was trying to use MSSP on my PIC to write to a serial eeprom but I encountered a problem writing some i2c routines. After successfully generating the start condition I try to send the control byte to the slave and two things happen..
1) hardware sets BCL2IF
2) both SDA and SCL lines go to 5V
the module remains in idle state and does not send other data. what that looks me strange is the fact that the bus collision flag is set during data transmission, because in the datasheet of the device this condition is not even mentioned.
there is my code, I adapted it from Plibs included in XC8 compilers..
Code: [Select]
void I2Csetup(){
    SSP2CON1 = 0b00001000;
   
    APFCON0bits.RXDTSEL=1;
    APFCON0bits.TXCKSEL=1;
   
    TRISBbits.TRISB5 = 1;
    TRISBbits.TRISB7 = 1;
    ANSELBbits.ANSB5 = 0;
    ANSELBbits.ANSB7 = 0;
    INLVLBbits.INLVLB5 = 0;
    INLVLBbits.INLVLB7 = 0;

    SSP2STATbits.CKE = 0;
    SSP2STATbits.SMP = 1;

    SSPADD = 0x27; //Clock 100KHz
    PIR4bits.SSP2IF = 0;
    PIR4bits.BCL2IF = 0;
    SSP2CON1 = 0b00101000;
    SSP2CON2 = 0;
}
void I2Cstart(void){
    SSP2IF = 0;
    BCL2IF = 0;
    SSP2CON2bits.SEN = 1; //Start I2C
    while(!PIR4bits.SSP2IF);
}
void I2Cstop (void){
    PIR4bits.SSP2IF = 0;
    SSP2CON2bits.PEN = 1; //Stop I2C
    while(!PIR4bits.SSP2IF);
}
void I2Crestart(void){
    PIR4bits.SSP2IF = 0;
    SSP2CON2bits.RSEN = 1; //Restart I2C
    while(!PIR4bits.SSP2IF);
}
void I2Cwait(void){
    while ( ( SSP2CON2 & 0x1F ) || ( SSP2STATbits.R_nW ) )
     continue;
}
bit I2Cwrite(unsigned char dat){
    PIR4bits.SSP2IF = 0;
    SSP2BUF=dat;
    I2Cwait();
    while(SSP2STATbits.BF && !PIR4bits.SSP2IF);
    PIR4bits.SSP2IF = 0;
    return ~SSPCON2bits.ACKSTAT;

}
unsigned char I2Cread(char ack){
    unsigned char temp;
    SSP2CON2bits.ACKDT = ack;
    PIR4bits.SSP2IF = 0;
    SSP2CON2bits.RCEN = 1;
    while(!PIR4bits.SSP2IF);
    temp = SSP2BUF;
    PIR4bits.SSP2IF = 0;
    SSP2CON2bits.ACKEN = 1;
    while(!PIR4bits.SSP2IF);
    return temp;
}

I use 4.7k pull up on both SDA and SCL
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf