STC12C5A60S2的片内EEPROM卡了一周了,请高手帮忙看下,感激不尽!
为了方便阅读,我只把关于EEPROM的代码贴出来。
#define CMD_READ 1
#define CMD_PROGRAM 2
#define CMD_ERASE 3
#define ENABLE_IAP 0x83
#define IAP_ADDRESS_1 0x0000
void Initialization()
{
DehumiTimeSet = IAP_Read_Byte(IAP_ADDRESS_1);
if(DehumiTimeSet > 99 || DehumiTimeSet < 1)
{
DehumiTimeSet = 48;
}
DehumiTimeSetBak = DehumiTimeSet;
}
void main()
{
Initialization();
while(1)
{
if(bit_Timer100ms) //中断2ms,计数器累加50次就是100ms
{
bit_Timer100ms = 0;
Display();
KeyDispose();
}
}
}
void KeyDispose()
{
if(Key_Value == 0) //60秒内无按键按下则显示DHT11温湿度
{
if(bit_NoKeyPress)
{
NoKeyPressDelayCnt++;
if(NoKeyPressDelayCnt >= 600)
{
NoKeyPressDelayCnt = 0;
if(DehumiTimeSetBak != DehumiTimeSet)
{
EEPROM_WriteDehumiTimeSet();
DehumiTimeSetBak = DehumiTimeSet;
}
}
}
}
}
void EEPROM_WriteDehumiTimeSet() //先擦除扇区,再写入数据
{
IAP_Erase_Sector(IAP_ADDRESS_1);
IAP_Write_Byte(IAP_ADDRESS_1,DehumiTimeSet);
}
void IAP_Idle()
{
IAP_CONTR = 0;
IAP_CMD = 0;
IAP_TRIG = 0;
IAP_ADDRH = 0x80;
IAP_ADDRL = 0;
}
unsigned char IAP_Read_Byte(unsigned int addr)
{
unsigned char dat;
IAP_CONTR = ENABLE_IAP; //允许IAP功能,Clock<12MHz
IAP_CONTR = CMD_READ; //读
IAP_ADDRL = addr;
IAP_ADDRH = addr >> 8;
IAP_TRIG = 0x5a;
IAP_TRIG = 0xa5;
_nop_();_nop_();_nop_();
dat = IAP_DATA;
IAP_Idle();
return dat;
}
void IAP_Write_Byte(unsigned int addr,unsigned char dat)
{
IAP_CONTR = ENABLE_IAP; //允许IAP功能
IAP_CMD = CMD_PROGRAM; //写
IAP_ADDRL = addr;
IAP_ADDRH = addr >> 8;
IAP_DATA = dat;
IAP_TRIG = 0x5a;
IAP_TRIG = 0xa5;
_nop_();_nop_();_nop_();
IAP_Idle();
}
void IAP_Erase_Sector(unsigned int addr)
{
IAP_CONTR = ENABLE_IAP; //允许IAP功能
IAP_CMD = CMD_ERASE; //擦除
IAP_ADDRL = addr;
IAP_ADDRH = addr >> 8;
IAP_TRIG = 0x5a;
IAP_TRIG = 0xa5;
_nop_();_nop_();_nop_();
IAP_Idle();
}
|