This example is completed under the win10 operating system using keil4 "c51v956" version
Sector distribution of STC89C52RC:
/********STC89C52 sector distribution*******
The first sector: 2000H--21FF
The second sector: 2200H--23FF
The third sector: 2400H--25FF
The fourth sector: 2600H--27FF
The fifth sector: 2800H--29FF
The sixth sector: 2A00H--2BFF
The seventh sector: 2C00H--2DFF
The eighth sector: 2E00H--2FFF
***************************************/
Operation steps of stc89c52rc internal EEPROM:
1. Erase EEPROM
2. Write EEPROM
3. Read EEPROM
Write bytes to EEPROM, then read bytes from EEPROM and send to the serial port. You can view and verify it through the serial port debugging assistant
Attached code:
Main function:
/**
******************************************************************************
* @file main.c
* @author waitstory
* @version V1.0
* @date 2018-4-11
* @brief STC89C52RC internal EEPROM reading and writing
******************************************************************************
* @attention
*
* Experimental platform: STC89C52RC chip, crystal oscillator 11.0592MHz
*
******************************************************************************
**/
#include
void InitUART(uint baud); //Serial port initialization function
void UartTXData(uchar str[]); //Serial port sending function
void Delay_ms(uint z); //delay function
float TxStr[3] = {0};
fly dat[2] ={0x01,0x02};
/********STC89C52 sector distribution*******
The first sector: 2000H--21FF
The second sector: 2200H--23FF
The third sector: 2400H--25FF
The fourth sector: 2600H--27FF
The fifth sector: 2800H--29FF
The sixth sector: 2A00H--2BFF
The seventh sector: 2C00H--2DFF
The eighth sector: 2E00H--2FFF
*******************************/
void main()
{
SectorErase(0x2000); // Erase the first sector
byte_write(0x2001,dat[0]); //Write a byte to the memory at address 0x2001
byte_write(0x2002,dat[1]); //Write a byte to the memory at address 0x2002
Delay_ms(1000); //Delay 1s
InitUART(9600); //Serial port initialization function
while(1)
{
TxStr[0] = byte_read(0x2001); //Read a byte from address 0x2001
TxStr[1] = byte_read(0x2002); //Read a byte from address 0x2002
UartTXData(TxStr); //Send the read bytes to the serial port
Delay_ms(2000); //Delay 2s
}
}
/* Serial port configuration function, baud-communication baud rate*/
void InitUART(uint baud)
{
EA = 1; // Enable interrupt master switch
SCON = 0x50; //Configure the serial port to mode 1
TMOD &= 0x0F; // Clear the control bit of T1
TMOD |= 0x20; //Configure T1 to mode 2
TH1 = 256 - (11059200/12/32)/baud; //Calculate T1 reload value
TL1 = TH1; //initial value equals reload value
ET1 = 0; //Disable T1 interrupt
ES = 1; // Enable serial port interrupt
TR1 = 1; //Start T1
}
/*Serial port send string function*/
void UartTXData(float str[])
{
fly i = 0;
while(str[i] != '\0')
{
SBUF= str[i];
i++;
while(!IF);
IF = 0;
}
}
/*Delay function*/
void Delay_ms(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
EEPROM header file:
#ifndef _EEPROM_H_
#define _EEPROM_H_
#include
#include
typedef unsigned int uint;
typedef unsigned char uchar;
/********STC89C52 sector distribution*******
The first sector: 2000H--21FF
The second sector: 2200H--23FF
The third sector: 2400H--25FF
The fourth sector: 2600H--27FF
The fifth sector: 2800H--29FF
The sixth sector: 2A00H--2BFF
The seventh sector: 2C00H--2DFF
The eighth sector: 2E00H--2FFF
*******************************/
#define RdCommand 0x01 //Byte programming data command
#define PrgCommand 0x02 //Byte read data command
#define EraseCommand 0x03 //Sector erase data command
#define Error 1
#define Ok 0
#define WaitTime 0x01 //Define the CPU waiting time, 0 for 40M or less, 1 for 20M or less, 2 for 10M or less, 3 for 5M or less
/*****ISP/IAP special function register declaration********/
sfr ISP_DATA = 0xE2;
sfr ISP_ADDRH = 0xE3;
sfr ISP_ADDRL = 0xE4;
sfr ISP_CMD = 0xE5;
sfr ISP_TRIG = 0xE6;
sfr ISP_CONTR = 0xE7;
unsigned char byte_read(unsigned int byte_addr);
void byte_write(unsigned int byte_addr,unsigned char Orig_data);
void SectorErase(unsigned int sector_addr);
#endif
C file for EEPROM:
#include "eeprom.h"
/**********Open ISP/IAP function**************/
void ISP_IAP_Enable(void)
{
EA = 0; //Disable interrupt
ISP_CONTR = ISP_CONTR & 0x18;
ISP_CONTR = ISP_CONTR | WaitTime; // Set the waiting time
ISP_CONTR = ISP_CONTR | 0x80; // Allow ISP/IAP operation
}
/**********Disable ISP/IAP function**************/
void ISP_IAP_Disable(void)
{
ISP_CONTR = ISP_CONTR & 0x7f; //Disable ISP/IAP operation
ISP_CMD = 0x00; //Remove ISP/IAP command
ISP_TRIG = 0x00; //Prevent ISP/IAP command from being triggered
EA = 1; // Enable interrupt
}
/**********Trigger ISP/IAP**************/
void ISPTrig(void)
{
ISP_TRIG = 0x46; //First send 46h, then send B9h to the ISP/IAP trigger register, this is required every time
ISP_TRIG = 0xb9; //After sending B9h, the ISP/IAP command is immediately triggered to start
_nop_();
}
/**********Bytes read**************/
unsigned char byte_read(unsigned int byte_addr)
{
unsigned char dat = 0; //Read EEPROM data cache
EA = 0; //Disable interrupt
ISP_ADDRH = (unsigned char)(byte_addr >> 8); // send address high byte
ISP_ADDRL = (unsigned char)(byte_addr & 0x00ff); //Send address low byte
ISP_IAP_Enable(); //Enable ISP/IAP function
ISP_CMD = ISP_CMD & 0xf8; // Clear the lower 3 bits of the ISP_CMD register
ISP_CMD = ISP_CMD | RdCommand; //Write read data command
ISPTrig(); //Trigger ISP/IAP
dat = ISP_DATA; //Save the data in the ISP_DATA register
ISP_IAP_Disable(); //Disable ISP/IAP function
EA = 1; // Enable interrupt
return dat; //Return the read data
}
/**********Byte Write**************/
void byte_write(unsigned int byte_addr,unsigned char Orig_data)
{
EA = 0; //Disable interrupt
ISP_ADDRH = (unsigned char)(byte_addr >> 8); // send address high byte
ISP_ADDRL = (unsigned char)(byte_addr & 0x00ff); //Send address low byte
ISP_IAP_Enable(); //Enable ISP/IAP function
ISP_CMD = ISP_CMD & 0xf8; // Clear the lower 3 bits of the ISP_CMD register
ISP_CMD = ISP_CMD | PrgCommand; //Write the write data command
ISP_DATA = Orig_data; //Write data to ISP_DATA register
ISPTrig(); //Trigger ISP/IAP
ISP_IAP_Disable(); //Disable ISP/IAP function
EA =1; //Enable interrupt
}
/**********Sector Erase**************/
void SectorErase(unsigned int sector_addr)
{
EA = 0;
ISP_ADDRH = (unsigned char)(sector_addr >> 8); //Send erase address high byte
ISP_ADDRL = (unsigned char)(sector_addr & 0x00ff); //Send erase address low byte
ISP_IAP_Enable();
ISP_CMD = ISP_CMD & 0xf8; // Clear the lower 3 bits of the ISP_CMD register
ISP_CMD = ISP_CMD | EraseCommand; //Write erase data command
ISPTrig(); //Trigger ISP/IAP
ISP_IAP_Disable(); //Disable ISP/IAP function
}
Experimental results:
Print the data to the serial port and display it in the computer serial port debugging assistant
Previous article:c51 soft reset, a real classic, a real thorough analysis
Next article:89 Summary of C language preprocessing instructions
- 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
- SHT31 evaluation + extended lithium battery power supply
- About functions that return pointers
- Electronics newbie
- PC controls two PLCs
- TI single-cell fuel gauge product classification and how to select
- Passive Low-Pass Filter and Non-Inverting Amplifier Related Issues
- ARM state and THUMB state
- Please tell me if you know what this thing is called
- Identification of long and short keys of MSP430G2553 external interrupt
- TI Embedded Live Month: In-depth exploration of new applications of embedded products in industry and automobiles. The fourth session will be live at 10:00 today.