读写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 读写 相关文章:
- 单片机C语言程序-依次点亮八个LED灯(12-01)
- 单片机C语言程序结构容易出问题的有一下几点(12-01)
- 单片机c语言程序(按键和消抖)(11-29)
- 单片机C语言程序设计师试题(11-28)
- 如何写出高效的单片机C语言程序代码(11-27)
- 51单片机C语言程序(一)发光二极管(11-25)