📄 24c256.c
字号:
/****************************************************************
* CopyRight Reserved: MR QIU
* 文件名:24c256.c
* 描述:提供24c256的底层驱动。
*
* 版本:v1.0
* 作者:ktop
* 日期:2004/12/31
****************************************************************/
#define _24C256_C
#include "24C256.h"
#include "stm32f10x_it.h"
/**************************************************************
* Name:I2C_EE_PageWrite
* input:u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite
* output:none
*
***************************************************************/
void I2C_EE_BufferWrite(u8* pBuffer, u16 WriteAddr, u8 NumByteToWrite)
{
/* Send START condition */
I2C_GenerateSTART(I2C_EPROM, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C_EPROM, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C_EPROM,EPROM_ADDR, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C_EPROM, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2C_EPROM, WriteAddr>>8);
/* Test on EV8 and clear it */
while(! I2C_CheckEvent(I2C_EPROM, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2C_EPROM, WriteAddr&0x00ff);
/* Test on EV8 and clear it */
while(! I2C_CheckEvent(I2C_EPROM, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* While there is data to be written */
while(NumByteToWrite--)
{
/* Send the current byte */
I2C_SendData(I2C_EPROM, *pBuffer);
/* Point to the next byte to be written */
pBuffer++;
/* Test on EV8 and clear it */
while (!I2C_CheckEvent(I2C_EPROM, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
/* Send STOP condition */
I2C_GenerateSTOP(I2C_EPROM, ENABLE);
}
/***********************************************************************
* Name: 2C_EE_BufferRead
* Input: pBuffer read to it,ReadAddr read address,NumByteToRead the
* number of ready to read
* Output: None
***********************************************************************/
void I2C_EE_BufferRead(u8* pBuffer, u16 ReadAddr, u8 NumByteToRead)
{
/* Send START condition */
I2C_GenerateSTART(I2C_EPROM, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C_EPROM, I2C_EVENT_MASTER_MODE_SELECT));
/* In the case of a single data transfer disable ACK before reading the data */
if(NumByteToRead==1)
{
I2C_AcknowledgeConfig(I2C_EPROM, DISABLE);
}
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C_EPROM,EPROM_ADDR, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C_EPROM, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2C_EPROM, ENABLE);
/* Send the EEPROM's internal address to write to (heigh)*/
I2C_SendData(I2C_EPROM, ReadAddr>>8);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C_EPROM, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send the EEPROM's internal address to write to (heigh)*/
I2C_SendData(I2C_EPROM, ReadAddr&0xff);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C_EPROM, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C_EPROM, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C_EPROM, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C_EPROM,EPROM_ADDR, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C_EPROM, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* While there is data to be read */
while(NumByteToRead)
{
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2C_EPROM, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
if(NumByteToRead == 2)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C_EPROM, DISABLE);
}
if(NumByteToRead == 1)
{
/* Send STOP Condition */
I2C_GenerateSTOP(I2C_EPROM, ENABLE);
}
/* Read a byte from the EEPROM */
*pBuffer = I2C_ReceiveData(I2C_EPROM);
/* Point to the next location where the byte read will be saved */
pBuffer++;
/* Decrement the read bytes counter */
NumByteToRead--;
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(I2C_EPROM, ENABLE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -