How to program Arduino with Atmel Studio

http://yalneb.blogspot.com/2017/05/how-to-program-arduino-with-atmel-studio.html

Arduino is a fun entry point for those of you who want to program microcontrollers. It comes in two parts: the Arduino development board itself, and the Arduino IDE, a very easy to use notepad-like environment where you can write your code.The Arduino board itself is nothing more than a development board for an ATmega328P microcontroller with some nice extras, while the Arduino IDE takes care of making programming a microcontroller a kid's game.

However, the IDE can sometimes get in the way of a more advanced programmer, as it ads a lot of extra code we sometimes do not want (o prefer to do our self). The remedy is to program the ATmega328P with Ateml Studio 7, just as if it were a regular microcontroller. But for that, we first need to tell it how to "talk" to the Arduino to program it.



Software installation


Download and install on your Windows machine (or your virtual instance) the following software:


Connect your Arduino board


Arduino connects to your PC over USB, but internally it emulates a serial connection. To program your Arduino from Atmel Studio, you first have to know on which serial interface it is connected. For that, plug your Arduino to you computer (if its the first time, windows will tell you that it is installing drivers), and open your device manager and look under COM devices for your Arduino.

 


As you can see, my Arduino got COM 19 assigned. Yours will probably be different.


Configure Atmel Studio


Open Atmel Studio, and if prompted, select Advanced Options (or something similar). Then go to tools, and select external tools.



Title

Once there, create a new tool (actually a scrip-tlike command) with the name ArduinoDownload.


Command

Because Atmel Studio is not capable of talking directly to Arduino, we will use part of Aruino's IDE, specifically, avrdude.exe.

C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude.exe



Arguments

We will call avrdude.exe with the following argument:

-C C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf -v -pATmega328p -carduino -P\\.\COM18 -b115200 -Uflash:w:$(ProjectDir)Debug\$(TargetName).hex:i

Notice the "COM18", replace it with your COM. If you want more details, here is what you just added:

  • -C path to avrdude.conf: this is the default configuration file for avrdude.exe.
  • -v: add more verbosity when connecting to Arduino, useful for debugging.
  • -pATmega328p -carduino: we are telling AVR dude that we want to program the ATmega328p microcontroller.
  • -P\\.\COM18: the COM port on which Arduino is connected.
  • -b115200: serial port speed for communications.
  • -Uflash:w:$(ProjectDir)Debug\$(TargetName).hex:i: where to look for the compiled code to send over to Arduino

 

 

 Configure test


Let's create a quick code and test that everything works. In Atmel Studio, go to File >> New >> Project, and create a new C/C++ project for the ATmega328p. Open main.c and overwrite its content with the following code:

#include <avr/io.h>
void main(void)
{
    DDRB = (0x01<<5);
    PORTB = (0x01<<5);
    while(1)
    {
        for(volatile unsigned long i=50000; i>0; i--);
        PORTB = ~PORTB;
    }
}


Now compile everything (press F7 by default) and go to Tools>>ArduinoDownload. A black terminal should open telling you that it is sending your compiled code over to Arduino. If it worked, you should see a LED blink on your Arduino. If you have any troubles, leave a comment below and I'll be glad to help you.

 

No comments :

Post a Comment