参考范例程序。
eeprom.c
#include "eeprom.h"
/*----------------------------
Disable ISP/IAP/EEPROM function
Make MCU in a safe state
----------------------------*/
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
}
/*----------------------------
Read one byte from ISP/IAP/EEPROM area
Input: addr (ISP/IAP/EEPROM address)
Output:Flash data
----------------------------*/
BYTE IapReadByte(WORD addr)
{
BYTE 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
}
/*----------------------------
Program one byte to ISP/IAP/EEPROM area
Input: addr (ISP/IAP/EEPROM address)
dat (ISP/IAP/EEPROM data)
Output:-
----------------------------*/
void IapProgramByte(WORD addr, BYTE 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();
}
/*----------------------------
Erase one sector area
Input: addr (ISP/IAP/EEPROM address)
Output:-
----------------------------*/
void IapEraseSector(WORD 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();
}
eeprom.h
#ifndef _EEPROM_H_
#define _EEPROM_H_
#include "stc12c5a60s2.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
/*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 0x83 //if SYSCLK<12MHz
//Start address for STC12C5A60S2 EEPROM - 两个扇区
#define IAP_ADDRESS 0x0000 // 第一扇区
#define IAP_SECOND_ADDRESS 0x0200 // 第二扇区
// 函数声明
extern BYTE IapReadByte(WORD addr);
extern void IapProgramByte(WORD addr, BYTE dat);
extern void IapEraseSector(WORD addr);
#endif
STC12C5A60S2 只有两个扇区,每个扇区有 512 个字节。
测试读取功能:
dat = IapReadByte(IAP_ADDRESS);
dat2 = IapReadByte(IAP_ADDRESS+0x0001);
printf("dat = %bx , dat2 = %bx.\r\n",dat, dat2); // 若以十进制形式读取用 %bu , 若以十六进制则 %bx
测试写入功能:
IapProgramByte(IAP_ADDRESS,0x32);
IapProgramByte(IAP_ADDRESS+0x0001, 0x52);
dat = IapReadByte(IAP_ADDRESS);
dat2 = IapReadByte(IAP_ADDRESS+0x0001);
printf("dat = %bx , dat2 = %bx.\r\n",dat, dat2);
测试擦除功能:
注意:如果擦除将擦除整个扇区。文章来源:https://www.toymoban.com/news/detail-605482.html
IapEraseSector(IAP_ADDRESS);
dat = IapReadByte(IAP_ADDRESS);
dat2 = IapReadByte(IAP_ADDRESS+0x0001);
printf("dat = %bx , dat2 = %bx.\r\n",dat, dat2);
运行结果如下:
文章来源地址https://www.toymoban.com/news/detail-605482.html
到了这里,关于STC12C5A系列单片机内部 EEPROM 的应用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!