STC12C5A系列单片机内部 EEPROM 的应用

STC12C5A系列单片机内部 EEPROM 的应用_第1张图片
STC12C5A系列单片机内部 EEPROM 的应用_第2张图片
STC12C5A系列单片机内部 EEPROM 的应用_第3张图片
参考范例程序。

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); 

测试擦除功能:
注意:如果擦除将擦除整个扇区。

IapEraseSector(IAP_ADDRESS);
dat = IapReadByte(IAP_ADDRESS);
dat2 = IapReadByte(IAP_ADDRESS+0x0001);
printf("dat = %bx , dat2 = %bx.\r\n",dat, dat2); 

运行结果如下:
STC12C5A系列单片机内部 EEPROM 的应用_第4张图片

你可能感兴趣的:(单片机,嵌入式硬件)