Digispark EEPROM Internal

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)

Examples

This example is eeprom_anything.h

            1. /*

            2. * This example is for Digispark Internal EEPROM

            3. *

            4. *

            5. *

            6. */

            7. #include <EEPROM.h>

            8. #include "eeprom_anything.h"

            9. #include <SoftSerial.h> /* Allows Pin Change Interrupt Vector Sharing */

            10. //#include <TinyPinChange.h> /* Ne pas oublier d'inclure la librairie <TinyPinChange> qui est utilisee par la librairie <RcSeq> */

            11. SoftSerial mySerial(5, 4); // RX, TX

            12. // 自行定義struct,放入你想讀取的資料

            13. struct dataType{

            14. char name[10];

            15. int age;

            16. char gender;

            17. float money;

            18. float bank;

            19. int hexCode;

            20. };

            21. void setup(){

            22. mySerial.begin(9600);

            23. dataType writeData; // 宣告結構變數,放入資料

            24. #ifndef eeprom_anything_h

            25. strcpy(writeData.name, "Arduino");

            26. #define eeprom_anything_h

            27. writeData.age = 9;

            28. writeData.gender = 'M';

            29. #include <Arduino.h>

            30. writeData.money = 9876.54;

            31. #include <EEPROM.h>

            32. writeData.bank = 1234.56;

            33. writeData.hexCode = 0xfa;

            34. // 利用這兩個C++模板,讀寫整組資料

            35. // 參數address是EEPROM位址,參數data是想讀寫的資料結構

            36. mySerial.print("name: ");

            37. template <class T> int EEPROM_writeAnything(int address, const T &data);

            38. mySerial.println(writeData.name);

            39. template <class T> int EEPROM_readAnything(int address, T &data);

            40. mySerial.print("age: ");

            41. mySerial.println(writeData.age);

            42. // 函式模板的定義

            43. mySerial.print("gender: ");

            44. // 使用EEPROM程式庫寫入一個一個的byte

            45. mySerial.println(writeData.gender);

            46. template <class T> int EEPROM_writeAnything(int address, const T &data)

            47. mySerial.print("money: ");

            48. {

            49. mySerial.println(writeData.money);

            50. const byte *p = (const byte *)(const void *)&data;

            51. mySerial.print("bank: ");

            52. int i, n;

            53. mySerial.println(writeData.bank);

            54. for(i = 0, n = sizeof(data); i < n; i++)

            55. mySerial.print("hexCode: 0x");

            56. EEPROM.write(address++, *p++);

            57. mySerial.println(writeData.hexCode, HEX);

            58. return i;

            59. }

            60. template <class T> int EEPROM_readAnything(int address, T &data)

            61. //------------------------------- Write -------------------------

            62. {

            63. // 寫入EEPROM,回傳值count代表總共寫入幾個byte

            64. byte *p = (byte *)(void *)&data;

            65. int count = EEPROM_writeAnything(0, writeData);

            66. int i, n;

            67. mySerial.print(count);

            68. for(i = 0, n = sizeof(data); i < n; i++)

            69. mySerial.println(" bytes written.");

            70. *p++ = EEPROM.read(address++);

            71. mySerial.println("---------------");

            72. return i;

            73. }

            74. //------------------------------- Read -------------------------

            75. // 讀取EEPROM,回傳值count代表總共寫入幾個byte

            76. #endif

            77. dataType readData;

            78. count = EEPROM_readAnything(0, readData);

            79. mySerial.print("name: ");

            80. mySerial.println(readData.name);

            81. mySerial.print("age: ");

            82. mySerial.println(readData.age);

            83. mySerial.print("gender: ");

            84. mySerial.println(readData.gender);

            85. mySerial.print("money: ");

            86. mySerial.println(readData.money);

            87. mySerial.print("bank: ");

            88. mySerial.println(readData.bank);

            89. mySerial.print("hexCode: 0x");

            90. mySerial.println(readData.hexCode, HEX);

            91. mySerial.print(count);

            92. mySerial.println(" bytes read.");

            93. }

            94. void loop(){

            95. }