#include<reg51.h>
//#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
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_WRITE 2
#define CMD_ERASE 3
#define ENABLE_IAP 0x82
//Start address for STC12C5A60S2 series EEPROM
#define IAP_ADDRESS 0x0000
void delay(uchar n)
{
uint x;
while(n--)
{
x=0;
while(++x);
}
}
/*----------------------------
Disable ISP/IAP/EEPROM function
Make MCU in a safe state
----------------------------*/
void IAP_Disable()
{
IAP_CONTR=0;
IAP_CMD=0;
IAP_TRIG=0;
IAP_ADDRH=0x80;
IAP_ADDRL=0x00;
}
/*---------------------------------------------
Read one byte from ISP/IAP/EEPROM area
Input: addr (ISP/IAP/EEPROM address)
Output:Flash data
--------------------------------------------*/
uchar IAP_ReadByte(unsigned int addr)
{
uchar i;
uchar dat;
IAP_CONTR=ENABLE_IAP;
IAP_CMD=CMD_READ;
IAP_ADDRL=addr;
IAP_ADDRH=(addr>>8);
IAP_TRIG=0x5A;
IAP_TRIG=0XA5;
for(i=0;i<3;i++);
dat=IAP_DATA;
IAP_Disable();
return dat;
}
/*------------------------------------------------
Write one byte to ISP/IAP/EEPROM area
Input: addr (ISP/IAP/EEPROM address)
dat (ISP/IAP/EEPROM data)
Output:-
------------------------------------------------*/
void IAP_WriteByte(unsigned int addr,unsigned char dat)
{
uchar i;
IAP_CONTR=ENABLE_IAP;
IAP_CMD=CMD_WRITE;
IAP_ADDRL=addr;
IAP_ADDRH=(addr>>8);
IAP_DATA=dat;
IAP_TRIG=0x5A;
IAP_TRIG=0XA5;
for(i=0;i<3;i++);
IAP_Disable();
}
/*---------------------------------------------------
Erase one sector area
Input: addr (ISP/IAP/EEPROM address)
Output:-
--------------------------------------------------*/
void IAP_EraseSector(unsigned int addr)
{
unsigned char i;
IAP_CONTR=ENABLE_IAP;
IAP_CMD=CMD_ERASE;
IAP_ADDRL=addr;
IAP_ADDRH=(addr>>8);
IAP_TRIG=0x5A;
IAP_TRIG=0XA5;
for(i=0;i<3;i++);
IAP_Disable();
}
void main()
{
uint i;
P1=0xfe;
delay(10);
IAP_EraseSector(IAP_ADDRESS);
for(i=0;i<512;i++)
{
if(IAP_ReadByte(IAP_ADDRESS+i)!=0xff)
goto ERROR;
}
P1=0xfc;
delay(10);
for(i=0;i<512;i++)
{
IAP_WriteByte(IAP_ADDRESS+i,(unsigned char)i);
}
P1=0xf8;
delay(10);
for(i=0;i<512;i++)
{
if(IAP_ReadByte(IAP_ADDRESS+i)!=(unsigned char)i)
goto ERROR;
}
P1=0xf0;
while(1);
ERROR:
P1&=0x7f;
while(1);
}
文章评论(0条评论)
登录后参与讨论