標題:
HK32F030M單片機FASH讀寫程序
[打印本頁]
作者:
sr861126
時間:
2024-3-4 17:09
標題:
HK32F030M單片機FASH讀寫程序
Demo例程
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (KEIL_startup_hk32f030m.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_hk32f030m.c file
*/
uint16_t i;
/* Inser delay time for debug purpose */
Delay(0xFFFFF);
HK_EVAL_LEDInit(LED1);
HK_EVAL_LEDToggle(LED1);
for(i=0; i<512; i++)
{
Write_Data[i] = 0xAB;
}
Write_Data_To_Flash(FLASH_USER_START_ADDR, Write_Data, 512);
Read_Data_From_Flash(FLASH_USER_START_ADDR, Read_Data, 512);
/* Infinite Loop */
while (1)
{
HK_EVAL_LEDToggle(LED1);
Delay(0xFFFFF);
}
}
//從指定地址寫入數(shù)據(jù)
void Write_Data_To_Flash(uint32_t address, uint8_t* data, uint16_t size)
{
uint8_t Temp;
/* Unlock the Flash to enable the flash control register access *************/
FLASH_Unlock();
/* Erase the user Flash area
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) *********/
/* Clear pending flags (if any) */
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR);
/* Define the number of page to be erased */
NbrOfPage = size / FLASH_PAGE_SIZE;
/* Erase the FLASH pages */
for(EraseCounter = 0; (EraseCounter <= NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
{
if(FLASH_ErasePage(FLASH_USER_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)) != FLASH_COMPLETE)
{
/* Error occurred while sector erase.
User can add here some code to deal with this error */
while(1)
{
}
}
}
/* Program the user Flash area half-word by half-word
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
uint16_t i=0;
FLASH_USER_END_ADDR = address + size;
while (address < FLASH_USER_END_ADDR)
{
Temp = data[i];
if (FLASH_ProgramByte(address, Temp) == FLASH_COMPLETE)
{
address = address + 1;
}
else
{
/* Error occurred while writing data in Flash memory.
User can add here some code to deal with this error */
while (1)
{
}
}
i++;
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
FLASH_Lock();
}
//從指定地址讀取數(shù)據(jù)
void Read_Data_From_Flash(uint32_t address, uint8_t* data, uint16_t size)
{
uint16_t i=0;
FLASH_USER_END_ADDR = address + size;
while (address < FLASH_USER_END_ADDR)
{
data[i] = *(uint32_t*)address;
address = address + 1;
i++;
}
for(i=0; i<512; i++)
{
if( Write_Data[i] != Read_Data[i] )
{
//讀取錯誤處理
MemoryProgramStatus = FAILED;
while(1);
}
}
}
復制代碼
原理圖: 無
仿真: 無
代碼:
HK32F030M-FLASH.7z
(1.03 MB, 下載次數(shù): 4)
2024-3-4 21:46 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1