Arduino EEPROM 的操作

qq513283439發表於2016-03-16
#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);
   }
}


 


 

相關文章