Atmega8A uart spi eeprom

时间:2014-06-11 09:24:46

标签: c++ avr avr-gcc

每个人,我想在spi eeprom编写并存储我的字符串,然后从spi eeprom读回并通过uart在终端中显示。我已经按照[1]中的步骤进行了操作:http://ww1.microchip.com/downloads/en/DeviceDoc/21822E.pdf。但它似乎只能显示一个字母。我不知道另一封信是否保存在spi eeprom中。我希望有人可以帮助我。

我正在使用: 芯片:Atmega8a 软件:avr studio 5 终端:Bray终端。

    #include <avr/io.h>
    #include <util/delay.h>

    void serial_init(void)
    {
        UBRRH = 0x00;
        UBRRL = 95;
        UCSRB =  (1 << RXEN) | (1 << TXEN) | (1<<RXCIE); 
        UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0)|(1 << UCSZ1); 
    }
    void SPI_MasterInit(void)
     {
        DDRB = 0b00101100;
        DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS);  
        SPCR = 0b01010000;
        SPSR = 0b00000001;
     } 
     char spi_transfer(volatile char data)
     {
         SPDR = data;
         while(!(SPSR & (1<<SPIF)));
         {
         }
         return SPDR;
     }
void SPI_MasterTransmit(unsigned long data)   
{   
    unsigned long address;
    DDR_SPI &= ~(1<<DD_SS);   //ss goes low 
    spi_transfer(WREN);  //enable write operation
    DDR_SPI |= (1<<DD_SS);   //ss goes high

    _delay_ms(10);

    DDR_SPI &= ~(1<<DD_SS);   //ss goes low             
    spi_transfer(WRITE); // write data to memory                                
    spi_transfer(address>>8);     // address MSB first
    spi_transfer(address);
    spi_transfer(data);           // send lsb
    DDR_SPI |= (1<<DD_SS);   //ss goes high 

}int resetEEPROM()
{
    DDR_SPI &= ~(1<<DD_SS);                // Select EEPROM
    spi_transfer(WREN);   // Send WRITE_ENABLE command
    DDR_SPI |= (1<<DD_SS);                // Release EEPROM
    DDR_SPI &= ~(1<<DD_SS);                 // Select EEPROM again after WREN cmd
    spi_transfer(WRDI);     // send CHIP_ERASE command
    DDR_SPI |= (1<<DD_SS);                // Release EEPROM
    return 0;
} // END eraseEEPROM()

unsigned long SPI_MasterReceive(unsigned long address)  //terima data  //read address
{
    unsigned long data;
    DDR_SPI &= ~(1<<DD_SS);   //ss goes low
    spi_transfer(READ);  //enable write operation   
    spi_transfer(address>>8);     // address MSB first
    spi_transfer(address);
    data = spi_transfer(0xff); 
    DDR_SPI |= (1<<DD_SS);   //goes high
    return data;
}
int main(void)
 {
        long int data;
    unsigned long address;
    serial_init();
    SPI_MasterInit();
    resetEEPROM();  
    data = Usart_Receive();

    while (1)
    {
        if (Usart_Receive() == '.')
            {
            USART_Print("\r\nStore\r\n");
                        SPI_MasterTransmit(data);   //store in spi eeprom
                        }
                if (Usart_Receive() == '>')
        {
            USART_Print("\nout \r\n");
            data = SPI_MasterReceive(address);  //read data from the memory
            Usart_Transmit(data);
        }

    }     
    return 0;   
}

2 个答案:

答案 0 :(得分:0)

有一种方法可以同时向EEPROM写入多个字节,但是您的代码不会这样做。相反,每个写操作写一个字节,并且始终在同一地址。您将使用每个新字节覆盖任何先前的字节。

如果要存储多个字节,则需要在写入时更改地址,或者一次更改写入多个字节的方式。 (请注意,如果它们是EEPROM存储器的同一页,则只能写入多个字节。)

答案 1 :(得分:0)

  

也许是循环缓冲区?

这是我的循环缓冲区代码。基于此http://www.rn-wissen.de/index.php/UART_mit_avr-gcc

    #include <avr/io.h> 
    #include <fifo.h> 
    #define FIFOBUF_SIZE 128
    uint8_t fifobuf[FIFOBUF_SIZE]; 
    fifo_t fifo; 


    ISR (USART_RXC_vect) 
    { 
       _inline_fifo_put(&fifo, UDR); 
    } 

    void serial_init(void) 
    { 
       cli(); 
       UBRRH = 0x00; 
       UBRRL = 95; 
       UCSRB = (1 << RXCIE) | (1 << RXEN) | (1 << TXEN); 
       UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);  
       sei(); 
    } 

    void fifo_init (fifo_t *f, uint8_t * buffer, const uint8_t size)
    {
        f-> count = 0;
        f-> pread = f-> pwrite = buffer;
        f-> read2end = f-> write2end = f-> size = size;
    }
    static inline int Usart_Transmit (const uint8_t c)
    {
        PORTD= 0b00000100;   //RTS Enable 
         while ((UCSRA & (1 << UDRE)) ==0) {};
        UDR = c;
        PORTD= 0b00000000;   //RTS Enable 
        return 1;
    }


    int main(void) 
{
   unsigned long data; 
   unsigned long address; 
   fifo_init(&fifo, fifobuf, FIFOBUF_SIZE); 
   serial_init();    
   while (1) 
   { 
   SPI_MasterInit(); 
   resetEEPROM();    
   SPI_MasterTransmit(Usart_Receive()); 
   _delay_ms(100);
   if (fifo.count > 0)            //; fifo.count >8 ; fifo.count 
         {    
         Usart_Transmit(_inline_fifo_get(&fifo)); 
         }       
   data = SPI_MasterReceive(address);  //read data from the memory 
   _delay_ms(100);
   Usart_Transmit(data);    
   }    
   return 0;    
}

它出来了所有的字母,但没有按照顺序。像" bfabeaabbfcabf "这样的示例,我只输入" abcdef "

您能告诉我如何在spi EEPROM中设置EEPROM地址。像例如给我看一些关于这个spi EEPROM地址的链接或示例。我请求你的善意帮助我,因为我已经在互联网上搜索了大约两个月,关于如何处理spi EEPROM地址的例子很少。我刚刚发现了ATmega EEPROM,LTD。他们所有人都没有给我一个好结果。提前感谢您的时间。 :)