单片机片内存储器的功能操作头文件
存成eeprom52.h 。H试下吧,
- #ifndef _EERPOM52_H_
- #define _EEPROM52_H_
- #include <reg51.h>
- #include "intrins.h"
- #define nop() _nop_()
- #define uint8 unsigned char
- #define uint16 unsigned int
- /*Declare SFR associated with the IAP */
- sfr IAP_DATA = 0xC2; //Flash data register
- sfr IAP_ADDRH = 0xC3; //Flash address HIGH
- sfr IAP_ADDRL = 0xC4; //Flash address LOW
- sfr IAP_CMD = 0xC5; //Flash command register
- sfr IAP_TRIG = 0xC6; //Flash command trigger
- sfr IAP_CONTR = 0xC7; //Flash control register
- /*Define ISP/IAP/EEPROM command*/
- #define CMD_IDLE 0 //Stand-By
- #define CMD_READ 1 //Byte-Read
- #define CMD_PROGRAM 2 //Byte-Program
- #define CMD_ERASE 3 //Sector-Erase
- /*Define ISP/IAP/EEPROM operation const for IAP_CONTR*/
- //#define ENABLE_IAP 0x80 //if SYSCLK<30MHz
- #define ENABLE_IAP 0x81 //if SYSCLK<24MHz
- //#define ENABLE_IAP 0x82 //if SYSCLK<20MHz
- //#define ENABLE_IAP 0x83 //if SYSCLK<12MHz
- //#define ENABLE_IAP 0x84 //if SYSCLK<6MHz
- //#define ENABLE_IAP 0x85 //if SYSCLK<3MHz
- //#define ENABLE_IAP 0x86 //if SYSCLK<2MHz
- //#define ENABLE_IAP 0x87 //if SYSCLK<1MHz
- //Start address for STC11/10xx EEPROM
- //#define IAP_ADDRESS 0x0400
- //Start address for STC12C5A60S2 EEPROM
- #define IAP_ADDRESS 0x0000
- void EEPROMDelay(uint8 n)
- {
- uint16 x;
- while (n--)
- {
- x =125;
- while (x--);
- }
- }
- /*----------------------------
- * 名称 : IapIdle();
- * 功能 : 关闭IAP EEPROM编辑功能,安全操作
- ----------------------------*/
- void IapIdle()
- {
- IAP_CONTR = 0; //Close IAP function
- IAP_CMD = 0; //Clear command to standby
- IAP_TRIG = 0; //Clear trigger register
- IAP_ADDRH = 0x80; //Data ptr point to non-EEPROM area
- IAP_ADDRL = 0; //Clear IAP address to prevent misuse
- }
- /*----------------------------
- * 名称 : IapReadByte(0x0000);
- * 功能 : 读扇区
- ----------------------------*/
- uint8 IapReadByte(uint16 addr)
- {
- uint8 dat; //Data buffer
- IAP_CONTR = ENABLE_IAP; //Open IAP function, and set wait time
- IAP_CMD = CMD_READ; //Set ISP/IAP/EEPROM READ command
- IAP_ADDRL = addr; //Set ISP/IAP/EEPROM address low
- IAP_ADDRH = addr >> 8; //Set ISP/IAP/EEPROM address high
- IAP_TRIG = 0x5a; //Send trigger command1 (0x5a)
- IAP_TRIG = 0xa5; //Send trigger command2 (0xa5)
- nop(); //MCU will hold here until ISP/IAP/EEPROM operation complete
- dat = IAP_DATA; //Read ISP/IAP/EEPROM data
- IapIdle(); //Close ISP/IAP/EEPROM function
- return dat; //Return Flash data
- }
- /*----------------------------
- * 名称 : IapEraseSector(0x0000);
- * 功能 : 扇区擦除
- ----------------------------*/
- void IapEraseSector(uint16 addr)
- {
- IAP_CONTR = ENABLE_IAP; //Open IAP function, and set wait time
- IAP_CMD = CMD_ERASE; //Set ISP/IAP/EEPROM ERASE command
- IAP_ADDRL = addr; //Set ISP/IAP/EEPROM address low
- IAP_ADDRH = addr >> 8; //Set ISP/IAP/EEPROM address high
- IAP_TRIG = 0x5a; //Send trigger command1 (0x5a)
- IAP_TRIG = 0xa5; //Send trigger command2 (0xa5)
- nop(); //MCU will hold here until ISP/IAP/EEPROM operation complete
- IapIdle();
- }
- /*----------------------------
- * 名称 : IapProgramByte(0x0000,data);
- * 功能 : 扇区编辑
- ----------------------------*/
- void IapProgramByte(uint16 addr, uint8 dat)
- {
- IAP_CONTR = ENABLE_IAP; //Open IAP function, and set wait time
- IAP_CMD = CMD_PROGRAM; //Set ISP/IAP/EEPROM PROGRAM command
- IAP_ADDRL = addr; //Set ISP/IAP/EEPROM address low
- IAP_ADDRH = addr >> 8; //Set ISP/IAP/EEPROM address high
- IAP_DATA = dat; //Write ISP/IAP/EEPROM data
- IAP_TRIG = 0x5a; //Send trigger command1 (0x5a)
- IAP_TRIG = 0xa5; //Send trigger command2 (0xa5)
- nop(); //MCU will hold here until ISP/IAP/EEPROM operation complete
- IapIdle();
- }
- /*----------------------------
- * 名称 : IapProgramWord(0x0000,data);
- * 功能 : 十六位扇区编辑
- ----------------------------*/
- void IapProgramWord(uint16 addr, uint16 dat)
- {
- uint8 dat_L,dat_H;
- dat_L=dat;
- dat_H=dat >> 8;
- IAP_CONTR = ENABLE_IAP;
- IAP_CMD = CMD_PROGRAM;
- IAP_ADDRL = addr+0x0001;
- IAP_ADDRH = (addr+0x0001)>> 8;
- IAP_DATA = dat_H;
- IAP_TRIG = 0x5a;
- IAP_TRIG = 0xa5;
- nop();
- IapIdle();
- IAP_CONTR = ENABLE_IAP;
- IAP_CMD = CMD_PROGRAM;
- IAP_ADDRL = addr;
- IAP_ADDRH = addr >> 8;
- IAP_DATA = dat_L;
- IAP_TRIG = 0x5a;
- IAP_TRIG = 0xa5;
- nop();
- IapIdle();
- }
- /*----------------------------
- * 名称 : IapReadWord(0x0000);
- * 功能 : 读十六位扇区
- ----------------------------*/
- uint16 IapReadWord(uint16 addr)
- {
- uint16 dat; //Data buffer
- uint8 dat_H,dat_L;
- IAP_CONTR = ENABLE_IAP; //Open IAP function, and set wait time
- IAP_CMD = CMD_READ; //Set ISP/IAP/EEPROM READ command
- IAP_ADDRL = (addr+0x0001); //Set ISP/IAP/EEPROM address low
- IAP_ADDRH = (addr+0x0001) >> 8; //Set ISP/IAP/EEPROM address high
- IAP_TRIG = 0x5a; //Send trigger command1 (0x5a)
- IAP_TRIG = 0xa5; //Send trigger command2 (0xa5)
- nop(); //MCU will hold here until ISP/IAP/EEPROM operation complete
- dat_H = IAP_DATA; //Read ISP/IAP/EEPROM data
- IapIdle();
- IAP_CONTR = ENABLE_IAP; //Open IAP function, and set wait time
- IAP_CMD = CMD_READ; //Set ISP/IAP/EEPROM READ command
- IAP_ADDRL = addr; //Set ISP/IAP/EEPROM address low
- IAP_ADDRH = addr >> 8; //Set ISP/IAP/EEPROM address high
- IAP_TRIG = 0x5a; //Send trigger command1 (0x5a)
- IAP_TRIG = 0xa5; //Send trigger command2 (0xa5)
- nop(); //MCU will hold here until ISP/IAP/EEPROM operation complete
- dat_L= IAP_DATA; //Read ISP/IAP/EEPROM data
- IapIdle(); //Close ISP/IAP/EEPROM function
- dat=(dat_H << 8)+dat_L;
- return dat; //Return Flash data
- }
- #endif
复制代码
|