N76E003 emulated EEPROM

Publisher:VolareLatest update time:2022-06-28 Source: eefocusKeywords:N76E003 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When using STM32, a part of the internal 1flash can often be allocated as a data storage area (EEPROM) to save data after power failure. The N76E003 microcontroller can also achieve similar functions.

insert image description here
insert image description here

The following figure is the memory allocation diagram of N76E003. We can see from the figure below that the address range used by APROM (APROM + LDROM = 18K, when LDROM is 0, the address of APROM is 0000~0x47FF)

insert image description here

The following program is a routine provided by Nuvoton, and I have only made some superficial comments.


//***********************************************************************************************************

//  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 // Located at the end of the address. 4700~47FF, a total of 256 bytes


#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];


/*********************************************************

Function: Read one bit of data in APROM

Parameter: memory address to be read (addr)

*********************************************************/

UINT8 Read_APROM_BYTE(UINT16 code *u16_addr)

{

UINT8 rdata;

rdata = *u16_addr>>8;

return rdata;

}

/*****************************************************************************************************************

write_DATAFLASH_BYTE :

Write flash data

user can copy all this subroutine into project, then call this function in main.

Users can copy all the code areas in the project, and then call this function in the main function


Function: Write data to the specified Flash address

Parameters: flash address to be written, data to be written,

******************************************************************************************************************/

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    */

The data flash uses the APROM area, please make sure that this part of the data does not conflict with your code address

/* APROM 0x3800~0x38FF demo as dataflash             */

APRON address is 0x3800~0x38ff, used as data memory for demonstration

/* Please use Memory window key in C:0x3800 to check earse result */   

Please use the memory window key (C:0x3800) to check the result???    

/* -------------------------------------------------------------------------*/

InitialUART0_Timer1(115200); //Serial port initialization

//call write byte 

Write_DATAFLASH_BYTE (0x3802,0x34); //Write data 0x34 at address 0x3802

Write_DATAFLASH_BYTE (FLASH_L_LOW,0x55); //Write data 0x55 at address 0x4700

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); // print the data in datatemp

}

}


Summary: When using flash as eeprom, it is best to write backwards from 47FF according to actual needs to ensure that it will not interfere with the normal operation of the program.

Keywords:N76E003 Reference address:N76E003 emulated EEPROM

Previous article:N76E003 ADC Multi-channel Sampling
Next article:N76E003 IIC OLED

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号