微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32的I2C-EEPROM已调试成功

STM32的I2C-EEPROM已调试成功

时间:11-17 来源:互联网 点击:

/*******************************************************************************
* Function Name : I2C_EE_PageWrite
* Description : Writes more than one byte to the EEPROM with a single WRITE
* cycle. The number of byte cant exceed the EEPROM page size.
* Input : - pBuffer : pointer to the buffer containing the data to be
* written to the EEPROM.
* - WriteAddr : EEPROMs internal address to write to.
* - NumByteToWrite : number of bytes to write to the EEPROM.
* Output : None
* Return : None
pBuffer:指向要写入数据数组的指针
WriteAddr:24c02中要写入数据的首地址
NumByteToWrite:写入的字节数
*******************************************************************************/
void I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)//写少于一页的数据
{
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE);//产生 I2Cx传输 START条件
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); //检查最近一次 I2C事件是否是输入的事件
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter);//向指定的从 I2C设备传送地址字,选择发送方向

/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //检查最近一次 I2C事件是否是输入的事件

/* Send the EEPROMs internal address to write to */
I2C_SendData(I2C1, WriteAddr); //通过外设 I2Cx发送地址

/* Test on EV8 and clear it */
while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //检查最近一次 I2C事件是否是输入的事件

/* While there is data to be written */
while(NumByteToWrite--)
{
/* Send the current byte */
I2C_SendData(I2C1, *pBuffer); //通过外设 I2Cx发送数据

/* Point to the next byte to be written */
pBuffer++;
/* Test on EV8 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//检查最近一次 I2C事件是否是输入的事件
}

/* Send STOP condition */
I2C_GenerateSTOP(I2C1, ENABLE);//产生 I2Cx传输 STOP条件
}

/*******************************************************************************
* Function Name : I2C_EE_BufferRead
* Description : Reads a block of data from the EEPROM.
* Input : - pBuffer : pointer to the buffer that receives the data read
* from the EEPROM.
* - ReadAddr : EEPROMs internal address to read from.
* - NumByteToRead : number of bytes to read from the EEPROM.
* Output : None
* Return : None
pBuffer:指向要保存读出数据的数组的指针
ReadAddr:24c02中要读出数据的首地址
NumByteToRead:读出的字节数
*******************************************************************************/
void I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)//将EEPROM的数据读入缓冲器
{
I2C_EE_WaitEepromStandbyState();//EEPROM设为待命状态
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE);//产生 I2Cx传输 START条件
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));//检查最近一次 I2C事件是否是输入的事件
/* In the case of a single data transfer disable ACK before reading the data */
if(NumByteToRead==1)
{
I2C_AcknowledgeConfig(I2C1, DISABLE);//使能或者失能指定 I2C的应答功能
}
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter);//向指定的从 I2C设备传送地址字,选择发送方向

/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//检查最近一次 I2C事件是否是输入的事件
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2C1, ENABLE);//使能或者失能 I2C外设

/* Send the EEPROMs internal address to write to */
I2C_SendData(I2C1, ReadAddr); //通过外设 I2Cx发送地址

/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//检查最近一次 I2C事件是否是输入的事件
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C1, ENABLE);//产生 I2Cx传输 START条件
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));//检查最近一次 I2C事件是否是输入的事件
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Receiver);//向指定的从 I2C设备传送地址字,选择接收方向
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));//检查最近一次 I2C事件是否是输入的事件
/* While there is data to be read */
while(NumByteToRead)
{
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) //检查最近一次 I2C事件是否是输入的事件
{
if(NumByteToRead == 2)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C1, DISABLE);//使能或者失能指定 I2C的应答功能
}

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

网站地图

Top