1、STC单片机内部FLASH读写程序(电子伙伴 发表于 2006-4-11 16:26:001推荐/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 HOUR0x8000#define MINUTE0x8001void IS
2、P_EN(void)EA=0;/ 关中断 ISP_CONTR=ISP_CONTR & 0x18; / 0001,1000ISP_CONTR=ISP_CONTR | WAIT_TIME;ISP_CONTR=ISP_CONTR | 0x80; / 1000,0000void ISP_DI(void)ISP_CONTR=ISP_CONTR & 0x7f;/ 0111,1111ISP_TRIG=0x00;EA= 1; / 开中断unsigned char Byte_read(unsigned int byte_addr)ISP_ADDRH=(unsigned char)(byte_addr 8);IS
3、P_ADDRL=(unsigned char)(byte_addr & 0x00ff);ISP_CMD=ISP_CMD&0xf8; / 1111,1000ISP_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,1
4、110,0000,0000; 取扇区地址ISP_ADDRH =addre&0xfe;ISP_ADDRL=0x00;ISP_CMD=ISP_CMD&0xf8;/ 1111,1000ISP_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 ch
5、ar)(byte_addr 8);ISP_ADDRL=(unsigned char)(byte_addr & 0x00ff);ISP_CMD=ISP_CMD&0xf8; / 1111,1000ISP_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, unsigne
6、d char DataBuf)ISP_ADDRH=(unsigned char)(byte_addr 8);ISP_ADDRL=(unsigned char)(byte_addr & 0x00ff);ISP_CMD=ISP_CMD&0xf8; / 1111,1000ISP_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,1000ISP_CMD=ISP_CMD|0x01; / 0000,0001ISP_TRIG=0x46; / 触发ISP_IAP命令ISP_TRIG=0xb9; / 触发ISP_IAP命令_nop_();ISP_DI();if(ISP_DATA=DataBuf)returnOK;elsereturnERROR;