请教ATmega16中,EEPROM断电后为什么数据不能保存????
[复制链接]
下面是我写的小程序,往EEPROM中0X02写入0X08:
#include
#include
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
delay()
{
int j;
for(j=0;j++;j<=2)
{;}
}
main()
{
char a,b,c;
a=b=c=0;
init_devices();
while(c==0)
{ EEARH=0X00;
if((EECR&0X02)==0)
{ if((SPMCR&0X00)==0)
{ EEARL=0X02;
EEDR=0X08;
EECR|=0X04;
EECR|=0X02; //写入0X08
a=1;
}
}
if(a==1)
{ if((EECR&0X02)==0)
{ EEARL=0X02;
EECR|=0X02;
b=EEDR; //读0X02值,赋给b
c=3;
}
}
}
while(b==0x08)
{ PORTA = 0x00;
}
while(1)
{
}
}
用仿真器监控,程序很正常,EEPROM中0X02位置也由FF变为08,PA脚也拉低了。
再往AVR灌入另一程序,检测EEPROM数据是否断电后保存:
#include
#include
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0xFF;
PORTB = 0xFF;
DDRB = 0xFF;
//PORTC = 0x00; //m103 output only
//DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
delay()
{
int j;
for(j=0;j++;j<=10)
{;}
}
main()
{
char a,b,c;
a=b=c=0;
init_devices();
if((EECR&0X02)==0)
{ EEARL=0X02;
EECR|=0X02;
b=EEDR; //读0X02值,赋给b
}
delay();
while(b==0x08)
{ PORTA = 0x02;
}
while(1)
{
}
}
但奇怪的是,仿真器一监控就看到EEPROM中0X02位置一直是FF,PA口也不拉低,设断点,发现执行完delay后,直接跳入最后的死循环。
请问各位,到底是哪里出现了问题呢???