The STC12C5410 series microcontroller has a built-in 2K EEPROM, which is divided into 4 sectors, each sector is 512 bytes. Please pay attention to the following points when using it:
1. There are three basic commands: byte read, byte write, sector erase
2. There is no byte erase, only sector erase. If you want to change a byte in a sector and keep the other bytes unchanged, you must first read out the other bytes and save them, then erase the entire sector, and then write the modified byte content and the saved other byte content into the sector.
3. The time for sector reading and writing is short, while the time for erasing is relatively long. Pay attention to the calling position of the sector erasing function. For example, it is best not to put it in the timer interrupt.
4. The fewer sectors are used, the more convenient it is.
The following are application functions:
#include
#include
#define ISP_IAP_BYTE_READ 0x1; //Byte read
#define ISP_IAP_BYTE_PROGRAM 0x2; //Byte programming, can write 1 to 0, to change 1 to 0, must perform byte programming
#define ISP_IAP_SECTOR_ERASE 0x3; //Sector erase, can erase 0 to 1, to change 0 to 1, must erase the entire sector
//Define Flash operation wait time and allow IAP/ISP/EEPROM operation constants
#define ENABLE_ISP 0x81; //When the system working clock <24M Hz , set this value to the ISP_CONTR register
#define DATA_FLASH_START_ADDRESS 0x2e00; //EEPROM of STC12C5410AD series microcontroller Test start address
void iap_d ISABle () {
ISP_ADDRL = 0xff;
ISP_ADDRH = 0xff;
ISP_CONTR = 0; //Turn off IAP function
ISP_CMD = 0; //Clear command register, make command register have no command, this sentence can be omitted
ISP_TRIG = 0; //Clear command trigger register, make command trigger register have no trigger, this sentence can be omitted
}
uchar byte_read (uint address){
uchar i;
ISP_ADDRL = address; //Set target unit address
ISP_ADDRH = address >> 8;
ISP_CONTR = ENABLE_ISP; //Turn on IAP function, set Flash operation waiting time
ISP_CMD = ISP_IAP_BYTE_READ; //Set to IAP/ISP/EEPROM byte read mode command
//EA=0;
ISP_TRIG = 0x46;
ISP_TRIG = 0xb9; //Send 46h first, then send B9h To the ISP/IAP trigger register, this is required every time. After sending B9h, the ISP/IAP command is triggered and started immediately
for (i = 0;i < 30;i++);
//EA=1;
iap_disable (); //; Turn off the IAP function, clear the relevant special function registers, and put the CPU in a safe state. It is recommended to turn off the IAP function after a continuous IAP operation is completed. It is not necessary to turn it off every time
i = ISP_DATA; //The read data is in the ISP_DATA unit and sent to the accumulator A
return i;
}
void byte_program (uint address,uchar wr_data) {
uchar i;
ISP_ADDRL = address; //Set the target unit address
ISP_ADDRH = address >> 8;
ISP_DATA = wr_data; //The data to be programmed is first sent to the ISP_DATA register
ISP_CONTR = ENABLE_ISP; //Turn on the IAP function and set the Flash operation waiting time
ISP_CMD = ISP_IAP_BYTE_PROGRAM; //Set to IAP/ISP/EEPROM byte programming mode command
//EA=0;
ISP_TRIG = 0x46;
ISP_TRIG = 0xb9; //First send 46h, then send B9h to the ISP/IAP trigger register, this is required every time, after sending B9h, the ISP/IAP command is triggered immediately
for (i = 0;i < 30;i++);
//EA=1;
iap_disable (); //;Turn off the IAP function, clear the relevant special function registers, and put the CPU in a safe state
}
void sector_erase (uint address) {
uchar i;
ISP_ADDRL = address; //Set the target unit
addressISP_ADDRH = address >> 8;
ISP_CONTR = ENABLE_ISP; //Turn on the IAP function, set the Flash operation waiting timeISP_CMD
= ISP_IAP_SECTOR_ERASE; //Set to IAP/ISP/EEPROM sector erase mode command
//EA=0;
ISP_TRIG = 0x46;
ISP_TRIG = 0xb9; //First send 46h, then send B9h to the ISP/IAP trigger register, each time it is necessary, after sending B9h, the ISP/IAP command is immediately triggered
for (i = 0;i < 30;i++);
//EA=1;
iap_disable ();
}
Previous article:STC12C5A60S2 AD conversion C language sample program (ADC query mode)
Next article:The use and application of STC89 microcontroller watchdog
- 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
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- The latest update of MicroPython optimizes the NeoPixel driver
- Interpretation of 5G modulation signals and continuous wave signals
- Do you know the future development trend of WiFi?
- ST's sensors can be purchased at the STM32 Tmall official flagship store
- "Dating in Spring" + "Just Strolling"
- C2000 CLA FAQ: Architecture and Configuration
- New infrared temperature measurement thermal imager is coming! AutoNavi holds a press conference in Beijing!
- Divide an assembly file into several assembly files. Compile prompt variable is not defined
- RF amplifier performance test
- What kind of feedback is introduced in the following circuit?