stc12c5a60s2 eeprom程序

stc12c5a60s2 eeprom程序,第1张

  TC12C5A60S2/AD/PWM系列单片机是单时钟/机器周期(1T)的单片机,是高速/低功耗/超强抗干扰的新一代8051单片机,指令代码完全兼容传统8051,但速度快8-12倍。内部集成MAX810专用复位电路,2路PWM,8路高速10位A/D转换(250K/S),针对电机控制,强干扰场合。

  #include “reg51.h”

  #include “intrins.h”

  #define CMD_IDLE 0

  #define CMD_READ 1

  #define CMD_PROGRAM 2

  #define CMD_ERASE 3

  //#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

  #define IAP_ADDRESS 0x0000

  typedef unsigned char BYTE;

  typedef unsigned int WORD;

  sfr IAP_DATA = 0xC2;

  sfr IAP_ADDRH = 0xC3;

  sfr IAP_ADDRL = 0xC4;

  sfr IAP_CMD = 0xC5;

  sfr IAP_TRIG = 0xC6;

  sfr IAP_CONTR = 0xC7;

  void Delay(BYTE n);

  void IapIdle();

  void IapWriteByte(WORD addr, BYTE dat);

  void IapEraseSector(WORD addr);

  BYTE IapReadByte(WORD addr);

  void main()

  {

  WORD i;

  P1 = 0xfe;

  Delay(10);

  IapEraseSector(IAP_ADDRESS);

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

  {

  if (IapReadByte(IAP_ADDRESS+i) != 0xff)

  goto Error;

  }

  P1 = 0xfc;

  Delay(10);

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

  {

  IapWriteByte(IAP_ADDRESS+i, (BYTE)i);

  }

  P1 = 0xf8;

  Delay(10);

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

  {

  if (IapReadByte(IAP_ADDRESS+i) != (BYTE)i)

  goto Error;

  }

  P1 = 0xf0;

  while (1);

  Error:

  P1 &= 0x7 f;

  while (1);

  }

  void Delay(BYTE n)

  {

  WORD x; while (n--)

  {

  x = 0;

  while (++x);

  }

  }

  void IapIdle() //退出IAP功能

  {

  IAP_CONTR = 0;

  IAP_CMD = 0;

  IAP_TRIG = 0;

  IAP_ADDRH = 0x80;

  IAP_ADDRL = 0;

  }

  BYTE IapReadByte(WORD addr) //读EEPROM

  {

  BYTE dat;

  IAP_CONTR = ENABLE_IAP;

  IAP_CMD = CMD_READ;

  IAP_ADDRL = addr;

  IAP_ADDRH = addr 》》 8;

  IAP_TRIG = 0x5a;

  IAP_TRIG = 0xa5;

  _nop_();

  dat = IAP_DATA;

  IapIdle();

  return dat;

  }

  void IapWriteByte(WORD addr, BYTE dat) //写EEPROM

  {

  IAP_CONTR = ENABLE_IAP;

  IAP_CMD = CMD_PROGRAM;

  IAP_ADDRL = addr;

  IAP_ADDRH = addr 》》 8;

  IAP_DATA = dat;

  IAP_TRIG = 0x5a;

  IAP_TRIG = 0xa5;

  _nop_();

  IapIdle();

  }

  void IapEraseSector(WORD addr) // 擦除EEPROM扇区

  {

  IAP_CONTR = ENABLE_IAP;

  IAP_CMD = CMD_ERASE;

  IAP_ADDRL = addr;

  IAP_ADDRH = addr 》》 8;

  IAP_TRIG = 0x5a;

  IAP_TRIG = 0xa5;

  _nop_();

  IapIdle();

  }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/dianzi/2596841.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-08-09
下一篇 2022-08-09

发表评论

登录后才能评论

评论列表(0条)

保存