Using Atmel Studio 7 to Program Arduino Uno

The Arduino Uno is a 5V, 8-bit, 16MHz Advanced Virtual Risc micro-controller board based on the Atmega328.

Arduino Tech Specs

The Uno has 14 digital input/output pins. Six of these pins can be used for PWM (Pulse Width Modulation), 6 analog inputs, a 16 MHz quartz crystal internal oscillator, a USB connection, a power jack, an In-Circuit Serial Programmer header and a reset button.

The Arduino Uno contains everything needed to support the micro-controller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started..

What makes the Arduino such a popular platform for IoT and Makers is the very simplified Arduino command language. The Arduino language is a C++ implementation with special header files that make it easy to code the Arduino with simple commands like pinMode() or analogRead().

Here is an example sketch in Arduino IDE:

/*
    LED Blinker in Arduino
*/

const int led = 13;

void setup() {

    // set up pin 13 as an output pin
    pinMode(led, OUTPUT);

}

void loop() {

    // blink the led
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);

}

I wanted to set up an experiment to get the Arduino Uno to run plain C without arduino.h. For this, we'll have to program the Uno outside of the Arduino IDE so we'll need to download and install Atmel Studio 7.

Atmel Studio 7

Atmel Studio 7 AS7 Reference

Atmel Studio is based on Visual Studio so for all you developers out there, this should be a very familiar interface.

Start a new project. Let's call it Blinker____Demo_In____C.

In Atmel Studio and C, the pins and ports of the micro-controller registers must be read from and written to by referencing the pin codes from the controller's data sheet.

Here are the ports and pins for the ATmega328P:

ATmega328p

Back in Atmel Studio, we have fired up a new project and are ready to begin setting up our program. I am going to use PORTB and pin PB5 as our output pin for this example. We'll setup DDRB as an output, then write to pin PB5 just as we did before in the Arduino IDE. Just a few different commands.

/* 
    main.c
    Blinker Demo in C 
*/


#include <avr/io.h>       /* Define pins & ports */
#include <util/delay.h>   /* built-in time function */

int main(void) {

// Data Direction Register B: Set Output 
DDRB |= ((1 << DDB5));  

  while (1) {

    PORTB = (( 1 << PB5 )); /* Turn on LED in PORTB */
    _delay_ms(1000);        /* wait */

    PORTB = (( 0 << PB5 )); /* Turn off LED in PORTB */
    _delay_ms(1000);        /* wait */

  }                                                    
  return 0;                                 
}

Instead of writing the pin HIGH or LOW as we did in the Arduino IDE, once we have accessed to correct data register, we set the value of the register to 1 or 0 by left-shifting the register bit.

See here for more on ports, pins and registers.

Now that we have completed coding our project, let's run our first Build. This creates a Make file that our programmer will use to flash the memory.

After our Build runs, make sure there are no warning or build errors. Next, click Tools/Device Programming. You should see the screen below.

Programming Mode

Under Flash, click Program. The output will read:

Erasing device... OK
Programming Flash... OK
Verifying Flash... OK

The finished output:
Arduino Pin13 (PINB5) continuously blinking on and off for 1 sec.

Project

To check out more of my Arduino sketches, please visit my Arduino GitHub repo.

Craig Derington

Veteran full stack web dev focused on deploying high-performance, responsive, modern web applications using Python, NodeJS, Django, Flask, MongoDB and MySQL.

comments powered by Disqus