找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1541|回复: 20
打印 上一主题 下一主题
收起左侧

求助哪位大师帮我看看单片机程序,怎样保存按键值,搞了几天没头绪

[复制链接]
跳转到指定楼层
楼主
  1. #include <reg52.h>
  2. #include <intrins.h>
  3. typedef unsigned char BYTE;
  4. typedef unsigned int WORD;
  5. //unsigned char table[8]={0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55};
  6. /*Declare SFR associated with the IAP */
  7. sfr  IAP_DATA      =   0xC2;       //Flash data register
  8. sfr  IAP_ADDRH     =   0xC3;             //Flash address HIGH
  9. sfr  IAP_ADDRL     =   0xC4;               //Flash address LOW
  10. sfr  IAP_CMD       =   0xC5;                //Flash command register
  11. sfr  IAP_TRIG       =   0xC6;                 //Flash command trigger
  12. sfr  IAP_CONTR     =   0xC7;             //Flash control register
  13. /*Define ISP/IAP/EEPROM command*/
  14. #define   CMD_IDLE      0                         //Stand-By
  15. #define   CMD_READ      1                      //Byte-Read
  16. #define   CMD_PROGRAM   2                 //Byte-Program
  17. #define   CMD_ERASE      3                      //Sector-Erase
  18. /*Define ISP/IAP/EEPROM operation const for IAP_CONTR*/
  19. //#define   ENABLE_IAP   0x80              //if SYSCLK<30MHz
  20. //#define   ENABLE_IAP   0x81              //if SYSCLK<24MHz
  21. //#define   ENABLE_IAP    0x82              //if SYSCLK<20MHz
  22.         #define   ENABLE_IAP   0x83              //if SYSCLK<12MHz
  23. //#define   ENABLE_IAP   0x84              //if SYSCLK<6MHz
  24. //#define   ENABLE_IAP   0x85              //if SYSCLK<3MHz
  25. //#define   ENABLE_IAP   0x86              //if SYSCLK<2MHz
  26. //#define   ENABLE_IAP   0x87              //if SYSCLK<1MHz
  27. //Start address for STC12C5A60S2 series EEPROM
  28. #define    IAP_ADDRESS   0x2000
  29. void Delay(BYTE n);
  30. void IapIdle();
  31. BYTE IapReadByte(WORD addr);
  32. void IapProgramByte(WORD addr, BYTE dat);
  33. void IapEraseSector(WORD addr);

  34. WORD i;
  35. void Delay(BYTE n)
  36. {
  37.       WORD x;
  38.       while (n--)
  39.       {
  40.             x = 0;
  41.             while (++x);
  42.       }
  43. }


  44. unsigned char RunMode;
  45. //**********************************System Fuction*************************************************
  46. void Delay1ms(unsigned int count)
  47. {
  48.         unsigned int i,j;
  49.         for(i=0;i<count;i++)
  50.         for(j=0;j<120;j++);
  51. }



  52. unsigned char GetKey(void)
  53. {
  54.         unsigned char KeyTemp,CheckValue,Key = 0x00;
  55.         CheckValue = P3&0x32;
  56.         if(CheckValue==0x32)
  57.                 return 0x00;

  58.         Delay1ms(10);
  59.         KeyTemp = P3&0x32;
  60.         if(KeyTemp==CheckValue)
  61.                 return 0x00;

  62.         if(!(CheckValue&0x02))
  63.                 Key|=0x01;
  64.         if(!(CheckValue&0x10))
  65.                 Key|=0x02;
  66.         if(!(CheckValue&0x20))
  67.                 Key|=0x04;
  68.         return Key;
  69. }

  70. unsigned int TimerCount,SystemSpeed,SystemSpeedIndex;
  71. void InitialTimer2(void)
  72. {
  73.                 TMOD=0X01;
  74.                 TH0=(65536-5000)/256;
  75.                 TL0=(65536-5000)%256;
  76.                 ET0=1;
  77.                 TR0=1;

  78.         EA=1;
  79. }

  80. unsigned int code SpeedCode[]={        5,   8,  10,  14,  17,  20,  30,
  81.                                   40,  50,  60,  70,  80,  90, 100, 120, 140, 160,
  82.                               180, 200, 300, 400, 500, 600, 700, 800, 900,1000};//27
  83. void SetSpeed(unsigned char Speed)
  84. {
  85.                
  86.         SystemSpeed =SpeedCode[Speed];
  87.         

  88. }

  89. void LEDShow(unsigned int LEDStatus)
  90. {
  91.         P1 = ~(LEDStatus&0xff);

  92. }

  93. void InitialCPU(void)
  94. {
  95.         RunMode = 0x00;
  96.         TimerCount = 0;
  97.         SystemSpeedIndex =10;
  98.          

  99.         P3 = 0xff;
  100.         Delay1ms(500);
  101.         P1 = 0xFF;

  102.         P3 = 0xFF;
  103.         SetSpeed(SystemSpeedIndex);

  104. }

  105. //Mode 0
  106. unsigned int LEDIndex = 0;
  107. bit LEDDirection = 1,LEDFlag = 1;
  108. void Mode_0(void)
  109. {
  110.         LEDShow(0x01<<LEDIndex);
  111.         LEDIndex = (LEDIndex+1)%8;
  112. }
  113. //Mode 1
  114. void Mode_1(void)
  115. {
  116.         LEDShow(0x80>>LEDIndex);
  117.         LEDIndex = (LEDIndex+1)%8;
  118. }
  119. //Mode 2
  120. void Mode_2(void)
  121. {
  122.         if(LEDDirection)
  123.                 LEDShow(0x01<<LEDIndex);
  124.         else
  125.                 LEDShow(0x80>>LEDIndex);
  126.         if(LEDIndex==7)
  127.                 LEDDirection = !LEDDirection;
  128.    LEDIndex = (LEDIndex+1)%8;
  129. }
  130. //Mode 3
  131. void Mode_3(void)
  132. {
  133.         if(LEDDirection)
  134.                 LEDShow(~(0xff<<LEDIndex));
  135.         /*else
  136.                 LEDShow(~(0x80>>LEDIndex));*/
  137.         if(LEDIndex==8)
  138.                 LEDDirection = !LEDDirection;
  139.    LEDIndex = (LEDIndex+1)%9;
  140. }


  141. void TimerEventRun(void)
  142. {
  143.         if(RunMode==0x00)
  144.         {
  145.                 Mode_0();        
  146.         }
  147.         else if(RunMode ==0x01)
  148.         {
  149.                 Mode_1();
  150.         }
  151.         else if(RunMode ==0x02)
  152.         {
  153.                 Mode_2();
  154.         }
  155.         else if(RunMode ==0x03)
  156.         {
  157.                 Mode_3();
  158.         }

  159. }

  160. void Timer2(void) interrupt 1 using 1
  161. {
  162.         TF0 = 0;                         //中断标志清除( Timer2 必须软件清标志!)
  163.                 TH0=(65536-2500)/256;
  164.                 TL0=(65536-2500)%256;        
  165.         if(++TimerCount>=SystemSpeed)
  166.         {
  167.                 TimerCount = 0;
  168.                 TimerEventRun();
  169.            }
  170.         
  171. }
  172. unsigned char MusicIndex = 0;
  173. void KeyDispose(unsigned char Key)
  174. {
  175.          
  176.          
  177.          
  178.                 // WORD i;
  179.          
  180.          
  181.         if(Key&0x01)
  182.                                 
  183.         {
  184.                                 
  185.                 LEDDirection = 1;
  186.                 LEDIndex = 0;
  187.                 //LEDFlag = 1;
  188.                 RunMode = (RunMode+1)%4;
  189.                
  190.                         
  191.         }
  192.                         
  193.         if(Key&0x02)
  194.                         
  195.         {
  196.                                 
  197.                 if(SystemSpeedIndex>0)
  198.                 {
  199.                         --SystemSpeedIndex;
  200.                         SetSpeed(SystemSpeedIndex);
  201.                         
  202.                                          }
  203.                
  204. }
  205.         if(Key&0x04)
  206.         {
  207.                 if(SystemSpeedIndex<26)
  208.                 {
  209.                         ++SystemSpeedIndex;
  210.                         SetSpeed(SystemSpeedIndex);
  211.                                        
  212.                                 }

  213.         }        
  214.         
  215.         
  216.         
  217. }

  218. //***********************************************************************************
  219. main()
  220. {
  221.         unsigned char Key;
  222.         InitialCPU();
  223.        InitialTimer2();
  224.         while(1)
  225.         {
  226.                 Key = GetKey();
  227.                   
  228.                 if(Key!=0x00)
  229.                 {
  230.                                        
  231.                         KeyDispose(Key);


  232.                         
  233. }
  234. }
  235. }



  236. void IapIdle()//关闭ISP,IAP功能
  237. {
  238.       IAP_CONTR = 0;       //Close IAP function
  239.       IAP_CMD = 0;              //Clear command to standby
  240.       IAP_TRIG = 0;                       //Clear trigger register
  241.       IAP_ADDRH = 0x80;                  //Data ptr point to non-EEPROM area
  242.       IAP_ADDRL = 0;                      //Clear IAP address to prevent misuse
  243. }
  244. /*----------------------------Read one byte from ISP/IAP/EEPROM area
  245. Input: addr (ISP/IAP/EEPROM address)
  246. Output:Flash data
  247. ----------------------------*/
  248. BYTE IapReadByte(WORD addr)//读字节
  249. {
  250.       BYTE dat;                           //Data buffer
  251.       IAP_CONTR = ENABLE_IAP;   //Open IAP function, and set wait time
  252.       IAP_CMD = CMD_READ;         //Set ISP/IAP/EEPROM READ command
  253.       IAP_ADDRL = addr;                  //Set ISP/IAP/EEPROM address low
  254.       IAP_ADDRH = addr >> 8;        //Set ISP/IAP/EEPROM address high
  255.       IAP_TRIG = 0x46;                  //Send trigger command1 (0x5a)
  256.       IAP_TRIG = 0xb9;                    //Send trigger command2 (0xa5)
  257.       _nop_();                            //MCU will hold here until ISP/IAP/EEPROM
  258.           //operation complete
  259.       dat = IAP_DATA;              //Read ISP/IAP/EEPROM data
  260.       IapIdle();                          //Close ISP/IAP/EEPROM function
  261.       return dat;                          //Return Flash data
  262. }
  263. /*----------------------------Program one byte to ISP/IAP/EEPROM area
  264. Input: addr (ISP/IAP/EEPROM address)
  265.        dat (ISP/IAP/EEPROM data)
  266. Output:-----------------------------*/
  267. void IapProgramByte(WORD addr, BYTE dat)//写字节
  268. {
  269.   IAP_CONTR = ENABLE_IAP;          //Open IAP function, and set wait time
  270.       IAP_CMD = CMD_PROGRAM;     //Set ISP/IAP/EEPROM PROGRAM command
  271.       IAP_ADDRL = addr;                  //Set ISP/IAP/EEPROM address low
  272.       IAP_ADDRH = addr >> 8;             //Set ISP/IAP/EEPROM address high
  273.       IAP_DATA = dat;                    //Write ISP/IAP/EEPROM data
  274.       IAP_TRIG = 0x46;                    //Send trigger command1 (0x5a)
  275.       IAP_TRIG = 0xb9;                    //Send trigger command2 (0xa5)
  276.       _nop_();                            //MCU will hold here until ISP/IAP/EEPROM
  277.           //operation complete
  278.       IapIdle();
  279. }
  280. /*----------------------------Erase one sector area
  281. Input: addr (ISP/IAP/EEPROM address)
  282. Output:-----------------------------*/
  283. void IapEraseSector(WORD addr)//擦除扇区
  284. {
  285.       IAP_CONTR = ENABLE_IAP;   //Open IAP function, and set wait time
  286.       IAP_CMD = CMD_ERASE;       //Set ISP/IAP/EEPROM ERASE command
  287.       IAP_ADDRL = addr;                //Set ISP/IAP/EEPROM address low
  288.       IAP_ADDRH = addr >> 8;          //Set ISP/IAP/EEPROM address high
  289.       IAP_TRIG = 0x46;                    //Send trigger command1 (0x5a)
  290.       IAP_TRIG = 0xb9;                    //Send trigger command2 (0xa5)
  291.       _nop_();                            //MCU will hold here until ISP/IAP/EEPROM
  292.           //operation complete
  293.       IapIdle();         
  294. }
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:526543 发表于 2022-1-10 18:12 | 只看该作者
我可能语文不是太好  啥叫咋保存按键值?
回复

使用道具 举报

板凳
ID:161164 发表于 2022-1-10 18:18 | 只看该作者
110行改:RunMode = IapReadByte(IAP_ADDRESS);

212行加:IapEraseSector(IAP_ADDRESS);
213行加:IapProgramByte(IAP_ADDRESS, RunMode);
回复

使用道具 举报

地板
ID:879080 发表于 2022-1-10 19:40 | 只看该作者
lkc8210 发表于 2022-1-10 18:18
110行改:RunMode = IapReadByte(IAP_ADDRESS);

212行加:IapEraseSector(IAP_ADDRESS);

先谢谢,我试一下看
回复

使用道具 举报

5#
ID:879080 发表于 2022-1-10 19:43 | 只看该作者
123456ZXC1 发表于 2022-1-10 18:12
我可能语文不是太好  啥叫咋保存按键值?

可能我说法不太准确,这是个调速流水灯程序,我想保存每次调好灯速度与模式,困扰了好几天,无从下手了
回复

使用道具 举报

6#
ID:879080 发表于 2022-1-10 19:49 | 只看该作者
希望能掉电保存每次调好的模式与速度,求大师们指点。非常感谢您的参与。
回复

使用道具 举报

7#
ID:213173 发表于 2022-1-10 20:53 | 只看该作者
  1. #include <reg52.h>
  2. #include <intrins.h>
  3. typedef unsigned char BYTE;
  4. typedef unsigned int WORD;
  5. //unsigned char table[8]={0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55};
  6. /*Declare SFR associated with the IAP */
  7. sfr  IAP_DATA      =   0xC2;       //Flash data register
  8. sfr  IAP_ADDRH     =   0xC3;             //Flash address HIGH
  9. sfr  IAP_ADDRL     =   0xC4;               //Flash address LOW
  10. sfr  IAP_CMD       =   0xC5;                //Flash command register
  11. sfr  IAP_TRIG       =   0xC6;                 //Flash command trigger
  12. sfr  IAP_CONTR     =   0xC7;             //Flash control register
  13. /*Define ISP/IAP/EEPROM command*/
  14. #define   CMD_IDLE      0                         //Stand-By
  15. #define   CMD_READ      1                      //Byte-Read
  16. #define   CMD_PROGRAM   2                 //Byte-Program
  17. #define   CMD_ERASE      3                      //Sector-Erase
  18. /*Define ISP/IAP/EEPROM operation const for IAP_CONTR*/
  19. //#define   ENABLE_IAP   0x80              //if SYSCLK<30MHz
  20. //#define   ENABLE_IAP   0x81              //if SYSCLK<24MHz
  21. //#define   ENABLE_IAP    0x82              //if SYSCLK<20MHz
  22. #define   ENABLE_IAP   0x83              //if SYSCLK<12MHz
  23. //#define   ENABLE_IAP   0x84              //if SYSCLK<6MHz
  24. //#define   ENABLE_IAP   0x85              //if SYSCLK<3MHz
  25. //#define   ENABLE_IAP   0x86              //if SYSCLK<2MHz
  26. //#define   ENABLE_IAP   0x87              //if SYSCLK<1MHz
  27. //Start address for STC12C5A60S2 series EEPROM
  28. #define IAP_ADDRESS   0x2000
  29. void Delay(BYTE n);
  30. void IapIdle();
  31. BYTE IapReadByte(WORD addr);
  32. void IapProgramByte(WORD addr, BYTE dat);
  33. void IapEraseSector(WORD addr);
  34. /*
  35. WORD i;
  36. void Delay(BYTE n)
  37. {
  38.         WORD x;
  39.         while (n--)
  40.         {
  41.                 x = 0;
  42.                 while (++x);
  43.         }
  44. }*/
  45. unsigned char RunMode;
  46. //**********************************System Fuction*************************************************
  47. void Delay1ms(unsigned int count)
  48. {
  49.         unsigned int i,j;
  50.         for(i=0;i<count;i++)
  51.                 for(j=0;j<120;j++);
  52. }
  53. unsigned char GetKey(void)
  54. {
  55.         unsigned char KeyTemp,CheckValue,Key = 0x00;
  56.         CheckValue = P3&0x32;
  57.         if(CheckValue==0x32)
  58.                 return 0x00;
  59.         Delay1ms(10);
  60.         KeyTemp = P3&0x32;
  61.         if(KeyTemp==CheckValue)
  62.                 return 0x00;
  63.        
  64.         if(!(CheckValue&0x02))
  65.                 Key|=0x01;
  66.         if(!(CheckValue&0x10))
  67.                 Key|=0x02;
  68.         if(!(CheckValue&0x20))
  69.                 Key|=0x04;
  70.         return Key;
  71. }

  72. unsigned int TimerCount,SystemSpeed,SystemSpeedIndex;
  73. void InitialTimer2(void)
  74. {
  75.         TMOD=0X01;
  76.         TH0=(65536-5000)/256;
  77.         TL0=(65536-5000)%256;
  78.         ET0=1;
  79.         TR0=1;
  80.         EA=1;
  81. }

  82. unsigned int code SpeedCode[]={5,   8,  10,  14,  17,  20,  30,
  83.                                   40,  50,  60,  70,  80,  90, 100, 120, 140, 160,
  84.                               180, 200, 300, 400, 500, 600, 700, 800, 900,1000};//27
  85. void SetSpeed(unsigned char Speed)
  86. {
  87.          SystemSpeed =SpeedCode[Speed];
  88. }

  89. void LEDShow(unsigned int LEDStatus)
  90. {
  91.         P1 = ~(LEDStatus&0xff);
  92. }

  93. void InitialCPU(void)
  94. {
  95.         RunMode = 0x00;
  96.         TimerCount = 0;
  97.         SystemSpeedIndex =10;
  98.         P3 = 0xff;
  99.         Delay1ms(500);
  100.         P1 = 0xFF;
  101.         P3 = 0xFF;
  102.         SetSpeed(SystemSpeedIndex);
  103. }
  104. //Mode 0
  105. unsigned int LEDIndex = 0;
  106. bit LEDDirection = 1,LEDFlag = 1;
  107. void Mode_0(void)
  108. {
  109.         LEDShow(0x01<<LEDIndex);
  110.         LEDIndex = (LEDIndex+1)%8;
  111. }
  112. //Mode 1
  113. void Mode_1(void)
  114. {
  115.         LEDShow(0x80>>LEDIndex);
  116.         LEDIndex = (LEDIndex+1)%8;
  117. }
  118. //Mode 2
  119. void Mode_2(void)
  120. {
  121.         if(LEDDirection)
  122.                 LEDShow(0x01<<LEDIndex);
  123.         else
  124.                 LEDShow(0x80>>LEDIndex);
  125.         if(LEDIndex==7)
  126.                 LEDDirection = !LEDDirection;
  127.         LEDIndex = (LEDIndex+1)%8;
  128. }
  129. //Mode 3
  130. void Mode_3(void)
  131. {
  132.         if(LEDDirection)
  133.                 LEDShow(~(0xff<<LEDIndex));
  134.         /*else
  135.         LEDShow(~(0x80>>LEDIndex));*/
  136.         if(LEDIndex==8)
  137.                 LEDDirection = !LEDDirection;
  138.         LEDIndex = (LEDIndex+1)%9;
  139. }

  140. void TimerEventRun(void)
  141. {
  142.         if(RunMode==0x00)
  143.         {
  144.                 Mode_0();        
  145.         }
  146.         else if(RunMode ==0x01)
  147.         {
  148.                 Mode_1();
  149.         }
  150.         else if(RunMode ==0x02)
  151.         {
  152.                 Mode_2();
  153.         }
  154.         else if(RunMode ==0x03)
  155.         {
  156.                 Mode_3();
  157.         }
  158. }

  159. void Timer2(void) interrupt 1 using 1
  160. {
  161.         TF0 = 0;                         //中断标志清除( Timer2 必须软件清标志!)
  162.         TH0=(65536-2500)/256;
  163.         TL0=(65536-2500)%256;        
  164.         if(++TimerCount>=SystemSpeed)
  165.         {
  166.                 TimerCount = 0;
  167.                 TimerEventRun();
  168.         }
  169. }
  170. unsigned char MusicIndex = 0;
  171. void KeyDispose(unsigned char Key)
  172. {
  173. // WORD i;
  174.         if(Key&0x01)
  175.         {
  176.                 LEDDirection = 1;
  177.                 LEDIndex = 0;
  178.                 //LEDFlag = 1;
  179.                 RunMode = (RunMode+1)%4;
  180.         }
  181.         if(Key&0x02)
  182.         {
  183.                 if(SystemSpeedIndex>0)
  184.                 {
  185.                         --SystemSpeedIndex;
  186.                         SetSpeed(SystemSpeedIndex);
  187.                 }
  188.         }
  189.         if(Key&0x04)
  190.         {
  191.                 if(SystemSpeedIndex<26)
  192.                 {
  193.                         ++SystemSpeedIndex;
  194.                         SetSpeed(SystemSpeedIndex);
  195.                 }
  196.         }        
  197. }

  198. //***********************************************************************************
  199. void main()
  200. {
  201.         unsigned char Key,Key1;
  202.         InitialCPU();
  203.         InitialTimer2();
  204.         Key1=IapReadByte(IAP_ADDRESS);
  205.         while(1)
  206.         {
  207.                 Key = GetKey();
  208.                 if(Key!=0x00 && Key!=Key1)
  209.                 {
  210.                         Key1=Key;
  211.                         IapEraseSector(IAP_ADDRESS);
  212.                         IapProgramByte(IAP_ADDRESS, Key1);
  213.                 }
  214.                 if(Key!=0x00)
  215.                 {
  216.                         KeyDispose(Key);
  217.                 }
  218.         }
  219. }



  220. void IapIdle()//关闭ISP,IAP功能
  221. {
  222.       IAP_CONTR = 0;       //Close IAP function
  223.       IAP_CMD = 0;              //Clear command to standby
  224.       IAP_TRIG = 0;                       //Clear trigger register
  225.       IAP_ADDRH = 0x80;                  //Data ptr point to non-EEPROM area
  226.       IAP_ADDRL = 0;                      //Clear IAP address to prevent misuse
  227. }
  228. /*----------------------------Read one byte from ISP/IAP/EEPROM area
  229. Input: addr (ISP/IAP/EEPROM address)
  230. Output:Flash data
  231. ----------------------------*/
  232. BYTE IapReadByte(WORD addr)//读字节
  233. {
  234.       BYTE dat;                           //Data buffer
  235.       IAP_CONTR = ENABLE_IAP;   //Open IAP function, and set wait time
  236.       IAP_CMD = CMD_READ;         //Set ISP/IAP/EEPROM READ command
  237.       IAP_ADDRL = addr;                  //Set ISP/IAP/EEPROM address low
  238.       IAP_ADDRH = addr >> 8;        //Set ISP/IAP/EEPROM address high
  239.       IAP_TRIG = 0x46;                  //Send trigger command1 (0x5a)
  240.       IAP_TRIG = 0xb9;                    //Send trigger command2 (0xa5)
  241.       _nop_();                            //MCU will hold here until ISP/IAP/EEPROM
  242.           //operation complete
  243.       dat = IAP_DATA;              //Read ISP/IAP/EEPROM data
  244.       IapIdle();                          //Close ISP/IAP/EEPROM function
  245.       return dat;                          //Return Flash data
  246. }
  247. /*----------------------------Program one byte to ISP/IAP/EEPROM area
  248. Input: addr (ISP/IAP/EEPROM address)
  249.        dat (ISP/IAP/EEPROM data)
  250. Output:-----------------------------*/
  251. void IapProgramByte(WORD addr, BYTE dat)//写字节
  252. {
  253.   IAP_CONTR = ENABLE_IAP;          //Open IAP function, and set wait time
  254.       IAP_CMD = CMD_PROGRAM;     //Set ISP/IAP/EEPROM PROGRAM command
  255.       IAP_ADDRL = addr;                  //Set ISP/IAP/EEPROM address low
  256.       IAP_ADDRH = addr >> 8;             //Set ISP/IAP/EEPROM address high
  257.       IAP_DATA = dat;                    //Write ISP/IAP/EEPROM data
  258.       IAP_TRIG = 0x46;                    //Send trigger command1 (0x5a)
  259.       IAP_TRIG = 0xb9;                    //Send trigger command2 (0xa5)
  260.       _nop_();                            //MCU will hold here until ISP/IAP/EEPROM
  261.           //operation complete
  262.       IapIdle();
  263. }
  264. /*----------------------------Erase one sector area
  265. Input: addr (ISP/IAP/EEPROM address)
  266. Output:-----------------------------*/
  267. void IapEraseSector(WORD addr)//擦除扇区
  268. {
  269.       IAP_CONTR = ENABLE_IAP;   //Open IAP function, and set wait time
  270.       IAP_CMD = CMD_ERASE;       //Set ISP/IAP/EEPROM ERASE command
  271.       IAP_ADDRL = addr;                //Set ISP/IAP/EEPROM address low
  272.       IAP_ADDRH = addr >> 8;          //Set ISP/IAP/EEPROM address high
  273.       IAP_TRIG = 0x46;                    //Send trigger command1 (0x5a)
  274.       IAP_TRIG = 0xb9;                    //Send trigger command2 (0xa5)
  275.       _nop_();                            //MCU will hold here until ISP/IAP/EEPROM
  276.           //operation complete
  277.       IapIdle();         
  278. }
复制代码
回复

使用道具 举报

8#
ID:977804 发表于 2022-1-10 23:13 来自手机 | 只看该作者
看你的诉求不难,
回复

使用道具 举报

9#
ID:977804 发表于 2022-1-10 23:14 来自手机 | 只看该作者
简单点,分3步。一,先做出按键能控制流水灯的速度,二,用内部eeprom读写,三,综合利用,读写第一步的按键值
回复

使用道具 举报

10#
ID:624769 发表于 2022-1-11 00:13 | 只看该作者
      IAP_TRIG = 0x46;                  //Send trigger command1 (0x5a)
      IAP_TRIG = 0xb9;                    //Send trigger command2 (0xa5)

单片机型号?
0x46 是 89系列    但是寄存器是  ISP_TRIG
0x5A 是 1T系列指令,寄存器 是  IAP_TRIG  所以你注释和内容不同,到底是什么型号的单片机?
回复

使用道具 举报

11#
ID:879080 发表于 2022-1-11 09:30 | 只看该作者
188610329 发表于 2022-1-11 00:13
IAP_TRIG = 0x46;                  //Send trigger command1 (0x5a)
      IAP_TRIG = 0xb9;       ...

单片机是stc12c2052AD
回复

使用道具 举报

12#
ID:879080 发表于 2022-1-11 09:38 | 只看该作者

感谢您的指导,这样改还是不行
回复

使用道具 举报

13#
ID:879080 发表于 2022-1-11 09:40 | 只看该作者

感谢!但是这样还是不行
回复

使用道具 举报

14#
ID:1001210 发表于 2022-1-11 09:52 | 只看该作者
1 搞清楚芯片型号   
2 查找使用手册
3 参考例程
4 修改程序,测试
回复

使用道具 举报

15#
ID:977804 发表于 2022-1-11 14:10 来自手机 | 只看该作者
我这个也是stc12c2052,逻辑是:按键值加一,擦除2000h区,写入2000h区。每次上电读取2000h的值附给显示的值 你搜一下我发的帖:关键字  stc12c2052的eeprom

16418812968455554832382237450722.jpg (1.9 MB, 下载次数: 23)

16418812968455554832382237450722.jpg
回复

使用道具 举报

16#
ID:624769 发表于 2022-1-11 16:01 | 只看该作者
本帖最后由 188610329 于 2022-1-11 17:27 编辑

STC12C2052AD 的 IAP启动指令是

ISP_TRIG = 0x46;  
ISP_TRIG = 0xB9;  

而不是你代码中的: IAP_TRIG你应该确认一下 寄存器地址
回复

使用道具 举报

17#
ID:624769 发表于 2022-1-11 16:09 | 只看该作者

多的不说了, 如果如你所说: STC12C2052AD 的话:
用的是ISP 指令, ISP_TRIG = 0x46; ISP_TRIG = 0xB9;  
但是,ISP 寄存器,是不同于 IAP寄存器的,
看下图修改你的  sfr 定义



回复

使用道具 举报

18#
ID:136485 发表于 2022-1-11 16:19 | 只看该作者
STC的芯片先擦除,接着写入,最后你要在什么时候读出就行
value=0x01;
   IapEraseSector(IAP_ADDRESS);
   _nop_();
   _nop_();
   IapProgramByte(0x0010, value);
  这是擦除和写入,我的读出是放在上电的时候
回复

使用道具 举报

19#
ID:879080 发表于 2022-1-11 16:34 | 只看该作者
3花木城 发表于 2022-1-11 14:10
我这个也是stc12c2052,逻辑是:按键值加一,擦除2000h区,写入2000h区。每次上电读取2000h的值附给显示的 ...

好的,谢谢
回复

使用道具 举报

20#
ID:899981 发表于 2022-1-11 16:54 | 只看该作者
建议楼主先分解问题,就是把程序简单化,例如一个按键,保存这个按下的反应值,例如按一下加一,否则这么长的程序哪有时间看?
回复

使用道具 举报

21#
ID:879080 发表于 2022-1-11 18:19 | 只看该作者
188610329 发表于 2022-1-11 16:09
多的不说了, 如果如你所说: STC12C2052AD 的话:
用的是ISP 指令, ISP_TRIG = 0x46; ISP_TRIG = 0xB9 ...

谢谢!我修改再试
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表