Sunday 2 June 2019

ATTiny EEPROM usage


Tutorial: Using EEPROM on the ATTiny Series of microcontrollers

 
ATTiny EEPROM Tutroial

Intro

This is the first tutorial by Barium Electronics that will be dealing with using the internal EEPROM of an ATTiny using the Arduino IDE and also using the AVR EEPROM library.

The Arduino EEPROM library is compatible with the ATTiny range of AVR microcontrollers as the library itself is built on the standard Atmel AVR avr/eeprom.h 'library' so it is compatible with all the AVR microcontrollers.

This was originally written back in 2016 (wow...) so some things may not match what people have floating around now :)

Things needed:

  • ATTiny of your choice, I prefer the t85
  • ISP programmer/ArduinoISP (Pocket ISP, USBasp)
  • Breadboard
  • Cup of coffee

Basics:

  • The EEPROM doesn't take to being written too often as EEPROM can wear very quickly. Reading though does not cause as much damage though, possibly none.
  • EEPROM is good for storing settings
A quick example of the way you can use EEPROM: you can run one sketch to set values in EEPROM and then use a different sketch, program to read the values you set.. That is usually the point of EEPROM, it is a memory type that "keeps its value while there is no power". By storing settings, you can use a ATTiny with minimal Flash space 1K, and then have another 500B of settings stored...

Note:

If you are programming your ATTiny using an ISP programmer (probably won't need it on a Trinket) you need to make sure the ATTiny is set to preserve EEPROM during upload with the ISP, this is done with the fuse settings. You need to look for a tutorial on fuse calculators for the AVRs. To set the EESAVE
you need to set the High fuse to 0xD7, you can change this in the boards.txt file. Here is a fuse calculator.

Start:


We will want to arrange our needed setup of the components. First need to setup the programming jig, this can be the ATTiny micro on a breadboard or using the Arduino as ArduinoISP with a custom or bought shield to program the chip.

I assume in this tutorial that you already have the ATTiny Arduino extension installed on the IDE.

Here is good tutorial on setting up the ATTiny libraries in the Arduino IDE:

High-Low Tech

It is really a lot easier with the boards manager for beginners. If you have older versions of the IDE, you can find a lot on the old way of copy and past, High-Low Tech also show how to do this.

Next we will get the IDE going and set it up in the following way:

IDE preferred setup
Now that the IDE is setup and the bootloader is burnt we move onto the code.

Code:

The first bit of code we are going to discuss is the way in which you will need to read/write the EEPROM on the MCU.

 For Reading you will do the following with the Arduino library:

  EEPROM.read(__addr);   //where __addr is an int of the EEPROM addr number

the Arduino library simplifies the use of the EEPROM by making it easy to use, if it were to be done with the AVR library, it would look something like:

  eeprom_read_byte((uint8_t*)__addr); 
//you need to make __addr a pointer to the type you are using  

avr-libc definition:

uint8_t eeprom_read_byte ( const uint8_t * __p )

For further explanation of more in depth discriptions of the AVR libraries, check avr-libc.

When you want to store data into the EEPROM, bear this in mind, each cell is 8 Bits / 1 Byte. When you write to the EEPROM using the Arduino library, do the following:

EEPROM.write(__addr, __val); // __addr = 0-1023 for UNO, __val 0-255

I prefer to use the eeprom_update_byte(uint8_t* __p, uint8_t __val); this is because it updates the bits that need to be change only, and it thus reduces the wear on the EEPROM, this coupled with wear leveling, it should make the EEPROM last a while.

Getting to some code:

Below is some code that writes a value to the EEPROM of a set value, and then makes use of that value later on in the code to blink an LED on one of the pins


 /* EEPROM demo code for the ATTiny. This code sets a variable in the
 * EEPROM that is then used in the main loop.
 * By Ba_E (RSM)
 * 2016-09-10
 */

 #include "EEPROM.h"
    byte CLK = 0; //This is a global variable with an initial value
    
    void setup() {
      // put your setup code here, to run once:
      if(!EEPROM.read(0)) EEPROM.write(0, 1); //set the pin to D1
 
      delay(5); 
      
      //read from EEPROM address 0
      //this sets the value of the global variable CLK
      CLK = EEPROM.read(0); //place this inside a function
    
      //just to give an example...
      byte PIN = CLK;   //PIN variable is a local variable 
      
      pinMode(PIN ,OUTPUT); //using that local variable
      
    }
    
    void loop() {
      
      if (!CLK) {
        for (int a = 0; a < 3; a++) {
          digitalWrite(CLK, HIGH);  //this uses the global variable 
          delay(1000);
          digitalWrite(CLK, LOW);
          delay(1000);
        }
      }
      else { 
        for (int a = 0; a < 100; a++) {
          digitalWrite(0, HIGH);  //this has a predefined pin no.
          delay(500);
          digitalWrite(0, LOW);   //this has a predefined pin no.
          delay(500);
        }
      }
    }
The program is quite simple, it sets the pin that you want to use for writing to in the EEPROM and then uses it to set the digital pin, this is just an example of showing you how you can use the EEPROM. 
An example of using it more extensively for a settings thing is to have a set of values set in hex or a single arduino program for the fun of it to interpret the serial data and set the eeprom, then extract that eprom data as a setting file you can flash onto all of your devices or program into an I2C eeprom :)
0_o - Reece

No comments:

Post a Comment