请教ATmega16中,EEPROM断电后为什么数据不能保存????

happy0755 2010-05-27 09:14:38
下面是我写的小程序,往EEPROM中0X02写入0X08:

#include <iom16v.h>
#include <macros.h>

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 <iom16v.h>
#include <macros.h>

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后,直接跳入最后的死循环。

请问各位,到底是哪里出现了问题呢???
...全文
576 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
maoy001 2011-07-29
  • 打赏
  • 举报
回复
学习学习了 继续关注
jldream110 2010-07-09
  • 打赏
  • 举报
回复
eeprom 烧程序的时候 单独烧 掉电不会没的
happy0755 2010-07-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 gwm1224 的回复:]
PORTC = 0x00; //m103 output only
DDRC = 0x00;
这条注释什么意思啊
[/Quote]

没意思,自动生成的。
gwm1224 2010-07-08
  • 打赏
  • 举报
回复
PORTC = 0x00; //m103 output only
DDRC = 0x00;
这条注释什么意思啊
lin0119 2010-05-27
  • 打赏
  • 举报
回复
很明显是你第二次把程序烧进AVR的时候把EEPROM也擦除了
Peasant_Lee 2010-05-27
  • 打赏
  • 举报
回复
首先,你确定你第一次已经写入,假如你不写入第二个程序,你去查看eeprom的数据,会丢失吗?
会不会是你写入第二个程序的时候,刷了EEPROOM,不是你想刷,而是编程器刷的,有没有这种可能?
jbb0523 2010-05-27
  • 打赏
  • 举报
回复
去年学习的AVR单片机,也弄过EEPROM,感觉一切正常,给楼主个参考,是我去年时写的吧,可以在ICCAVR论坛搜索我的帖子,应该还有关于EEPROM的。
http://www.iccavr.com/bbs/dispbbs.asp?boardid=2&Id=27589

27,374

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧