M0有 4K DATAFLASH,可以当做1K 32Bit EEPROM用,非常方便!
前几天说共享,说到要做到,呵呵,现将资料整理一下上传,可以不用再外扩EEPROM了,谁用谁知道。注意在用DATA FLASH前,请先将DATA FLASH使能,要不然没办法存数据进去。
设置见截图:
运行图片:
源码:#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvUART.h"
#include "Driver\DrvFMC.h"
uint32_t temp1,temp2,temp3;
#define DATA_Flash_Start_ADD 0x0001F000
#define PAGE_SIZE 128 //512 byte
void DATA_FLASH_Write(uint32_t add,uint32_t data);
uint32_t DATA_FLASH_Read(uint32_t add);
//============================================================================
// DATA FLASH当做EEPROM用。4k 8BIT当做1K 32位用,这样读写更方便!---烈火狂龙
// u32addr : 0-1024 写入地址
// u32data : 0-0xFFFFFFFF 写入数据
//============================================================================
void DATA_FLASH_Write(uint32_t u32addr,uint32_t u32data)
{
uint32_t data_buff[128]={0};
uint32_t i=0;
__set_PRIMASK(1);//关闭全局中断,不关中断就等着出错吧,呵呵。
UNLOCKREG();
DrvFMC_EnableISP();
for(i=0;i<AGE_SIZE;i++)
DrvFMC_Read(DATA_Flash_Start_ADD+i*4+ u32addr/128*512, &data_buff);//读出一页
DrvFMC_Erase(DATA_Flash_Start_ADD+u32addr/128*512);//擦除一页
data_buff[u32addr%128]=u32data; //修改要写入的数据
for(i=0; i<AGE_SIZE; i++) //写入数据
DrvFMC_Write(DATA_Flash_Start_ADD+i*4+ u32addr/128*512, data_buff);
DrvFMC_DisableISP();
LOCKREG();
__set_PRIMASK(0);//打开全局中断
}
//============================================================================
// u32addr : 0-1024 读取地址
//============================================================================
uint32_t DATA_FLASH_Read(uint32_t u32add)
{
uint32_t u32data;
__set_PRIMASK(1);//关闭全局中断,不关中断就等着出错吧,呵呵。
UNLOCKREG();
DrvFMC_EnableISP();
DrvFMC_Read(u32add*4+DATA_Flash_Start_ADD, &u32data);
DrvFMC_DisableISP();
LOCKREG();
__set_PRIMASK(0);//打开全局中断
return u32data;
}
//============================================================================
// 主程序
//============================================================================
int32_t main (void)
{
STR_UART_T sParam;
/* Unlock the protected registers */
UNLOCKREG();
/* Enable the 12MHz oscillator oscillation */
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);
/* Waiting for 12M Xtal stalble */
DrvSYS_Delay(5000);
/* HCLK clock source. 0: external 12MHz; 4:internal 22MHz RC oscillator */
DrvSYS_SelectHCLKSource(0);
/*lock the protected registers */
LOCKREG();
DrvSYS_SetClockDivider(E_SYS_HCLK_DIV, 0); /* HCLK clock frequency = HCLK clock source / (HCLK_N + 1) */
/* Set UART Pin */
DrvGPIO_InitFunction(E_FUNC_UART0);
/* UART Setting */
sParam.u32BaudRate = 115200;
sParam.u8cDataBits = DRVUART_DATABITS_8;
sParam.u8cStopBits = DRVUART_STOPBITS_1;
sParam.u8cParity = DRVUART_PARITY_NONE;
sParam.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES;
/* Set UART Configuration */
if(DrvUART_Open(UART_PORT0,&sParam) != E_SUCCESS)
{
printf("UART0 open failed\n");
return FALSE;
}
printf("================================================\n\r");
printf(" 菜农新唐M0助学板范例 \n\r");
printf(" 实验十: DATA FLASH读写例程 \n\r");
printf(" 烈火狂龙 论坛ID: plc_avr \n\r");
printf("================================================\n\r");
if(DATA_FLASH_Read(0)==0xFFFFFFFF)
{
DATA_FLASH_Write(0,0);
DATA_FLASH_Write(1,1);
DATA_FLASH_Write(2,2);
}
else
{
temp1 = DATA_FLASH_Read(0);
printf("当前DATA FLASH 地址0数据: %ld\n", temp1);
temp2 = DATA_FLASH_Read(1);
printf("当前DATA FLASH 地址1数据: %ld\n", temp2);
temp3 = DATA_FLASH_Read(2);
printf("当前DATA FLASH 地址2数据: %ld\n", temp3);
DATA_FLASH_Write(0,temp1+1);
DATA_FLASH_Write(1,temp2+1);
DATA_FLASH_Write(2,temp3+1);
}
printf("演示结束,按板子上面复位,可以看到DATA FLASH数据累加并保存\n");
while(1)
{
}
}
//============================================================================
// END
//============================================================================
工程包:
DATA_FLASH_RW.rar
(1.73 MB)
|