Does Tools .. Board affect hex file uploaded with AVRdude cmd line ?

Just starting to look at moving a small sketch from an Uno to an Atmega328p chip.

I have found that I can use the IDE ( 1.5.6r2 ) to Verify the sketch, and this creates a temp hex file.

Then use the location of the hex file to write to the chip.
Arduino Uno that is used to transfer the hex to the target Atmega328 is loaded with the ArduinoISP sketch.

Batch file for writing contains :

set /p Input=Enter the hex path :
avrdude -P COM15 -b 19200 -c avrisp -p m328p -u -U flash:w:"%Input%":i

This is working great, with the exception of EEPROM data.

I am writing byte data to the EEPROM using code :

byte EEget = 0;
byte MyVar1 = 0;
byte MyVar1 = 0;

void GetEpromVals(){
	EEget = EEPROM.read(1);
	MyVar1 = EEget;
	EEget = EEPROM.read(2);
	MyVar2 = EEget;
}


void SetEpromVals(){
	EEPROM.write(1, MyVar1);
	EEPROM.write(2, MyVar2);
}

The byte values being returned when reading the EEPROM are complete different to the values I wrote to the EEPROM.

So am wondering if the Board setting in the IDE affects the hex file when it is created ( Verified toolbar button ), and if the board setting affects the internal chip address / location of the EEPROM space ( if this does differ between chips ? ), and if so, how to change the Board to an Atmega328p

I am having a difficult time finding your General Electronics question. Did you, perhaps, post your question in the wrong section?

drat, drat and double drat. Sorry, was far too early in the morning and I just saw the general.

How do I move the question, or do I just repost ?

DaveO:
How do I move the question, or do I just repost ?

Ask a moderator (personal message) or report to moderator.

(Thread moved.)

When you prepared the m328p processor, did you set the preserve EEPROM fuse?

Thank You Coding Badly

Did you notice Reply #4?

Thanks Coding Badly. Was just busy typing this when you asked.

'prepared' ? If you are referring to a bootloader, I am not using the bootloader on the Atmega328. I am simply using the Arduino IDE to create the hex file ( during the 'verify' process ) and then uploading that hex file to the Atmega328 using the Uno as the ISP.

If I understood the process ( and that is not a certainty ) I am uploading the hex file using AVRdude with the 'm328p' in the AVRdude command switches, shouldn't that use the correct fuses ?

If so, then my thinking is that the Arduino IDE uses the 'specs' for the board ( as set in Tools > Board ) and this affects the location of the eeprom addresses in the hex file ?

If this is the case, where / how do I add the 328p chip to the list of Boards in the IDE, so that the addresses of the EEPROM are correct in the hex file ?

DaveO:
'prepared' ?

The processor left the factory with the fuses set to default values. For example, from the factory, the processor is configured to run at 1 MHz.

By "prepare the processor" I mean "change the fuses to what you want for your application". For example, you may have disabled the divide-by-eight fuse bit so the processor now runs at 8 MHz.

What I want to know is if you changed the fuse bits so preserve-EEPROM is enabled. If you did not then every time you upload a new image the EEPROM is erased (0xFF is returned when you read from EEPROM). That may explain the symptom.

avrdude -P COM15 -b 19200 -c avrisp -p m328p -u -U flash:w:"%Input%":i

The byte values being returned when reading the EEPROM are complete different to the values I wrote to the EEPROM.

When programming the chip using a programmer (like avrisp), each upload will first do a "chip erase", which normally will erase all of the EEPROM as well. There is a fuse you can program (EESAVE) that prevents EEPROM from being erased by a chip erase, but that fuse is NOT included in the standard Arduino fuse settings.

This is what Coding Badly was talking about. Even if you use a programmer to upload your sketch, at some point you have to set up the fuses as well - "preparing" the chip.

Many Thanks to you both for the education.

So my problem is most likely the fuses not being set correctly.

I am not concerned with losing the EEPROM contents when uploading the hex file, as my sketch replaces the values with a user selected setting when run.

Can the fuses be set with the AVRdude command line, and at the same time as uploading the hex file ?

If so, do you possibly have a link to the idiots guide to setting fuses for the Atmega328p using AVRdude, and are there any 'standard' fuse settings that should be used as a starting point, making the chip setup closely resemble the Arduino 'standard' like on the Uno board chips ?

OK. Been a looong night trying to solve this, but think I have it now.

Following advice from earlier, I have been looking at the fuse settings.

Eventually found an online fuse calculator, and set the fuses with :

avrdude -v -P COM15 -b 19200 -c avrisp -p m328p -u -U lfuse:w:0xFF:m -U hfuse:w:0xDB:m -U efuse:w:0x05:m

Then used this to load the hex file that was compiled in the Arduino IDE using the 'verify' button :

set /p Input=Enter the hex path :
avrdude -v -P COM15 -b 19200 -c avrisp -p m328p -u -U flash:w:"%Input%":i

The code still did not work with the EEPROM.

Then I read somewhere that the EEPROM value would return 255 if nothing in the EEPROM location. So I set an LED to ON for 3 seconds if the EEPROM value was > 200 - it was.

So then I have to assume that either the EEPROM values were not being written to the same location where I was reading them from, or the value was not being written at all.

Changed the code for the value to be written / read from Byte to Int, and BINGO - works 100% as required.

Test code :

#include <EEPROM.h>

int EEget = 0;
int LEDpin = 4;

int EEval1 = 5;  // IF THIS IS BYTE THEN EEPROM WRITE FAILS

void GetEpromVals(){

 EEget = EEPROM.read(1);
 EEval1 = EEget;
 EEval1 = EEval1 + 2;

 digitalWrite(LEDpin,HIGH);
 delay(1000);
 digitalWrite(LEDpin,LOW);
 delay(1000);

 for(int cntr = 0;cntr < EEval1;cntr++){
 digitalWrite(LEDpin,HIGH);
 delay(200);
 digitalWrite(LEDpin,LOW);
 delay(200);
 }

}


void SetEpromVals(){

 EEPROM.write(1, EEval1);
 for(int cntr = 0;cntr < EEval1;cntr++){
 digitalWrite(LEDpin,HIGH);
 delay(200);
 digitalWrite(LEDpin,LOW);
 delay(200);
 }
 
}


void SetOUTPUTpins(){

 pinMode(LEDpin, OUTPUT);
 digitalWrite(LEDpin, LOW);

}


void setup(){

 SetOUTPUTpins();
 SetEpromVals();

}


void loop(){

 delay(3000);
 GetEpromVals();

}