单片机成长之路(51基础篇) - 021 STC89C51系列单片机 内部EEPROM 驱动

  最近又看了一下关于stc单片机的知识,感觉在使用中EEPROM是个经常用到的东西,特地学习了一下,给大家分享一下心得,如有不足,多多包涵,废话不多说,一图解千言,先上图:

 

/*--------------------------------------------------------------分界线-----------------------------------------------------------------------------------*/

main_eepprom.c

 1 #include <STC89C5xRC.H>
 2 
 3 #ifdef EEPROM
 4 #include "eeprom.h"
 5 #endif
 6 
 7 # define LEDALL P1
 8 
 9 void Delay(int n){
10     unsigned int x;
11     while (n--) {
12         x = 0;
13         while (++x);
14     }
15 }
16 
17 void main(){
18     char i;
19     unsigned char temp,temp0,temp1;
20     while (1){
21         for(i=0; i<4; i++){
22             temp0 = STC_EEPROM_assign_Read(0,0);
23             temp1 = STC_EEPROM_assign_Read(0,1);
24             temp = (temp0 %16)+((temp1/16)*16);
25             LEDALL = ~temp;
26             Delay(1);
27             temp = 0x11<<i;
28             temp0 = temp %16;
29             temp1 = (temp / 16)*16;
30             SCT_EEPROM_assign_Write(0,0,temp0);
31             SCT_EEPROM_assign_Write(0,1,temp1);
32             temp = 0;
33         }
34     }
35 }

Eeprom.h

  1 /*--------------------------------------------------------------------------
  2 eeprom.h
  3 
  4 --------------------------------------------------------------------------*/
  5 
  6 #ifndef __EEPROM_H__
  7 #define __EEPROM_H__
  8 #include "intrins.h"
  9 
 10 /*声明与IAP相关的SFR寄存器 */
 11 sfr IAP_DATA    =   0xE2;           //闪存数据寄存器
 12 sfr IAP_ADDRH   =   0xE3;           //Flash地址为HIGH
 13 sfr IAP_ADDRL   =   0xE4;           //Flash地址为LOW
 14 sfr IAP_CMD     =   0xE5;           //Flash命令寄存器
 15 sfr IAP_TRIG    =   0xE6;           //Flash命令触发器
 16 sfr IAP_CONTR     =     0xE7;           //闪存控制寄存器
 17 /*宏定义 ISP/IAP/EEPROM 命令*/
 18 #define EEPROM_CMD_IDLE        0    //支持
 19 #define EEPROM_CMD_READ        1    //字节读
 20 #define EEPROM_CMD_PROGRAM     2    //字节编程
 21 #define EEPROM_CMD_ERASE    3    //扇区擦除
 22 /*IAP_CONTR定义ISP / IAP / EEPROM 时钟操作常量*/
 23 #define EEPROM_ENABLE_IAP    0x80            //开启IAP功能
 24 //Clock 时钟配置
 25 #define Clock_20M_40M      0x00           //if SYSCLK<40MHz
 26 #define Clock_10M_20M      0x01           //if SYSCLK<20MHz
 27 #define Clock_5M_10M    0x02           //if SYSCLK<10MHz
 28 #define Clock_0M_5M     0x03           //if SYSCLK<5MHz
 29 
 30 //EEPROM的起始地址
 31 #define EEPROM_BASE_ADDRESS    0x0000
 32 //STC89C51系列
 33 #define STC89x51_EEPROM_Start_ADDRESS   EEPROM_BASE_ADDRESS+0x01000            //STC89C/LE51
 34 #define STC89x52_EEPROM_Start_ADDRESS   EEPROM_BASE_ADDRESS+0x02000            //STC89C/LE52
 35 #define STC89x53_EEPROM_Start_ADDRESS   EEPROM_BASE_ADDRESS+0x03000            //STC89C/LE53
 36 #define STC89x54_EEPROM_Start_ADDRESS   EEPROM_BASE_ADDRESS+0x04000            //STC89C/LE54
 37 #define STC89x58_EEPROM_Start_ADDRESS   EEPROM_BASE_ADDRESS+0x08000            //STC89C/LE58
 38 #define STC89x510_EEPROM_Start_ADDRESS    EEPROM_BASE_ADDRESS+0x0A000            //STC89C/LE510
 39 #define STC89x512_EEPROM_Start_ADDRESS  EEPROM_BASE_ADDRESS+0x0C000            //STC89C/LE512
 40 #define STC89x514_EEPROM_Start_ADDRESS    EEPROM_BASE_ADDRESS+0x0E000            //STC89C/LE514
 41 //STC89C51RC/RD+系列
 42 #define STC89x51RC_EEPROM_Start_ADDRESS       EEPROM_BASE_ADDRESS+0x01000            //STC89C/LE51RC/RD+
 43 #define STC89x52RC_EEPROM_Start_ADDRESS       EEPROM_BASE_ADDRESS+0x02000            //STC89C/LE52RC/RD+
 44 #define STC89x54RC_EEPROM_Start_ADDRESS       EEPROM_BASE_ADDRESS+0x04000            //STC89C/LE54RC/RD+
 45 #define STC89x58RC_EEPROM_Start_ADDRESS       EEPROM_BASE_ADDRESS+0x08000            //STC89C/LE58RC/RD+
 46 #define STC89x510RC_EEPROM_Start_ADDRESS    EEPROM_BASE_ADDRESS+0x0A000            //STC89C/LE510RC/RD+
 47 #define STC89x512RC_EEPROM_Start_ADDRESS      EEPROM_BASE_ADDRESS+0x0C000            //STC89C/LE512RC/RD+
 48 #define STC89x514RC_EEPROM_Start_ADDRESS    EEPROM_BASE_ADDRESS+0x0E000            //STC89C/LE514RC/RD+
 49 //STC90C51系列
 50 #define STC90x51_EEPROM_Start_ADDRESS   EEPROM_BASE_ADDRESS+0x01000            //STC90C/LE51
 51 #define STC90x52_EEPROM_Start_ADDRESS   EEPROM_BASE_ADDRESS+0x02000            //STC90C/LE52
 52 #define STC90x54_EEPROM_Start_ADDRESS   EEPROM_BASE_ADDRESS+0x04000            //STC90C/LE54
 53 #define STC90x58_EEPROM_Start_ADDRESS   EEPROM_BASE_ADDRESS+0x08000            //STC90C/LE58
 54 #define STC90x510_EEPROM_Start_ADDRESS    EEPROM_BASE_ADDRESS+0x0A000            //STC90C/LE510
 55 #define STC90x512_EEPROM_Start_ADDRESS  EEPROM_BASE_ADDRESS+0x0C000            //STC90C/LE512
 56 #define STC90x514_EEPROM_Start_ADDRESS    EEPROM_BASE_ADDRESS+0x0E000            //STC90C/LE514
 57 //STC90C51RC/RD+系列
 58 #define STC90x51RC_EEPROM_Start_ADDRESS       EEPROM_BASE_ADDRESS+0x01000            //STC90C/LE51RC/RD+
 59 #define STC90x52RC_EEPROM_Start_ADDRESS       EEPROM_BASE_ADDRESS+0x02000            //STC90C/LE52RC/RD+
 60 #define STC90x12RC_EEPROM_Start_ADDRESS       EEPROM_BASE_ADDRESS+0x03000            //STC90C/LE52RC/RD+
 61 #define STC90x54RC_EEPROM_Start_ADDRESS       EEPROM_BASE_ADDRESS+0x04000            //STC90C/LE54RC/RD+
 62 #define STC90x58RC_EEPROM_Start_ADDRESS       EEPROM_BASE_ADDRESS+0x08000            //STC90C/LE58RC/RD+
 63 #define STC90x510RC_EEPROM_Start_ADDRESS    EEPROM_BASE_ADDRESS+0x0A000            //STC90C/LE510RC/RD+
 64 #define STC90x512RC_EEPROM_Start_ADDRESS      EEPROM_BASE_ADDRESS+0x0C000            //STC90C/LE512RC/RD+
 65 #define STC90x514RC_EEPROM_Start_ADDRESS    EEPROM_BASE_ADDRESS+0x0E000            //STC90C/LE514RC/RD+
 66 //STC12xx052AD系列
 67 #define STC12xx052AD_EEPROM_Start_ADDRESS       EEPROM_BASE_ADDRESS+0x00000        //STC12xx052AD
 68 
 69 
 70 //EEPROM SIZE
 71 #define Sector_size    0x0200
 72 //STC89C51系列
 73 #define STC89x51_EEPROM_Sector_Number     18
 74 #define STC89x52_EEPROM_Sector_Number    10
 75 #define STC89x53_EEPROM_Sector_Number    4
 76 #define STC89x54_EEPROM_Sector_Number    90
 77 #define STC89x58_EEPROM_Sector_Number      58
 78 #define STC89x510_EEPROM_Sector_Number    44
 79 #define STC89x512_EEPROM_Sector_Number    28
 80 #define STC89x514_EEPROM_Sector_Number    12
 81 //STC89C51RC/RD+系列
 82 #define STC89x51RC_EEPROM_Sector_Number     8
 83 #define STC89x52RC_EEPROM_Sector_Number        8
 84 #define STC89x54RC_EEPROM_Sector_Number        90
 85 #define STC89x58RC_EEPROM_Sector_Number      58
 86 #define STC89x510RC_EEPROM_Sector_Number    42
 87 #define STC89x512RC_EEPROM_Sector_Number    26
 88 #define STC89x514RC_EEPROM_Sector_Number    10
 89 //STC90C51系列
 90 #define STC90x51_EEPROM_Sector_Number     10
 91 #define STC90x52_EEPROM_Sector_Number    10
 92 #define STC90x54_EEPROM_Sector_Number    90
 93 #define STC90x58_EEPROM_Sector_Number      58
 94 #define STC90x510_EEPROM_Sector_Number    42
 95 #define STC90x512_EEPROM_Sector_Number    26
 96 #define STC90x514_EEPROM_Sector_Number    10
 97 //STC89C51RC/RD+系列
 98 #define STC90x51RC_EEPROM_Sector_Number     18
 99 #define STC90x52RC_EEPROM_Sector_Number        10
100 #define STC90x12RC_EEPROM_Sector_Number        2
101 #define STC90x54RC_EEPROM_Sector_Number        90
102 #define STC90x58RC_EEPROM_Sector_Number      58
103 #define STC90x510RC_EEPROM_Sector_Number    42
104 #define STC90x512RC_EEPROM_Sector_Number    26
105 #define STC90x514RC_EEPROM_Sector_Number    10
106 
107 //STC89C51RC/RD+系列
108 #define STC90x51RC_EEPROM_Sector_Number     18
109 #define STC90x52RC_EEPROM_Sector_Number        10
110 #define STC90x12RC_EEPROM_Sector_Number        2
111 #define STC90x54RC_EEPROM_Sector_Number        90
112 #define STC90x58RC_EEPROM_Sector_Number      58
113 #define STC90x510RC_EEPROM_Sector_Number    42
114 #define STC90x512RC_EEPROM_Sector_Number    26
115 #define STC90x514RC_EEPROM_Sector_Number    10
116 
117 //STC12xx052AD系列
118 #define STC12xx052AD_EEPROM_Sector_Number     20
119 
120 
121 //EEPROM end
122 //STC89C51系列
123 #define STC89x51_EEPROM_End_ADDRESS STC89x51_EEPROM_Start_ADDRESS+Sector_size*(STC89x51_EEPROM_Sector_Number)-1
124 #define STC89x52_EEPROM_End_ADDRESS STC89x52_EEPROM_Start_ADDRESS+Sector_size*(STC89x52_EEPROM_Sector_Number)-1
125 #define STC89x53_EEPROM_End_ADDRESS STC89x53_EEPROM_Start_ADDRESS+Sector_size*(STC89x53_EEPROM_Sector_Number)-1
126 #define STC89x54_EEPROM_End_ADDRESS STC89x54_EEPROM_Start_ADDRESS+Sector_size*(STC89x54_EEPROM_Sector_Number)-1
127 #define STC89x58_EEPROM_End_ADDRESS STC89x58_EEPROM_Start_ADDRESS+Sector_size*(STC89x58_EEPROM_Sector_Number)-1
128 #define STC89x510_EEPROM_End_ADDRESS STC89x510_EEPROM_Start_ADDRESS+Sector_size*(STC89x510_EEPROM_Sector_Number)-1
129 #define STC89x512_EEPROM_End_ADDRESS STC89x512_EEPROM_Start_ADDRESS+Sector_size*(STC89x512_EEPROM_Sector_Number)-1
130 #define STC89x514_EEPROM_End_ADDRESS STC89x514_EEPROM_Start_ADDRESS+Sector_size*(STC89x514_EEPROM_Sector_Number)-1
131 //STC89C51RC/RD+系列
132 #define STC89x51RC_EEPROM_End_ADDRESS STC89x51RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x51RC_EEPROM_Sector_Number)-1
133 #define STC89x52RC_EEPROM_End_ADDRESS STC89x52RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x52RC_EEPROM_Sector_Number)-1
134 #define STC89x54RC_EEPROM_End_ADDRESS STC89x54RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x54RC_EEPROM_Sector_Number)-1
135 #define STC89x58RC_EEPROM_End_ADDRESS STC89x58RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x58RC_EEPROM_Sector_Number)-1
136 #define STC89x510RC_EEPROM_End_ADDRESS STC89x510RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x510RC_EEPROM_Sector_Number)-1
137 #define STC89x512RC_EEPROM_End_ADDRESS STC89x512RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x512RC_EEPROM_Sector_Number)-1
138 #define STC89x514RC_EEPROM_End_ADDRESS STC89x514RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x514RC_EEPROM_Sector_Number)-1
139 //STC90C51系列
140 #define STC90x51_EEPROM_End_ADDRESS STC90x51_EEPROM_Start_ADDRESS+Sector_size*(STC90x51_EEPROM_Sector_Number)-1
141 #define STC90x52_EEPROM_End_ADDRESS STC90x52_EEPROM_Start_ADDRESS+Sector_size*(STC90x52_EEPROM_Sector_Number)-1
142 #define STC90x54_EEPROM_End_ADDRESS STC90x54_EEPROM_Start_ADDRESS+Sector_size*(STC90x54_EEPROM_Sector_Number)-1
143 #define STC90x58_EEPROM_End_ADDRESS STC90x58_EEPROM_Start_ADDRESS+Sector_size*(STC90x58_EEPROM_Sector_Number)-1
144 #define STC90x510_EEPROM_End_ADDRESS STC90x510_EEPROM_Start_ADDRESS+Sector_size*(STC90x510_EEPROM_Sector_Number)-1
145 #define STC90x512_EEPROM_End_ADDRESS STC90x512_EEPROM_Start_ADDRESS+Sector_size*(STC90x512_EEPROM_Sector_Number)-1
146 #define STC90x514_EEPROM_End_ADDRESS STC90x514_EEPROM_Start_ADDRESS+Sector_size*(STC90x514_EEPROM_Sector_Number)-1
147 //STC90C51RC/RD+系列
148 #define STC90x51RC_EEPROM_End_ADDRESS STC90x51RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x51RC_EEPROM_Sector_Number)-1
149 #define STC90x52RC_EEPROM_End_ADDRESS STC90x52RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x52RC_EEPROM_Sector_Number)-1
150 #define STC90x12RC_EEPROM_End_ADDRESS STC90x12RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x12RC_EEPROM_Sector_Number)-1
151 #define STC90x54RC_EEPROM_End_ADDRESS STC90x54RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x54RC_EEPROM_Sector_Number)-1
152 #define STC90x58RC_EEPROM_End_ADDRESS STC90x58RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x58RC_EEPROM_Sector_Number)-1
153 #define STC90x510RC_EEPROM_End_ADDRESS STC90x510RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x510RC_EEPROM_Sector_Number)-1
154 #define STC90x512RC_EEPROM_End_ADDRESS STC90x512RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x512RC_EEPROM_Sector_Number)-1
155 #define STC90x514RC_EEPROM_End_ADDRESS STC90x514RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x514RC_EEPROM_Sector_Number)-1
156 //STC12xx052AD系列
157 #define STC12xx052AD_EEPROM_End_ADDRESS STC12xx052AD_EEPROM_Start_ADDRESS+Sector_size*(STC12xx052AD_EEPROM_Sector_Number)-1
158 
159 
160 
161 
162 /*-----------set eeprom parameter-----------*/
163 //STC89C51系列
164 #ifdef STC89x51
165 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
166 #define EEPROM_Start_ADDRESS             STC89x51_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
167 #define EEPROM_buf_ADDRESS                 STC89x51_EEPROM_Start_ADDRESS + Sector_size*(STC89x51_EEPROM_Sector_Number -1)    //eeprom起始地址
168 #define EEPROM_end_ADDRESS                 STC89x51_EEPROM_End_ADDRESS                                                        //eeprom结束地址
169 #endif
170 #ifdef STC89x52
171 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
172 #define EEPROM_Start_ADDRESS             STC89x52_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
173 #define EEPROM_buf_ADDRESS                 STC89x52_EEPROM_Start_ADDRESS + Sector_size*(STC89x52_EEPROM_Sector_Number -1)    //eeprom起始地址
174 #define EEPROM_end_ADDRESS                 STC89x52_EEPROM_End_ADDRESS                                                        //eeprom结束地址
175 #endif
176 #ifdef STC89x53
177 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
178 #define EEPROM_Start_ADDRESS             STC89x53_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
179 #define EEPROM_buf_ADDRESS                 STC89x53_EEPROM_Start_ADDRESS + Sector_size*(STC89x53_EEPROM_Sector_Number -1)    //eeprom起始地址
180 #define EEPROM_end_ADDRESS                 STC89x53_EEPROM_End_ADDRESS                                                        //eeprom结束地址
181 #endif
182 #ifdef STC89x54
183 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
184 #define EEPROM_Start_ADDRESS             STC89x54_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
185 #define EEPROM_buf_ADDRESS                 STC89x54_EEPROM_Start_ADDRESS + Sector_size*(STC89x54_EEPROM_Sector_Number -1)    //eeprom起始地址
186 #define EEPROM_end_ADDRESS                 STC89x54_EEPROM_End_ADDRESS                                                        //eeprom结束地址
187 #endif
188 #ifdef STC89x58
189 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
190 #define EEPROM_Start_ADDRESS             STC89x58_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
191 #define EEPROM_buf_ADDRESS                 STC89x58_EEPROM_Start_ADDRESS + Sector_size*(STC89x58_EEPROM_Sector_Number -1)    //eeprom起始地址
192 #define EEPROM_end_ADDRESS                 STC89x58_EEPROM_End_ADDRESS                                                        //eeprom结束地址
193 #endif
194 #ifdef STC89x510
195 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                        //开启IAP,配置时钟
196 #define EEPROM_Start_ADDRESS             STC89x510_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
197 #define EEPROM_buf_ADDRESS                 STC89x510_EEPROM_Start_ADDRESS + Sector_size*(STC89x510_EEPROM_Sector_Number -1)    //eeprom起始地址
198 #define EEPROM_end_ADDRESS                 STC89x510_EEPROM_End_ADDRESS                                                        //eeprom结束地址
199 #endif
200 #ifdef STC89x512
201 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                        //开启IAP,配置时钟
202 #define EEPROM_Start_ADDRESS             STC89x512_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
203 #define EEPROM_buf_ADDRESS                 STC89x512_EEPROM_Start_ADDRESS + Sector_size*(STC89x512_EEPROM_Sector_Number -1)    //eeprom起始地址
204 #define EEPROM_end_ADDRESS                 STC89x512_EEPROM_End_ADDRESS                                                        //eeprom结束地址
205 #endif
206 #ifdef STC89x514
207 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                        //开启IAP,配置时钟
208 #define EEPROM_Start_ADDRESS             STC89x514_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
209 #define EEPROM_buf_ADDRESS                 STC89x514_EEPROM_Start_ADDRESS + Sector_size*(STC89x514_EEPROM_Sector_Number -1)    //eeprom起始地址
210 #define EEPROM_end_ADDRESS                 STC89x514_EEPROM_End_ADDRESS                                                        //eeprom结束地址
211 #endif
212 //STC89C51RC系列
213 #ifdef STC89x51RC
214 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
215 #define EEPROM_Start_ADDRESS             STC89x51RC_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
216 #define EEPROM_buf_ADDRESS                 STC89x51RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x51RC_EEPROM_Sector_Number -1)    //eeprom起始地址
217 #define EEPROM_end_ADDRESS                 STC89x51RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
218 #endif
219 #ifdef STC89x52RC
220 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
221 #define EEPROM_Start_ADDRESS             STC89x52RC_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
222 #define EEPROM_buf_ADDRESS                 STC89x52RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x52RC_EEPROM_Sector_Number -1)    //eeprom起始地址
223 #define EEPROM_end_ADDRESS                 STC89x52RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
224 #endif
225 #ifdef STC89x54RC
226 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
227 #define EEPROM_Start_ADDRESS             STC89x54RC_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
228 #define EEPROM_buf_ADDRESS                 STC89x54RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x54RC_EEPROM_Sector_Number -1)    //eeprom起始地址
229 #define EEPROM_end_ADDRESS                 STC89x54RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
230 #endif
231 #ifdef STC89x58RC
232 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
233 #define EEPROM_Start_ADDRESS             STC89x58RC_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
234 #define EEPROM_buf_ADDRESS                 STC89x58RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x58RC_EEPROM_Sector_Number -1)    //eeprom起始地址
235 #define EEPROM_end_ADDRESS                 STC89x58RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
236 #endif
237 #ifdef STC89x510RC
238 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                        //开启IAP,配置时钟
239 #define EEPROM_Start_ADDRESS             STC89x510RC_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
240 #define EEPROM_buf_ADDRESS                 STC89x510RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x510RC_EEPROM_Sector_Number -1)    //eeprom起始地址
241 #define EEPROM_end_ADDRESS                 STC89x510RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
242 #endif
243 #ifdef STC89x512RC
244 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                        //开启IAP,配置时钟
245 #define EEPROM_Start_ADDRESS             STC89x512RC_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
246 #define EEPROM_buf_ADDRESS                 STC89x512RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x512RC_EEPROM_Sector_Number -1)    //eeprom起始地址
247 #define EEPROM_end_ADDRESS                 STC89x512RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
248 #endif
249 #ifdef STC89x514RC
250 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                        //开启IAP,配置时钟
251 #define EEPROM_Start_ADDRESS             STC89x514RC_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
252 #define EEPROM_buf_ADDRESS                 STC89x514RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x514RC_EEPROM_Sector_Number -1)    //eeprom起始地址
253 #define EEPROM_end_ADDRESS                 STC89x514RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
254 #endif
255 //STC90C51系列
256 #ifdef STC90x51
257 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
258 #define EEPROM_Start_ADDRESS             STC90x51_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
259 #define EEPROM_buf_ADDRESS                 STC90x51_EEPROM_Start_ADDRESS + Sector_size*(STC90x51_EEPROM_Sector_Number -1)    //eeprom起始地址
260 #define EEPROM_end_ADDRESS                 STC90x51_EEPROM_End_ADDRESS                                                        //eeprom结束地址
261 #endif
262 #ifdef STC90x52
263 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
264 #define EEPROM_Start_ADDRESS             STC90x52_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
265 #define EEPROM_buf_ADDRESS                 STC90x52_EEPROM_Start_ADDRESS + Sector_size*(STC90x52_EEPROM_Sector_Number -1)    //eeprom起始地址
266 #define EEPROM_end_ADDRESS                 STC90x52_EEPROM_End_ADDRESS                                                        //eeprom结束地址
267 #endif
268 #ifdef STC90x54
269 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
270 #define EEPROM_Start_ADDRESS             STC90x54_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
271 #define EEPROM_buf_ADDRESS                 STC90x54_EEPROM_Start_ADDRESS + Sector_size*(STC90x54_EEPROM_Sector_Number -1)    //eeprom起始地址
272 #define EEPROM_end_ADDRESS                 STC90x54_EEPROM_End_ADDRESS                                                        //eeprom结束地址
273 #endif
274 #ifdef STC90x58
275 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
276 #define EEPROM_Start_ADDRESS             STC90x58_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
277 #define EEPROM_buf_ADDRESS                 STC90x58_EEPROM_Start_ADDRESS + Sector_size*(STC90x58_EEPROM_Sector_Number -1)    //eeprom起始地址
278 #define EEPROM_end_ADDRESS                 STC90x58_EEPROM_End_ADDRESS                                                        //eeprom结束地址
279 #endif
280 #ifdef STC90x510
281 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                        //开启IAP,配置时钟
282 #define EEPROM_Start_ADDRESS             STC90x510_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
283 #define EEPROM_buf_ADDRESS                 STC90x510_EEPROM_Start_ADDRESS + Sector_size*(STC90x510_EEPROM_Sector_Number -1)    //eeprom起始地址
284 #define EEPROM_end_ADDRESS                 STC90x510_EEPROM_End_ADDRESS                                                        //eeprom结束地址
285 #endif
286 #ifdef STC90x512
287 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                        //开启IAP,配置时钟
288 #define EEPROM_Start_ADDRESS             STC90x512_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
289 #define EEPROM_buf_ADDRESS                 STC90x512_EEPROM_Start_ADDRESS + Sector_size*(STC90x512_EEPROM_Sector_Number -1)    //eeprom起始地址
290 #define EEPROM_end_ADDRESS                 STC90x512_EEPROM_End_ADDRESS                                                        //eeprom结束地址
291 #endif
292 #ifdef STC90x514
293 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                        //开启IAP,配置时钟
294 #define EEPROM_Start_ADDRESS             STC90x514_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
295 #define EEPROM_buf_ADDRESS                 STC90x514_EEPROM_Start_ADDRESS + Sector_size*(STC90x514_EEPROM_Sector_Number -1)    //eeprom起始地址
296 #define EEPROM_end_ADDRESS                 STC90x514_EEPROM_End_ADDRESS                                                        //eeprom结束地址
297 #endif
298 //STC90C51RC系列
299 #ifdef STC90x51RC
300 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
301 #define EEPROM_Start_ADDRESS             STC90x51RC_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
302 #define EEPROM_buf_ADDRESS                 STC90x51RC_EEPROM_Start_ADDRESS + Sector_size*(STC90x51RC_EEPROM_Sector_Number -1)    //eeprom起始地址
303 #define EEPROM_end_ADDRESS                 STC90x51RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
304 #endif
305 #ifdef STC90x52RC
306 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
307 #define EEPROM_Start_ADDRESS             STC90x52RC_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
308 #define EEPROM_buf_ADDRESS                 STC90x52RC_EEPROM_Start_ADDRESS + Sector_size*(STC90x52RC_EEPROM_Sector_Number -1)    //eeprom起始地址
309 #define EEPROM_end_ADDRESS                 STC90x52RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
310 #endif
311 #ifdef STC90x12RC
312 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
313 #define EEPROM_Start_ADDRESS             STC90x12RC_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
314 #define EEPROM_buf_ADDRESS                 STC90x12RC_EEPROM_Start_ADDRESS + Sector_size*(STC90x12RC_EEPROM_Sector_Number -1)    //eeprom起始地址
315 #define EEPROM_end_ADDRESS                 STC90x12RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
316 #endif
317 #ifdef STC90x54RC
318 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
319 #define EEPROM_Start_ADDRESS             STC90x54RC_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
320 #define EEPROM_buf_ADDRESS                 STC90x54RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x54RC_EEPROM_Sector_Number -1)    //eeprom起始地址
321 #define EEPROM_end_ADDRESS                 STC90x54RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
322 #endif
323 #ifdef STC90x58RC
324 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                    //开启IAP,配置时钟
325 #define EEPROM_Start_ADDRESS             STC90x58RC_EEPROM_Start_ADDRESS                                                     //eeprom起始地址
326 #define EEPROM_buf_ADDRESS                 STC90x58RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x58RC_EEPROM_Sector_Number -1)    //eeprom起始地址
327 #define EEPROM_end_ADDRESS                 STC90x58RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
328 #endif
329 #ifdef STC90x510RC
330 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                        //开启IAP,配置时钟
331 #define EEPROM_Start_ADDRESS             STC90x510RC_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
332 #define EEPROM_buf_ADDRESS                 STC90x510RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x510RC_EEPROM_Sector_Number -1)    //eeprom起始地址
333 #define EEPROM_end_ADDRESS                 STC90x510RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
334 #endif
335 #ifdef STC90x512RC
336 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                        //开启IAP,配置时钟
337 #define EEPROM_Start_ADDRESS             STC90x512RC_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
338 #define EEPROM_buf_ADDRESS                 STC90x512RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x512RC_EEPROM_Sector_Number -1)    //eeprom起始地址
339 #define EEPROM_end_ADDRESS                 STC90x512RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
340 #endif
341 #ifdef STC90x514RC
342 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                        //开启IAP,配置时钟
343 #define EEPROM_Start_ADDRESS             STC90x514RC_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
344 #define EEPROM_buf_ADDRESS                 STC90x514RC_EEPROM_Start_ADDRESS + Sector_size*(STC90x514RC_EEPROM_Sector_Number -1)    //eeprom起始地址
345 #define EEPROM_end_ADDRESS                 STC90x514RC_EEPROM_End_ADDRESS                                                        //eeprom结束地址
346 #endif
347 //STC12xx052AD系列
348 #if defined(STC12xx052AD) || defined(STC12x1052AD) || defined(STC12x2052AD) || defined(STC12x4052AD) || defined(STC12x5052AD)
349 #define ENABLE_EEPROM                   EEPROM_ENABLE_IAP+Clock_10M_20M                                                            //开启IAP,配置时钟
350 #define EEPROM_Start_ADDRESS             STC12xx052AD_EEPROM_Start_ADDRESS                                                         //eeprom起始地址
351 #define EEPROM_buf_ADDRESS                 STC12xx052AD_EEPROM_Start_ADDRESS + Sector_size*(STC12xx052AD_EEPROM_Sector_Number -1)    //eeprom起始地址
352 #define EEPROM_end_ADDRESS                 STC12xx052AD_EEPROM_End_ADDRESS                                                            //eeprom结束地址
353 #endif
354 
355 
356 /*----------------函数-----------------*/
357 /*-----------eeprom------------*/
358 //Eeprom_Unlock(解锁)
359 void Eeprom_Unlock(void);
360 //Eeprom_Locked(上锁)
361 void Eeprom_Locked(void);
362 //Eeprom_Effective(配置生效)
363 void Eeprom_Effective(void);
364 //EEPROM区域读取一个字节
365 unsigned char EEPROM_Read_Byte(unsigned int addr);
366 //将一个字节编程到EEPROM区域
367 void EEPROM_Write_Byte(unsigned int addr, unsigned char dat);
368 //擦除一个扇区
369 void EEPROM_Erase_sector(unsigned int addr);
370 //读取指定地址数据
371 unsigned char EEPROM_assign_Read(unsigned char Sector,unsigned int position);
372 //写入指定地址数据
373 void EEPROM_assign_Write(unsigned char Sector,unsigned int position,unsigned char value);
374 /************************************************************/
375 //字节对比
376 bit Compared(unsigned char Evalue,unsigned char value);
377 //stc 读取指定地址数据
378 unsigned char STC_EEPROM_assign_Read(unsigned char Sector,unsigned int position);
379 //stc 写入指定地址数据
380 void SCT_EEPROM_assign_Write(unsigned char Sector,unsigned int position,unsigned char value);
381 
382 
383 #endif

Eeprom.c

  1 #include "eeprom.h"
  2 
  3 /*-----------eeprom------------*/
  4 //-------------------------------
  5 //Eeprom_Unlock(解锁)
  6 //Input: on
  7 //Output:on
  8 //-------------------------------
  9 void Eeprom_Unlock(void)
 10 {
 11     IAP_CONTR = ENABLE_EEPROM;         //打开IAP功能,并设置等待时间
 12 }
 13 //-------------------------------
 14 //Eeprom_Locked(上锁)
 15 //Input: on
 16 //Output:on
 17 //-------------------------------
 18 void Eeprom_Locked(void)
 19 {
 20     IAP_CONTR = 0;                  //关闭IAP功能
 21     IAP_CMD = 0;                    //清除命令到待机状态
 22     IAP_TRIG = 0;                   //清除触发寄存器
 23     IAP_ADDRH = 0x80;               //数据ptr指向非EEPROM区域
 24     IAP_ADDRL = 0;                  //清除IAP地址以防止滥用
 25 }
 26 //-------------------------------
 27 //Eeprom_Effective(配置生效)
 28 //Input: on
 29 //Output:on
 30 //-------------------------------
 31 void Eeprom_Effective(void)
 32 {
 33     IAP_TRIG = 0x46;                //发送触发器命令1(0x46)
 34     IAP_TRIG = 0xb9;                //发送触发器命令2(0xb9)
 35     _nop_();                        //MCU将保持在此处,直到ISP / IAP / EEPROM操作完成
 36 }
 37 //-------------------------------
 38 //EEPROM区域读取一个字节
 39 //Input: addr 16位EEPROM地址
 40 //Output:on
 41 //-------------------------------
 42 unsigned char EEPROM_Read_Byte(unsigned int addr)
 43 {
 44     unsigned char dat;                //数据缓冲区
 45     Eeprom_Unlock();
 46     IAP_CMD = EEPROM_CMD_READ;      //设置ISP / IAP / EEPROM READ命令
 47     IAP_ADDRL = addr;               //将ISP / IAP / EEPROM地址低8位设置
 48     IAP_ADDRH = addr >> 8;          //将ISP / IAP / EEPROM地址高8位设置
 49     Eeprom_Effective();        //生效
 50     dat = IAP_DATA;                 //读取ISP / IAP / EEPROM数据
 51     Eeprom_Locked();                //关闭ISP / IAP / EEPROM功能
 52     return dat;                     //返回Flash数据
 53 }
 54 //-------------------------------
 55 //将一个字节编程到EEPROM区域
 56 //Input: addr 16位EEPROM地址
 57 //        value 8位数据
 58 //Output:on
 59 //-------------------------------
 60 void EEPROM_Write_Byte(unsigned int addr, unsigned char value)
 61 {
 62     Eeprom_Unlock();
 63     IAP_CMD = EEPROM_CMD_PROGRAM;   //设置EEPROM PROGRAM命令
 64     IAP_ADDRL = addr;               //将EEPROM地址低8位设置
 65     IAP_ADDRH = addr >> 8;          //将EEPROM地址高8位设置
 66     IAP_DATA = value;               //数据写入寄存器
 67     Eeprom_Effective();        //生效
 68     Eeprom_Locked();
 69 }
 70 //-------------------------------
 71 //擦除EEPROM一个扇区
 72 //Input: addr 16位EEPROM地址
 73 //Output:on
 74 //-------------------------------
 75 void EEPROM_Erase_sector(unsigned int addr)
 76 {
 77     Eeprom_Unlock();
 78     IAP_CMD = EEPROM_CMD_ERASE;     //Set ISP/IAP/EEPROM ERASE command
 79     IAP_ADDRL = addr;               //Set ISP/IAP/EEPROM address low
 80     IAP_ADDRH = addr >> 8;          //Set ISP/IAP/EEPROM address high
 81     Eeprom_Effective();                //生效
 82     Eeprom_Locked();
 83 }
 84 //-------------------------------
 85 //读取指定地址数据
 86 //Input: Sector 扇区编号
 87 //        position 扇区内相对地址
 88 //Output:指定位置的数据
 89 //-------------------------------
 90 unsigned char EEPROM_assign_Read(unsigned char Sector,unsigned int position){
 91     return EEPROM_Read_Byte(EEPROM_Start_ADDRESS + Sector*Sector_size + position);
 92 }
 93 //-------------------------------
 94 //写入指定地址数据
 95 //Input: Sector 扇区编号
 96 //        position 扇区内相对地址
 97 //        value 写入指定位置的数据
 98 //Output:on
 99 //-------------------------------
100 void EEPROM_assign_Write(unsigned char Sector,unsigned int position,unsigned char value){
101     EEPROM_Write_Byte(EEPROM_Start_ADDRESS + Sector*Sector_size + position,value);    //鉴定是否需要擦除
102 }
103 /*--------------------------------------------------------------------*/
104 //-------------------------------
105 //写入指定地址数据
106 //Input: Evalue EEPROM读取的数据
107 //        value 新的数据
108 //Output:0 符合,直接写入
109 //        1要进行数据处理
110 //-------------------------------
111 bit Compared(unsigned char Evalue,unsigned char value){
112     unsigned char i;
113     unsigned char date = 0;
114     bit Ev;
115     bit V;
116     for(i = 0; i < 8; i++){
117         Ev     = (Evalue & (0x01 << i));
118         V    = (value & (0x01 << i));
119         if((Ev == 0)&&(V == 1))return 1; 
120     }
121     return 0;
122 }
123 //-------------------------------
124 //stc 读取指定地址数据
125 //Input: Sector 扇区编号
126 //        position 扇区内相对地址
127 //Output:指定位置的数据
128 //-------------------------------
129 unsigned char STC_EEPROM_assign_Read(unsigned char Sector,unsigned int position){
130     return EEPROM_assign_Read(Sector,position);
131 }
132 //stc 写入指定地址数据(根据STC单片机特点,对EEPROM写入进行特别的处理)
133 void SCT_EEPROM_assign_Write(unsigned char Sector,unsigned int position,unsigned char value){
134     int count;
135     if(0 == Compared(STC_EEPROM_assign_Read(Sector,position),value))EEPROM_assign_Write(Sector,position,value);        //鉴定是否需要擦除
136     else {
137         EEPROM_Erase_sector(EEPROM_buf_ADDRESS);                    //清洗EEPROM缓存区
138         for(count = 0; count < Sector_size;count++){
139             if(position == count)EEPROM_Write_Byte( EEPROM_buf_ADDRESS+position,value);                    //替换数据
140             else EEPROM_Write_Byte(EEPROM_buf_ADDRESS + count,EEPROM_assign_Read(Sector,count));        //复制数据
141         }
142         EEPROM_Erase_sector(EEPROM_Start_ADDRESS + Sector*Sector_size);                                  //擦除一个扇区(起始地址)
143         for(count = 0; count < Sector_size;count++)EEPROM_assign_Write(Sector,count,EEPROM_Read_Byte(EEPROM_buf_ADDRESS + count));
144     }
145 }

本代码支持只stc89/90系列自带EEPROM的单片机。

 

  

posted @ 2019-04-08 17:54  极客先锋  阅读(1922)  评论(0编辑  收藏  举报