24LCxx系列的读写EEPROM的例C语言源程序实例
EPROM (WRITE)
I2C_Done(); // Clear SSPIF flag
while (SSPCON2bits.ACKSTAT) // test for Acknowledge from EEPROM
{
for (i=0;i100;i++); // Delay for next Repet-Start
RestartI2C(); // initiate Repet-Start condition
I2C_Done(); // Wait Repet-Start condition completed
WriteI2C(ctrl); // Write Control to EEPROM (WRITE)
I2C_Done(); // Clear SSPIF flag
}
StopI2C(); // send STOP condition
I2C_Done(); // wait until stop condition is over
}
//***********************************************
//* Random Read a Byte from EEPROM *
//* - ctrl : Control Byte of EEPROM (Write) *
//* (Ctrl +1 ) : Read Command *
//* - addr : Address Byte of EEPROM *
//* - Return : Read Data from EEPROM *
//***********************************************
unsigned char EEPROM_Read(unsigned char ctrl,unsigned char addr)
{
unsigned char f;
IdleI2C(); // ensure module is idle
StartI2C(); // Start condition
I2C_Done(); // Wait Start condition completed
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
f=ReadI2C(); // Enable I2C Receiver wait BF=1 until received data
I2C_Done(); // Clear SSPIF flag
NotAckI2C(); // Genarate Non_Acknowledge to EEPROM
I2C_Done();
StopI2C(); // send STOP condition
I2C_Done(); // wait until stop condition is over
return(f); // Return Data from EEPROM
}
//***********************************************
//* Sequential Read from EEPROM
//*
//* - ctrl : Control Byte of EEPROM
//* - addr : Location of EEPROM
//* - length : Read counter
//* - *dptr : Store EEPROM data to RAM
//*
//***********************************************
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
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;
}
语言 源程序 实例 的例 EEPROM 系列 读写 24LCxx 相关文章:
- 基于TMS320C62X DSP的混合编程研究(07-12)
- 语音识别及其定点DSP实现(06-14)
- C语言编译过程中的错误分析(08-25)
- 基于仿真的嵌入式软件白盒覆盖测试系统设计(08-26)
- 基于FPGA的AD7862接口电路设计(01-01)
- 嵌入式系统设计方法的演化——从单片机到单片系统(01-28)