【晒FRAM铁电存储器样片】+ STM32硬件IIC驱动FRAM成功
- UID
- 753323
- 性别
- 女
|
【晒FRAM铁电存储器样片】+ STM32硬件IIC驱动FRAM成功
现在大多数工业控制中都是使用的32位机器,而这些32位机器一般来说都有硬件IIC,使用硬件IIC不但节约CPU资源,同时也能得到更快的速度。最近在研究STM32,所以也就顺便测试了一下如何使用STM32的IIC来操作,核心代码包含IIC初始化,DMA初始化,中断处理函数。
IIC初始化函数
//*************************************************//
// 函数名 : I2C_Configuration //
// 功能 : 配置I2C //
// Input : None //
// Output : None //
// Return : None //
//*************************************************//
void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
/* I2C configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = EEPROM_ADDRESS;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
#ifdef I2C_1
I2C_InitStructure.I2C_ClockSpeed = I2C_Speed_I1;
/* I2C Peripheral Enable */
I2C_Cmd(I2C1, ENABLE);
/* Apply I2C configuration after enabling it */
I2C_Init(I2C1, &I2C_InitStructure);
#endif
#ifdef I2C_2
I2C_InitStructure.I2C_ClockSpeed = I2C_Speed_I2;
/* I2C Peripheral Enable */
I2C_Cmd(I2C2, ENABLE);
/* Apply I2C configuration after enabling it */
I2C_Init(I2C2, &I2C_InitStructure);
#endif
}
//*************************************************//
// 函数名 : I2C_EE_Init //
// 功能 : 初始化I2C //
// Input : None //
// Output : None //
// Return : None //
//*************************************************//
void I2C_EE_Init()
{
//配置时钟
RCC_Configuration_I2C();
//配置IO
GPIO_Configuration_I2C();
#ifdef I2C_DMA_I1
//配置NVIC
NVIC_Configuration_I2C_1();
#endif
#ifdef I2C_DMA_I2
//配置NVIC
NVIC_Configuration_I2C_2();
#endif
//配置I2C
I2C_Configuration();
}
IIC写入数据:
void I2C_EE_MultiByteWrite_I1(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)
{
u8 i = 0;
if(NumByteToWrite > I2C_PageSize)
NumByteToWrite = I2C_PageSize;
/* Send STRAT condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, (u8)WriteAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
for (i = 0; i < NumByteToWrite; i++,pBuffer++)
{
I2C_SendData(I2C1, *pBuffer);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
/* Send STOP condition */
I2C_GenerateSTOP(I2C1, ENABLE);
}
IIC读取数据
void I2C_EE_MultiByteRead_I1(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{
u8 i = 0;
if(NumByteToRead > I2C_PageSize)
NumByteToRead = I2C_PageSize;
/* Send STRAT condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, (u8)ReadAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STOP condition */
I2C_GenerateSTOP(I2C1, ENABLE);
/* Send STRAT condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
for (i = NumByteToRead; i > 0; i--,pBuffer++)
{
if (i == 1)
{
I2C_AcknowledgeConfig(I2C1, DISABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
*pBuffer = I2C_ReceiveData(I2C1);
I2C_AcknowledgeConfig(I2C1, ENABLE);
}
else
{
I2C_AcknowledgeConfig(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
*pBuffer = I2C_ReceiveData(I2C1);
i=i;
}
}
/* Send STOP condition */
I2C_GenerateSTOP(I2C1, ENABLE);
}
关键函数就是这几个,具体程序可以看附件,直接拷贝到你的工程,更改下引脚用就行了 |
-
-
桌面.rar (5.05 KB)
|
|
|
|
|