Digispark EEPROM Internal

    This page is to Setup and Config Digispark EEPROM Internal
 

Hardware and Software

Hardware 
    
Digispark EEPROM Internal

Software 
    Arduino IDE


Sanki Notes


  • What I did was COPY the EEPROM FOLDER in the arduino avr directory to the digispark library directory.
    ARDUNIO: 1.6.5
    DIGISPARK: Software isntalled via Arduino (15/07/2015)

    PC 
    SOURCE: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM\
    DESTINATION: C:\Users\{user.name}\AppData\Roaming\Arduino15\packages\digistump\hardware\avr\1.6.5\libraries\

    MAC 
    SOURCE: \Applications\Arduino\hardware\arduino\avr\libraries\EEPROM\
    DESTINATION: \users\{user.name}\Library\Arduino15\packages\digistump\hardware\avr\1.6.5\libraries\

Examples

          This example is (.ino)
  1. /*
     * This example is for Digispark Internal EEPROM
     * 
     * 
     * 
     */
    #include <EEPROM.h>
    #include "eeprom_anything.h"

    #include <SoftSerial.h>     /* Allows Pin Change Interrupt Vector Sharing */
    //#include <TinyPinChange.h>  /* Ne pas oublier d'inclure la librairie <TinyPinChange> qui est utilisee par la librairie <RcSeq> */
    SoftSerial mySerial(5, 4); // RX, TX

    // 自行定義struct,放入你想讀取的資料
    struct dataType{
      char name[10];  
      int age;
      char gender;
      float money;
      float bank;
      int hexCode;
    };

    void setup(){
      mySerial.begin(9600);
      
      dataType writeData; // 宣告結構變數,放入資料
      strcpy(writeData.name, "Arduino");
      writeData.age = 9;
      writeData.gender = 'M';
      writeData.money = 9876.54;
      writeData.bank = 1234.56;
      writeData.hexCode = 0xfa;
      
      mySerial.print("name: ");
      mySerial.println(writeData.name);
      mySerial.print("age: ");
      mySerial.println(writeData.age);
      mySerial.print("gender: ");
      mySerial.println(writeData.gender);
      mySerial.print("money: ");
      mySerial.println(writeData.money);
      mySerial.print("bank: ");
      mySerial.println(writeData.bank);
      mySerial.print("hexCode: 0x");
      mySerial.println(writeData.hexCode, HEX);


    //------------------------------- Write -------------------------
      // 寫入EEPROM,回傳值count代表總共寫入幾個byte 
      int count = EEPROM_writeAnything(0, writeData);
      mySerial.print(count);
      mySerial.println(" bytes written.");
      mySerial.println("---------------");

    //------------------------------- Read -------------------------
      // 讀取EEPROM,回傳值count代表總共寫入幾個byte 
      dataType readData;
      count = EEPROM_readAnything(0, readData);
      
      mySerial.print("name: ");
      mySerial.println(readData.name);
      mySerial.print("age: ");
      mySerial.println(readData.age);
      mySerial.print("gender: ");
      mySerial.println(readData.gender);
      mySerial.print("money: ");
      mySerial.println(readData.money);
      mySerial.print("bank: ");
      mySerial.println(readData.bank);
      mySerial.print("hexCode: 0x");
      mySerial.println(readData.hexCode, HEX);
      mySerial.print(count);
      mySerial.println(" bytes read.");


    }
    void loop(){

    }

Examples

          This example is eeprom_anything.h
  1. #ifndef eeprom_anything_h
    #define eeprom_anything_h

    #include <Arduino.h>
    #include <EEPROM.h>

    // 利用這兩個C++模板,讀寫整組資料
    // 參數address是EEPROM位址,參數data是想讀寫的資料結構
    template <class T> int EEPROM_writeAnything(int address, const T &data);
    template <class T> int EEPROM_readAnything(int address, T &data);

    // 函式模板的定義
    // 使用EEPROM程式庫寫入一個一個的byte
    template <class T> int EEPROM_writeAnything(int address, const T &data)
    {
      const byte *p = (const byte *)(const void *)&data;
      int i, n;
      for(i = 0, n = sizeof(data); i < n; i++)
        EEPROM.write(address++, *p++);
      return i;
    }
    template <class T> int EEPROM_readAnything(int address, T &data)
    {
      byte *p = (byte *)(void *)&data;
      int i, n;
      for(i = 0, n = sizeof(data); i < n; i++)
        *p++ = EEPROM.read(address++);
      return i;
    }

    #endif