微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > PIC:读写24LCxx系列的EEPROM的实例C语言程序

PIC:读写24LCxx系列的EEPROM的实例C语言程序

时间:05-06 来源:互联网 点击:

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;
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top