0

I'm new to saving data on eeprom and I'm obviously doing something wrong but I'm not sure what. I have one program which both writes to and reads the internal eeprom on a attiny85 and this seems to work. However when I try to use this program to write and then remove the power from the attiny, then reprogram it with another program to read the eeprom it gives values of -1 for time and NAN for temperature.

My program which works is as follows, the program to read the eeprom only is the same however I deleted all the sections which write. If you want me to add that program let me know.

I am using at attinyavr programmer from sparkfun and I have connected pins 3 & 4 on the attiny to an arduino uno which is connected to the serial monitor so that I can see the output (described here: http://www.instructables.com/id/Easy-ATTiny-Serial-Communication-with-Tiny-AVR-Pro/#CP78RMAJ76GI6JQ)

Feel free to direct me to another answer if someone else has had the same problem, at the moment I think I will figure this out eventually but I suspect it will take me a few days so thought I would ask hopefully it's obvious to someone else.

(I'm aware that the time won't be correct since it should be unsigned long not an int, was just changing it round to test if that was the problem which it seems not to be).

Any help is really appreciated!!! Thanks :) /*

ATTINY85 pin 3 -> Arduino Uno pin 0
ATTINY85 pin 4 -> Arduino Uno pin 1
Program choose 1MHz clock
 */

#include <EEPROM.h>
#include <SoftwareSerial.h>
// Definitions
#define rxPin 3     // Make pin 3 on the 
#define txPin 4

SoftwareSerial mySerial(rxPin, txPin);

int currentTime;
int getTime;

int sensorPin = 7; // This is the 7th pin on the ATtiny85, 
                         // it is an analog input when reading as digital 
call it 7? analogpintochannel function 
                          // (On the HLT tutorial it is labelled as Pin2: Analog input 1, SCK )  
float temperature = -500.00f;     // Create a new float called temperature and give it the value -500 to start with
float getTemperature = -500.00f;
int eepromAddress = 0;       // initialize variable for position in eeprom to store data collected
int getEepromAddress = 0;

// the setup routine runs once when you press reset:
void setup() {                
  pinMode(sensorPin, INPUT);    // make pin 7 on the ATTiny an input pin
  mySerial.begin(9600);         // begin software serial at rate of 9600

}

// the loop routine runs over and over forever:
void loop() {

  static int countDataCollected = 0;  // initialize a count variable to count the data collected, static int should set to 0 only first time, second time loop goes round it shouldn't reset to 0  

  if (countDataCollected==0) delay(90000); // put delay here so it gives me time to load the read eeprom program if required before it changes the eeprom in case that was the problem.  

  if((countDataCollected>=0) and (countDataCollected < 10)) {

    currentTime = millis();
    temperature = analogRead(sensorPin)*5.0/1024.0;  // read value on pin 7, multiply it to find temperature
    temperature = temperature - 0.5;
    temperature = temperature / 0.01;

    mySerial.print("\n\nData number: ");
    mySerial.print(countDataCollected);
    mySerial.print("\tTime: ");
    mySerial.print(currentTime);
    mySerial.print("\tTemperature: ");      
    mySerial.print(temperature);
    mySerial.print("\tCurrent eeprom address: ");
    mySerial.print(eepromAddress);
    mySerial.print("\t");
    mySerial.print(getEepromAddress);    

    EEPROM.put(eepromAddress, currentTime);
    EEPROM.get(getEepromAddress, getTime);
    mySerial.print("\tGot time: ");
    mySerial.print(getTime);

    eepromAddress = eepromAddress + sizeof(currentTime);
    getEepromAddress = getEepromAddress + sizeof(getTime);
    mySerial.print("\teeprom address: ");
    mySerial.print(eepromAddress);
    mySerial.print("\t");
    mySerial.print(getEepromAddress);  

    EEPROM.put(eepromAddress, temperature);
    EEPROM.get(getEepromAddress, getTemperature);

    eepromAddress = eepromAddress + sizeof(temperature); //Move address to the next byte after float 'myfloat'.
    getEepromAddress = getEepromAddress + sizeof(getTemperature);
    mySerial.print("\tGot temperature: ");     
    mySerial.print(getTemperature);
    mySerial.print("\teeprom address: ");
    mySerial.print(eepromAddress);
    mySerial.print("\t");
    mySerial.print(getEepromAddress);  
    mySerial.println(); 



    }


  // wait two seconds so as not to send massive amounts of data
  delay(2000);  
  countDataCollected=countDataCollected+1;


  mySerial.print("\n\nNext data number =");
  mySerial.print(countDataCollected);
}
  • See stackoverflow.com/questions/36623186/… for an explanation to why eeprom is erased. – Mikael Falkvidd Sep 22 '17 at 10:44
  • @MikaelFalkvidd, thanks!!!, that sounds like it's probably the problem. I'm just trying to learn how to program the Attiny85 with a bootloader, when/if I do I will get back to confirm that was the reason. (might take a while as I'm in China and need to find out how to order the components I need to build the circuit) – S.H. Sep 25 '17 at 0:16
  • Great that you found a solution. I think you have a better grasp of the solution than I do, so you'll be able to write a higher quality answer :) – Mikael Falkvidd Nov 28 '17 at 8:51
  • @ Mikael Falvidd, I'm not sure, but thanks!! :) – S.H. Dec 9 '17 at 21:45
  • 1
    @ Mikael Falvidd Actually in the end found my comment above about the programmer was incorrect, (such a long time since I asked the question I forgot to try disconnecting the power and when I did the programmer made no difference. Have deleted the comment so as not to confuse other people) but you did direct me the correct answer, thanks!!!, it was fuses that made a difference. Will add answer below, so happy it's working!! – S.H. Dec 11 '17 at 0:12
0

Fixed by resetting the attiny85 fuse to stop the EEPROM being cleared when the attiny was reprogrammed.

As far as I understand (I am a newbie at AVR programming so may be wrong)

The arduino bootloader (in the Arduino IDE) sets the attiny fuses. One of the fuses that it sets (also the attiny85 default setting) clears the EEPROM when reprogramming. To see how the bootloader is setting the fuses to you can turn on verbose comments in the Arduino IDE, and run the bootloader. When I ran the bootloader it set the fuses as follows: pattiny85 -cusbtiny -e -Uefuse:w:0xff:m -Uhfuse:w:0xdf:m -Ulfuse:w:0xe2:m

i.e. arduino bootloader settings, efuse: 0xff, hfuse: 0xdf (need to change to d7), lfuse: 0xe2 (affects the clock),

You can use a fuse calculator (http://www.engbedded.com/fusecalc/) to determine how to change the fuse settings so the the EEPROM is not cleared. (In the fuse calculator tick the box for "Preserve EEPROM memory through the chip erase cycle, EESAVE=0"). When the box is ticked the hfuse setting is D7 rather than DF

Also note that the bootloader in Arduino sets the lfuse to 0xe2, this is different from the default (it affects the setting 'Divide clock internally by 8' if 0xe2 is used it's an 8Mhz clock, the default prior to loading the boatloader is 1Mhz). Noting this because I initally reset everything to default and then had clock problems.

The hfuse can be set to d7 by downloading and running the AVR programming software https://sourceforge.net/projects/winavr/ and then setting the fuse using avrdude in a command window. (more information on AVR setting fuses here http://www.ladyada.net/learn/avr/index.html)

I ran avrdude in a command window with the instruction: avrdude -c usbtiny -p attiny85 -U hfuse:w:0xd7:m

("-c usbtiny" specifies the programmer used so this may vary, the attiny was connected to my computer USB port using the sparkfun attiny AVR programmer).

| improve this answer | |
  • The bootloader itself does not set fuses. However, the Arduino IDE's Tools > Burn Bootloader command does set the fuses according to the boards.txt configuration for the currently selected board in addition to flashing the microcontroller with the bootloader. – per1234 Dec 11 '17 at 1:47
  • ok, thanks for the correction! I'm not very clear on how everything works. – S.H. Dec 11 '17 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.