Society of Robots - Robot Forum

Software => Software => Topic started by: BANE on April 16, 2011, 12:16:38 PM

Title: Reading/Writing to Axon II's eeprom
Post by: BANE on April 16, 2011, 12:16:38 PM
Hello all,
just try to get my feet wet with reading/writing to my Axon II's eeprom.  Ive read all of webbots info (not sure if i understand it all yet, but hopefully will) and im getting some warning from AVR studio.

here is just the parts of code involving eeprom
Code: [Select]
//eeprom store var
int x_max_position;
//write to eeprom
x_max_position=10;
eeprom_write_byte(x_max_position, left_joy_x_max);
//display
rprintf("XMP=%d",eeprom_read_byte(x_max_position));

it gives me a warning saying:
Quote
warning: passing argument 1 of 'eeprom_write_byte' makes pointer from integer without a cast
also, returns the value 0, not 10

I read about having to include "EEMEM" in the variable but the compiler doesnt like it :P    
Can anybody see my error?  probably syntax error
thanks,
Title: Re: Reading/Writing to Axon II's eeprom
Post by: Webbot on April 16, 2011, 02:15:18 PM
You definitely need the EEMEM in the variable definition.
If the compiler doesn't like it then you probably dont have
Code: [Select]
#include <avr/eeprom.h>
Title: Re: Reading/Writing to Axon II's eeprom
Post by: BANE on April 16, 2011, 04:40:45 PM
Ok, ive include that avr/eeprom but I still dont understand how to include EEMEM in the variables definition
Code: [Select]
uint8_t EEMEM x_max_position;  AVR studio says

Quote
error: section attribute cannot be specified for local variables

here is the new code:
Code: [Select]
#include <avr/eeprom.h>

uint8_t EEMEM x_max_position;

eeprom_write_byte(x_max_position, 10);

rprintf("XMP=%d",eeprom_read_byte(x_max_position));
Title: Re: Reading/Writing to Axon II's eeprom
Post by: Webbot on April 16, 2011, 06:40:10 PM
I'm guessing that you are trying to do it wit a variable that is defined inside a function - which is a no no. It must be a global variable.
ie

Code: [Select]
#include <avr/eeprom.h>

uint8_t EEMEM globalVar; // Should be ok

void foobar(void){
    uint8_t EEMEM localVar; // Probably gives your error and cannot work
}

The difference is that 'globalVar' will always exist at the same memory location whereas 'localVar' comes and goes and is only valid whilst 'foobar' is running so can have a different memory slot every time its called.
Title: Re: Reading/Writing to Axon II's eeprom
Post by: BANE on April 16, 2011, 07:44:51 PM
ahhh i now know that global variables are :D

However, im still getting the two same warnings and every time i read and i get 255 not 10

Code: [Select]
#include <avr/eeprom.h>
uint8_t EEMEM x_max_position;

TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
eeprom_write_byte(x_max_position, 10);
rprintf("LJM=%d",eeprom_read_byte(x_max_position));

return 0;

Can i use x_max_position like this inside TICK_COUNT?
}
Title: Re: Reading/Writing to Axon II's eeprom
Post by: Webbot on April 17, 2011, 06:35:06 PM
when you compile your program you will be getting warning errors like this:

Code: [Select]
warning: passing argument 1 of '__eewr_byte_m640' makes pointer from integer without a cast
This is because you should be passing the address of your variable - not the contents of the variable - ie it should be prefixed with an '&' symbol. So try changing your code to this:-

Code: [Select]
#include <avr/eeprom.h>
uint8_t EEMEM x_max_position;

TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
eeprom_write_byte(&x_max_position, 10);
rprintf("LJM=%d",eeprom_read_byte(&x_max_position));

return 0;

C is a real pain with this stuff - and its made worse by the avr-gcc compiler just issuing them as warnings rather than errors.
Title: Re: Reading/Writing to Axon II's eeprom
Post by: BANE on April 17, 2011, 07:10:40 PM
yep that little "&" made all the difference :D

helpful+1

thanks a ton
Title: Re: Reading/Writing to Axon II's eeprom
Post by: waltr on April 17, 2011, 08:44:23 PM
Quote
This is because you should be passing the address of your variable - not the contents of the variable - ie it should be prefixed with an '&' symbol.
Yep, this is called a 'pointer'.
For a better understanding see the K&R C book.
http://en.wikipedia.org/wiki/The_C_Programming_Language (http://en.wikipedia.org/wiki/The_C_Programming_Language)
http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628 (http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628)