我正在尝试从EEPROM(微控制器ATmega2560)写入和读取数据,这给了我错误的答案。当我调试它时,尽管看到数据正在写入不同的地址,但我看到的只是读取了最后一个字符。

在uiAddress = 1时,数据为A,在uiAddress = 2时,数据为B,uiAddress = 3 data = 67'C',依此类推。因此,当您从uiAddress = 0读取到最后一个地址时,应该得到ABCDE。我正在一次读取一个字符。

EESAVE已启用。

为什么会这样呢? (我尝试包含尽可能多的代码,原始文件太大。但这是相关的领域)。

#include<avr/io.h>
#include<avr/eeprom.h>
#include<avr/interrupt.h>

volatile UINT intrs, i = 1, count, switch_pressed = 0, uiAdd, uiAddEnd, flag_led_intr;
volatile UINT record, play_recorded_keys, flag_serial_receiver;

volatile unsigned char get_switch=0, data, TX_complete, TX, RX;

extern void __vector_25 (void) __attribute__ ((signal)); //Interrupt vector

#define LED_DELAY 10

#define F_CPU 2000000L

#define BAUDRATE 9600

#define BAUD_PRESCALER (((F_CPU/(BAUDRATE * 16UL)))-1)

void ReadWriteSerialPort(void)
{
    while(((UCSR0A) & (1<<UDRE0)) == 0)
        ;

    UDR0 = RX;

    if(RX == 0x1A) //CRTL-z
    {
        record = !record;
        play_recorded_keys = 0;
    }
    else
        if(RX == 0x19) //CRTL-y
        {
            record = 0;
            uiAdd = 0;
            play_recorded_keys = !play_recorded_keys;
        }

    if(record == 1)
    {
        EEPROM_write(uiAdd++, RX);
    }

    if(uiAdd == 4096)
    {
        record = 0;
        uiAddEnd = 4096;
    }
    else
        uiAddEnd = uiAdd;
}

void initialize(void)
{
    cli();        //Stop all interrupts

    flag_led_intr = 0;
    record = 0;
    play_recorded_keys = 0;
    RX = 0;
    TX = 0;
    flag_serial_receiver = 0;

    uiAdd = 0;
    uiAddEnd = 0;

    enable_ports();
    usart_init();

    sei();
}

void enable_ports() //Enables PORTB, PORTD
{
    DDRB = 0xff;  //PORTB as output for leds

    PORTB = 0xff; //Initialize PORTB

    DDRD = 0x00;  //PORTD as input for switches
}

void usart_init(void) //Enables USART
{
   /* Set baud rate */

   UBRR0L = BAUD_PRESCALER);
   UBRR0H = (BAUD_PRESCALER>>8);

   /* Set frame format: 8 bit data + start bit + stop bit */

   UCSR0C = 0x06;

   /* Enable reciever and transmitter */

   UCSR0B = 0x98;
}

void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
    while(EECR & (1<<EEPE));    /* Wait for completion of previous write */

        EEARH = (uiAddress>>8); /* Set up address and Data Registers */
        EEARL = uiAddress;

        EEDR = ucData;
        cli();
        EECR |= (1<<EEMPE);     /* Write logical one to EEMPE */

        EECR |= (1<<EEPE);      /* Start eeprom write by setting EEPE */
        sei();
}
unsigned char EEPROM_read(unsigned int uiAddress)
{
        while(EECR & (1<<EEPE)); /* Wait for completion of previous write */

        EEARH = (uiAddress>>8);  /* Set up address register */
        EEARL = uiAddress;

        EECR |= (1<<EERE);       /* Start eeprom read by writing EERE */

        return EEDR;             /* Return data from Data Register */
}

void __vector_25 (void)
{
    RX = UDR0;
    flag_serial_receiver = 1;
    sei();
}

int main(void)
{
   initialize();

   while(1)
   {
        if(flag_serial_receiver == 1)
        {
            ReadWriteSerialPort();
            flag_serial_receiver = 0;
        }

        if(play_recorded_keys)
        {
            TX = EEPROM_read(uiAdd);
            uiAdd++;

            if(uiAdd == 4096 || uiAdd >= uiAddEnd)
            {
                play_recorded_keys = 0;
                uiAdd = 0;
            }
            while(((UCSR0A) & (1<<UDRE0)) == 0)
                ;
            UDR0 = TX;
        }
    }
    return(0);
}

最佳答案

我敢打赌您的写或读或两者都没有发生。请使用调试器或调试输出,根据芯片数据手册手动检查配置寄存器的内容,以确保您按照注释中的ruslik正确构造了标志位。

一个有用的测试是在调用读取之前将EEDR设置为意外的测试值。如果读取返回此意外值,则说明您实际上并未读取数据,而只是获得了过时的EEDR值。这可能是由于未设置正确的标志,或者可能是需要等待读取完成但没有这样做。

您也可以尝试按顺序播放或进行读写操作-例如,以递增顺序书写,但以递减顺序读出。

尝试构建可揭示各种错误的测试,我相信您会很快发现它。

关于c - AVR EEPROM读写,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4412111/

10-12 20:42