2013-04-18 124 views
0

這是這裏的後續工作 - Writting data to the Arduino's onboard EEPROM 我剛剛嘗試在URL中使用片段,但不起作用。請幫我解決下面的錯誤。將數據寫入Arduino EEPROM

write_to_eeprom.cpp:8:5: error: expected unqualified-id before '[' token 
write_to_eeprom.cpp: In function 'void setup()': 
write_to_eeprom.cpp:12:16: error: 'stringToWrite' was not declared in this scope 
write_to_eeprom.cpp: In function 'void loop()': 
write_to_eeprom.cpp:22:33: error: invalid conversion from 'uint8_t {aka unsigned char}' to 'char*' [-fpermissive] 
write_to_eeprom.cpp: In function 'void EEPROM_write(void*, byte)': 
write_to_eeprom.cpp:32:32: error: 'void*' is not a pointer-to-object type 

下面是代碼

#include <EEPROM.h> 
#include <LiquidCrystal.h> 
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7); 
char[] stringToWrite = "Test"; 
void setup() { 
    lcd.begin(16, 2); 
    delay(5000); 
    EEPROM_write(stringToWrite, strlen(stringToWrite)); 
} 

void loop() { 
    delay(10000); 
    int addr = 0; 
    byte datasize = EEPROM.read(addr++); 
    char stringToRead[0x20];   // allocate enough space for the string here! 
    char * readLoc = stringToRead; 
    for (int i=0;i<datasize; i++) { 
    readLoc = EEPROM.read(addr++); 
    readLoc++; 
    } 
} 
// Function takes a void pointer to data, and how much to write (no other way to know) 
// Could also take a starting address, and return the size of the reach chunk, to be more generic 
void EEPROM_write(void * data, byte datasize) { 
    int addr = 0; 
    EEPROM.write(addr++, datasize); 
    for (int i=0; i<datasize; i++) { 
    EEPROM.write(addr++, data[i]); 
    } 
} 

回答

0

那麼,你需要修復您的代碼:

線8 - []需要去後stringToWrite 線12 - 應該得到更好的固定行8

第22行 - 您需要解除引用readLoc。在它之前添加一個'*'。

第32行 - 你的參數「data」是指向void的指針,它沒有大小。因此,您將無法將其用作數組。你可以改變聲明:

無效EEPROM_write(字符 *數據字節命令datasize)

修復該編譯器錯誤。快速瀏覽一下代碼的語義,似乎是在做你想做的事情。祝你好運。