CH32V系列MCU:关于将“变量”放在FLASH某一位置方法

以CH32V307为例,配置方法如下:

 

关于给定义的变量赋值,注意要通过FLASH编程操作写入,将值写入FLASH该位置。完成后可直接通过读取该变量获取该值。如下图:

 配置代码如下:

/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/06/06
* Description        : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for 
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/

/*
 *@Note
 USART Print debugging routine:
 USART1_Tx(PA9).
 This example demonstrates using USART1(PA9) as a print debug port output.

*/

#include "debug.h"


/* Global typedef */

/* Global define */

/* Global Variable */

volatile u8 flag __attribute__((section(".TEST")));

//读取指定地址的一个字节(8位数据)
uint8_t FLASH_ReadByte(uint32_t address)
{
   return *(__IO uint8_t*)address;
}

void Flash_Program(uint32_t Address, uint16_t Data)
{
    FLASH_Unlock();
    FLASH_ErasePage(Address);
    FLASH_ProgramHalfWord(Address, Data);


}
/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main(void)
{
    u8 i=0;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init(115200);    
    printf("SystemClk:%d\r\n",SystemCoreClock);
    printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
    printf("This is printf example\r\n");


    while(1)
    {
        i++;
        Flash_Program(0x0800F000,i);
        printf("flag:%d\r\n",flag);
        printf("u8:%02x\r\n",FLASH_ReadByte(0x0800F000));
        Delay_Ms(1000);

    }
}

 

posted @ 2024-07-26 10:47  ZaiLi  阅读(54)  评论(0编辑  收藏  举报