STM32 f405 对内部FLASH 写入0123数据
时间: 2024-01-23 15:03:09 浏览: 42
好的,您可以使用以下代码将数据0123写入STM32F405内部FLASH中:
```c
#include "stm32f4xx_hal.h"
#define FLASH_USER_START_ADDR ADDR_FLASH_SECTOR_6 /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR ADDR_FLASH_SECTOR_7 /* End @ of user Flash area */
/* Base address of the Flash sectors */
#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base address of Sector 0, 16 Kbytes */
#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base address of Sector 1, 16 Kbytes */
#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base address of Sector 2, 16 Kbytes */
#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base address of Sector 3, 16 Kbytes */
#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base address of Sector 4, 64 Kbytes */
#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base address of Sector 5, 128 Kbytes */
#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base address of Sector 6, 128 Kbytes */
#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base address of Sector 7, 128 Kbytes */
/* Variables used for Erase procedure */
static FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t SectorError = 0;
int main(void)
{
HAL_Init();
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
/* Erase the user Flash area ********************************************************/
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.Sector = FLASH_SECTOR_6;
EraseInitStruct.NbSectors = 1;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK)
{
/*
Error occurred while sector erase.
User can add here some code to deal with this error.
SectorError will contain the faulty sector and then to know the code error on this sector,
user can call function 'HAL_FLASH_GetError()'
*/
while (1)
{
}
}
/* Program the user Flash area word by word ********************************/
uint32_t Address = FLASH_USER_START_ADDR;
uint32_t Data = 0x01230123;
while (Address < FLASH_USER_END_ADDR)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, Data) == HAL_OK)
{
Address = Address + 4;
}
else
{
/*
Error occurred while writing data in Flash memory.
User can add here some code to deal with this error.
FLASH_GetError() returns the error value.
*/
while (1)
{
}
}
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock();
while (1)
{
}
}
```
这段代码使用了HAL库的函数来进行内部FLASH的擦除和编程。具体实现步骤如下:
1. 解锁FLASH
2. 擦除内部FLASH的指定扇区
3. 编程内部FLASH,将数据写入指定地址
4. 锁定FLASH
需要注意的是,这段代码将数据写入了内部FLASH的第6个扇区,如果您需要写入其他扇区,需要修改代码中的地址。
另外,由于内部FLASH每个扇区都有擦除和编程次数的限制,建议在实际应用中进行合理的擦除和编程操作,以避免对内部FLASH造成损坏。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)