gpt4 book ai didi

c - 按顺序向 EEPROM 写入和读取数据

转载 作者:行者123 更新时间:2023-12-03 03:30:43 24 4
gpt4 key购买 nike

我正在做我的大学项目,需要将数据存储在AtMega32的EEPROM中。我能够在内存的任何特定位置写入和读取数据。但是当我尝试从地址 0 到 1023 顺序写入数据时,我得到了错误的值。

这是我编写的函数。

读写数据的函数定义

#include "eeprom.h"

uint8_t EEPROMRead(uint16_t uiAddress)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE));
/* Set up address register */
EEAR = uiAddress;
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE);
/* Return data from data register */
return EEDR;
}

void EEPROMWrite(uint16_t uiAddress, uint8_t ucData)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE));
/* Set up address and data registers */
EEAR = uiAddress;
EEDR = ucData;
/* Write logical one to EEMWE */
EECR |= (1<<EEMWE);
/* Start eeprom write by setting EEWE */
EECR |= (1<<EEWE);
}

这是主要功能

static int epadr=0;
epread=EEPROMRead(epadr); //reading from address stored in epadr
printf("%d",epread); //printing values


if(epadr<=1023)
{
EEPROMWrite(epadr,high); //writing at address stored in epadr
epadr++; //increment address
}
}

if(epadr>1023)
printf("Memory Full\n");

我想存储位置0到1023的数据。请告诉我这段代码有什么问题。

最佳答案

无需定义自己的函数来读取和写入内部 EEPROM 中的数据。 AVR为此目的提供了库。这是示例代码:-

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/eeprom.h>

int main(void)
{
char read[5];

eeprom_write_byte (0, '0');
eeprom_write_byte (1, '1');
eeprom_write_byte (2, '2');
eeprom_write_byte (3, '3');
eeprom_write_byte (4, '4');

for (int count=0;count<5;count++)
{
read[count]=eeprom_read_byte((const uint8_t *)(count));
}

while (1);

}

关于c - 按顺序向 EEPROM 写入和读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40082368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com