STC89C58RD+ 内部EEPROM 放数据 FLASH-ISP-IAP
扫描二维码
随时随地手机看文章
#include <reg52.h>
#include <intrins.h> /* use _nop_() function */
//sfr16 DPTR = 0x82;
/* 新增特殊功能寄存器定义 */
sfr ISP_DATA = 0xe2;
sfr ISP_ADDRH = 0xe3;
sfr ISP_ADDRL = 0xe4;
sfr ISP_CMD = 0xe5;
sfr ISP_TRIG = 0xe6;
sfr ISP_CONTR = 0xe7;
/* 定义命令 */
#define READ_AP_and_Data_Memory_Command 0x01 /* 字节读数据存储区 */
#define PROGRAM_AP_and_Data_Memory_Command 0x02 /* 字节编程数据存储区 */
#define SECTOR_ERASE_AP_and_Data_Memory_Command 0x03 /* 扇区擦除数据存储区 */
typedef unsigned char INT8U; /* 8 bit 无符号整型 */
typedef unsigned int INT16U; /* 16 bit 无符号整型 */
#define DELAY_CONST 60000
/* 定义常量 */
#define ERROR 0
#define OK 1
sbit Begin_LED = P1^0;
sbit ERROR_LED = P1^3;
sbit OK_LED = P1^7;
/* 定义Flash 操作等待时间 */
#define MCU_CLOCK_40MHz
//#define MCU_CLOCK_20MHz
//#define MCU_CLOCK_10MHz
//#define MCU_CLOCK_5MHz
#ifdef MCU_CLOCK_40MHz
#define WAIT_TIME 0x00
#endif
#ifdef MCU_CLOCK_20MHz
#define WAIT_TIME 0x01
#endif
#ifdef MCU_CLOCK_10MHz
#define WAIT_TIME 0x02
#endif
#ifdef MCU_CLOCK_5MHz
#define WAIT_TIME 0x03
#endif
/* 调试控制项 */
//#define DEBUG_STC89C_LE58RD+
#define DEBUG_STC89C_LE52RC
//#define USED_BYTE_QTY_IN_ONE_SECTOR 1
//#define USED_BYTE_QTY_IN_ONE_SECTOR 2
//#define USED_BYTE_QTY_IN_ONE_SECTOR 4
//#define USED_BYTE_QTY_IN_ONE_SECTOR 8
//#define USED_BYTE_QTY_IN_ONE_SECTOR 16
//#define USED_BYTE_QTY_IN_ONE_SECTOR 32
//#define USED_BYTE_QTY_IN_ONE_SECTOR 64
#define USED_BYTE_QTY_IN_ONE_SECTOR 128
//#define USED_BYTE_QTY_IN_ONE_SECTOR 256
//#define USED_BYTE_QTY_IN_ONE_SECTOR 512
INT8U xdata protect_buffer[USED_BYTE_QTY_IN_ONE_SECTOR];
#ifdef DEBUG_STC89C_LE58RD+ //STC89C58RD+, 89LE58RD+
#define DEBUG_AP_Memory_Begin_Sector_addr 0x0000
#define DEBUG_AP_Memory_End_Sector_addr 0x7e00
#define DEBUG_AP_Memory_End_Byte_addr 0x7fff
#define DEBUG_Data_Memory_Begin_Sector_addr 0x8000
#endif
#ifdef DEBUG_STC89C_LE52RC //STC89C52RC, 89LE52RC
#define DEBUG_AP_Memory_Begin_Sector_addr 0x0000
#define DEBUG_AP_Memory_End_Sector_addr 0x1e00
#define DEBUG_AP_Memory_End_Byte_addr 0x1fff
#define DEBUG_Data_Memory_Begin_Sector_addr 0x2000
#endif
/* 打开 ISP,IAP 功能 */
void ISP_IAP_enable(void)
{
EA = 0; /* 关中断 */
ISP_CONTR = ISP_CONTR & 0x18; /* 0001,1000 */
ISP_CONTR = ISP_CONTR | WAIT_TIME;
ISP_CONTR = ISP_CONTR | 0x80; /* 1000,0000 */
}
/* 关闭 ISP,IAP 功能 */
void ISP_IAP_dISAble(void)
{
ISP_CONTR = ISP_CONTR & 0x7f; /* 0111,1111 */
ISP_TRIG = 0x00;
EA = 1; /* 开中断 */
}
/* 字节读 */
INT8U byte_read(INT16U byte_addr)
{
ISP_ADDRH = (INT8U)(byte_addr >> 8);
ISP_ADDRL = (INT8U)(byte_addr & 0x00ff);
ISP_CMD = ISP_CMD & 0xf8; /* 1111,1000 */
ISP_CMD = ISP_CMD | READ_AP_and_Data_Memory_Command; /* 0000,0001 */
ISP_IAP_enable();
ISP_TRIG = 0x46;
ISP_TRIG = 0xb9;
_nop_();
ISP_IAP_disable();
return (ISP_DATA);
}
/* 扇区擦除 */
INT8U sector_erase(INT16U sector_addr)
{
INT16U get_sector_addr = 0;
get_sector_addr = (sector_addr & 0xfe00); /* 1111,1110,0000,0000; 取扇区地址 */
ISP_ADDRH = (INT8U)(get_sector_addr >> 8);
ISP_ADDRL = 0x00;
ISP_CMD = ISP_CMD & 0xf8; /* 1111,1000 */
ISP_CMD = ISP_CMD | SECTOR_ERASE_AP_and_Data_Memory_Command; /* 0000,0011 */
ISP_IAP_enable();
ISP_TRIG = 0x46; /* 触发ISP_IAP命令 */
ISP_TRIG = 0xb9; /* 触发ISP_IAP命令 */
_nop_();
ISP_IAP_disable();
return OK;
}
/* 字节编程 */
INT8U byte_program(INT16U byte_addr, INT8U original_data)
{
ISP_ADDRH = (INT8U)(byte_addr >> 8);
ISP_ADDRL = (INT8U)(byte_addr & 0x00ff);
ISP_CMD = ISP_CMD & 0xf8; /* 1111,1000 */
ISP_CMD = ISP_CMD | PROGRAM_AP_and_Data_Memory_Command; /* 0000,0010 */
ISP_DATA = original_data;
ISP_IAP_enable();
ISP_TRIG = 0x46; /* 触发ISP_IAP命令 */
ISP_TRIG = 0xb9; /* 触发ISP_IAP命令 */
_nop_();
ISP_IAP_disable();
return OK;
}
/* 字节编程并校验 */
INT8U byte_program_and_verify(INT16U byte_addr, INT8U original_data)
{
ISP_ADDRH = (INT8U)(byte_addr >> 8);
ISP_ADDRL = (INT8U)(byte_addr & 0x00ff);
ISP_CMD = ISP_CMD & 0xf8; /* 1111,1000 */
ISP_CMD = ISP_CMD | PROGRAM_AP_and_Data_Memory_Command; /* 0000,0010 */
ISP_DATA = original_data;
ISP_IAP_enable();
ISP_TRIG = 0x46;
ISP_TRIG = 0xb9;
_nop_();
ISP_DATA = 0x00;
ISP_CMD = ISP_CMD & 0xf8; /* 1111,1000 */
ISP_CMD = ISP_CMD | READ_AP_and_Data_Memory_Command; /* 0000,0001 */
ISP_TRIG = 0x46; /* 触发ISP_IAP命令 */
ISP_TRIG = 0xb9; /* 触发ISP_IAP命令 */
_nop_();
ISP_IAP_disable();
if(ISP_DATA == original_data)
return OK;
else
return ERROR;
}
/* 写数据进 数据Flash存储器, 只在同一个扇区内写,不保留原有数据 */
/* begin_addr,被写数据Flash开始地址;counter,连续写多少个字节; array[],数据来源 */
INT8U sequential_write_flash_in_one_sector(INT16U begin_addr, INT16U counter, INT8U array[])
{
INT16U i = 0;
INT16U in_sector_begin_addr = 0;
INT16U sector_addr = 0;
/* 判是否是有效范围,此函数不允许跨扇区操作 */
if(counter > USED_BYTE_QTY_IN_ONE_SECTOR)
return ERROR;
in_sector_begin_addr = begin_addr & 0x01ff; /* 0000,0001,1111,1111 */
if( (in_sector_begin_addr + counter) > USED_BYTE_QTY_IN_ONE_SECTOR )
return ERROR;
/* 擦除 要修改/写入 的扇区 */
sector_addr = (begin_addr & 0xfe00); /* 1111,1110,0000,0000; 取扇区地址 */
ISP_ADDRH = (INT8U)(sector_addr >> 8);
ISP_ADDRL = 0x00;
ISP_CMD = ISP_CMD & 0xf8; /* 1111,1000 */
ISP_CMD = ISP_CMD | SECTOR_ERASE_AP_and_Data_Memory_Command; /* 0000,0011 */