单片机stm32f103c8t6,程序存储器64Kb:
对其最后一页,第63页进行读写操作,空间1Kb。
写入一个32位的数据0x12345678到Flash首地址为0x0800FC00.则在Flash中存储情况如下:
即,低位地址存储数据的低位,高位地址存储数据的高位。数据的首地址为存储地址的低位。
浮点数存储格式参考:
浮点数double在内存中的存储方式_double存储格式_HeisenbergWDG的博客-CSDN博客
Flash读写参考:
STM32CUBEIDE(16)----内部Flash读写_记帖的博客-CSDN博客
main.c代码:
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** Copyright (c) 2022 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "rtc.h"
#include "usart.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */
uint8_t rxdata1;
/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF);return ch;
}/* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 *///发字符串函数
uint8_t Usart_SendString(const uint8_t* str)
{while(*str!='\0'){if(HAL_UART_Transmit(&huart1, (uint8_t *)str, 1, 1000)!=HAL_OK){return 0; //发送失败}str++;}return 1; //发送成功
}void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{if (huart->Instance == USART1){/* 将接收成功的数据通过串口发出*///HAL_UART_Transmit_IT(&huart1,&rxdata1, 1); //不要在此处用中断式发送,不然会丢失信息HAL_UART_Transmit(&huart1,&rxdata1, 1, 0xffff);//查询法发HAL_UART_Receive_IT(&huart1, &rxdata1, 1); //重新启,接收1个数}}// 芯片的Flash为64K,最后一页Flash的首地址为0x0x0800FC00,每页1Kb。
#define FLASH_FOR_EEPROM_ADDRESS 0x0800FC00
uint32_t WriteFlashData[3] = {0x12345678,0x22222222,0x33333333};//数据
uint32_t addr = 0x0800FC00; // 芯片的Flash为64K,最后一页Flash的首地址为0x0x0800FC00,每页1Kb。
/*FLASH写入程序*/
void WriteFlashTest(uint32_t L,uint32_t Data[],uint32_t addr)
{uint32_t i=0;/* 1/4解锁FLASH*/HAL_FLASH_Unlock();/* 2/4擦除FLASH*//*初始化FLASH_EraseInitTypeDef*//*擦除方式页擦除FLASH_TYPEERASE_PAGES,块擦除FLASH_TYPEERASE_MASSERASE*//*擦除页数*//*擦除地址*/FLASH_EraseInitTypeDef FlashSet;FlashSet.TypeErase = FLASH_TYPEERASE_PAGES;FlashSet.PageAddress = addr;FlashSet.NbPages = 1;/*设置PageError,调用擦除函数*/uint32_t PageError = 0;HAL_FLASHEx_Erase(&FlashSet, &PageError);/* 3/4对FLASH烧写*/for(i=0;i<L;i++){HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr+4*i, Data[i]);}/* 4/4锁住FLASH*/HAL_FLASH_Lock();
}
/*FLASH读取打印程序*/
void PrintFlashTest(uint32_t L,uint32_t addr)
{uint32_t i=0;for(i=0;i<L;i++){printf("addr is:0x%x, data is:0x%x\n", addr+i*4, *(__IO uint32_t*)(addr+i*4));}
}double WriteFlashData2[3]={3.5,1234567890,-3.5};
typedef union{double doubleNum;uint64_t u64Num;
} DoubleAndU64;
void WriteFlashDouble(uint32_t L,double Data[],uint32_t addr)
{DoubleAndU64 data2u64;uint32_t i=0;/* 1/4解锁FLASH*/HAL_FLASH_Unlock();/* 2/4擦除FLASH*//*初始化FLASH_EraseInitTypeDef*//*擦除方式页擦除FLASH_TYPEERASE_PAGES,块擦除FLASH_TYPEERASE_MASSERASE*//*擦除页数*//*擦除地址*/FLASH_EraseInitTypeDef FlashSet;FlashSet.TypeErase = FLASH_TYPEERASE_PAGES;FlashSet.PageAddress = addr;FlashSet.NbPages = 1;/*设置PageError,调用擦除函数*/uint32_t PageError = 0;HAL_FLASHEx_Erase(&FlashSet, &PageError);/* 3/4对FLASH烧写*/for(i=0;i<L;i++){data2u64.doubleNum=Data[i];HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr+8*i, data2u64.u64Num);}/* 4/4锁住FLASH*/HAL_FLASH_Lock();
}
void PrintFlashDouble(uint32_t L,uint32_t addr)
{DoubleAndU64 data2double;uint32_t i=0;for(i=0;i<L;i++){data2double.u64Num=*(__IO uint64_t*)(addr+i*8);printf("addr is:0x%x, data is:%f\n", addr+i*8, data2double.doubleNum );}
}/* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 */uint8_t dat1[]={"China "};uint8_t dat2[]={"Back!\r\n"};
// uint8_t *dat2="7777 ";/* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_USART1_UART_Init();MX_RTC_Init();/* USER CODE BEGIN 2 */HAL_UART_Receive_IT(&huart1, &rxdata1, 1); //中断接收函数WriteFlashDouble(3,WriteFlashData2,addr);PrintFlashDouble(3,addr);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){//记得看看奇偶校验HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_RESET);HAL_Delay(2000);HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_SET);// HAL_UART_Transmit(&huart1,(uint8_t*)"China ",sizeof("China "), 0xffff);HAL_Delay(2000);while(huart1.gState != HAL_UART_STATE_READY);HAL_UART_Transmit_IT(&huart1,dat1,6); //不要在此处用sizeof(dat1),不然下一次连续发送就会失效!while(huart1.gState != HAL_UART_STATE_READY);HAL_UART_Transmit_IT(&huart1,dat2,7);while(huart1.gState != HAL_UART_STATE_READY);printf("Hello\r\n"); //使用printf必须用\n结束!!/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}/*** @brief System Clock Configuration* @retval None*/
void SystemClock_Config(void)
{RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;RCC_OscInitStruct.LSEState = RCC_LSE_ON;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK){Error_Handler();}PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 *//* USER CODE END 4 *//*** @brief This function is executed in case of error occurrence.* @retval None*/
void Error_Handler(void)
{/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */__disable_irq();while (1){}/* USER CODE END Error_Handler_Debug */
}#ifdef USE_FULL_ASSERT
/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval None*/
void assert_failed(uint8_t *file, uint32_t line)
{/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
debug中的memory:
正好对应WriteFlashData2[3]={3.5,1234567890,-3.5}中的三个双精度浮点数。
串口打印出来的数据,与实际一致。
这里double类型数据的存储,采用了联合体(union)完成的。
完整stm32cubeide工程:
https://download.csdn.net/download/fengyuzhe13/88049497