关于
STM32F4 HAL库操作外部EEPROM的疑惑
最近在搞24C16存储器,用的是STM32F429,工程使用CUBEMX工具生成的,因此使用的是HAL库,
但是发现HAL库提供存储器的函数就两个HAL_I2C_Mem_Write和HAL_I2C_Mem_Read,HAL库已经没有了原来DSP库的那么多函数了
我的代码如下,
sta
tic void I2Cx_Init(void)
{
if(HAL_I2C_GetState(&heval_I2c) == HAL_I2C_STATE_RESET)
{
heval_I2c.Instance = I2C2; //I2C1
heval_I2c.Init.ClockSpeed = I2C_SPEED;
heval_I2c.Init.DutyCycle = I2C_DUTYCYCLE_2;
heval_I2c.Init.OwnAddress1 = 0;
heval_I2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
heval_I2c.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
heval_I2c.Init.OwnAddress2 = 0;
heval_I2c.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
heval_I2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
/* Init the I2C */
I2Cx_MspInit();
HAL_I2C_Init(&heval_I2c);
}
}
void Delayiic()
{
unsigned int i;
for(i=0;i<500000;i++);
}
void EEPROM_WriteByte(uint16_t addr,uint8_t dat)
{
HAL_I2C_Mem_Write(&heval_I2c,0xA0,addr,I2C_MEMADD_SIZE_8BIT,&dat,1,1000);
Delayiic();
}
uint8_t EEPROM_ReadByte(uint16_t addr)
{
uint8_t dat;
HAL_I2C_Mem_Read(&heval_I2c, 0xA0, addr, I2C_MEMADD_SIZE_8BIT, &dat, 1,1000);
Delayiic();
return dat;
}
void TestIIC(void)
{
unsigned int i=0;
tmp12= HAL_I2C_IsDeviceReady(&heval_I2c,0xA0,3000,1000); //检查器件准备好
for(i=0;i<100;i++)
EEPROM_WriteByte(i,0x77);
for(i=0;i<100;i++)
{
IIC_DAT
=EEPROM_ReadByte(i);
if(IIC_DAT!=0x77)
err++;
}
while(1);
}
经过测试,读写极不稳定,有时候对,有时候不对。
坛子里有用HAL库IIC的吗,或者版主来一下,帮忙解决一下
一周热门 更多>