之前发了一篇,举了一个Arduino读写EEPROM的例子, 可以读写EEPROM里存储的短整型数据。
但是现实当中,我们需要在EEPROM里面存储的东西可不止短整型这一种,可能是char,可能是long,可能是array,甚至可能是一个struct。 那难道需要针对每一种数据类型做一个读写函数?
这里我做了一套读写函数,可以适应任何一种数据类型,甚至可以写入结构体数据。 事实上在实际应用中,结构体的使用,极大的方便了需要保存数据变量多、类型多的情况。 不过需要小心的是,如果需要写入EEPROM的数据比较多,要注意是否会超出EEPROM的size,还要考虑写入时间。我没有实际试验过,但是我觉得Arduino写入数据到EEPROM应该不是非常快。
如下的例子里面,我在EEPROM里面写入一个结构体数据,然后在 loop() 里面读出来。
定义的 EEPROM_write_short, EEPROM_read_short, EEPROM_clear_all 实际上并没有使用。 EEPROM_write_short, EEPROM_read_short 的功能在之前的一篇里面举过例子了。 而 EEPROM_clear_all 的作用是清空EEPROM的全部内容。
例子里面的方法,主要使用的技术是变量指针,和强制类型转换,不明白的话,需要去啃一下C语言的书了。
C++语言: 高亮代码由发芽网提供
//
// 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)
{
}