Proteksi firmware ATmega dengan Nomor Seri

Menggunakan bantuan 1-Wire device, dengan jenis yang bermacam macam, misal: DS1820

#include <OneWire.h>
#include <EEPROM.h>

#define DEBUG 1

OneWire  ds(A1);  // This is where DQ of your DS18B20 will connect.
bool validity = false;
void setup(void) {

#ifdef DEBUG
  Serial.begin(9600);
#endif

  validity = getDeviceAddress();
}

bool getDeviceAddress(void) {
  byte i;
  byte addr[8];
  bool valid = false;

  unsigned long upper = 0;
  unsigned long lower = 0;

#ifdef DEBUG
  Serial.println("Getting serial number...\n\r");
#endif
  while (ds.search(addr)) {
    if ( OneWire::crc8( addr, 7) != addr[7]) {
#ifdef DEBUG
      Serial.print("CRC is not valid!\n");
#endif
      return;
    }

    for (byte i = 0; i < 4; i++) {
      upper = upper << 8;
      upper = upper | addr[i];
    }
    for (byte i = 4; i < 8; i++) {
      lower = lower << 8;
      lower = lower | addr[i];
    }

    unsigned long result = upper & lower;
    Serial.println(result);

    bool initsn = false;
    if (EEPROM.read(0) == 0xFF) initsn = true;

    //initsn=true;
#ifdef DEBUG
    if (!initsn) Serial.println("Checking serial number...\n\r");
    else Serial.println("Writing serial number...\n\r");
#endif

    valid = true;
    for (byte i = 0; i < 4; i++) {
      byte e = EEPROM.read(i + 1);
      byte r = result & 0xff;
      if (initsn)
        EEPROM.write(i + 1, r);
      if (e != r) valid = false;
      result = result >> 8;
    }
    if (initsn) EEPROM.write(0, addr[7]);

#ifdef DEBUG
    if (valid) Serial.println("Good serial number...\n\r");
    else Serial.println("Bad serial number...\n\r");
#endif

  }
  ds.reset_search();
  return valid;
}


void loop(void) {
  // do nothing
}