PIC:读写24LCxx系列的EEPROM的实例C语言程序
EPROM (WRITE) while (SSPCON2bits.ACKSTAT) // test for Acknowledge from EEPROM RestartI2C(); // initiate Repet-Start condition WriteI2C(ctrl); // Write Control to EEPROM (WRITE) //*********************************************** WriteI2C(ctrl); // Write Control to EEPROM WriteI2C(addr); // Write Address to EEPROM RestartI2C(); // initiate Restart condition WriteI2C(ctrl+1); // Write Control to EEPROM f=ReadI2C(); // Enable I2C Receiver wait BF=1 until received data NotAckI2C(); // Genarate Non_Acknowledge to EEPROM return(f); // Return Data from EEPROM //*********************************************** WriteI2C(ctrl); // Write Control to EEPROM WriteI2C(addr); // Write Address to EEPROM RestartI2C(); // initiate Restart condition WriteI2C(ctrl+1); // Write Control to EEPROM while (length!=0) StopI2C(); // send STOP condition //*********************************************** //************************************************
I2C_Done(); // Clear SSPIF flag
{
for (i=0;i100;i++); // Delay for next Repet-Start
I2C_Done(); // Wait Repet-Start condition completed
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
while(SSPCON2bits.ACKSTAT); // test for ACK condition, if received
I2C_Done(); // Clear SSPIF flag
while(SSPCON2bits.ACKSTAT); // test for ACK condition, if received
I2C_Done(); // Clear SSPIF flag
I2C_Done();
while(SSPCON2bits.ACKSTAT); // test for ACK condition, if received
I2C_Done(); // Clear SSPIF flag
I2C_Done(); // Clear SSPIF flag
I2C_Done();
StopI2C(); // send STOP condition
I2C_Done(); // wait until stop condition is over
}
//* 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
while(SSPCON2bits.ACKSTAT); // test for ACK condition, if received
I2C_Done(); // Clear SSPIF flag
while(SSPCON2bits.ACKSTAT); // test for ACK condition, if received
I2C_Done(); // Clear SSPIF flag
I2C_Done();
while(SSPCON2bits.ACKSTAT); // Test for ACK condition, if received
I2C_Done(); // Clear SSPIF flag
{
*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();
}
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 PIC: 相关文章:
- 国外现场总线应用实例两则(12-21)
- 三菱PLC编程技巧及实例图解(12-19)
- 单片机的七个创意设计实例(从易到难),附电路图、源代码、演示视频(06-11)
- PCI驱动程序开发实例(02-21)
- 51单片机做信号发生器实例编程(01-16)
- 简单的单片机I/O口扩展实例(05-18)