stm32 cubemx at24c64
时间: 2023-10-21 13:05:11 浏览: 170
stm32cubemx
这是一个关于STM32和AT24C64的问题。AT24C64是一种64K位的串行EEPROM存储器,而STM32是一种微控制器。Cubemx是一个用于生成STM32代码的图形化工具。
如果你想在STM32上使用AT24C64,你需要连接它们并编写相应的代码。你可以使用STM32的I2C接口来连接AT24C64,然后使用HAL库或者LL库来编写读写AT24C64的代码。
以下是一个使用HAL库读取AT24C64的示例代码:
```c
#include "stm32f4xx_hal.h"
#define EEPROM_ADDRESS 0xA0
I2C_HandleTypeDef hi2c1;
void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(i2cHandle->Instance==I2C1)
{
/* I2C1 clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* I2C1 interrupt Init */
HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
HAL_NVIC_SetPriority(I2C1_ER_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
}
}
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
{
if(i2cHandle->Instance==I2C1)
{
/* Peripheral clock disable */
__HAL_RCC_I2C1_CLK_DISABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
/* I2C1 interrupt Deinit */
HAL_NVIC_DisableIRQ(I2C1_EV_IRQn);
HAL_NVIC_DisableIRQ(I2C1_ER_IRQn);
}
}
void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
}
void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
{
/* Memory write completed callback */
}
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
/* Memory read completed callback */
}
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{
/* I2C error callback */
}
int main(void)
{
uint8_t data[2];
data[0] = 0x00; // 写入地址
data[1] = 0x55; // 写入数据
HAL_Init();
MX_I2C1_Init();
if (HAL_I2C_Mem_Write(&hi2c1, EEPROM_ADDRESS, 0x00, I2C_MEMADD_SIZE_16BIT, data, 2, 1000) != HAL_OK)
{
Error_Handler();
}
if (HAL_I2C_Mem_Read(&hi2c1, EEPROM_ADDRESS, 0x00, I2C_MEMADD_SIZE_16BIT, data, 2, 1000) != HAL_OK)
{
Error_Handler();
}
while (1)
{
/* Infinite loop */
}
}
```
阅读全文