每個人,我想寫我的字符串並存儲在spi eeprom中,然後從spi eeprom讀回,並通過uart在終端中顯示。我已經按照[1]中的步驟操作:http://ww1.microchip.com/downloads/en/DeviceDoc/21822E.pdf。但它似乎只能顯示一個字母。我不知道另一封信是否保存在spi eeprom中。我希望有一個人可以幫助我。Atmega8A uart 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;
}
你能指點我的一些示例代碼的方向,讓我開始瞭解地址? 例如這是否與它有關: 'for(i = 0; i <100; i ++){}' 謝謝 – OooO
我剛纔查看了您提供的數據表。你的代碼很好地保存了一個字節。您現在需要做的是決定如何管理EEPROM。也許是循環緩衝區?我不知道你的計劃。在任何情況下,您都需要跟蹤要寫入下一個位置的變量,以及您想要從中讀取的位置。當你寫時,增加地址變量。如果您希望在設備關閉時保持該值,那麼您甚至可以將該值保存在EEPROM本身中,可能在第一個地址。 – UncleO
我無法在評論部分發布整個編碼,因此我將其發佈在答案部分。 – OooO