반응형

8bit MCU 기준.

4Byte 데이터를  EEPROM에 저장, 읽는 방법

EEPROM 한 개의 주소에 1Byte(8bit) 씩 저장되므로, 큰 데이터는 나눠서 저장해야 한다.

#include <EEPROM.h>

void EEPROMWrite(int address, long value)
{
  byte b4= (value & 0xFF);
  byte b3= ((value >> 8) & 0xFF);
  byte b2= ((value >> 16) & 0xFF);
  byte b1= ((value >> 24) & 0xFF);

  EEPROM.write(address * 4, b4);
  EEPROM.write(address * 4 + 1, b3);
  EEPROM.write(address * 4 + 2, b2);
  EEPROM.write(address * 4 + 3, b1);
}

long EEPROMRead(int address)
{
  long four = EEPROM.read(address * 4);
  long three = EEPROM.read(address * 4 + 1);
  long two = EEPROM.read(address * 4 + 2);
  long one = EEPROM.read(address * 4 + 3);

  return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}

EEPROMWrite(0, 12345678);
EEPROMWrite(1, 23456789);

long data0 = EEPROMRead(0);
long data1 = EEPROMRead(1);

 

반응형

관련글