Reading and writing of STC89C52RC internal EEPROM

Publisher:runaway2000Latest update time:2018-08-22 Source: eefocusKeywords:STC89C52RC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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


Keywords:STC89C52RC Reference address:Reading and writing of STC89C52RC internal EEPROM

Previous article:c51 soft reset, a real classic, a real thorough analysis
Next article:89 Summary of C language preprocessing instructions

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号