之前發(fā)了一篇,舉了一個Arduino讀寫EEPROM的例子, 可以讀寫EEPROM里存儲的短整型數(shù)據(jù)。
但是現(xiàn)實當(dāng)中,我們需要在EEPROM里面存儲的東西可不止短整型這一種,可能是char,可能是long,可能是array,甚至可能是一個struct。 那難道需要針對每一種數(shù)據(jù)類型做一個讀寫函數(shù)?
這里我做了一套讀寫函數(shù),可以適應(yīng)任何一種數(shù)據(jù)類型,甚至可以寫入結(jié)構(gòu)體數(shù)據(jù)。 事實上在實際應(yīng)用中,結(jié)構(gòu)體的使用,極大的方便了需要保存數(shù)據(jù)變量多、類型多的情況。 不過需要小心的是,如果需要寫入EEPROM的數(shù)據(jù)比較多,要注意是否會超出EEPROM的size,還要考慮寫入時間。我沒有實際試驗過,但是我覺得Arduino寫入數(shù)據(jù)到EEPROM應(yīng)該不是非常快。
如下的例子里面,我在EEPROM里面寫入一個結(jié)構(gòu)體數(shù)據(jù),然后在 loop() 里面讀出來。
定義的 EEPROM_write_short, EEPROM_read_short, EEPROM_clear_all 實際上并沒有使用。 EEPROM_write_short, EEPROM_read_short 的功能在之前的一篇里面舉過例子了。 而 EEPROM_clear_all 的作用是清空EEPROM的全部內(nèi)容。
例子里面的方法,主要使用的技術(shù)是變量指針,和強制類型轉(zhuǎn)換,不明白的話,需要去啃一下C語言的書了。
C++語言: 高亮代碼由發(fā)芽網(wǎng)提供
//
// EEPROM rw Testing
//
#include "EEPROM.h"
#define EEPROM_START_ADDRESS 0 // Start Address in EEPROM
#define EEPROM_SIZE 1024 // EEPROM size
// Define data structure for eeprom storage
typedef struct _eeprom_storage
{
unsigned char a;
unsigned short b;
char c[4];
unsigned long d;
} EEPROM_storage;
EEPROM_storage e2_memory;
void setup()
{
Serial.begin(9600);
// Clear all the data in EEPROM
//EEPROM_clear_all(EEPROM_SIZE);
//while(1)
//{
// Serial.println("Clear");
//}
e2_memory.a = 250;
e2_memory.b = 257;
e2_memory.c[0] = 'a';
e2_memory.c[1] = 'b';
e2_memory.c[2] = 'c';
e2_memory.d = 4294967295;
// write the struct data to EEPROM
EEPROM_write_block((unsigned char*)&e2_memory, EEPROM_START_ADDRESS, sizeof(EEPROM_storage));
}
void loop()
{
// read the data struct from EEPROM
EEPROM_read_block((unsigned char*)&e2_memory, EEPROM_START_ADDRESS, sizeof(EEPROM_storage));
delay(500);
Serial.println(e2_memory.a);
Serial.println(e2_memory.b);
Serial.println(e2_memory.c);
Serial.println(e2_memory.d);
}
void EEPROM_write_block(unsigned char *memory_block, unsigned int start_address, unsigned int block_size)
{
unsigned char Count = 0;
for (Count=0; Count<</SPAN>block_size; Count++)
{
EEPROM.write(start_address + Count, memory_block[Count]);
}
}
void EEPROM_read_block(unsigned char *memory_block, unsigned int start_address, unsigned int block_size)
{
unsigned char Count = 0;
for (Count=0; Count<</SPAN>block_size; Count++)
{
memory_block[Count]= EEPROM.read(start_address + Count);
//Serial.println((unsigned int)(memory_block[Count])); delay(400);
}
}
void EEPROM_clear_all(unsigned int eeprom_size)
{
unsigned int Count = 0;
unsigned char data = 0;
for (Count=0; Count<</SPAN>eeprom_size; Count++)
{
EEPROM.write(Count, data);
}
}
// Write an uint value to EEPROM
void EEPROM_write_short(unsigned int Address, unsigned int Data)
{
unsigned int DataL = Data&0x00FF;
unsigned int DataH = Data>>8;
EEPROM.write(Address, DataL);
EEPROM.write(Address+1, DataH);
}
// Read an uint value from EEPROM
unsigned int EEPROM_read_short(unsigned int Address)
{
unsigned int DataL = EEPROM.read(Address);
unsigned int DataH = EEPROM.read(Address+1);
return((DataH<<8) + DataL);
}
// EEPROM rw Testing
//
#include "EEPROM.h"
#define EEPROM_START_ADDRESS
#define EEPROM_SIZE
// Define data structure for eeprom storage
typedef struct _eeprom_storage
{
}
EEPROM_storage
void setup()
{
}
void loop()
{
}
void EEPROM_write_block(unsigned char *memory_block, unsigned int start_address, unsigned int block_size)
{
}
void EEPROM_read_block(unsigned char *memory_block, unsigned int start_address, unsigned int block_size)
{
}
void EEPROM_clear_all(unsigned int eeprom_size)
{
}
// Write an uint value to EEPROM
void EEPROM_write_short(unsigned int Address, unsigned int Data)
{
}
// Read an uint value from EEPROM
unsigned int EEPROM_read_short(unsigned int Address)
{
}