24LCxx系列的讀寫EEPROM的例C語言源程序實例
//*
//***********************************************
void EE_SEQU_Read(unsigned char ctrl,unsigned char addr,unsigned char length,unsigned char *dptr)
{
IdleI2C(); // ensure module is idle
StartI2C(); // Start condition
I2C_Done(); // Wait Start condition completed本文引用地址:http://m.butianyuan.cn/article/171029.htm
WriteI2C(ctrl); // Write Control to EEPROM
while(SSPCON2bits.ACKSTAT); // test for ACK condition, if received
I2C_Done(); // Clear SSPIF flag
WriteI2C(addr); // Write Address to EEPROM
while(SSPCON2bits.ACKSTAT); // test for ACK condition, if received
I2C_Done(); // Clear SSPIF flag
RestartI2C(); // initiate Restart condition
I2C_Done();
WriteI2C(ctrl+1); // Write Control to EEPROM
while(SSPCON2bits.ACKSTAT); // Test for ACK condition, if received
I2C_Done(); // Clear SSPIF flag
while (length!=0)
{
*dptr=ReadI2C(); // Enable I2C Receiver Store EEPROM data to Point buffer
I2C_Done();
dptr++;
length--;
if (length==0) NotAckI2C();
else AckI2C(); // Continue read next data, send a acknowledge to EEPROM
I2C_Done();
}
StopI2C(); // send STOP condition
I2C_Done(); // wait until stop condition is over
}
//***********************************************
//* Check I2C action that is completed *
//***********************************************
void I2C_Done(void)
{
while (!PIR1bits.SSPIF); // Completed the action when the SSPIF is Hi.
PIR1bits.SSPIF=0; // Clear SSPIF
}
//************************************************
//* Initial I2C Master Mode with 7 bits Address *
//* Clock Speed : 100KHz @4MHz *
//************************************************
void Initialize_I2C_Master(void)
{
OpenI2C(MASTER,SLEW_ON);
SSPADD= 9;
}
評論