STC89C58RD+ 內(nèi)部EEPROM 放數(shù)據(jù) FLASH-ISP-IAP
掃描二維碼
隨時(shí)隨地手機(jī)看文章
#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 /* 字節(jié)讀數(shù)據(jù)存儲(chǔ)區(qū) */
#define PROGRAM_AP_and_Data_Memory_Command 0x02 /* 字節(jié)編程數(shù)據(jù)存儲(chǔ)區(qū) */
#define SECTOR_ERASE_AP_and_Data_Memory_Command 0x03 /* 扇區(qū)擦除數(shù)據(jù)存儲(chǔ)區(qū) */
typedef unsigned char INT8U; /* 8 bit 無符號(hào)整型 */
typedef unsigned int INT16U; /* 16 bit 無符號(hào)整型 */
#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 操作等待時(shí)間 */
#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
/* 調(diào)試控制項(xiàng) */
//#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; /* 關(guān)中斷 */
ISP_CONTR = ISP_CONTR & 0x18; /* 0001,1000 */
ISP_CONTR = ISP_CONTR | WAIT_TIME;
ISP_CONTR = ISP_CONTR | 0x80; /* 1000,0000 */
}
/* 關(guān)閉 ISP,IAP 功能 */
void ISP_IAP_dISAble(void)
{
ISP_CONTR = ISP_CONTR & 0x7f; /* 0111,1111 */
ISP_TRIG = 0x00;
EA = 1; /* 開中斷 */
}
/* 字節(jié)讀 */
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);
}
/* 扇區(qū)擦除 */
INT8U sector_erase(INT16U sector_addr)
{
INT16U get_sector_addr = 0;
get_sector_addr = (sector_addr & 0xfe00); /* 1111,1110,0000,0000; 取扇區(qū)地址 */
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; /* 觸發(fā)ISP_IAP命令 */
ISP_TRIG = 0xb9; /* 觸發(fā)ISP_IAP命令 */
_nop_();
ISP_IAP_disable();
return OK;
}
/* 字節(jié)編程 */
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; /* 觸發(fā)ISP_IAP命令 */
ISP_TRIG = 0xb9; /* 觸發(fā)ISP_IAP命令 */
_nop_();
ISP_IAP_disable();
return OK;
}
/* 字節(jié)編程并校驗(yàn) */
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; /* 觸發(fā)ISP_IAP命令 */
ISP_TRIG = 0xb9; /* 觸發(fā)ISP_IAP命令 */
_nop_();
ISP_IAP_disable();
if(ISP_DATA == original_data)
return OK;
else
return ERROR;
}
/* 寫數(shù)據(jù)進(jìn) 數(shù)據(jù)Flash存儲(chǔ)器, 只在同一個(gè)扇區(qū)內(nèi)寫,不保留原有數(shù)據(jù) */
/* begin_addr,被寫數(shù)據(jù)Flash開始地址;counter,連續(xù)寫多少個(gè)字節(jié); array[],數(shù)據(jù)來源 */
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;
/* 判是否是有效范圍,此函數(shù)不允許跨扇區(qū)操作 */
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;
/* 擦除 要修改/寫入 的扇區(qū) */
sector_addr = (begin_addr & 0xfe00); /* 1111,1110,0000,0000; 取扇區(qū)地址 */
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 */