Tutorial: How to upload .c code to an Atmega

On this tutorial, we will show how to upload .c codes to an Atmega microcontroller using the avr-gcc and avrdude tools.

First, if you are using Windows, download and install the WinAVR, or if you are using Ubuntu, open the terminal and type: sudo apt-get install gcc-avr avr-libc to install. For other linux distributions, check here.

Now, take an Arduino board and make it act as an AVR ISP Programmer, upload the ArduinoISP sketch and wire to your arduino like this:

We will use a Blink made in C as an example. Paste this code on a text file, name it "blink.c":

/*
Blink
Turns on an LED at pin PB5 on for one second, then off for one second, repeatedly.

by Fernando Gil
*/

#define F_CPU 8000000UL // Clock Speed
#include <avr/io.h>
#include <util/delay.h>
#define LED_PIN PB5

void delayms(uint16_t millis) {
    while ( millis ) {
        _delay_ms(1);
        millis--;
    }
}

int main(void)
{
    DDRB |= 1LED_PIN; /* set LED_PIN to output */

    while(1)
    {
        PORTB |= 1LED_PIN; /* 1 - LED ON */
    delayms(1000);
    PORTB &= ~(1LED_PIN); /* 0 - LED OFF */
    delayms(1000);
    }
    return 0;
}

Open the "command prompt" (win) or "terminal" (linux), go to your folder and type:

avr-g++ -g -Os -Wall -mcall-prologues -mmcu=atmega328p -fno-exceptions -o blink.obj blink.c

This will compile your code, where:

          -mmcu=atmega328p is the microcontroller name (the target), on this case it is atmega328p, if you are using another one, look on this list.

         blink.obj is the name where your compiled program will be;

         blink.c is the input file, with your C code.

Now, to upload, you have to convert your binary blink.obj file to a blink.hex one, to do this, type:

avr-objcopy -R .eeprom -O ihex blink2.obj blink3.hex

where:

          blink2.obj is the input file;

          blink3.hex is the output hex file.

Almost done, you have your program ready to upload! To upload it, type:

avrdude -P com1 -b 19200 -c avrisp -p m328p -U flash:w:blink3.hex  para gravar no atmega.

where:

          19200 is the programmer's baud rate

          avrisp is the programmer's type

          m328p is the processor model

          para maiores informações, clique aqui

Congrats! Your ATmega should working with your code.

References:

http://arduino.cc/playground/Learning/Linux

http://www.ladyada.net/learn/avr/index.html

http://nongnu.org/avr-libc/user-manual/

http://www.nongnu.org/avrdude/user-manual/avrdude.html

Views: 13452

Comment

You need to be a member of GarageLab (arduino, electronics, robotics, hacking) to add comments!

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

Profile IconJiangbin ZHENG and Riszter István joined GarageLab (arduino, electronics, robotics, hacking)
yesterday
Profile Iconkaan and guner guvenc joined GarageLab (arduino, electronics, robotics, hacking)
Monday
Devang Rameshbhai Zalavadiya is now a member of GarageLab (arduino, electronics, robotics, hacking)
May 18
Profile Iconraj kumar, Raffaello Palandri and Jigga Boo joined GarageLab (arduino, electronics, robotics, hacking)
May 7
Profile IconGabriele D'Angelo, dale wolver, Ibrahim Aykut and 2 more joined GarageLab (arduino, electronics, robotics, hacking)
May 5
Profile IconJaschaSaurus Rex and Christian Mock joined GarageLab (arduino, electronics, robotics, hacking)
May 1
Igor Golov is now a member of GarageLab (arduino, electronics, robotics, hacking)
Apr 27
Marco is now a member of GarageLab (arduino, electronics, robotics, hacking)
Apr 24
Muhammad Ali commented on Marcelo Rodrigues's blog post Arduino generated VGA (color) signal - Complete!
"Thanks for the advice. I will persist with the code till I get it done. thanks once again."
Apr 18
Marcelo Rodrigues commented on Marcelo Rodrigues's blog post Arduino generated VGA (color) signal - Complete!
""The output waveforms on oscilloscope for Hsync and Vsync and for that matter RGB pins are not…"
Apr 17
Muhammad Ali commented on Marcelo Rodrigues's blog post Arduino generated VGA (color) signal - Complete!
"Hi  Marcelo I am trying to make this code work with Attiny 44a operating @ 20 MHz. I have…"
Apr 17
Profile IconReal NOiseux, Muhammad Ali, bob v and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Apr 17
baiqian is now a member of GarageLab (arduino, electronics, robotics, hacking)
Apr 10
Profile IconDavide and Almas Amandossov joined GarageLab (arduino, electronics, robotics, hacking)
Apr 6
Profile Iconvamoalrenga and John Ferrell joined GarageLab (arduino, electronics, robotics, hacking)
Apr 3
Marcelo Rodrigues replied to FiniousFingers's discussion New to group
"Yes, please share your progress here. Thanks and good"
Mar 27
FiniousFingers replied to FiniousFingers's discussion New to group
"Thanks, good to know someone is out there. I will be…"
Mar 27
Edward Evans replied to GarageLab's discussion Serious claims against BeagleBone's manufacturer CircuitCo
"Have you moved forward with any action?  I would be interested in discussing your…"
Mar 27
Marcelo Rodrigues replied to GarageLab's discussion Serious claims against BeagleBone's manufacturer CircuitCo
"Ed, No, they didn't deliver yet.  :( Good luck for us all."
Mar 23
Profile IconWesenachin Asfaw and Muhammad Basil joined GarageLab (arduino, electronics, robotics, hacking)
Mar 23

© 2017   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service