Writing/reading EEPROM on Orangutan

Hi,

i need to program a usage counter on my Orangutan. For that reason i need a function that reads the int value on a certain EEPROM address, alters it +1, and overwrites the certain value.

Where should i start?

thanks!

Hello.

This thread might help you get started:

Please post here if you have any further questions.

- Ben

i tried to write some code:


#include <pololu/orangutan.h>
#include <avr/eeprom.h>
int variable_1;


int main()
{
	
	while(1){
		eeprom_write_word(0,23);
	
		variable_1= eeprom_read_word(0);
		lcd_goto_xy(0,0);
		print_long(variable_1);

	}
}

what happens is, that the display shows “23” correctly. but when i delet the row

“eeprom_write_word(0,23);”

and run the program again, it Shows “-1” and not “23”. any suggestion why??

EEPROM is only rated for a certain number of write cycles, so you should never put a write inside of a loop like that (especially with no delays to slow the loop down)!

It is probably not working because the EEPROM is getting erased (set to 0xFF) every time you reprogram your Orangutan. Atmel studio and avrdude should both have options for preserve EEPROM when reprogramming.

Try a test like this:

#include <pololu/orangutan.h>
#include <avr/eeprom.h>
int variable_1;


int main()
{
   variable_1 = eeprom_read_byte(0);
   if (variable_1 != 0xFF)
      print_long(variable_1);
   else
   {
      print("writing");
      eeprom_write_byte(0, 23);
   }

   while(1);
}

The first time you run the program, you should see “writing” printed to the LCD. If you press reset or cycle power, you should then see “23” printed to the LCD.

- Ben

thanks ben,

your program works fine, but the “preserve eeprom” function doesn’t. there is a checkbox for preserving in the project-settings of atmel studio 6, but it has no effect and the eeprom is overwritten, when i reprogram the orangutan. do you know why?

I haven’t tried this with Atmel Studio, but I did some googling around and found some things that might be helpful:

avrfreaks.net/index.php?name … 18&start=0
avrfreaks.net/index.php?name … c&t=129728

- Ben

thanks ben,

i found some similar hints before, but it is still not possible for me to keep the eeprom values. i have atmel studio 6 installed, checked the “preserve eeprom” and also tried “start without debugging”…

It might be a bug with Atmel Studio 6. I know I was able to do it with AVR Studio 4 and you should be able to preserve EEPROM if you use avrdude to load your hex file.

- Ben