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

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

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的单片机。

  

单片机成长之路(51基础篇) - 021 STC89C51系列单片机 内部EEPROM 驱动的更多相关文章

  1. 单片机成长之路(51基础篇) - 022 N76e003 APROM模拟EEPROM驱动

    N76e003单片机内部没有EEPROM,但是可以使用 APROM模拟EEPROM功能,代码如下: eeprom.h #ifndef _EEPROM_H_ #define _EEPROM_H_ //E ...

  2. 单片机成长之路(51基础篇) - 002 STC单片机冷启动和复位有什么区别

    STC单片机简介 STC单片机是一款增强型51单片机,完全兼容MCS-51,还增加了新的功能,比如新增两级中断优先级,多一个外中断,内置EEPROM,硬件看门狗,具有掉电模式,512B内存等.还支持I ...

  3. 单片机成长之路(51基础篇) - 015 关于sdcc的多文件编译范例二

    本文是续 单片机成长之路(51基础篇) - 009 关于sdcc的多文件编译范例(一)编写的. 在实际的工作中,单片机的头文件和功能函数不可能同全部放在同一个文件夹下面,我们把单片机成长之路(51基础 ...

  4. 单片机成长之路(51基础篇) - 009 关于sdcc的多文件编译范例(一)

    本文是续 单片机成长之路(51基础篇) - 006 在Linux下搭建51单片机的开发烧写环境编写的. 本范例主要由(main.c ,delay.h,delay.c,makefile)4个文件组成,s ...

  5. (转)Python成长之路【第九篇】:Python基础之面向对象

    一.三大编程范式 正本清源一:有人说,函数式编程就是用函数编程-->错误1 编程范式即编程的方法论,标识一种编程风格 大家学习了基本的Python语法后,大家就可以写Python代码了,然后每个 ...

  6. 单片机成长之路(51基础篇) - 026 基于stm89c52之单片机看门狗

    基于stc89c52的看门狗,代码如下: main.c #include "stc89c5x_Quick_configuration.h" // 自定义头文件 #include & ...

  7. 单片机成长之路(avr基础篇)- 001 ISP与IAP的区别

    ISP(In-System Programming)在系统可编程,指电路板上的空白器件可以编程写入最终用户代码, 而不需要从电路板上取下器件,已经编程的器件也可以用ISP方式擦除或再编程.IAP(In ...

  8. Sass进阶之路,之一(基础篇)

    Sass 学习Sass之前,应该要知道css预处理器这个东西,css预处理器是什么呢? Css预处理器定义了一种新的语言将Css作为目标生成文件,然后开发者就只要使用这种语言进行编码工作了.预处理器通 ...

  9. python成长之路第三篇(1)_初识函数

    目录: 函数 为什么要使用函数 什么是函数 函数的返回值 文档化函数 函数传参数 文件操作(二) 1.文件操作的步骤 2.文件的内置方法 函数: 一.为什么要使用函数 在日常写代码中,我们会发现有很多 ...

随机推荐

  1. Jenkins使用遇到的问题总结

    学新技能最方便的就是在网上找教程了,我找到一个还不错的易百教程 Jenkins教程 别看教程写了一堆,其实真正用到的没多少.后面的自动化测试,服务器维护啥的,等用到的时候再深入研究吧 No1: 按照教 ...

  2. 发布xxl-job executor dotnet core 执行器的实现

    DotXxlJob [github][https://github.com/xuanye/DotXxlJob] xxl-job的dotnet core 执行器实现,支持XXL-JOB 2.0+ 1 X ...

  3. 003.DNS主从正反解析部署

    一 实验环境 1.1 实验需求 配置正向解析bind 配置反向解析bind 配置辅助dns的bind 实现主辅dns之间的区域传送 1.2 环境规划 主dns:CentOS6.8-01 172.24. ...

  4. 记录初学者学习Hive时踩过的坑

    1. 缺少MySQL驱动包 1.1 问题描述 Caused by: org.datanucleus.store.rdbms.connectionpool.DatastoreDriverNotFound ...

  5. swap

    添加交换分区 SWAP(交换)分区是一种通过在硬盘中预先划分一定的空间,然后将把内存中暂时不常用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,其设计目的是为了解决真实物理 ...

  6. asp.net core选项Options模块的笔记

    这篇博客是写给自己看的.已经不止一次看到AddOptions的出现,不管是在.net core源码还是别人的框架里面,都充斥着AddOptions.于是自己大概研究了下,没有深入,因为,我的功力还是不 ...

  7. sudo passwd root输入普通用户密码后显示用户不再sudoers文件中

    在写上一篇VirtualBox创建共享文件夹的时候,在运行下图授权时,root密码一直输入错误 然后我就在终端输入 su root,却发现需要密码,但我却不知道密码是什么 于是我就在终端输入如下命令, ...

  8. 【Excel】将IP按照IP地址(v4)增长序列排序

    Background: Excel列中,有多个net-block, 将这些net-block按照IP地址(v4)自己的大小从小到大排序. Idea: IPv4地址的格式是点分十进制的,也就是说每一个点 ...

  9. Jmeter5 实现多机集群压测(局域网组成多机集群)

    想要模拟高并发用户访问的场景,用Jmeter5实现的话,单靠一台PC机,资源是不够的,包括单机的内存.使用端口数量等,所以最好是通过多台PC机组成几个集群来对服务器进行压测. 本文目录: 1.软硬件配 ...

  10. C语言基础四(敲打键盘、寻找资料,循环语句)请一个个字读,助于您的学会机率

    循环语句无非几种,常用的就有if()else()结构类型的,while(){}类型的,do(){}while(1);类型,switch()类型,for()类型. 而这章就将前面的所有知识全部汇总下运用 ...