标题: STC89C52Rc单片机怎么把数据存入eeprom [打印本页]

作者: 白亚涛147    时间: 2021-8-4 15:17
标题: STC89C52Rc单片机怎么把数据存入eeprom
count1>=115就是通过按键修改115这个数据后掉电后怎么保持,谢谢朋友们了

Screenshot_20210804_150838.jpg (55.38 KB, 下载次数: 26)

Screenshot_20210804_150838.jpg

作者: hhdsdy    时间: 2021-8-4 16:41
打开STC的下载软件,找到范例程序,选STC89CxRC点EEPROM就行,也有汇编的

1.png (82.1 KB, 下载次数: 23)

1.png

作者: lkc8210    时间: 2021-8-4 16:42
  1. /*------------------------------------------------------------------*/
  2. /* --- STC MCU Limited ---------------------------------------------*/
  3. /* --- STC89-90xx Series MCU ISP/IAP/EEPROM Demo -------------------*/
  4. /* If you want to use the program or the program referenced in the  */
  5. /* article, please specify in which data and procedures from STC    */
  6. /*------------------------------------------------------------------*/

  7. #include "reg51.h"
  8. #include "intrins.h"

  9. typedef unsigned char BYTE;
  10. typedef unsigned int WORD;

  11. /*Declare SFR associated with the IAP */
  12. sfr IAP_DATA    =   0xE2;           //Flash data register
  13. sfr IAP_ADDRH   =   0xE3;           //Flash address HIGH
  14. sfr IAP_ADDRL   =   0xE4;           //Flash address LOW
  15. sfr IAP_CMD     =   0xE5;           //Flash command register
  16. sfr IAP_TRIG    =   0xE6;           //Flash command trigger
  17. sfr IAP_CONTR   =   0xE7;           //Flash control register

  18. /*Define ISP/IAP/EEPROM command*/
  19. #define CMD_IDLE    0               //Stand-By
  20. #define CMD_READ    1               //Byte-Read
  21. #define CMD_PROGRAM 2               //Byte-Program
  22. #define CMD_ERASE   3               //Sector-Erase

  23. /*Define ISP/IAP/EEPROM operation const for IAP_CONTR*/
  24. //#define ENABLE_IAP 0x80           //if SYSCLK<40MHz
  25. #define ENABLE_IAP   0x81           //if SYSCLK<20MHz
  26. //#define ENABLE_IAP x82            //if SYSCLK<10MHz
  27. //#define ENABLE_IAP 0x83           //if SYSCLK<5MHz

  28. //Start address for STC89C58xx EEPROM
  29. #define IAP_ADDRESS 0x08000

  30. void Delay(BYTE n);
  31. void IapIdle();
  32. BYTE IapReadByte(WORD addr);
  33. void IapProgramByte(WORD addr, BYTE dat);
  34. void IapEraseSector(WORD addr);

  35. BYTE count1_Old;

  36. void main()
  37. {
  38.         count1 = IapReadByte(0x2000);
  39.         count1_Old = count1;
  40.     while (1)
  41.         {
  42.                 if(count1_Old != count1)
  43.                 {
  44.                         IapEraseSector(0x2000);
  45.                         IapProgramByte(0x2000, count1);
  46.                         count1_Old = count1;
  47.                 }
  48.                 //你的代码
  49.                
  50.         };
  51. }

  52. /*----------------------------
  53. Software delay function
  54. ----------------------------*/
  55. void Delay(BYTE n)
  56. {
  57.     WORD x;

  58.     while (n--)
  59.     {
  60.         x = 0;
  61.         while (++x);
  62.     }
  63. }

  64. /*----------------------------
  65. Disable ISP/IAP/EEPROM function
  66. Make MCU in a safe state
  67. ----------------------------*/
  68. void IapIdle()
  69. {
  70.     IAP_CONTR = 0;                  //Close IAP function
  71.     IAP_CMD = 0;                    //Clear command to standby
  72.     IAP_TRIG = 0;                   //Clear trigger register
  73.     IAP_ADDRH = 0x80;               //Data ptr point to non-EEPROM area
  74.     IAP_ADDRL = 0;                  //Clear IAP address to prevent misuse
  75. }

  76. /*----------------------------
  77. Read one byte from ISP/IAP/EEPROM area
  78. Input: addr (ISP/IAP/EEPROM address)
  79. Output:Flash data
  80. ----------------------------*/
  81. BYTE IapReadByte(WORD addr)
  82. {
  83.     BYTE dat;                       //Data buffer

  84.     IAP_CONTR = ENABLE_IAP;         //Open IAP function, and set wait time
  85.     IAP_CMD = CMD_READ;             //Set ISP/IAP/EEPROM READ command
  86.     IAP_ADDRL = addr;               //Set ISP/IAP/EEPROM address low
  87.     IAP_ADDRH = addr >> 8;          //Set ISP/IAP/EEPROM address high
  88.     IAP_TRIG = 0x46;                //Send trigger command1 (0x46)
  89.     IAP_TRIG = 0xb9;                //Send trigger command2 (0xb9)
  90.     _nop_();                        //MCU will hold here until ISP/IAP/EEPROM operation complete
  91.     dat = IAP_DATA;                 //Read ISP/IAP/EEPROM data
  92.     IapIdle();                      //Close ISP/IAP/EEPROM function

  93.     return dat;                     //Return Flash data
  94. }

  95. /*----------------------------
  96. Program one byte to ISP/IAP/EEPROM area
  97. Input: addr (ISP/IAP/EEPROM address)
  98.        dat (ISP/IAP/EEPROM data)
  99. Output:-
  100. ----------------------------*/
  101. void IapProgramByte(WORD addr, BYTE dat)
  102. {
  103.     IAP_CONTR = ENABLE_IAP;         //Open IAP function, and set wait time
  104.     IAP_CMD = CMD_PROGRAM;          //Set ISP/IAP/EEPROM PROGRAM command
  105.     IAP_ADDRL = addr;               //Set ISP/IAP/EEPROM address low
  106.     IAP_ADDRH = addr >> 8;          //Set ISP/IAP/EEPROM address high
  107.     IAP_DATA = dat;                 //Write ISP/IAP/EEPROM data
  108.     IAP_TRIG = 0x46;                //Send trigger command1 (0x46)
  109.     IAP_TRIG = 0xb9;                //Send trigger command2 (0xb9)
  110.     _nop_();                        //MCU will hold here until ISP/IAP/EEPROM operation complete
  111.     IapIdle();
  112. }

  113. /*----------------------------
  114. Erase one sector area
  115. Input: addr (ISP/IAP/EEPROM address)
  116. Output:-
  117. ----------------------------*/
  118. void IapEraseSector(WORD addr)
  119. {
  120.     IAP_CONTR = ENABLE_IAP;         //Open IAP function, and set wait time
  121.     IAP_CMD = CMD_ERASE;            //Set ISP/IAP/EEPROM ERASE command
  122.     IAP_ADDRL = addr;               //Set ISP/IAP/EEPROM address low
  123.     IAP_ADDRH = addr >> 8;          //Set ISP/IAP/EEPROM address high
  124.     IAP_TRIG = 0x46;                //Send trigger command1 (0x46)
  125.     IAP_TRIG = 0xb9;                //Send trigger command2 (0xb9)
  126.     _nop_();                        //MCU will hold here until ISP/IAP/EEPROM operation complete
  127.     IapIdle();
  128. }

复制代码



作者: jizhongbiao    时间: 2021-8-4 16:58
直接用写flash就行,stcisp上有例程。




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1