您的位置:首页 > 产品设计 > UI/UE

Arduino EEPROM 的操作

2016-03-16 11:24 861 查看
#include <EEPROM.h>
struct MyObject{
float field1;
byte field2;
char name[10];
};
void setup(){
Serial.begin(9600);
while (!Serial) {
;
}
float f = 123.456f;
unsigned int eeAddress = 0;
EEPROM.write( eeAddress, f );
Serial.println("Written float data type!");

MyObject customVar = {
3.14f,
65,
"Working"
};
eeAddress += sizeof(float);
//EEPROM.write( eeAddress, customVar );
EEPROM_write_block  ((unsigned char*)&customVar,eeAddress,sizeof(MyObject));
Serial.print( "Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!" );

MyObject xixi ;

EEPROM_read_block((unsigned char*)&xixi,eeAddress,sizeof(MyObject));

Serial.print(xixi.field1);
Serial.print(xixi.field2);
Serial.print(xixi.name);

}

void loop(){
//int address = 0;
//float f ;
// EEPROM.read(address,f);
//  address += sizeof(MyObject);

}

void EEPROM_write_block(unsigned char *memory_block, unsigned int start_address, unsigned int block_size)
{
unsigned char Count = 0;
for (Count=0; Count < 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 < block_size; Count++)
{
memory_block[Count]= EEPROM.read(start_address + Count);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: