⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iap.h

📁 stc单片机IAP源码,可将数据写入单片机内部FLASH中,无须外扩FLASH,速度快
💻 H
字号:
#include <reg52.h>
#include <intrins.h>        /* use _nop_() function */

/*        新增特殊功能寄存器定义        */
sfr		ISP_DATA	=	0xe2;	/*        ISP_DATA      DATA        0E2H;        汇编程序定义 or EQU */
sfr     ISP_ADDRH	=	0xe3;  	/*        ISP_ADDRH     DATA        0E3H;        汇编程序定义 */
sfr		ISP_ADDRL	=   0xe4;   /*        ISP_ADDRL     DATA        0E4H;        汇编程序定义 */
sfr		ISP_CMD		=   0xe5;   /*        ISP_CMD		DATA        0E5H;        汇编程序定义 */
sfr		ISP_TRIG	=   0xe6;   /*        ISP_TRIG      DATA        0E6H;        汇编程序定义 */
sfr		ISP_CONTR   =   0xe7;   /*        ISP_CONTR     DATA        0E7H;        汇编程序定义 */

/*  SFR: ISP_DATA  (ISP Flash Data Register):        E2H ------------------------*/
/*--|        b7  |        b6  |        b5  |        b4  |        b3  |        b2  |        b1  |         b0  |---*/
/*--|        Data to be written into flash, or Data got from flash       |---*/

/*  SFR: ISP_ADDRH  (ISP Flash Address High):        E3H --------------------*/
/*--|        b7  |        b6  |        b5  |        b4  |        b3  |        b2  |        b1  |        b0  |---*/
/*--|        High byte of the address pointing to flash                  |---*/

/*  SFR: ISP_ADDRL  (ISP Flash Address Low):        E4H --------------------*/
/*--|        b7  |        b6  |         b5  |        b4  |        b3  |        b2  |        b1  |        b0  |---*/
/*--|        Low byte of the address pointing to flash                   |---*/

/*  SFR: ISP_CMD         (ISP Flash Mode Table):        E5H --------------------*/
/*--|        b7  |        b6  |        b5  |        b4  |        b3  |        b2  |        b1  |        b0  |---*/
/*--|                        reserved                                |           Mode Selection             |---*/
//0 0 0-none,0 0 1-read,0 1 0-wtire,0 1 1-erase

/*  SFR: ISP_TRIG  (Sequential Command Data register for ISP):        E6H ----*/
/*--|        b7  |        b6  |        b5  |        b4  |        b3  |        b2  |        b1  |        b0  |---*/
/*--|                            ISP-Command                                                                |---*/
// -> 0x46 -> 0xb9

/*  SFR: ISP_CONTR  (ISP Control register):                E7H --------------------*/
/*--|        b7  |        b6  |        b5  |        b4  |        b3  |        b2  |        b1  |        b0  |---*/
/*--| ISPEN |  SWBS | SWRST |        -   |        -   |              WAIT                |---*/
/*-- ISPEN: ISP function enabling bit                         ----------------*/
/*--     0: Disable ISP program to change flash                    ----------------*/
/*--     1: Enable ISP program to change flash                    ----------------*/
/*-- SWBS: Secondary Booting program selecting                    ----------------*/
/*--     0: Boot from AP-Memory / Main-Memory                    ----------------*/
/*--     1: Boot from ISP-Memory                                ----------------*/
/*-- SWRST: Software reset trigger                                    ----------------*/
/*--     0: No operation                                                ----------------*/
/*--     1: Generate software system reset. It will be cleared by hardware automatically  ---*/

/* 定义命令 */
#define READ_Command				0x01        /*  字节读应用程序区和数据存储区   */
#define PROGRAM_Command          0x02        /*  字节编程应用程序区和数据存储区 */
#define ERASE_Command     0x03        /*  扇区擦除应用程序区和数据存储区 */

typedef     unsigned char	INT8U;		/* 8 bit 无符号整型  */
typedef     unsigned int    INT16U;     /* 16 bit 无符号整型 */
#define		DELAY_CONST         60000

/* 定义常量 */
#define ERROR   0    //错误
#define OK      1    //正确

/* 定义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_LE516RD
//#define         DEBUG_STC89C_LE58RD
#define         DEBUG_STC89C_LE54RD
//#define         DEBUG_STC89C_LE53RC
//#define         DEBUG_STC89C_LE52RC
//#define         DEBUG_STC89C_LE51RC

//#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_LE516RD                //STC89C516RD+, 89LE516RD+
        #define DEBUG_AP_Memory_Begin_Sector_addr		0x0000
        #define DEBUG_AP_Memory_End_Sector_addr			0xf600
        #define DEBUG_AP_Memory_End_Byte_addr           0xf7ff
#endif
#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
        #define DEBUG_Data_Memory_End_Sector_addr       0xf600
        #define DEBUG_Data_Memory_End_Byte_addr         0xf7ff
#endif

#ifdef DEBUG_STC89C_LE54RD                     //STC89C54RD+,        89LE54RD+
        #define DEBUG_AP_Memory_Begin_Sector_addr		0x0000
        #define DEBUG_AP_Memory_End_Sector_addr         0x3e00
        #define DEBUG_AP_Memory_End_Byte_addr           0x3fff

        #define DEBUG_Data_Memory_Begin_Sector_addr     0x8000
        #define DEBUG_Data_Memory_End_Sector_addr       0xf000
        #define DEBUG_Data_Memory_End_Byte_addr         0xf2ff

//        #define DEBUG_Data_Memory_End_Sector_addr		0xf600
//        #define DEBUG_Data_Memory_End_Byte_addr		0xf7ff
#endif

#ifdef DEBUG_STC89C_LE53RC                        //STC89C53RC,        89LE53RC
        #define DEBUG_AP_Memory_Begin_Sector_addr		0x0000
        #define DEBUG_AP_Memory_End_Sector_addr			0x3600
        #define DEBUG_AP_Memory_End_Byte_addr			0x37ff
#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
        #define DEBUG_Data_Memory_End_Sector_addr		0x2e00
        #define DEBUG_Data_Memory_End_Byte_addr			0x2fff

//        #define DEBUG_Data_Memory_End_Sector_addr		0x3600
//        #define DEBUG_Data_Memory_End_Byte_addr		0x37ff
#endif
#ifdef DEBUG_STC89C_LE51RC                        //STC89C51RC,        89LE51RC
        #define DEBUG_AP_Memory_Begin_Sector_addr		0x0000
        #define DEBUG_AP_Memory_End_Sector_addr			0x0e00
        #define DEBUG_AP_Memory_End_Byte_addr			0x0fff

        #define DEBUG_Data_Memory_Begin_Sector_addr		0x1000
        #define DEBUG_Data_Memory_End_Sector_addr		0x1e00
        #define DEBUG_Data_Memory_End_Byte_addr			0x1fff

//        #define DEBUG_Data_Memory_End_Sector_addr		0x3600
//        #define DEBUG_Data_Memory_End_Byte_addr		0x37ff
#endif
/* 测试常量数组 */


INT8U sector_erase(INT16U sector_addr);  //扇区擦除

INT8U Byte_flash(INT16U byte_addr, INT8U original_data);  //字节编程并校验

/* 写数据进数据Flash存储器, 只在同一个扇区内写,不保留原有数据	*/
/* begin_addr,被写数据Flash开始地址;counter,连续写多少个字节; array[],数据来源	*/
INT8U Sequential_write_flash(INT16U begin_addr, INT16U counter, INT8U array[]);

/* 写数据进数据Flash存储器(EEPROM), 只在同一个扇区内写,保留同一扇区中不需修改的数据	*/
/* begin_addr,被写数据Flash开始地址;counter,连续写多少个字节; array[],数据来源		*/
INT8U Write_flash(INT16U begin_addr, INT16U counter, INT8U array[]);

INT8U Read_flash(INT16U begin_addr, INT16U counter, INT8U array[]);   //读FLASH

void delay(INT16U counter);   //延时

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -