我基本上有一个困扰我一段时间的问题。我有一个PIC32MX128L128H处理器和一个基本I / O板。引脚和所有操作均正确完成,但我与具有I2C的EEPROM通讯除外。我将在下面复制我的代码,但是在此之前,我有一些控件:
I2C1CON: bit 0 SEN:启动条件使能位 bit 2 PEN:停止条件使能位 bit 3 RCEN:接收使能位 bit 4 ACKEN:应答序列使能位 bit 5 ACKDT:确认数据位
I2C1STAT: bit 15 ACKSTAT:应答状态位(1 =未接收,0 =已接收) bit 14 TRSTAT:发送状态位 bit 6 I2COV:I2C接收溢出状态位
谢谢!
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;
}
答案 0 :(得分:0)
我意识到了问题所在。 Chipkit总线时钟频率设置与导致中间写入或误读的i2c总线时钟不同。