最近又看了一下关于stc单片机的知识,感觉在使用中EEPROM是个经常用到的东西,特地学习了一下,给大家分享一下心得,如有不足,多多包涵,废话不多说,一图解千言,先上图:

单片机成长之路(51基础篇) - 021 STC89C51系列单片机 内部EEPROM  驱动-LMLPHP

/*--------------------------------------------------------------分界线-----------------------------------------------------------------------------------*/

main_eepprom.c

 #include <STC89C5xRC.H>

 #ifdef EEPROM
#include "eeprom.h"
#endif # define LEDALL P1 void Delay(int n){
unsigned int x;
while (n--) {
x = ;
while (++x);
}
} void main(){
char i;
unsigned char temp,temp0,temp1;
while (){
for(i=; i<; i++){
temp0 = STC_EEPROM_assign_Read(,);
temp1 = STC_EEPROM_assign_Read(,);
temp = (temp0 %)+((temp1/)*);
LEDALL = ~temp;
Delay();
temp = 0x11<<i;
temp0 = temp %;
temp1 = (temp / )*;
SCT_EEPROM_assign_Write(,,temp0);
SCT_EEPROM_assign_Write(,,temp1);
temp = ;
}
}
}

Eeprom.h

 /*--------------------------------------------------------------------------
eeprom.h --------------------------------------------------------------------------*/ #ifndef __EEPROM_H__
#define __EEPROM_H__
#include "intrins.h" /*声明与IAP相关的SFR寄存器 */
sfr IAP_DATA = 0xE2; //闪存数据寄存器
sfr IAP_ADDRH = 0xE3; //Flash地址为HIGH
sfr IAP_ADDRL = 0xE4; //Flash地址为LOW
sfr IAP_CMD = 0xE5; //Flash命令寄存器
sfr IAP_TRIG = 0xE6; //Flash命令触发器
sfr IAP_CONTR = 0xE7; //闪存控制寄存器
/*宏定义 ISP/IAP/EEPROM 命令*/
#define EEPROM_CMD_IDLE 0 //支持
#define EEPROM_CMD_READ 1 //字节读
#define EEPROM_CMD_PROGRAM 2 //字节编程
#define EEPROM_CMD_ERASE 3 //扇区擦除
/*IAP_CONTR定义ISP / IAP / EEPROM 时钟操作常量*/
#define EEPROM_ENABLE_IAP 0x80 //开启IAP功能
//Clock 时钟配置
#define Clock_20M_40M 0x00 //if SYSCLK<40MHz
#define Clock_10M_20M 0x01 //if SYSCLK<20MHz
#define Clock_5M_10M 0x02 //if SYSCLK<10MHz
#define Clock_0M_5M 0x03 //if SYSCLK<5MHz //EEPROM的起始地址
#define EEPROM_BASE_ADDRESS 0x0000
//STC89C51系列
#define STC89x51_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x01000 //STC89C/LE51
#define STC89x52_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x02000 //STC89C/LE52
#define STC89x53_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x03000 //STC89C/LE53
#define STC89x54_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x04000 //STC89C/LE54
#define STC89x58_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x08000 //STC89C/LE58
#define STC89x510_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x0A000 //STC89C/LE510
#define STC89x512_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x0C000 //STC89C/LE512
#define STC89x514_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x0E000 //STC89C/LE514
//STC89C51RC/RD+系列
#define STC89x51RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x01000 //STC89C/LE51RC/RD+
#define STC89x52RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x02000 //STC89C/LE52RC/RD+
#define STC89x54RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x04000 //STC89C/LE54RC/RD+
#define STC89x58RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x08000 //STC89C/LE58RC/RD+
#define STC89x510RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x0A000 //STC89C/LE510RC/RD+
#define STC89x512RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x0C000 //STC89C/LE512RC/RD+
#define STC89x514RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x0E000 //STC89C/LE514RC/RD+
//STC90C51系列
#define STC90x51_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x01000 //STC90C/LE51
#define STC90x52_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x02000 //STC90C/LE52
#define STC90x54_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x04000 //STC90C/LE54
#define STC90x58_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x08000 //STC90C/LE58
#define STC90x510_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x0A000 //STC90C/LE510
#define STC90x512_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x0C000 //STC90C/LE512
#define STC90x514_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x0E000 //STC90C/LE514
//STC90C51RC/RD+系列
#define STC90x51RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x01000 //STC90C/LE51RC/RD+
#define STC90x52RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x02000 //STC90C/LE52RC/RD+
#define STC90x12RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x03000 //STC90C/LE52RC/RD+
#define STC90x54RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x04000 //STC90C/LE54RC/RD+
#define STC90x58RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x08000 //STC90C/LE58RC/RD+
#define STC90x510RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x0A000 //STC90C/LE510RC/RD+
#define STC90x512RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x0C000 //STC90C/LE512RC/RD+
#define STC90x514RC_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x0E000 //STC90C/LE514RC/RD+
//STC12xx052AD系列
#define STC12xx052AD_EEPROM_Start_ADDRESS EEPROM_BASE_ADDRESS+0x00000 //STC12xx052AD //EEPROM SIZE
#define Sector_size 0x0200
//STC89C51系列
#define STC89x51_EEPROM_Sector_Number 18
#define STC89x52_EEPROM_Sector_Number 10
#define STC89x53_EEPROM_Sector_Number 4
#define STC89x54_EEPROM_Sector_Number 90
#define STC89x58_EEPROM_Sector_Number 58
#define STC89x510_EEPROM_Sector_Number 44
#define STC89x512_EEPROM_Sector_Number 28
#define STC89x514_EEPROM_Sector_Number 12
//STC89C51RC/RD+系列
#define STC89x51RC_EEPROM_Sector_Number 8
#define STC89x52RC_EEPROM_Sector_Number 8
#define STC89x54RC_EEPROM_Sector_Number 90
#define STC89x58RC_EEPROM_Sector_Number 58
#define STC89x510RC_EEPROM_Sector_Number 42
#define STC89x512RC_EEPROM_Sector_Number 26
#define STC89x514RC_EEPROM_Sector_Number 10
//STC90C51系列
#define STC90x51_EEPROM_Sector_Number 10
#define STC90x52_EEPROM_Sector_Number 10
#define STC90x54_EEPROM_Sector_Number 90
#define STC90x58_EEPROM_Sector_Number 58
#define STC90x510_EEPROM_Sector_Number 42
#define STC90x512_EEPROM_Sector_Number 26
#define STC90x514_EEPROM_Sector_Number 10
//STC89C51RC/RD+系列
#define STC90x51RC_EEPROM_Sector_Number 18
#define STC90x52RC_EEPROM_Sector_Number 10
#define STC90x12RC_EEPROM_Sector_Number 2
#define STC90x54RC_EEPROM_Sector_Number 90
#define STC90x58RC_EEPROM_Sector_Number 58
#define STC90x510RC_EEPROM_Sector_Number 42
#define STC90x512RC_EEPROM_Sector_Number 26
#define STC90x514RC_EEPROM_Sector_Number 10 //STC89C51RC/RD+系列
#define STC90x51RC_EEPROM_Sector_Number 18
#define STC90x52RC_EEPROM_Sector_Number 10
#define STC90x12RC_EEPROM_Sector_Number 2
#define STC90x54RC_EEPROM_Sector_Number 90
#define STC90x58RC_EEPROM_Sector_Number 58
#define STC90x510RC_EEPROM_Sector_Number 42
#define STC90x512RC_EEPROM_Sector_Number 26
#define STC90x514RC_EEPROM_Sector_Number 10 //STC12xx052AD系列
#define STC12xx052AD_EEPROM_Sector_Number 20 //EEPROM end
//STC89C51系列
#define STC89x51_EEPROM_End_ADDRESS STC89x51_EEPROM_Start_ADDRESS+Sector_size*(STC89x51_EEPROM_Sector_Number)-1
#define STC89x52_EEPROM_End_ADDRESS STC89x52_EEPROM_Start_ADDRESS+Sector_size*(STC89x52_EEPROM_Sector_Number)-1
#define STC89x53_EEPROM_End_ADDRESS STC89x53_EEPROM_Start_ADDRESS+Sector_size*(STC89x53_EEPROM_Sector_Number)-1
#define STC89x54_EEPROM_End_ADDRESS STC89x54_EEPROM_Start_ADDRESS+Sector_size*(STC89x54_EEPROM_Sector_Number)-1
#define STC89x58_EEPROM_End_ADDRESS STC89x58_EEPROM_Start_ADDRESS+Sector_size*(STC89x58_EEPROM_Sector_Number)-1
#define STC89x510_EEPROM_End_ADDRESS STC89x510_EEPROM_Start_ADDRESS+Sector_size*(STC89x510_EEPROM_Sector_Number)-1
#define STC89x512_EEPROM_End_ADDRESS STC89x512_EEPROM_Start_ADDRESS+Sector_size*(STC89x512_EEPROM_Sector_Number)-1
#define STC89x514_EEPROM_End_ADDRESS STC89x514_EEPROM_Start_ADDRESS+Sector_size*(STC89x514_EEPROM_Sector_Number)-1
//STC89C51RC/RD+系列
#define STC89x51RC_EEPROM_End_ADDRESS STC89x51RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x51RC_EEPROM_Sector_Number)-1
#define STC89x52RC_EEPROM_End_ADDRESS STC89x52RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x52RC_EEPROM_Sector_Number)-1
#define STC89x54RC_EEPROM_End_ADDRESS STC89x54RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x54RC_EEPROM_Sector_Number)-1
#define STC89x58RC_EEPROM_End_ADDRESS STC89x58RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x58RC_EEPROM_Sector_Number)-1
#define STC89x510RC_EEPROM_End_ADDRESS STC89x510RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x510RC_EEPROM_Sector_Number)-1
#define STC89x512RC_EEPROM_End_ADDRESS STC89x512RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x512RC_EEPROM_Sector_Number)-1
#define STC89x514RC_EEPROM_End_ADDRESS STC89x514RC_EEPROM_Start_ADDRESS+Sector_size*(STC89x514RC_EEPROM_Sector_Number)-1
//STC90C51系列
#define STC90x51_EEPROM_End_ADDRESS STC90x51_EEPROM_Start_ADDRESS+Sector_size*(STC90x51_EEPROM_Sector_Number)-1
#define STC90x52_EEPROM_End_ADDRESS STC90x52_EEPROM_Start_ADDRESS+Sector_size*(STC90x52_EEPROM_Sector_Number)-1
#define STC90x54_EEPROM_End_ADDRESS STC90x54_EEPROM_Start_ADDRESS+Sector_size*(STC90x54_EEPROM_Sector_Number)-1
#define STC90x58_EEPROM_End_ADDRESS STC90x58_EEPROM_Start_ADDRESS+Sector_size*(STC90x58_EEPROM_Sector_Number)-1
#define STC90x510_EEPROM_End_ADDRESS STC90x510_EEPROM_Start_ADDRESS+Sector_size*(STC90x510_EEPROM_Sector_Number)-1
#define STC90x512_EEPROM_End_ADDRESS STC90x512_EEPROM_Start_ADDRESS+Sector_size*(STC90x512_EEPROM_Sector_Number)-1
#define STC90x514_EEPROM_End_ADDRESS STC90x514_EEPROM_Start_ADDRESS+Sector_size*(STC90x514_EEPROM_Sector_Number)-1
//STC90C51RC/RD+系列
#define STC90x51RC_EEPROM_End_ADDRESS STC90x51RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x51RC_EEPROM_Sector_Number)-1
#define STC90x52RC_EEPROM_End_ADDRESS STC90x52RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x52RC_EEPROM_Sector_Number)-1
#define STC90x12RC_EEPROM_End_ADDRESS STC90x12RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x12RC_EEPROM_Sector_Number)-1
#define STC90x54RC_EEPROM_End_ADDRESS STC90x54RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x54RC_EEPROM_Sector_Number)-1
#define STC90x58RC_EEPROM_End_ADDRESS STC90x58RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x58RC_EEPROM_Sector_Number)-1
#define STC90x510RC_EEPROM_End_ADDRESS STC90x510RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x510RC_EEPROM_Sector_Number)-1
#define STC90x512RC_EEPROM_End_ADDRESS STC90x512RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x512RC_EEPROM_Sector_Number)-1
#define STC90x514RC_EEPROM_End_ADDRESS STC90x514RC_EEPROM_Start_ADDRESS+Sector_size*(STC90x514RC_EEPROM_Sector_Number)-1
//STC12xx052AD系列
#define STC12xx052AD_EEPROM_End_ADDRESS STC12xx052AD_EEPROM_Start_ADDRESS+Sector_size*(STC12xx052AD_EEPROM_Sector_Number)-1 /*-----------set eeprom parameter-----------*/
//STC89C51系列
#ifdef STC89x51
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x51_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x51_EEPROM_Start_ADDRESS + Sector_size*(STC89x51_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x51_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x52
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x52_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x52_EEPROM_Start_ADDRESS + Sector_size*(STC89x52_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x52_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x53
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x53_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x53_EEPROM_Start_ADDRESS + Sector_size*(STC89x53_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x53_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x54
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x54_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x54_EEPROM_Start_ADDRESS + Sector_size*(STC89x54_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x54_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x58
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x58_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x58_EEPROM_Start_ADDRESS + Sector_size*(STC89x58_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x58_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x510
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x510_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x510_EEPROM_Start_ADDRESS + Sector_size*(STC89x510_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x510_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x512
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x512_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x512_EEPROM_Start_ADDRESS + Sector_size*(STC89x512_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x512_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x514
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x514_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x514_EEPROM_Start_ADDRESS + Sector_size*(STC89x514_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x514_EEPROM_End_ADDRESS //eeprom结束地址
#endif
//STC89C51RC系列
#ifdef STC89x51RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x51RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x51RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x51RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x51RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x52RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x52RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x52RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x52RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x52RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x54RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x54RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x54RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x54RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x54RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x58RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x58RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x58RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x58RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x58RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x510RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x510RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x510RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x510RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x510RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x512RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x512RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x512RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x512RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x512RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC89x514RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC89x514RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC89x514RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x514RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC89x514RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
//STC90C51系列
#ifdef STC90x51
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x51_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x51_EEPROM_Start_ADDRESS + Sector_size*(STC90x51_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x51_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x52
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x52_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x52_EEPROM_Start_ADDRESS + Sector_size*(STC90x52_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x52_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x54
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x54_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x54_EEPROM_Start_ADDRESS + Sector_size*(STC90x54_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x54_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x58
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x58_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x58_EEPROM_Start_ADDRESS + Sector_size*(STC90x58_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x58_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x510
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x510_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x510_EEPROM_Start_ADDRESS + Sector_size*(STC90x510_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x510_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x512
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x512_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x512_EEPROM_Start_ADDRESS + Sector_size*(STC90x512_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x512_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x514
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x514_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x514_EEPROM_Start_ADDRESS + Sector_size*(STC90x514_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x514_EEPROM_End_ADDRESS //eeprom结束地址
#endif
//STC90C51RC系列
#ifdef STC90x51RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x51RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x51RC_EEPROM_Start_ADDRESS + Sector_size*(STC90x51RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x51RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x52RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x52RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x52RC_EEPROM_Start_ADDRESS + Sector_size*(STC90x52RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x52RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x12RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x12RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x12RC_EEPROM_Start_ADDRESS + Sector_size*(STC90x12RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x12RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x54RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x54RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x54RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x54RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x54RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x58RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x58RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x58RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x58RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x58RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x510RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x510RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x510RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x510RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x510RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x512RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x512RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x512RC_EEPROM_Start_ADDRESS + Sector_size*(STC89x512RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x512RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
#ifdef STC90x514RC
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC90x514RC_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC90x514RC_EEPROM_Start_ADDRESS + Sector_size*(STC90x514RC_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC90x514RC_EEPROM_End_ADDRESS //eeprom结束地址
#endif
//STC12xx052AD系列
#if defined(STC12xx052AD) || defined(STC12x1052AD) || defined(STC12x2052AD) || defined(STC12x4052AD) || defined(STC12x5052AD)
#define ENABLE_EEPROM EEPROM_ENABLE_IAP+Clock_10M_20M //开启IAP,配置时钟
#define EEPROM_Start_ADDRESS STC12xx052AD_EEPROM_Start_ADDRESS //eeprom起始地址
#define EEPROM_buf_ADDRESS STC12xx052AD_EEPROM_Start_ADDRESS + Sector_size*(STC12xx052AD_EEPROM_Sector_Number -1) //eeprom起始地址
#define EEPROM_end_ADDRESS STC12xx052AD_EEPROM_End_ADDRESS //eeprom结束地址
#endif /*----------------函数-----------------*/
/*-----------eeprom------------*/
//Eeprom_Unlock(解锁)
void Eeprom_Unlock(void);
//Eeprom_Locked(上锁)
void Eeprom_Locked(void);
//Eeprom_Effective(配置生效)
void Eeprom_Effective(void);
//EEPROM区域读取一个字节
unsigned char EEPROM_Read_Byte(unsigned int addr);
//将一个字节编程到EEPROM区域
void EEPROM_Write_Byte(unsigned int addr, unsigned char dat);
//擦除一个扇区
void EEPROM_Erase_sector(unsigned int addr);
//读取指定地址数据
unsigned char EEPROM_assign_Read(unsigned char Sector,unsigned int position);
//写入指定地址数据
void EEPROM_assign_Write(unsigned char Sector,unsigned int position,unsigned char value);
/************************************************************/
//字节对比
bit Compared(unsigned char Evalue,unsigned char value);
//stc 读取指定地址数据
unsigned char STC_EEPROM_assign_Read(unsigned char Sector,unsigned int position);
//stc 写入指定地址数据
void SCT_EEPROM_assign_Write(unsigned char Sector,unsigned int position,unsigned char value); #endif

Eeprom.c

 #include "eeprom.h"

 /*-----------eeprom------------*/
//-------------------------------
//Eeprom_Unlock(解锁)
//Input: on
//Output:on
//-------------------------------
void Eeprom_Unlock(void)
{
IAP_CONTR = ENABLE_EEPROM; //打开IAP功能,并设置等待时间
}
//-------------------------------
//Eeprom_Locked(上锁)
//Input: on
//Output:on
//-------------------------------
void Eeprom_Locked(void)
{
IAP_CONTR = ; //关闭IAP功能
IAP_CMD = ; //清除命令到待机状态
IAP_TRIG = ; //清除触发寄存器
IAP_ADDRH = 0x80; //数据ptr指向非EEPROM区域
IAP_ADDRL = ; //清除IAP地址以防止滥用
}
//-------------------------------
//Eeprom_Effective(配置生效)
//Input: on
//Output:on
//-------------------------------
void Eeprom_Effective(void)
{
IAP_TRIG = 0x46; //发送触发器命令1(0x46)
IAP_TRIG = 0xb9; //发送触发器命令2(0xb9)
_nop_(); //MCU将保持在此处,直到ISP / IAP / EEPROM操作完成
}
//-------------------------------
//EEPROM区域读取一个字节
//Input: addr 16位EEPROM地址
//Output:on
//-------------------------------
unsigned char EEPROM_Read_Byte(unsigned int addr)
{
unsigned char dat; //数据缓冲区
Eeprom_Unlock();
IAP_CMD = EEPROM_CMD_READ; //设置ISP / IAP / EEPROM READ命令
IAP_ADDRL = addr; //将ISP / IAP / EEPROM地址低8位设置
IAP_ADDRH = addr >> ; //将ISP / IAP / EEPROM地址高8位设置
Eeprom_Effective(); //生效
dat = IAP_DATA; //读取ISP / IAP / EEPROM数据
Eeprom_Locked(); //关闭ISP / IAP / EEPROM功能
return dat; //返回Flash数据
}
//-------------------------------
//将一个字节编程到EEPROM区域
//Input: addr 16位EEPROM地址
// value 8位数据
//Output:on
//-------------------------------
void EEPROM_Write_Byte(unsigned int addr, unsigned char value)
{
Eeprom_Unlock();
IAP_CMD = EEPROM_CMD_PROGRAM; //设置EEPROM PROGRAM命令
IAP_ADDRL = addr; //将EEPROM地址低8位设置
IAP_ADDRH = addr >> ; //将EEPROM地址高8位设置
IAP_DATA = value; //数据写入寄存器
Eeprom_Effective(); //生效
Eeprom_Locked();
}
//-------------------------------
//擦除EEPROM一个扇区
//Input: addr 16位EEPROM地址
//Output:on
//-------------------------------
void EEPROM_Erase_sector(unsigned int addr)
{
Eeprom_Unlock();
IAP_CMD = EEPROM_CMD_ERASE; //Set ISP/IAP/EEPROM ERASE command
IAP_ADDRL = addr; //Set ISP/IAP/EEPROM address low
IAP_ADDRH = addr >> ; //Set ISP/IAP/EEPROM address high
Eeprom_Effective(); //生效
Eeprom_Locked();
}
//-------------------------------
//读取指定地址数据
//Input: Sector 扇区编号
// position 扇区内相对地址
//Output:指定位置的数据
//-------------------------------
unsigned char EEPROM_assign_Read(unsigned char Sector,unsigned int position){
return EEPROM_Read_Byte(EEPROM_Start_ADDRESS + Sector*Sector_size + position);
}
//-------------------------------
//写入指定地址数据
//Input: Sector 扇区编号
// position 扇区内相对地址
// value 写入指定位置的数据
//Output:on
//-------------------------------
void EEPROM_assign_Write(unsigned char Sector,unsigned int position,unsigned char value){
EEPROM_Write_Byte(EEPROM_Start_ADDRESS + Sector*Sector_size + position,value); //鉴定是否需要擦除
}
/*--------------------------------------------------------------------*/
//-------------------------------
//写入指定地址数据
//Input: Evalue EEPROM读取的数据
// value 新的数据
//Output:0 符合,直接写入
// 1要进行数据处理
//-------------------------------
bit Compared(unsigned char Evalue,unsigned char value){
unsigned char i;
unsigned char date = ;
bit Ev;
bit V;
for(i = ; i < ; i++){
Ev = (Evalue & (0x01 << i));
V = (value & (0x01 << i));
if((Ev == )&&(V == ))return ;
}
return ;
}
//-------------------------------
//stc 读取指定地址数据
//Input: Sector 扇区编号
// position 扇区内相对地址
//Output:指定位置的数据
//-------------------------------
unsigned char STC_EEPROM_assign_Read(unsigned char Sector,unsigned int position){
return EEPROM_assign_Read(Sector,position);
}
//stc 写入指定地址数据(根据STC单片机特点,对EEPROM写入进行特别的处理)
void SCT_EEPROM_assign_Write(unsigned char Sector,unsigned int position,unsigned char value){
int count;
if( == Compared(STC_EEPROM_assign_Read(Sector,position),value))EEPROM_assign_Write(Sector,position,value); //鉴定是否需要擦除
else {
EEPROM_Erase_sector(EEPROM_buf_ADDRESS); //清洗EEPROM缓存区
for(count = ; count < Sector_size;count++){
if(position == count)EEPROM_Write_Byte( EEPROM_buf_ADDRESS+position,value); //替换数据
else EEPROM_Write_Byte(EEPROM_buf_ADDRESS + count,EEPROM_assign_Read(Sector,count)); //复制数据
}
EEPROM_Erase_sector(EEPROM_Start_ADDRESS + Sector*Sector_size); //擦除一个扇区(起始地址)
for(count = ; count < Sector_size;count++)EEPROM_assign_Write(Sector,count,EEPROM_Read_Byte(EEPROM_buf_ADDRESS + count));
}
}

本代码支持只stc89/90系列自带EEPROM的单片机。

  

05-11 12:59