51 MCU study notes: continuous reading and writing of STC89C52RC internal EEPROM memory

Publisher:快乐舞蹈Latest update time:2017-02-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The internal EEPROM of the STC microcontroller is simulated by DATAFLASH, not a real EEPROM memory, and cannot be operated by ordinary methods.
Here are some points to note:
1. Before writing a byte, read the other valid data of the sector where the byte is located into RAM for temporary storage (this step is not necessary).
2. After temporary storage, erase the entire sector (512 bytes). After erasing, the data in each address of the entire sector becomes 0xFF.
3. Write the N bytes of data to be written into EEPROM using the byte write function
. 4. Write other useful EEPROM values ​​temporarily stored in RAM back to EEPROM using the byte write function.
5. The byte write function of the EEPROM simulated by STC using FLASH can only change 1 to 0, but not 0 to 1.
  The data is all 1 only after the sector is erased.
  For example: at address 0x21f0, write 11010110 for the first time, and write 111010 for the second time. The read result is the AND of these two values ​​10010.
         Therefore, it is wrong to write new data when the value at an address is not 0xff. The sector must be erased first to change it to 0xff.
         For writing a single byte, we can first check whether the data at the address is 0xff. If it is, there is no need to erase the sector.

----------------------------------------------------------------------
The reading and writing process of the internal EEPROM of the STC89C52 microcontroller 
1. Configure the ISP_CONTR register, enable the 7th bit ISPEN, make the ISP_IAP function effective, and configure the waiting time of the lower 3 bits
2. Write instructions: the 3 commands of read/write/erase sector 3. Assignment
: the address value of ISP_ADDRH and ISP_ADDRL
4. Turn off the total interrupt EA, because the 2 trigger instructions to be written below must be operated continuously and cannot be interrupted
5. Execute the common ISP_IAP trigger instruction. The read and write operations can be performed after the trigger
6. Turn on the interrupt EA, turn off the ISP_IAP function: clear the relevant registers

C Code  Favorite Code

  1. #include "my51.h"  

  2.   

  3. /******************Define command bytes******************/         

  4. #define read_cmd 0x01 //Byte read data command       

  5. #define wirte_cmd 0x02 //byte programming data command       

  6. #define erase_cmd 0x03 // sector erase data command                 

  7.     

  8. /****************Special function register declaration****************/    

  9. sfr ISP_DATA = 0xe2;     

  10. sfr ISP_ADDRH = 0xe3;       

  11. sfr ISP_ADDRL = 0xe4;     

  12. sfr ISP_CMD = 0xe5;     

  13. sfr ISP_TRIG = 0xe6;        

  14. sfr ISP_CONTR = 0xe7;  

  15.   

  16. /*Define Flash operation waiting time and constants to allow IAP/ISP/EEPROM operations******************/  

  17. //#define enable_waitTime 0x80 //When the system operating clock is <30MHz, set this value to the IAP_CONTR register  

  18. //#define enable_waitTime 0x81 //When the system operating clock is <24MHz, set this value to the IAP_CONTR register  

  19. //#define enable_waitTime 0x82 //When the system operating clock is <20MHz, set this value for the IAP_CONTR register  

  20. #define enable_waitTime 0x83 //When the system working clock is <12MHz, set this value to the IAP_CONTR register  

  21. //#define enable_waitTime 0x84 //When the system operating clock is <6MHz, set this value for the IAP_CONTR register  

  22.     

  23.       

  24. void ISP_IAP_disable(void) //Close ISP_IAP  

  25. {     

  26.     EA=1; //Resume interrupt  

  27.     ISP_CONTR = 0x00;       

  28.     ISP_CMD = 0x00;        

  29.     ISP_TRIG = 0x00;                                               

  30. }  

  31.   

  32. void ISP_IAP_trigger() //trigger   

  33. {     

  34.     EA=0; //The following two instructions must be executed continuously, so the interrupt is disabled             

  35.     ISP_TRIG = 0x46; //Send trigger command word 0x46       

  36.     ISP_TRIG = 0xB9; //Send trigger command word 0xB9       

  37. }  

  38.          

  39.   

  40. void ISP_IAP_readData(u16 beginAddr, u8* pBuf, u16 dataSize) //Read data  

  41. {  

  42.     ISP_DATA=0; //clear to zero, or not clear  

  43.     ISP_CMD = read_cmd; //Command: read  

  44.     ISP_CONTR = enable_waitTime; // Enable ISP_IAP and send the waiting time   

  45.     while(dataSize--) //Loop reading  

  46.     {  

  47.         ISP_ADDRH = (u8)(beginAddr >> 8); //Send address high byte        

  48.         ISP_ADDRL = (u8)(beginAddr & 0x00ff); //Send address low byte  

  49.         ISP_IAP_trigger(); //Trigger  

  50.         beginAddr++; //Address++  

  51.         *pBuf++ = ISP_DATA; //Save data to the receiving buffer  

  52.     }  

  53.     ISP_IAP_disable(); //Disable ISP_IAP function  

  54. }         

  55.      

  56. void ISP_IAP_writeData(u16 beginAddr,u8* pDat,u16 dataSize) //write data      

  57. {             

  58.     ISP_CONTR = enable_waitTime; // Enable ISP_IAP and send the waiting time         

  59.     ISP_CMD = wirte_cmd; //Send byte programming command word  

  60.     while(dataSize--)  

  61.     {  

  62.         ISP_ADDRH = (u8)(beginAddr >> 8); //Send address high byte         

  63.         ISP_ADDRL = (u8)(beginAddr & 0x00ff); //Send address low byte             

  64.         ISP_DATA = *pDat++; //Send data         

  65.         beginAddr++;   

  66.         ISP_IAP_trigger(); //Trigger  

  67.     }            

  68.     ISP_IAP_disable(); //Disable                                        

  69. }      

  70.           

  71. void ISP_IAP_sectorErase(u16 sectorAddr) // sector erase  

  72. {             

  73.     ISP_CONTR = enable_waitTime; // Enable ISP_IAP; and send the waiting time         

  74.     ISP_CMD = erase_cmd; //Send sector erase command word           

  75.     ISP_ADDRH = (u8)(sectorAddr >> 8); //Send address high byte         

  76.     ISP_ADDRL = (u8)(sectorAddr & 0X00FF); //Send address low byte             

  77.     ISP_IAP_trigger(); //Trigger  

  78.     ISP_IAP_disable(); //Disable ISP_IAP function           

  79. }  

  80.       

  81. void main() //test  

  82. {     

  83.     u8 buf[3]={0}; //Receive data buffer  

  84.     u8 dat[5]={b(111010),b(1001),b(1),b(1011),b(1110)}; //I wrote it in binary to observe the LED light     

  85.     ISP_IAP_sectorErase(0x2000); // sector erase, one block of 512 bytes         

  86.     ISP_IAP_writeData(0x21f0,dat,sizeof(dat)); //Write EEPROM             

  87.     ISP_IAP_readData(0x21f0,buf,sizeof(buf)); //Read  

  88.     P1=buf[2]; //Write 11010110 at address 0x21f0 for the first time, and 111010 for the second time. The result is the AND of these two values ​​10010  

  89.     while(1); //So if the value at an address is not 0xff, it is wrong to write new data. You must first erase it to 0xff   

  90. }  

 

C Code  Favorite Code

  1. #ifndef _MY51_H  

  2. #define _MY51_H  

  3. #include   

  4. //#include   

  5. #include   

  6. #include   

  7. #include "mytype.h"  

  8.   

  9. /*************Binary Input Macro****************************/  

  10. #ifndef _LongToBin_  

  11. #define _LongToBin_  

  12. #define LongToBin(n) \               

  13. ( \   

  14. ((n >> 21) & 0x80) | \   

  15. ((n >> 18) & 0x40) | \   

  16. ((n >> 15) & 0x20) | \   

  17. ((n >> 12) & 0x10) | \   

  18. ((n >> 9) & 0x08) | \   

  19. ((n >> 6) & 0x04) | \   

  20. ((n >> 3) & 0x02) | \   

  21. ((n) & 0x01) \   

  22. )  

  23. #define bin(n) LongToBin(0x##n##l)  

  24. #define BIN(n) bin(n)  

  25. #define B(n) bin(n)  

  26. #define b(n) bin(n)             

  27. #endif  

  28.   

  29. /*************Setting macro for a single data bit*********************/  

  30. #ifndef _BIT_  

  31. #define _BIT_   

  32. #define BIT(n) (1<

  33. #define bit(n) BIT(n)  

  34. #endif  

  35.   

  36. #define high 1 //High level  

  37. #define low 0 //Low level  

  38.   

  39. #define led P1 //light bus control  

  40.   

  41. sbit led0=P1^0; //8 LED lights, the cathode sends a low level to light up  

  42. sbit led1=P1^1;  

  43. sbit led2=P1^2;  

  44. sbit led3=P1^3;  

  45. sbit led4=P1^4;  

  46. sbit led5=P1^5;  

  47. sbit led6=P1^6;  

  48. sbit led7=P1^7;   

  49. sbit ledLock=P2^5; //LED latch status, 0 for lock, 1 for unlock  

  50.   

  51. sbit beep=P2^3; //Buzzer  

  52.   

  53. void delayms(u16 ms);  

  54. //void delayXus(u8 us); //The function executes (8+6x) machine cycles, i.e. t=(8+6x)*1.085  

  55. ///////////////////////////////////////////////////// //////////////////////////////  

  56.   

  57.   

  58. #endif  

 


Reference address:51 MCU study notes: continuous reading and writing of STC89C52RC internal EEPROM memory

Previous article:51 MCU learning notes: stepper motor control, forward and reverse rotation, etc.
Next article:51 MCU Study Notes: Infrared Receiver

Recommended ReadingLatest update time:2024-11-16 12:44

8051 MCU development environment construction under Ubuntu 16.04
Due to work requirements, I am preparing to learn MCU, and I am also preparing to learn embedded Linux in the future. Recently, I am fed up with the automatic updates of Win10, so I gave up the Windows environment and prepared to start from 51 MCU in Linux environment, learning both MCU and Linux. I am a novice in b
[Microcontroller]
Based on 51 single chip microcomputer + SYN6288 voice broadcast
material I use the module from Greensun Electronic Technology, which has complete documentation and related examples. SYN6288 module schematic diagram Speech synthesis commands wiring How to wire The wiring is actually very simple. Implementation principle The 51 single-chip microcomputer serial port receives
[Microcontroller]
Based on 51 single chip microcomputer + SYN6288 voice broadcast
Simulation design of 51 microcontroller parking space management system
basic skills: The chip can be replaced by STC89C52/STC89C51/AT89C52/AT89C51 and other 51 microcontroller chips. 1. Press the button to simulate infrared detection and count the number of vehicles entering and exiting the parking lot. 2. The LED light set simulates the parking situation in the parking lot, and lights u
[Microcontroller]
Simulation design of 51 microcontroller parking space management system
Embedded Learning Notes 14——51 MCU A/D Converter
1. A/D converter: converts analog signals into digital signals. 2. The speed of A/D conversion is slower than that of D/A. The principle is as shown in the figure below:  
[Microcontroller]
Embedded Learning Notes 14——51 MCU A/D Converter
[MCU] [Learning Log] 51 MCU Learning Log [Day1, 2022.1.09]
first part 1. Introduction to MCU: 1. Basic knowledge Micro Controller Unit, or MCU for short; It integrates a series of common computer hardware functions such as CPU, RAM, ROM, timer, interrupt system, communication interface, etc. The tasks of the microcontroller: information collection (relying on sensors),
[Microcontroller]
[MCU] [Learning Log] 51 MCU Learning Log [Day1, 2022.1.09]
C51 MCU Learning Notes (Part 3) - Key control of lights, buzzers and other devices
1. The principle of independent buttons A general independent button has four pins. Regardless of whether the button is pressed or not, pins 1 and 2 are always connected, and pins 3 and 4 are also connected. When the button is pressed, pins 1 and 2 are connected to pins 3 and 4. If the button is pressed and held, it w
[Microcontroller]
C51 MCU Learning Notes (Part 3) - Key control of lights, buzzers and other devices
[51 MCU] EEPROM 24c02 [I2C code package-save to realize running light]
Here, EEPROM 24c02 is packaged and can be directly called in the future. Its connection method is: SDA-P2.1; SCL-P2.0; WP-VCC _ :i2c.c   1 /*-----------------------------------------------   2 Name: IIC Protocol    3 Content: The function uses software delay method to generate SCL pulse, so it is necessary to make
[Microcontroller]
RAID memory: write-back technology and hot-swap capacity technology
This article summarizes the lessons learned from working with RAID memory. The following introduces two small technologies used in HP's hot-swap technology, namely scrubbing technology and hot-plug capabilities technology.   Write-back technology   HP hot-swap RAID memory provides a write-back hardware method that
[Analog Electronics]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号