Reputation: 11
I basically have this problem that has been bugging me for a while. I have a PIC32MX128L128H processor and a Basic I/O Shield. The pins and everything is done correctly, except for my communication with the EEPROM with I2C. I will copy my code below, but before, I there are the controls:
I2C1CON: bit 0 SEN: Start Condition Enable bit bit 2 PEN: Stop Condition Enable bit bit 3 RCEN: Receive Enable bit bit 4 ACKEN: Acknowledge Sequence Enable bit bit 5 ACKDT: Acknowledge Data bit
I2C1STAT: bit 15 ACKSTAT: Acknowledge Status bit (1 = not received, 0 = received) bit 14 TRSTAT: Transmit Status bit bit 6 I2COV: I2C Receive Overflow Status bit
Thank you!
void EEPROM_wait(){
while(I2C1STAT & (1 << 15)){
I2C1CON |= 0x1;
while(I2C1CON & 0x1);
I2C1TRN = 0xa0;
while(I2C1STAT & (1 << 14));
}
}
void i2c_wait(){
while(I2C1CON & 0x1f || I2C1STAT & (1 << 14));
}
void writeByte(uint8_t lowByte, uint8_t highByte, uint8_t data){
//Start Procedure
do{
I2C1CON |= 0x1;
i2c_wait();
I2C1TRN = 0xa0;
i2c_wait();
} while(I2C1STAT & (1 << 15));
//Send the adress of the EEPROM and also R/W bit
//Send highByte
I2C1TRN = highByte;
i2c_wait();
//Send lowByte
I2C1TRN = lowByte;
i2c_wait();
//Send actual data
I2C1TRN = data;
i2c_wait();
//Stop the procedure
I2C1CON |= 0x4;
i2c_wait();
EEPROM_wait();
}
uint8_t readByte(uint8_t lowByte, uint8_t highByte)
{
//Initialize Procedure
do{
I2C1CON |= 0x1;
i2c_wait();
I2C1TRN = 0xa0;
i2c_wait();
}while(I2C1STAT & (1 << 15));
//Send the address of the EEPROM
//I2C1TRN = 0xa0;
//i2c_wait();
//Send highByte
I2C1TRN = highByte;
i2c_wait();
//Send lowByte
I2C1TRN = lowByte;
i2c_wait();
//Initialize Procedure Read
do{
I2C1CON |= 0x1;
i2c_wait();
I2C1TRN = 0xa1;
i2c_wait();
} while (I2C1STAT & (1 << 15));
//Send the address of the EEPROM with Read bit
//I2C1TRN = 0xa1;
//i2c_wait();
//Enable Receive Procedure
I2C1CON |= 1 << 3;
i2c_wait();
I2C1STATCLR = 1 << 6;
//Read from the EEPROM
uint8_t rtn = I2C1RCV;
//Acknowledge
I2C1CON &= ~(1 << 5);
I2C1CON |= 1 << 4;
//Stop Procedure
I2C1CON |= 1 << 2;
i2c_wait();
return rtn;
}
Upvotes: 0
Views: 251
Reputation: 11
I realised what the problem was. The chipkit bus clock rate was not set the same as the i2c bus clock which led in midwriting or misreading.
Upvotes: 0