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.
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)
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.
Previous article:N76E003 ADC Multi-channel Sampling
Next article:N76E003 IIC OLED
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- How to select all device bit numbers or nominal values in the SCH file for AD1904 version
- Testing methods and diagnostic analysis for DSP-containing circuit boards
- Fundamentals of Circuits and Analog Electronics Technology
- Need a low current charging IC and a Bluetooth chip with integrated MCU
- PCB Layout Guidelines
- LAUNCHXL-F28379D comprehensive routine
- 【DWIN Serial Port Screen】One of the Nucleic Acid Sampling and Registration Systems
- Live broadcast at 10 am today [TI's new generation MSP430 helps low-cost ultrasonic water meter]
- Playing with Zynq Serial 24 - Porting of User-Defined IP Cores
- [Only in Hangzhou, register now!] 12/21 (Friday) WPI Group/ON Semiconductor Industrial Internet of Things (IIoT) Application...