我是 PIC 的新手。我知道这个论坛上有很多关于 I2C 的帖子,我已经尝试了所有给出的方法,但仍然没有解决我的问题。我正在使用 pic16f1516 和外部 EEPROM CAT24C04TDI 来存储一些数据。我使用了 4k7 欧姆电阻进行上拉,我的代码是:
void I2CWrite(void);
void WaitMSSP(void);
void I2CRead(void);
void i2c_init(void);
//void _delay_ms(unsigned int);
void main()
{
_delay_ms(100); // Give delay for power up
i2c_init(); // Initialize I2C
_delay_ms(20);
I2CWrite(); // Sends the data to I2C EEPROM
_delay_ms(50);
while(1)
{
I2CRead(); // Read back the data?s
TXREG='\n';
while(TXIF==0);
TXREG='\r';
_delay_ms(500);
}
}
void I2CWrite()
{
SSPCON2bits.SEN=1;
WaitMSSP(); // wait for the operation to be finished
SSPBUF=0xa0;//Send Slave address write command
WaitMSSP();
SSPBUF=0x00; // Send the starting address to write
WaitMSSP();
SSPBUF=0x34; // rough data to be written
WaitMSSP();
}
PEN=1; // Send stop bit
WaitMSSP();
}
void I2CRead()
{
SEN=1; //Send start bit
WaitMSSP(); //wait for the operation to be finished
SSPBUF=0xa0;//Send Slave address write command
WaitMSSP();
SSPBUF=0x00; // Send the starting address to write
WaitMSSP();
RSEN=1; // Send re-start bit
WaitMSSP();
SSPBUF=0xa1; // Slave address read command
WaitMSSP();
RCEN=1; // Enable receive
WaitMSSP();
ACKDT=1; // Acknowledge data 1: NACK, 0: ACK
ACKEN=1; // Enable ACK to send
PEN=1; // Stop condition
WaitMSSP();
putch(SSPBUF); // Send the received data to PC
_delay_ms(30);
}
PEN=1;
WaitMSSP();
}
void WaitMSSP()
{
while(!SSPIF); // while SSPIF=0 stay here else exit the loop
SSPIF=0; // operation completed clear the flag
}
void i2c_init()
{
TRISCbits.TRISC3=1; // Set up I2C lines by setting as input
TRISCbits.TRISC4=1;
SSPCON1 = 0b00101000;
// SSPADD=(FOSC / (4 * I2C_FREQ)) - 1; //clock 100khz
SSPADD = 0x18 ; //clock 100khz
SSPSTAT=80; // Slew rate control disabled
PIR1bits.SSPIF = 0; // Clear MSSP interrupt request flag
PIE1bits.SSPIE = 1; // Enable MSSP interrupt enable bit
}
当我将它连接到示波器时,发送 SEN=1 后,SDA 和 SCL 端口没有任何反应。请帮我解决这个问题。很长一段时间以来,我一直坚持这一点。