//#i nclude "STC89C51.h"
//#i nclude
#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 HOUR
0x8000
#define MINUTE
0x8001void ISP_EN(void)
{
EA
=
0;
//
关中断
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 字节读数据存储区
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; 取扇区地址
ISP_ADDRH =addre&0xfe;
ISP_ADDRL=0x00;
ISP_CMD=ISP_CMD&0xf8;// 1111,1000
ISP_CMD=ISP_CMD|0x03;// 0000,0011 扇区擦除数据存储区
ISP_EN();
ISP_TRIG=0x46; // 触发ISP_IAP命令
ISP_TRIG=0xb9; // 触发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 字节编程数据存储区
ISP_DATA=DataBuf;
ISP_EN();
ISP_TRIG=0x46; // 触发ISP_IAP命令
ISP_TRIG=0xb9; // 触发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 字节编程数据存储区
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; // 触发ISP_IAP命令
ISP_TRIG=0xb9; // 触发ISP_IAP命令
_nop_();
ISP_DI();
if(ISP_DATA==DataBuf)returnOK;
elsereturnERROR;
}