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: 13115

Comment

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

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

Marcelo Rodrigues replied to FiniousFingers's discussion New to group
"Yes, please share your progress here. Thanks and good"
12 hours ago
FiniousFingers replied to FiniousFingers's discussion New to group
"Thanks, good to know someone is out there. I will be…"
15 hours ago
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…"
20 hours ago
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."
Thursday
Profile IconWesenachin Asfaw and Muhammad Basil joined GarageLab (arduino, electronics, robotics, hacking)
Thursday
Marcelo Rodrigues replied to FiniousFingers's discussion New to group
"Hey man! A good point to start is reading basic tutorials on Arduino coding and wiring. Then, you…"
Thursday
FiniousFingers posted a discussion

New to group

Couple quick things,I am just starting to learn, or wanting to learn, how to use the Arduino UNO…See More
Thursday
Profile IconDan Urbauer, Bjorn and Jiaming Zhao joined GarageLab (arduino, electronics, robotics, hacking)
Wednesday
FiniousFingers is now a member of GarageLab (arduino, electronics, robotics, hacking)
Mar 20
Erva updated their profile
Mar 16
Erva and Mrs. Priscilla Bonneval are now friends
Mar 16
Profile IconSamantha Dorr, Lee Robinson and Celso Eiju Ito joined GarageLab (arduino, electronics, robotics, hacking)
Mar 16
Edward Evans updated their profile
Mar 15
Edward Evans replied to GarageLab's discussion Serious claims against BeagleBone's manufacturer CircuitCo
"Hi Marcelo, I am curious if you have had any resolution on this?  We may be in the same boat…"
Mar 15
Celso Eiju Ito left a comment for Mrs. Priscilla Bonneval
"I just accept your invitation. Thank You for contact me. Sincerelly,   'Eiju"
Mar 15
Mohasin Ahamed Chinnapattan commented on Mauro Assis's blog post How to hack a remote control (and a flying shark) I
"You mentioned collector is set to ground but emitter is set to ground in your diagram "
Mar 14
Profile Iconbadrkhan mijbas khalifa, Bethany Cruz, Mrs. Priscilla Bonneval and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Mar 13
Edward Evans is now a member of GarageLab (arduino, electronics, robotics, hacking)
Mar 9
Profile IconAllan Zilkowsky, Shubham Dewangan, FAUSTO PEREIRA ROMANO and 4 more joined GarageLab (arduino, electronics, robotics, hacking)
Mar 1
Dave Lewing is now a member of GarageLab (arduino, electronics, robotics, hacking)
Feb 27

© 2017   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service