求教ATMEGA48对EEPROM写数据出错的问题
对EEPROM写四个字节的数据时,只有第一个字节写进去了,其他都没写进去,仿真单步调试时四个字节都可以写进去,全速运行就只有一个字节写进去了,不明白什么原因,我的写函数如下:void EEPROM_writeword(unsigned char uiaddress,unsigned char uidata)
{
while( (EECR & 0x02) == 1 );
EEARL = uiaddress;
EEDR = uidata;
EECR |= 0x04;
EECR |= 0x02;
}
/*********************************************************************************
* 函数名 : void EEPROM_write(unsigned int *Data,unsigned int Length,unsigned char SatatIntByte)
* 描述 : 向EEPROM中写入数据(写入本系统中所有需要掉电保存的数据)
* 输入 : *Data:需要写入数据的指针Length:需要写入数据的长度(1字节长度为1)
SatatIntByte:写入数据地址的偏移量(不包括总偏移量,1为偏移1个字节,即一个字节的长度)
* 输出 : None
* 返回值 : None
* 调用 :
* 其它 : None
*********************************************************************************/
void EEPROM_write(unsigned char *Data,unsigned char Length,unsigned char SatatIntByte)
{
cli();
unsigned char address;
for(address = SatatIntByte;address < (SatatIntByte+Length);address++)
{
EEPROM_writeword(address,*(Data+(address-SatatIntByte)));
}
sei();
}
看你的程序,没发现什么问题。
如果用gcc的话建议你直接 #include <avr/eeprom.h> zeta1989 发表于 2012-11-7 10:31 static/image/common/back.gif
看你的程序,没发现什么问题。
如果用gcc的话建议你直接 #include
我用的是STUDIO4.18 avrstudio+winavr的话,可以用avr/eeprom.h
然后直接调用eeprom_readbyte()和eeprom_writebyte()就可以了,很安全的 zeta1989 发表于 2012-11-7 10:49 static/image/common/back.gif
avrstudio+winavr的话,可以用avr/eeprom.h
然后直接调用eeprom_readbyte()和eeprom_writebyte()就可以了, ...
那个应该是可以,但解决不了这个问题,谢谢 CLI();
EECR|=4;
EECR|=2;
SEI();
这样试下 中断给打断了吧 刚没仔细看。好像中断在写之前都是关了的。看起来好像没问题。应该是延时问题吧。加点延时。 lalapunk1983 发表于 2012-11-7 15:15 static/image/common/back.gif
刚没仔细看。好像中断在写之前都是关了的。看起来好像没问题。应该是延时问题吧。加点延时。 ...
延时也加了,改成这样试了下还是不行;
cli();
unsigned char address;
unsigned int timer,i;
for(address = SatatIntByte;address < (SatatIntByte+Length);address++)
{
EEPROM_writeword(address,*(Data+(address-SatatIntByte)));
timer = 60000;
while(timer--)
{
i=1000;
while(i--)
{}
}
} 问题解决了,还是程序写错了, while( (EECR & 0x02) == 1 );
这个判断语句错了
页:
[1]