#include"reg51.h"
#include"intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
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 CMD_IDLE 0
#define CMD_READ 1
#define CMD_PROGRAM 2
#define CMD_ERASE 3
#define URMD 0
sfr T2H = 0XD6;
sfr T2L = 0XD7;
sfr AUXR = 0X8E;
#define ENABLE_IAP 0X82
#define IAP_ADDRESS 0X0400
void Delay(BYTE n);
void IapIdle();
BYTE IapReadByte(WORD addr);
void IapProgramByte(WORD addr,BYTE dat);
void IapEraseSector(WORD addr);
void InitUart();
BYTE SendData(BYTE dat);
void main()
{
WORD i;
P1 = 0XFE;
InitUart();
Delay(10);
IapEraseSector(IAP_ADDRESS);
for(i=0;i<512;i++)
{
if(SendData(IapReadByte(IAP_ADDRESS+i)) != 0XFF)
goto Error;
}
P1 = 0XFC;
Delay(10);
for(i=0;i<512;i++)
{
IapProgramByte(IAP_ADDRESS+i,(BYTE)i);
}
P1 = 0XF8;
Delay(10);
for(i=0;i<512;i++)
{
if(SendData(IapReadByte(IAP_ADDRESS+i))!=(BYTE)i)
goto Error;
}
P1 = 0XF0;
while(1);
Error:
P1 &= 0X7F;
while(1);
}
void Delay(BYTE n)
{
WORD x;
while(n--)
{
x = 0;
while(++x);
}
}
void IapIdle()
{
IAP_CONTR = 0;
IAP_CMD = 0;
IAP_TRIG = 0;
IAP_ADDRH = 0X08;
IAP_ADDRL = 0;
}
BYTE IapReadByte(WORD addr)
{
BYTE dat;
IAP_CONTR = ENABLE_IAP;
IAP_CMD = CMD_READ;
IAP_ADDRL = addr;
IAP_ADDRH = addr>>8;
IAP_TRIG = 0X5A;
IAP_TRIG = 0XA5;
_nop_();
dat = IAP_DATA;
IapIdle();
return dat;
}
void IapProgramByte(WORD addr,BYTE dat)
{
IAP_CONTR = ENABLE_IAP;
IAP_CMD = CMD_PROGRAM;
IAP_ADDRL = addr;
IAP_ADDRH = addr>>8;
IAP_DATA = dat;
IAP_TRIG = 0X5A;
IAP_TRIG = 0XA5;
_nop_();
IapIdle();
}
void IapEraseSector(WORD addr)
{
IAP_CONTR = ENABLE_IAP;
IAP_CMD = CMD_ERASE;
IAP_ADDRL = addr;
IAP_ADDRH = addr>>8;
IAP_TRIG = 0X5A;
IAP_TRIG = 0XA5;
_nop_();
IapIdle();
}
void InitUart()
{
SCON = 0X5A;
#if URMD == 0
T2L = 0XD8;
T2H = 0XFF;
AUXR = 0X14;
AUXR = 0X01;
#elif URMD == 1
AUXR = 0X40;
TMOD = 0X00;
TL1 = 0XD8;
TH1 = 0XFF;
TR1 = 1;
#else
TMOD = 0X20;
AUXR = 0X40;
TH1 = TL1 = 0XFB;
TR1 = 1;
#endif
}
BYTE SendData(BYTE dat)
{
while(!TI);
TI = 0;
SBUF = dat;
return dat;
}
|