How to use STC51 microcontroller EEPROM

Publisher:BlossomJoyLatest update time:2023-06-25 Source: elecfansKeywords:STC51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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. .

How to use STC51 microcontroller EEPROM

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


Keywords:STC51 Reference address:How to use STC51 microcontroller EEPROM

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)

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号