AVR EEPROM读写

时间:2010-12-10 18:35:51

标签: c avr-gcc

我正在尝试从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);
}

2 个答案:

答案 0 :(得分:2)

在AVR DATA BOOK中找到的代码中添加了两个宏。

#define sbi(port,bit)  __asm__ __volatile__ ( "sbi %0, %1" :: "I" (_SFR_IO_ADDR(port)),"I" (bit))
#define cbi(port,bit)  __asm__ __volatile__ ( "cbi %0, %1" :: "I" (_SFR_IO_ADDR(port)),"I" (bit))

//Write data to EEPROM
void EEPROM_WRITE(unsigned int uiAddress, unsigned char ucData)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEPE));
/* Set up address and Data Registers */
EEAR = uiAddress;
EEDR = ucData;
/* Write logical one to EEMPE */
//EECR |= (1<<EEMPE);
sbi(EECR,EEMPE);
/* Start eeprom write by setting EEPE */
//EECR |= (1<<EEPE);
sbi(EECR,EEPE); //You need to set EEPE within four clock cycles to `enter code here`initiate writing.
}

将GCC与优化器设置-O0一起使用时间过长,因此超过4个时钟周期,因此不会写入。 一些简单的宏可以解决这个问题。

答案 1 :(得分:0)

我敢打赌,无论是你的写作还是阅读,两者都没有发生。请通过使用调试器或调试输出手动检查芯片数据表中的配置寄存器内容,验证您正在正确构造标志位,正如ruslik在注释中所暗示的那样。

一个有用的测试是在调用read之前将EEDR设置为意外的测试值。如果读取返回此意外值,您知道您实际上没有读过但只是获得了EEDR的陈旧值。这可能是由于没有设置正确的标志,或者可能需要等待读取完成但不这样做。

您也可以尝试使用订单或写入和读取 - 例如,按递增顺序写入但以递减方式读取。

尝试构建可以揭示不同类型错误的测试,我相信你会很快发现它。