2

Background

I am programming a 40-pin ATMega4809 (datasheet) using ATMEL-ICE programmer. I'm simply using the Atmel Studio to upload a HEX file to the chip. I generate the HEX file using the Arduino IDE.

I'm using the very simple blink sample sketch with a slight modification which uses a different output pin for the LED.

const byte LED = 6;

void setup() {
  // initialize digital pin LED as an output.
  pinMode(LED, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

I have a LED (with resistor) connected to PA6 on the ATMega4809.

Code Result

I use the Export compiled binary choice in the Arduino IDE to generate the HEX file.

export compiled binary

I then use Atmel Studio to upload the HEX file to the ATMega4809.

When I do, the program runs and the LED turns on.

However, I never see the LED turn off (blink).

Do you know why this might be true?

Things I've Tried

  1. I've tried increasing the delay value to see if internal crystal is set different and the timing is different or something.
  2. I've set the internal crystal to different values (ranging from 1Mhz to 20Mhz) and haven't seen any difference.
  3. I've considered that it could just be not looping, so I've tried various things to see if that were true or not.
  4. I've placed all of the code in the setup() function to see if it would working differently -- it didn't, it still just turned the LED on but never off.
  5. I've tried turning LED on in setup() & off in loop() to insure both are at least running once and they are.

NOTE: I know the HEX file is being properly uploaded because when I alter the program and upload different ones (turn on/turn off led) then the code does seem to either turn the LED on or off. I've also verified the code using Atmel Studio -- reading the chip's memory out to a file and doing diff.

2
  • 2
    for what board do you compile in Arduino IDE? why do you think pin 6 is PA6?
    – Juraj
    Aug 8 '21 at 16:45
  • I was trying not to get to deep into the weeds, but I downloaded special boards from github.com/MCUdude/MegaCoreX These support the 4809. Also, that's a good question about PA6. I just noticed that when I used pin 7 (in IDE) I could turn on PA7 and when I used pin 6 I could turn on PA7. But, you're right, I need to look closer at that. Do you see something different somewhere? If so let me know. thanks. Also, if I build the HEX for the Nano Every (which uses the 4809) it does not turn on PA6 / PA7 using 6, 7.
    – raddevus
    Aug 8 '21 at 21:54
1

Because of the great question asked by @Juraj I thought about how pins are mapped (and that I was probably thinking wrong about that mapping).

I searched for and found the following diagram:

nano every pin map

Notice that D7 is mapped to PA1

I switched the code to use Digital Pin 7 (const byte LED = 7;) I then switched the LED in the circuit to be connected (with appropriate resistor) to PA1 which is actually PIN 34 (on my ATMega4809 40 pin).

Back in the Arduino IDE I switched the board to Nano Every (which also uses the ATMega4809).

Arduino IDE - Nano Every board

I compiled the code. Then I exported the HEX file and uploaded it to the 40 pin ATMega4809 using Atmel Studio.

With Bootloader Or Without

Didn't Work: With Bootloader

The first time I tried it i selected the HEX file which contains the bootloader and it didn't work. The LED didn't flash.

Did Work: Without Bootloader

I then tried uploading the HEX file which does not contain the bootloader and the LED began flashing as expected.

Solved! All because of the great question which got me thinking better. Thanks, Here's a very fast video of it in action (youtube).

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.