stc12c5a60s2 eeprom程序

HDL语言及源代码

6人已加入

描述

  于stc自带EEPROM来说,用起来唯一不太方便的地方就是每次修改任何一个字节需要将整个扇区都擦除才能修改,所以每次修改都要讲整个扇区内容用数组全部读出缓存起来,进行擦除操作,讲需要修改的内容在相应的数组位置修改好,再讲整个数组内容重新写入到EEPROM里,头文件中我已经将接口函数都写好啦。直接调用就行,不用考虑任何问题,而且地址也直接从0开始到 USING_CHAR_NUM-1(USING_CHAR_NUM最大可到512)

  #ifndef __EEPROM_H__

  #define __EEPROM_H__

  unsigned char EepromReadByte(unsigned char addr);

  void EepromWriteByte(unsigned char addr, unsigned char dat);

  #endif#include “intrins.h”

  typedef unsigned char BYTE;

  typedef unsigned int WORD;

  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 CMD_IDLE 0 //Stand-By

  #define CMD_READ 1 //Byte-Read

  #define CMD_PROGRAM 2 //Byte-Program

  #define CMD_ERASE 3 //Sector-Erase

  #define ENABLE_IAP 0x82 //if SYSCLK《20MHz

  //Start address for STC12C5A60S2 series EEPROM

  #define IAP_ADDRESS 0x0000

  #define USING_CHAR_NUM 11

  void IapIdle();

  BYTE IapReadByte(WORD addr);

  void IapProgramByte(WORD addr, BYTE dat);

  void IapEraseSector(WORD addr);

  unsigned char EepromReadByte(unsigned char addr)

  {

  return(IapReadByte(addr));

  }

  void EepromWriteByte(unsigned char addr, unsigned char dat)

  {

  unsigned char eepromBUF[USING_CHAR_NUM];

  unsigned char i;

  for(i=0; i《 USING_CHAR_NUM; i++)

  {

  eepromBUF[IAP_ADDRESS+i] = IapReadByte(IAP_ADDRESS+i);

  }

  if(addr 《 USING_CHAR_NUM)

  {

  eepromBUF[IAP_ADDRESS+addr] = dat;

  IapEraseSector(IAP_ADDRESS);

  for(i=0; i《 USING_CHAR_NUM; i++)

  {

  IapProgramByte(IAP_ADDRESS+i, eepromBUF[IAP_ADDRESS+i]);

  }

  }

  }

  //void EepromInit(void)

  //{

  // unsigned int tmp = 0;

  // _nop_();

  // _nop_();

  // _nop_();

  // tmp = IapReadByte(IAP_ADDRESS+1);

  // tmp = (tmp 《《 8) + IapReadByte(IAP_ADDRESS);

  // if(0xffff == tmp)

  // {

  // IapProgramByte(IAP_ADDRESS,(BYTE)0);

  // IapProgramByte(IAP_ADDRESS+1,(BYTE)0);

  // tmp = 0;

  // }

  // tmp++;

  // IapEraseSector(IAP_ADDRESS);

  // IapProgramByte(IAP_ADDRESS,(BYTE)tmp );

  // IapProgramByte(IAP_ADDRESS+1,(BYTE)(tmp》》8) );

  //

  //}

  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

  }

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

  }

  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_(); _nop_(); //MCU will hold here until ISP/IAP/EEPROM

  //operation complete

  IapIdle();

  }

  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_(); _nop_(); //MCU will hold here until ISP/IAP/EEPROM

  //operation complete

  IapIdle();

  }

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分