STC51 microcontroller has a flash-like function EEPROM, which can save data when power is turned off. Different models can save data of different sizes. Taking 12C5A60S2 as an example, the size of the EEPROM is 2K and is divided into two sectors. It needs to be saved in many places when power is turned off. .
First of all, there is the problem of registers. Different series of STC microcontrollers have different registers related to EEPROM. For example, the 10/11/12 series of sfr ISP_DATA = 0xC2; sfr ISP_ADDRH = 0xC3; sfr ISP_ADDRL = 0xC4; sfr ISP_CMD = 0xC5; sfr ISP_TRIG = 0xC6 ;sfr ISP_CONTR = 0xC7; sfr ISP_DATA = 0xe2 for 89/90 series; sfr ISP_ADDRH = 0xe3; sfr ISP_ADDRL = 0xe4; sfr ISP_CMD = 0xe5; sfr ISP_TRIG = 0xe6; sfr ISP_CONTR = 0xe7; Note that the register must be as specified in the data manual Configuration and ISP trigger commands are also different. Please see the code below for details.
The second is about the sector issue. Different models of MCU have different numbers of sectors. Please check the data manual for details. Here I take 12C5A60S2 as an example. There are 2 sectors in total, each sector is 512B, and the address range is 0x0000~0x03FF. Do not exceed the range when using.
EEPROM has three functions when used, namely reading, writing, and erasing, which are controlled by the ISP_CMD register and correspond to 1/2/3 respectively.
The following is the code part of this content, which are initialization, reading, erasing and writing respectively. This program is for 11.0592MHz crystal oscillator and STC12C5A60S2 MCU. The following program can be used for testing
EEPROM.c
void DisableEEPROM(void)
{
ISP_CONTR = 0;//Disable ISP/IAP operations
ISP_CMD = 0; //Remove ISP/IAP command
ISP_TRIG = 0; //Prevent ISP/IAP commands from being triggered accidentally
ISP_ADDRH = 0xff; //Point to non-EEPROM area to prevent misoperation
ISP_ADDRL = 0xff; //Point to non-EEPROM area to prevent misoperation
}
void EEPROM_Read_n(unsigned int EE_address,unsigned char *DataAddress,unsigned char lenth)
{
EA = 0;//ban interruption
ISP_ENABLE();//Macro call, set waiting time, allow ISP/IAP operation, sending once is enough
ISP_READ();//Macro call, send byte read command. If the command does not need to be changed, there is no need to send the command again.
do
{
ISP_ADDRH = EE_address / 256; //Send the high byte of the address (you need to resend the address when the address needs to be changed)
ISP_ADDRL = EE_address % 256; //Send address low byte
ISP_TRIG();//Macro call, first send 5AH, then send A5H to the ISP/IAP trigger register, this needs to be done every time
_nop_();
*DataAddress = ISP_DATA;//The read data is sent to
EE_address++;
DataAddress++;
}while(--lenth);
DisableEEPROM();
EA = 1; //Re-enable interrupts
}
void EEPROM_SectorErase(unsigned int EE_address)
{
EA = 0;//ban interruption
//Only sector erase, no byte erase, 512 bytes/sector.
//Any byte address in the sector is the sector address.
ISP_ADDRH = EE_address / 256; //Send the high byte of the sector address (you need to resend the address when the address needs to be changed)
ISP_ADDRL = EE_address % 256; //Send the low byte of the sector address
ISP_ENABLE();//Set the waiting time to allow ISP/IAP operations. Just send it once.
ISP_ERASE();//Macro call, send sector erase command. If the command does not need to be changed, there is no need to send the command again.
ISP_TRIG();//Macro call, first send 5AH, then send A5H to the ISP/IAP trigger register, this needs to be done every time
DisableEEPROM();
EA = 1; //Re-enable interrupts
}
void EEPROM_Write_n(unsigned int EE_address,unsigned char *DataAddress,unsigned char lenth)
{
EA = 0;//ban interruption
ISP_ENABLE();//Set the waiting time to allow ISP/IAP operations. Just send it once.
ISP_WRITE();//Macro call, send byte write command. If the command does not need to be changed, there is no need to re-send the command.
do
{
ISP_ADDRH = EE_address / 256; //Send the high byte of the address (you need to resend the address when the address needs to be changed)
ISP_ADDRL = EE_address % 256; //Send address low byte
ISP_DATA = *DataAddress;//Send data to ISP_DATA. Only when the data changes, it needs to be sent again.
ISP_TRIG();//Macro call, first send 5AH, then send A5H to the ISP/IAP trigger register, this needs to be done every time
_nop_();
EE_address++;//Next address
DataAddress++;//Next data
}while(--lenth);//until the end
DisableEEPROM();
EA = 1; //Re-enable interrupts
}
EPPROM.h
#ifndef __EEPROM_H__
#define __EEPROM_H__
#include“max52.h”
#include
#defineISP_WAIT_FREQUENCYISP_WAIT_2MHZ
#defineISP_WAIT_2MHZ6
#define ISP_TRIG() ISP_TRIG12()
#define ISP_TRIG12() ISP_TRIG = 0x5A, ISP_TRIG = 0xA5//Waiting instructions are divided into 12 and 89 series
#define ISP_TRIG89()ISP_TRIG = 0x46,ISP_TRIG = 0xB9
#define ISP_EN(1《《7)
#define ISP_SWBS(1《《6)
#define ISP_SWRST(1《《5)
#define ISP_CMD_FAIL(1《《4)
#defineISP_STANDBY()ISP_CMD = 0 //ISP idle command (disabled)
#defineISP_READ()ISP_CMD = 1//ISP read command
#defineISP_WRITE()ISP_CMD=2//ISP write command
#defineISP_ERASE()ISP_CMD = 3 //ISP erase command
#defineISP_ENABLE()ISP_CONTR = (ISP_EN + ISP_WAIT_FREQUENCY)
#defineISP_DISABLE()ISP_CONTR = 0; ISP_CMD = 0; ISP_TRIG = 0; ISP_ADDRH = 0xff; ISP_ADDRL = 0xff
void DisableEEPROM(void);
void EEPROM_Read_n(unsigned int EE_address,unsigned char *DataAddress,unsigned char lenth);
void EEPROM_SectorErase(unsigned int EE_address);
void EEPROM_Write_n(unsigned int EE_address,unsigned char *DataAddress,unsigned char lenth);
#endif
Previous article:What is a single-chip microcomputer_51 pre-study knowledge of single-chip microcomputer
Next article:Summary of issues related to the crystal oscillator of the 51 microcontroller (dry information)
- 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
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- 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?
- Common basic settings of timer T0, T1
- [nRF52840 DK Review] Improve custom services - send data
- Please recommend a cheap io expansion chip iic spi can be used for keyboard and led indication
- Application of high-precision current source in gyroscope testing
- Young people have no moral principles and do not consider production capacity in via impedance design
- During the Chinese New Year, we can talk about job plans.
- Qorvo Online Design Conference - Improving System Performance with Low Phase Noise Amplifiers
- Smart Internet of Things Dormitory Based on STM32
- As a former smart wear developer, I prefer smart rings.
- How to rewrite C++ source program into C language