Author Topic: How to output HEX file for AVR programming?  (Read 42306 times)

Offline slow_hand

  • Single posting newcomer
  • *
  • Posts: 4
How to output HEX file for AVR programming?
« on: June 19, 2009, 10:24:44 pm »
Hey all,

I'm kind of new at AVRs and C but I know Java pretty well. Many of my friends use CB and I thought to try it as well and I like it much better than PN2.
I wrote a simple 'Hello World' kind of program that I want to burn on an ATMEGA168 AVR.

Here is the code:

#define F_CPU 1000000UL

#include <inttypes.h>
#include <avr/io.h>
#include <util/delay.h>

void _delay_ms(); // Function deceleration

int main(void)
{
    DDRB = 0b11111111; // Set PORTB pins as output
    PORTB = 0b00000000; // Set output on all pins to logic 0

    while(1)
    {
        PORTB=00000000; // LED OFF - at bit 0
        _delay_ms(500); // Wait 0.5 sec
        PORTB=00000001; // LED ON - at bit 0
        _delay_ms(500); //Wait 0.5 sec
    }
    return 0;
}


I've noticed a few "strange things".

1. On every tutorial I saw no one defined function prototypes before main, but I'll get all kind of errors if I won't do that. Do I really have to do that?

2. On the tutorials I saw the function was called delay_ms and not _delay_ms, however the one without the underscore won't work.

3. I have WinAVR installed so I changed the default compiler to GNU AVR GCC and set the path to WinAVR folder. After I build the file I will get an EXE file but not a HEX file like I need to burn to the MC. How can I solve this?

Thanks!
« Last Edit: June 19, 2009, 10:28:04 pm by slow_hand »

mariocup

  • Guest
Re: How to output HEX file for AVR programming?
« Reply #1 on: June 19, 2009, 11:23:47 pm »
3. I have WinAVR installed so I changed the default compiler to GNU AVR GCC and set the path to WinAVR folder. After I build the file I will get an EXE file but not a HEX file like I need to burn to the MC. How can I solve this?

You can generate a hex file in a post build step with CB like

Code
cmd /c <architecture>-objcopy -O ihex Input.elf Output.hex

Offline slow_hand

  • Single posting newcomer
  • *
  • Posts: 4
Re: How to output HEX file for AVR programming?
« Reply #2 on: June 19, 2009, 11:35:50 pm »
Thank you for the reply, however, can you be a little more specific as I could not understand what to do exactly. Another thing, I don't have an .elf file after buidling... I get main.o & main.exe files.
« Last Edit: June 19, 2009, 11:39:12 pm by slow_hand »

mariocup

  • Guest
Re: How to output HEX file for AVR programming?
« Reply #3 on: June 20, 2009, 12:10:55 am »
Hi,

put the command of avr objcopy in the bost-puilt step like

Code
cmd /c avr-objcopy -O ihex <executable> <converted_hex_file>

Replace the <executable> by name of your executable or use instead a CB variable like $(TARGET_OUTPUT_FILE). Then specify the name for the resulting hex file.

If the programmer is a command line tool you could even do the flash programming in a post build step too.

Offline slow_hand

  • Single posting newcomer
  • *
  • Posts: 4
Re: How to output HEX file for AVR programming?
« Reply #4 on: June 20, 2009, 01:10:48 am »
Oh OK I understand! I actually got my code working! :)
Where is the post-build step located? I can't find it in the compiler settings or anywhere else...

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: How to output HEX file for AVR programming?
« Reply #5 on: June 20, 2009, 01:34:37 am »
Oh OK I understand! I actually got my code working! :)
Where is the post-build step located? I can't find it in the compiler settings or anywhere else...
Right click your project in the management pane, chose build options, and you will find a tab called "Pre/Post build steps".

Offline slow_hand

  • Single posting newcomer
  • *
  • Posts: 4
Re: How to output HEX file for AVR programming?
« Reply #6 on: June 20, 2009, 01:42:57 am »
Got it! Thank you very much for the quick help. Now that everything works I can finally go to sleep (it's 2:41AM) :)