找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 209|回复: 0
收起左侧

HK32F030M单片机FASH读写程序

[复制链接]
ID:302293 发表于 2024-3-4 17:09 | 显示全部楼层 |阅读模式
    Demo例程
  1. int main(void)
  2. {
  3.   /*!< At this stage the microcontroller clock setting is already configured,
  4.        this is done through SystemInit() function which is called from startup
  5.        file (KEIL_startup_hk32f030m.s) before to branch to application main.
  6.        To reconfigure the default setting of SystemInit() function, refer to
  7.        system_hk32f030m.c file
  8.      */
  9.   
  10.   uint16_t i;
  11.   
  12.   /* Inser delay time for debug purpose */
  13.   Delay(0xFFFFF);
  14.   
  15.   HK_EVAL_LEDInit(LED1);
  16.   HK_EVAL_LEDToggle(LED1);
  17.   
  18.   for(i=0; i<512; i++)
  19.   {
  20.     Write_Data[i] = 0xAB;
  21.   }
  22.   
  23.   Write_Data_To_Flash(FLASH_USER_START_ADDR, Write_Data, 512);
  24.   
  25.   Read_Data_From_Flash(FLASH_USER_START_ADDR, Read_Data, 512);


  26.   /* Infinite Loop */
  27.   while (1)
  28.   {
  29.    
  30.     HK_EVAL_LEDToggle(LED1);
  31.     Delay(0xFFFFF);
  32.         
  33.   }
  34. }


  35. //从指定地址写入数据
  36. void Write_Data_To_Flash(uint32_t address, uint8_t* data, uint16_t size)
  37. {
  38.   
  39.   uint8_t Temp;
  40.   
  41.   /* Unlock the Flash to enable the flash control register access *************/
  42.   FLASH_Unlock();

  43.   /* Erase the user Flash area
  44.      (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) *********/

  45.   /* Clear pending flags (if any) */
  46.   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR);

  47.   /* Define the number of page to be erased */
  48.   NbrOfPage = size / FLASH_PAGE_SIZE;

  49.   /* Erase the FLASH pages */
  50.   for(EraseCounter = 0; (EraseCounter <= NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
  51.   {
  52.     if(FLASH_ErasePage(FLASH_USER_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)) != FLASH_COMPLETE)
  53.     {
  54.       /* Error occurred while sector erase.
  55.           User can add here some code to deal with this error */
  56.       while(1)
  57.       {
  58.       }
  59.     }
  60.   }

  61.   /* Program the user Flash area half-word by half-word
  62.     (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/  
  63.   uint16_t i=0;  
  64.   FLASH_USER_END_ADDR = address + size;
  65.   while (address < FLASH_USER_END_ADDR)
  66.   {
  67.    
  68.     Temp = data[i];
  69.    
  70.     if (FLASH_ProgramByte(address, Temp) == FLASH_COMPLETE)
  71.     {
  72.       address = address + 1;
  73.     }
  74.     else
  75.     {
  76.       /* Error occurred while writing data in Flash memory.
  77.          User can add here some code to deal with this error */
  78.       while (1)
  79.       {
  80.       }
  81.     }
  82.    
  83.     i++;        
  84.   }

  85.   /* Lock the Flash to disable the flash control register access (recommended
  86.      to protect the FLASH memory against possible unwanted operation) *********/
  87.   FLASH_Lock();  
  88.   
  89.   
  90. }





  91. //从指定地址读取数据
  92. void Read_Data_From_Flash(uint32_t address, uint8_t* data, uint16_t size)
  93. {
  94.   uint16_t i=0;  
  95.   FLASH_USER_END_ADDR = address + size;
  96.   while (address < FLASH_USER_END_ADDR)
  97.   {
  98.     data[i] = *(uint32_t*)address;
  99.     address = address + 1;
  100.    
  101.     i++;
  102.   }
  103.       
  104.   for(i=0; i<512; i++)
  105.   {
  106.     if( Write_Data[i] != Read_Data[i] )
  107.     {      
  108.       //读取错误处理
  109.       MemoryProgramStatus = FAILED;
  110.       while(1);            
  111.     }      
  112.   }      
  113. }
复制代码

原理图: 无
仿真: 无
代码: HK32F030M-FLASH.7z (1.03 MB, 下载次数: 3)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表