arduino EEPROM 二进制转长整形(米思奇)

#include <EEPROM.h>
//读出长整型
volatile long  EP0;
volatile long  EP1;
unsigned long eepromReadLong(int address) {
  union u_tag {
    byte b[4];
    unsigned long ULtime;
  }
  time;
  time.b[0] = EEPROM.read(address);
  time.b[1] = EEPROM.read(address+1);
  time.b[2] = EEPROM.read(address+2);
  time.b[3] = EEPROM.read(address+3);
  return time.ULtime;
}
//写入长整型
void eepromWriteLong(int address, unsigned long value){
  union u_tag {
    byte b[4];
    unsigned long ULtime;
  }
  time;
  time.ULtime=value;
  EEPROM.write(address, time.b[0]);
  EEPROM.write(address+1, time.b[1]);
  if(time.b[2] != EEPROM.read(address+2))
    EEPROM.write(address+2, time.b[2]);
  if(time.b[3] != EEPROM.read(address+3))
    EEPROM.write(address+3, time.b[3]);
}
 EP0 = eepromReadLong(0);//读取 
  EP1 = eepromReadLong(4);//读取
  if (EP0==0){
    eepromWriteLong(0, 32);
    };
    if (EP1==0){
    eepromWriteLong(4, 27);
    };

初始化eeprom数值,如果无数值分别写入32,27

猜你喜欢

转载自blog.csdn.net/taogunet/article/details/115103547