//***********************************************************************************************************
// File Function: N76E003 APROM program DATAFLASH as EEPROM way
//***********************************************************************************************************
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#define ADDR_BASE 0x4700//位于地址末端.4700~47FF共256字节
#define FLASH_L_LOW ADDR_BASE+1
#define FLASH_L_HIGH ADDR_BASE+2
#define FLASH_H_LOW ADDR_BASE+3
#define FLASH_H_HIGH ADDR_BASE+4
volatile unsigned char xdata page_buffer[128];
/*********************************************************
功能:读取APROM中的一位数据
参数:需要读取的内存地址(addr)
*********************************************************/
UINT8 Read_APROM_BYTE(UINT16 code *u16_addr)
{
UINT8 rdata;
rdata = *u16_addr>>8;
return rdata;
}
/*****************************************************************************************************************
write_DATAFLASH_BYTE :
写入flash数据
user can copy all this subroutine into project, then call this function in main.
用户能复制这所有的代码区工程中,然后调用这个函数在主函数中
功能:在指定Flash地址写入数据
参数:需要写入的flash地址、需要写入的数据、
******************************************************************************************************************/
void Write_DATAFLASH_BYTE(unsigned int u16EPAddr,unsigned char u8EPData)
{
unsigned char looptmp=0;
unsigned int u16_addrl_r;
unsigned int RAMtmp;
//Check page start address
u16_addrl_r=(u16EPAddr/128)*128;
//Save APROM data to XRAM0
for(looptmp=0;looptmp<0x80;looptmp++)
{
RAMtmp = Read_APROM_BYTE((unsigned int code *)(u16_addrl_r+looptmp));
page_buffer[looptmp]=RAMtmp;
}
// Modify customer data in XRAM
page_buffer[u16EPAddr&0x7f] = u8EPData;
//Erase APROM DATAFLASH page
IAPAL = u16_addrl_r&0xff;
IAPAH = (u16_addrl_r>>8)&0xff;
IAPFD = 0xFF;
set_IAPEN;
set_APUEN;
IAPCN = 0x22;
set_IAPGO;
//Save changed RAM data to APROM DATAFLASH
set_IAPEN;
set_APUEN;
IAPCN = 0x21;
for(looptmp=0;looptmp<0x80;looptmp++)
{
IAPAL = (u16_addrl_r&0xff)+looptmp;
IAPAH = (u16_addrl_r>>8)&0xff;
IAPFD = page_buffer[looptmp];
set_IAPGO;
}
clr_APUEN;
clr_IAPEN;
}
/******************************************************************************************************************/
void main (void)
{
UINT8 datatemp;
UINT16 system16highsite;
/* -------------------------------------------------------------------------*/
/* Dataflash use APROM area, please ALWAYS care the address of you code */
数据闪存使用APROM区,请一定保证这部分数据和你的代码地址不会冲突
/* APROM 0x3800~0x38FF demo as dataflash */
APRON 地址为0x3800~0x38ff 演示作为数据内存
/* Please use Memory window key in C:0x3800 to check earse result */
请使用内存窗口密匙 (C:0x3800)去检查结果?????
/* -------------------------------------------------------------------------*/
InitialUART0_Timer1(115200);//串口初始化
//call write byte
Write_DATAFLASH_BYTE (0x3802,0x34);//在0x3802地址写入数据0x34
Write_DATAFLASH_BYTE (FLASH_L_LOW,0x55);//在0x4700地址写入数据0x55
Write_DATAFLASH_BYTE (FLASH_L_HIGH,0x56);//0x4701写入0x56
Write_DATAFLASH_BYTE (FLASH_H_LOW,0xaa);//0x4702写入0xaa
Write_DATAFLASH_BYTE (FLASH_H_HIGH,0x66);//0x4703写入0x66
//call read byte
datatemp = Read_APROM_BYTE(0x3802);
system16highsite = ((Read_APROM_BYTE(FLASH_H_HIGH)<<8)+Read_APROM_BYTE(FLASH_H_LOW));
while(1)
{
// printf ("\n data temp = 0x%bx", datatemp);//打印datatemp中数据
}
}
复制代码
|