Can not blink led on atmega2560

Go To Last Post
29 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello!

 

I have strange problem. I have arduino mega2560. When I compile and upload code for led blinking arduino ide, it works fine, but when i compile the same code with avr-gcc and upload via usbasp, led not blinking.

 

Code:

 

#include <stdbool.h>
#include <avr/io.h>

const int pin = 7;

int main(void)
{
    DDRB |= 1 << pin;

    while (true)
    {
        volatile long int i;
        for (i = 0; i < 50000; i++)
          ;
        PORTB |= 1 << pin;
        for (i = 0; i < 50000; i++)
          ;
        PORTB &= ~(1 << pin);
    }

    return 0;
}

 

I compile and upload with

 

avr-gcc -mmcu=atmega2560 -o test.elf test2.c
avr-objcopy -O binary test.elf test.bin
avrdude -c usbasp -p atmega2560 -U flash:w:test.bin

 

If i use atmega328p (arduino uno) all works fine.

 

Fuses: E:FD, H:D8, L:FF

This topic has a solution.
Last Edited: Thu. Oct 1, 2020 - 06:40 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

VladTsVs wrote:
for (i = 0; i < 50000; i++)
          ;

 

gcc will see this code and optimize it out of existence! So no delay means your eyes can not see it blinking!

 

For delays, use builtin delay functions or use a timer.

https://www.nongnu.org/avr-libc/...

add header and use delay_ms(500) function.

 

 

Jim

edit: added more detail

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

Last Edited: Thu. Oct 1, 2020 - 04:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"volatile" prevents optimization. And when i compile and upload in arduino, or to atmega328p, led blinks as required. Also I have checked LED with logical probe, there is no blinking

Last Edited: Thu. Oct 1, 2020 - 04:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
gcc will see this code and optimize it out of existence!

See: https://www.avrfreaks.net/forum/tutcoptimization-and-importance-volatile-gcc

 

 

Presumably this:

volatile long int i;

was an attempt to avoid that?

 

use builtin delay functions or use a timer

+1

 

https://www.nongnu.org/avr-libc/...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you posted this wile I was typing

VladTsVs wrote:
"volatile" prevents optimization.

That's not entirely true: it only prevents certain optimisations - I think it remains valid for the optimiser to remove a completely useless loop

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In my case it prevents optimization. And when I compile in arduino, or compile for atmega328p, all works fine

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

It would be very simple to look at the compiler output and see if your delay routine survived the optimizer.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But i have checked with _delay_ms instead of loop. Nothing changes

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually, i try to blink led because I can not force atmega2560 work at all, when i use avr-gcc. USARTs also doesn't work

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just do what my avatar does (but the deliberate error in there is the missing #include <util/delay.h> so you need that !)

 

PS if you do include util/delay.h then you MUST define F_CPU before the inclusion - set it to the speed you know the CPU is running at (or the speed you *think* it is anyway)

 

So something like:

#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRB = 0xFF;
    PORTB = 0x55;
    while(1) {
        PORTB ^= 0xFF;
        delay_ms(100);
    }
}

 

Last Edited: Thu. Oct 1, 2020 - 05:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
    DDRB = 0xFF;

    while (1)
    {
        _delay_ms(200);
        PORTB ^= 0xFF;
    }

    return 0;
}

 

Still nothing changes. In arduino ide led blinks, with avr-gcc - no

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you compiling for the correct processor?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avr-gcc -mmcu=atmega2560 -o test.elf test2.c -Os

avr-objcopy -O binary test.elf test.bin

Last Edited: Thu. Oct 1, 2020 - 05:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also are your LEDs wired to the pins you think they are (ie PORTB!) are they the right way round and do they have the right value of current limiting resistor ? Also in the specific circuit you are using is there an alternative use being made for PORT B pins in fact?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm using arduino mega2560 board. And try to blink it's build-in LED. And when i compile the same code in arduino ide, it blinks fine. So there is no problems with resistiors, connections and so on

Last Edited: Thu. Oct 1, 2020 - 05:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Disassemble of avr-gcc compilation result:

test.elf:     file format elf32-avr

Disassembly of section .text:

00000000 <__vectors>:
   0:    0c 94 72 00     jmp    0xe4    ; 0xe4 <__ctors_end>
   4:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
   8:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
   c:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  10:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  14:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  18:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  1c:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  20:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  24:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  28:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  2c:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  30:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  34:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  38:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  3c:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  40:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  44:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  48:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  4c:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  50:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  54:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  58:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  5c:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  60:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  64:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  68:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  6c:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  70:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  74:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  78:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  7c:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  80:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  84:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  88:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  8c:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  90:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  94:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  98:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  9c:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  a0:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  a4:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  a8:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  ac:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  b0:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  b4:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  b8:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  bc:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  c0:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  c4:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  c8:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  cc:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  d0:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  d4:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  d8:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  dc:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>
  e0:    0c 94 7e 00     jmp    0xfc    ; 0xfc <__bad_interrupt>

000000e4 <__ctors_end>:
  e4:    11 24           eor    r1, r1
  e6:    1f be           out    0x3f, r1    ; 63
  e8:    cf ef           ldi    r28, 0xFF    ; 255
  ea:    d1 e2           ldi    r29, 0x21    ; 33
  ec:    de bf           out    0x3e, r29    ; 62
  ee:    cd bf           out    0x3d, r28    ; 61
  f0:    00 e0           ldi    r16, 0x00    ; 0
  f2:    0c bf           out    0x3c, r16    ; 60
  f4:    0e 94 80 00     call    0x100    ; 0x100 <main>
  f8:    0c 94 8f 00     jmp    0x11e    ; 0x11e <_exit>

000000fc <__bad_interrupt>:
  fc:    0c 94 00 00     jmp    0    ; 0x0 <__vectors>

00000100 <main>:
 100:    8f ef           ldi    r24, 0xFF    ; 255
 102:    84 b9           out    0x04, r24    ; 4
 104:    2f ef           ldi    r18, 0xFF    ; 255
 106:    83 ec           ldi    r24, 0xC3    ; 195
 108:    99 e0           ldi    r25, 0x09    ; 9
 10a:    21 50           subi    r18, 0x01    ; 1
 10c:    80 40           sbci    r24, 0x00    ; 0
 10e:    90 40           sbci    r25, 0x00    ; 0
 110:    e1 f7           brne    .-8          ; 0x10a <main+0xa>
 112:    00 c0           rjmp    .+0          ; 0x114 <main+0x14>
 114:    00 00           nop
 116:    85 b1           in    r24, 0x05    ; 5
 118:    80 95           com    r24
 11a:    85 b9           out    0x05, r24    ; 5
 11c:    f3 cf           rjmp    .-26         ; 0x104 <main+0x4>

0000011e <_exit>:
 11e:    f8 94           cli

00000120 <__stop_program>:
 120:    ff cf           rjmp    .-2          ; 0x120 <__stop_program>

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

VladTsVs wrote:
Disassemble of avr-gcc compilation result:

and how does that compare to your Arduino IDE compilation result?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There are two ICSP sockets on a mega, is it possible the OP is using the wrong one?

And thus programming the mega16u2 instead.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

.text section is absolutely identical

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> is it possible the OP is using the wrong one?

 

No, i use the right one

Last Edited: Thu. Oct 1, 2020 - 05:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is usbasp one of the programmers that doesn’t work on parts with more than 128k?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


VladTsVs wrote:
Fuses: E:FD, H:D8, L:FF

try these fuses:

This disables jtag, I assume your not using a jtag debugger!

 

Jim

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

westfw wrote:

Is usbasp one of the programmers that doesn’t work on parts with more than 128k?

 

 

It is possible that problem is with usbasp. Because when i readed atmega2560 flash with usbasp result was incorrect, it was filled with 0xFF. Reading by arduino methods (avrdude -cwiring) returns correct firmware

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, problem was in usbasp. I have uploaded my code by -cwiring, and it works

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The only thing i still don't understand - if usbasp works incorrectly, how could it load bootloader to atmega2560.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

VladTsVs wrote:

The only thing i still don't understand - if usbasp works incorrectly, how could it load bootloader to atmega2560.

 

the boot loader section is only 4K in size. Well in its capacity

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

VladTsVs wrote:
Yes, problem was in usbasp. 
jolly good - mystery solved!

 

Now, please mark the solution - see Tip #5 in my signature, below, for details:

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

if usbasp works incorrectly, how could it load bootloader to atmega2560.

I'm not sure.  I would have expected the other way around (blink works, uploading bootloader doesn't.)

https://petervanhoyweghen.wordpr... explains it, perhaps?  (I didn't look too carefully.)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

VladTsVs wrote:
I have arduino mega2560.

Woah! Loads of posts have appeared whilst I was trying your program on my Arduino Mega2560. And yes it works.

 

Hold on - On Arduino mega2560 the USB is handled by ATmega16u2 and the protocol is different to the mega328P version.

 

For reference here's the avrdude command line that I used. NB: I'm on Linux - hence the Unix like COM port. USB Abstract Control Model (ACM)

 

avrdude -p atmega2560 -P /dev/ttyACM0 -c stk500v2 -D -U flash:w:test.hex

 

 

Last Edited: Thu. Oct 1, 2020 - 08:45 PM