STC12C5A60S2 EEPROM应用程序
扫描二维码
随时随地手机看文章
#include "stc12c5a60s2.h"
#include "intrins.h"
#define nop() _nop_()
#define uchar unsigned char
#define uint unsigned int
#define CMD_IDLE 0
#define CMD_READ 1
#define CMD_PROGRAM 2
#define CMD_ERASE 3
#define ENABLE_IAP 0x83
sbit s=P1^1;
sbit h=P1^0;
void delay(uchar i)
{
uchar j;
while(i--)
{
for(j=0;j<125;j++);
}
}
void IapIdle()
{
IAP_CONTR=0;
IAP_CMD=0;
IAP_TRIG=0;
IAP_ADDRH=0X80;
IAP_ADDRL=0;
}
uchar IapReadByte(uint addr)
{
uchar 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(uint addr,uchar 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(uint 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 main()
{
uchar temp=0;
if(IapReadByte(0x0000)==0xff||h==0)
{
IapEraseSector(0x0000);
IapProgramByte(0x0000,0x00);
}
while(1)
{
P0=IapReadByte(0x0000);
while(s==0)
{
delay(50);
if(s==1)
{
IapEraseSector(0x0000);
IapProgramByte(0x0000,0x55);
temp=0;
}
}
}
}