STC15F104 EEPROM读写测试

效果

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <reg51.H>
#include <intrins.H>

typedef unsigned char INT8U;
typedef unsigned int INT16U;

sfr IAP_DATA = 0xC2;
sfr IAP_ADDRH = 0xC3;
sfr IAP_ADDRL = 0xC4;
sfr IAP_CMD = 0xC5;
sfr IAP_TRIG = 0xC6;
sfr IAP_CONTR = 0xC7;

//#define ENABLE_ISP 0x80 //if SYSCLK<30MHz
//#define ENABLE_ISP 0x81 //if SYSCLK<24MHz
//#define ENABLE_ISP 0x82 //if SYSCLK<20MHz
#define ENABLE_ISP 0x83 //if SYSCLK<12MHz
//#define ENABLE_ISP 0x84 //if SYSCLK<6MHz
//#define ENABLE_ISP 0x85 //if SYSCLK<3MHz
//#define ENABLE_ISP 0x86 //if SYSCLK<2MHz
//#define ENABLE_ISP 0x87 //if SYSCLK<1MHz



union union_temp16
{
INT16U un_temp16;
INT8U un_temp8[2];
}my_unTemp16;

void IAP_Disable()
{
IAP_CONTR = 0;
IAP_CMD = 0;
IAP_TRIG = 0;
IAP_ADDRH = 0;
IAP_ADDRL = 0;
}


INT8U eeprom_read(INT16U add)
{
IAP_DATA = 0x00;
IAP_CONTR = ENABLE_ISP;
IAP_CMD = 0x01;

my_unTemp16.un_temp16 = add;
IAP_ADDRH = my_unTemp16.un_temp8[0];
IAP_ADDRL = my_unTemp16.un_temp8[1];

IAP_TRIG = 0x5A;
IAP_TRIG = 0xA5;
_nop_();
IAP_Disable();

return (IAP_DATA);
}

void eeprom_write(INT16U add, INT8U ch)
{
IAP_CONTR = ENABLE_ISP;
IAP_CMD = 0x02;

my_unTemp16.un_temp16 = add;
IAP_ADDRH = my_unTemp16.un_temp8[0];
IAP_ADDRL = my_unTemp16.un_temp8[1];

IAP_DATA = ch;
//EA = 0;
IAP_TRIG = 0x5A;
IAP_TRIG = 0xA5;
_nop_();
//EA = 1;
IAP_Disable();

}

void eeprom_erase(INT16U add)
{
IAP_CONTR = ENABLE_ISP;
IAP_CMD = 0x03;

my_unTemp16.un_temp16 = add;
IAP_ADDRH = my_unTemp16.un_temp8[0];
IAP_ADDRL = my_unTemp16.un_temp8[1];

//EA = 0;
IAP_TRIG = 0x5A;
IAP_TRIG = 0xA5;
_nop_();
_nop_();
_nop_();
//EA = 1;
IAP_Disable();

}


#include "reg51.h"
#define BAUD 0xF400 // 1200bps @ 11.0592MHz
//#define BAUD 0xFA00 // 2400bps @ 11.0592MHz
//#define BAUD 0xFD00 // 4800bps @ 11.0592MHz
//#define BAUD 0xFE80 // 9600bps @ 11.0592MHz
//#define BAUD 0xFF40 //19200bps @ 11.0592MHz
//#define BAUD 0xFFA0 //38400bps @ 11.0592MHz


sfr AUXR = 0x8E;
sbit RXB = P3^0;
sbit TXB = P3^1;

typedef bit BOOL;
typedef unsigned char BYTE;
typedef unsigned int WORD;

BYTE TBUF,RBUF;
BYTE TDAT,RDAT;
BYTE TCNT,RCNT;
BYTE TBIT,RBIT;
BOOL TING,RING;
BOOL TEND,REND;

void UART_INIT();

BYTE t, r;
BYTE buf[16];

//-----------------------------------------
//Timer interrupt routine for UART

void tm0() interrupt 1
{
if (RING)
{
if (--RCNT == 0)
{
RCNT = 3; //reset send baudrate counter
if (--RBIT == 0)
{
RBUF = RDAT; //save the data to RBUF
RING = 0; //stop receive
REND = 1; //set receive completed flag
}
else
{
RDAT >>= 1;
if (RXB)
RDAT |= 0x80; //shift RX data to RX buffer
}
}
}
else if (!RXB)
{
RING = 1; //set start receive flag
RCNT = 4; //initial receive baudrate counter
RBIT = 9; //initial receive bit number (8 data bits + 1 stop bit)
}

if (--TCNT == 0)
{
TCNT = 3; //reset send baudrate counter
if (TING) //judge whether sending
{
if (TBIT == 0)
{
TXB = 0; //send start bit
TDAT = TBUF; //load data from TBUF to TDAT
TBIT = 9; //initial send bit number (8 data bits + 1 stop bit)
}
else
{
TDAT >>= 1; //shift data to CY
if (--TBIT == 0)
{
TXB = 1;
TING = 0; //stop send
TEND = 1; //set send completed flag
}
else
{
TXB = CY; //write CY to TX port
}
}
}
}
}

void UART_INIT()
{
TMOD = 0x00; //timer0 in 16-bit auto reload mode
AUXR = 0x80; //timer0 working at 1T mode
TL0 = BAUD;
TH0 = BAUD>>8; //initial timer0 and set reload value
TR0 = 1; //tiemr0 start running
ET0 = 1; //enable timer0 interrupt
PT0 = 1; //improve timer0 interrupt priority
EA = 1; //open global interrupt switch

TING = 0;
RING = 0;
TEND = 1;
REND = 0;
TCNT = 0;
RCNT = 0;
}

void SendByte(char aByte)
{
while(!TEND);
TEND = 0;
TBUF = aByte;
TING = 1;
}
char RecvByte()
{
while(!REND);
REND = 0;
return RBUF;
}

void SendData(char* pData, int len)
{
int i;
for(i=0; i<len; i++)
{
SendByte(pData[i]);
}
}


void SendHEX(unsigned char Data)
{
unsigned char highByte = (Data >> 4);
unsigned char lowByte = (Data & 0X0F);

highByte += 0x30;
if (highByte > 0x39)
{
SendByte(highByte + 0x07);
}
else
{
SendByte(highByte);
}

lowByte += 0x30;
if (lowByte > 0x39)
{
SendByte(lowByte + 0x07);
}
else
{
SendByte(lowByte);
}
}



void SendLR()
{
SendByte('\r');
SendByte('\n');
}

void SendStr(char* pData)
{
int i = 0;;
while(pData[i] != '\0')
{
SendByte(pData[i++]);
}
}



/*----------------------------
软件延时
----------------------------*/

void Delay(BYTE n)
{
WORD x;

while (n--)
{
x = 0;
while (++x);
}
}



#define IAP_ADDRESS_1 0x0000 // 扇区1 起始地址 共512 Byte
#define IAP_ADDRESS_2 0x0200 // 扇区2 起始地址 共512 Byte


void main()
{
unsigned int aByte;
UART_INIT();

while (1)
{ //user's function
eeprom_erase(IAP_ADDRESS_1);
Delay(15);
eeprom_erase(IAP_ADDRESS_2);
Delay(15);

//////////////////////////////////////////////////
SendStr("sector_1 after eeprom_erase");
SendLR();
for(aByte = 0; aByte < 512; aByte++) // [0 ~ 511]
{
SendHEX(eeprom_read(IAP_ADDRESS_1 + aByte));
SendStr(" ");
}
SendLR();

SendStr("sector_2 after eeprom_erase");
SendLR();
for(aByte = 0; aByte < 512; aByte++) // [0 ~ 511]
{
SendHEX(eeprom_read(IAP_ADDRESS_2 + aByte));
SendStr(" ");
}
SendLR();

//////////////////////////////////////////////////
SendStr("sector_1 Write Read");
SendLR();
for(aByte = 0; aByte < 512; aByte++) // [0 ~ 511]
{
eeprom_write(IAP_ADDRESS_1 + aByte, (aByte % 256));
}

for(aByte = 0; aByte < 512; aByte++) // [0 ~ 511]
{
SendHEX(eeprom_read(IAP_ADDRESS_1 + aByte));
SendStr(" ");
}
SendLR();

SendStr("sector_2 Write Read");
SendLR();
for(aByte = 0; aByte < 512; aByte++) // [0 ~ 511]
{
eeprom_write(IAP_ADDRESS_2 + aByte, 0xff - (aByte % 256));
}

for(aByte = 0; aByte < 512; aByte++) // [0 ~ 511]
{
SendHEX(eeprom_read(IAP_ADDRESS_2 + aByte));
SendStr(" ");
}
SendLR();

Delay(45);

}
}