//#i nclude "STC89C51.h"//#i nclude
本文引用地址:http://www.ex-cimer.com/article/201611/320127.htm
#define ERROR 0
#define OK 1
#define WAIT_TIME0x00//mcu clock 40mhz
//#define WAIT_TIME0x01//mcu clock 20mhz
//#define WAIT_TIME0x02//mcu clock 10mhz
//#define WAIT_TIME0x03//mcu clock 5mhz
#define HOUR0x8000
#define MINUTE0x8001
void ISP_EN(void)
{
EA=0;// 關(guān)中斷
ISP_CONTR=ISP_CONTR & 0x18; // 0001,1000
ISP_CONTR=ISP_CONTR | WAIT_TIME;
ISP_CONTR=ISP_CONTR | 0x80; // 1000,0000
}
void ISP_DI(void)
{
ISP_CONTR=ISP_CONTR & 0x7f;// 0111,1111
ISP_TRIG=0x00;
EA= 1; // 開中斷
}
unsigned char Byte_read(unsigned int byte_addr)
{
ISP_ADDRH=(unsigned char)(byte_addr >> 8);
ISP_ADDRL=(unsigned char)(byte_addr & 0x00ff);
ISP_CMD=ISP_CMD&0xf8; // 1111,1000
ISP_CMD=ISP_CMD|0x01; // 0000,0001 字節(jié)讀數(shù)據(jù)存儲區(qū)
ISP_EN();
ISP_TRIG=0x46;
ISP_TRIG=0xb9;
_nop_();
ISP_DI();
return (ISP_DATA);
}
void Sector_erase(unsigned int Sector_addr)
{
unsigned char addre;
addre=(unsigned char)(Sector_addr >> 8); // 1111,1110,0000,0000; 取扇區(qū)地址
ISP_ADDRH =addre&0xfe;
ISP_ADDRL=0x00;
ISP_CMD=ISP_CMD&0xf8;// 1111,1000
ISP_CMD=ISP_CMD|0x03;// 0000,0011 扇區(qū)擦除數(shù)據(jù)存儲區(qū)
ISP_EN();
ISP_TRIG=0x46; // 觸發(fā)ISP_IAP命令
ISP_TRIG=0xb9; // 觸發(fā)ISP_IAP命令
_nop_();
ISP_DI();
}
void Byte_program(unsigned int byte_addr, unsigned char DataBuf)
{
ISP_ADDRH=(unsigned char)(byte_addr >> 8);
ISP_ADDRL=(unsigned char)(byte_addr & 0x00ff);
ISP_CMD=ISP_CMD&0xf8; // 1111,1000
ISP_CMD=ISP_CMD|0x02;// 0000,0010 字節(jié)編程數(shù)據(jù)存儲區(qū)
ISP_DATA=DataBuf;
ISP_EN();
ISP_TRIG=0x46; // 觸發(fā)ISP_IAP命令
ISP_TRIG=0xb9; // 觸發(fā)ISP_IAP命令
_nop_();
ISP_DI();
}
unsigned char ByteProgramVerify(unsigned int byte_addr, unsigned char DataBuf)
{
ISP_ADDRH=(unsigned char)(byte_addr >> 8);
ISP_ADDRL=(unsigned char)(byte_addr & 0x00ff);
ISP_CMD=ISP_CMD&0xf8; // 1111,1000
ISP_CMD=ISP_CMD|0x02;// 0000,0010 字節(jié)編程數(shù)據(jù)存儲區(qū)
ISP_DATA=DataBuf;
ISP_EN();
ISP_TRIG=0x46;
ISP_TRIG=0xb9;
_nop_();
ISP_DATA=0x00;
ISP_CMD=ISP_CMD&0xf8; // 1111,1000
ISP_CMD=ISP_CMD|0x01; // 0000,0001
ISP_TRIG=0x46; // 觸發(fā)ISP_IAP命令
ISP_TRIG=0xb9; // 觸發(fā)ISP_IAP命令
_nop_();
ISP_DI();
if(ISP_DATA==DataBuf)returnOK;
elsereturnERROR;
}
評論