STC internal EEPROM application function

Publisher:chenxiaohong68Latest update time:2018-02-01 Source: eefocusKeywords:STC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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 (); 
}


Keywords:STC Reference address:STC internal EEPROM application function

Previous article:STC12C5A60S2 AD conversion C language sample program (ADC query mode)
Next article:The use and application of STC89 microcontroller watchdog

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号