单片机成长之路(51基础篇) - 021 STC89C51系列单片机 内部EEPROM 驱动
最近又看了一下关于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 驱动的更多相关文章
- 单片机成长之路(51基础篇) - 022 N76e003 APROM模拟EEPROM驱动
N76e003单片机内部没有EEPROM,但是可以使用 APROM模拟EEPROM功能,代码如下: eeprom.h #ifndef _EEPROM_H_ #define _EEPROM_H_ //E ...
- 单片机成长之路(51基础篇) - 002 STC单片机冷启动和复位有什么区别
STC单片机简介 STC单片机是一款增强型51单片机,完全兼容MCS-51,还增加了新的功能,比如新增两级中断优先级,多一个外中断,内置EEPROM,硬件看门狗,具有掉电模式,512B内存等.还支持I ...
- 单片机成长之路(51基础篇) - 015 关于sdcc的多文件编译范例二
本文是续 单片机成长之路(51基础篇) - 009 关于sdcc的多文件编译范例(一)编写的. 在实际的工作中,单片机的头文件和功能函数不可能同全部放在同一个文件夹下面,我们把单片机成长之路(51基础 ...
- 单片机成长之路(51基础篇) - 009 关于sdcc的多文件编译范例(一)
本文是续 单片机成长之路(51基础篇) - 006 在Linux下搭建51单片机的开发烧写环境编写的. 本范例主要由(main.c ,delay.h,delay.c,makefile)4个文件组成,s ...
- (转)Python成长之路【第九篇】:Python基础之面向对象
一.三大编程范式 正本清源一:有人说,函数式编程就是用函数编程-->错误1 编程范式即编程的方法论,标识一种编程风格 大家学习了基本的Python语法后,大家就可以写Python代码了,然后每个 ...
- 单片机成长之路(51基础篇) - 026 基于stm89c52之单片机看门狗
基于stc89c52的看门狗,代码如下: main.c #include "stc89c5x_Quick_configuration.h" // 自定义头文件 #include & ...
- 单片机成长之路(avr基础篇)- 001 ISP与IAP的区别
ISP(In-System Programming)在系统可编程,指电路板上的空白器件可以编程写入最终用户代码, 而不需要从电路板上取下器件,已经编程的器件也可以用ISP方式擦除或再编程.IAP(In ...
- Sass进阶之路,之一(基础篇)
Sass 学习Sass之前,应该要知道css预处理器这个东西,css预处理器是什么呢? Css预处理器定义了一种新的语言将Css作为目标生成文件,然后开发者就只要使用这种语言进行编码工作了.预处理器通 ...
- python成长之路第三篇(1)_初识函数
目录: 函数 为什么要使用函数 什么是函数 函数的返回值 文档化函数 函数传参数 文件操作(二) 1.文件操作的步骤 2.文件的内置方法 函数: 一.为什么要使用函数 在日常写代码中,我们会发现有很多 ...
随机推荐
- ContentProvider工作原理
--摘自<android插件化开发指南> 1.系统管理类app,比如手机助手,有机会频繁使用ContentProvider 2.通讯录或者短信数据,是以ContentProvider的形式 ...
- 大数据项目之_15_电信客服分析平台_03&04_数据分析
3.3.数据分析3.3.1.Mysql 表结构设计3.3.2.需求:按照不同的维度统计通话3.3.3.环境准备3.3.4.编写代码:数据分析3.3.5.运行测试3.3.6.bug 解决 3.3.数据分 ...
- 移动端滑屏全应用【四】移动端动画贞动画函数mTween封装
首先此函数是基于大家都知道的Tween动画算法的,在此基础上使用了三中讲到的兼容版动画贞,可以使动画变得更流畅. 1. 首先要记得引入Tween.js 2. 引入mTween.js 3. 调用 * m ...
- angular笔记_2
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- [OC] @property时,copy、strong、weak、assign的区别
@property(copy,nonatomic)NSMutableString*copyStr; @property(strong,nonatomic)NSMutableString*strongS ...
- SQL总结——存储过程
SQL总结(五)存储过程 概念 存储过程(Stored Procedure):已预编译为一个可执行过程的一个或多个SQL语句. 创建存储过程语法 CREATE proc | procedure pro ...
- [python] 查询mysql返回datetime类型数据的处理
Python 查询Mysql,如果是datetime类型,在json序列化的时候会出现问题. 在网上查了一下,解决方案基本都是遍历dict数据,如果是datetime则转化为字符串. from dat ...
- 在mongoose中使用正则,参数为变量时的写法
const word = ctx.params.word userModel.find({"name":{$regex: eval(`/${word}/ig`)}}) 如果mong ...
- MySql中的事务、JDBC事务、事务隔离级别
一.MySql事务 之前在Oracle中已经学习过事务了,这个东西就是这个东西,但是在MySql中用法还是有一点不同,正好再次回顾一下. 先看看MySql中的事务,默认情况下,每执行一条SQL语句,都 ...
- git常用命令2--- git rebase
git rebase:简单而言就是把某个分支上的提交commit嫁接到另一个commit的后面,在这个过程中这些commit的base相对就改变了,也就叫变基. git rebase <upst ...