Arduino Programming Without Arduino Libraries - Atmel Studio

5.1k views Asked by At

I would think this question has been asked thousands of times, I simply cannot find many resources on the subject.

I would like to program my Arduino Uno (ATmega328P) using Atmel Studio and the C language, minus the Arduino Libraries. What I mean by this is that I would like to write code as follows:

int main(void) {
     /* set pin 5 of PORTB for output*/
     DDRB |= _BV(DDB5);

     while (1) {
       /* set pin 5 high to turn led on */
       PORTB |= _BV(PORTB5);
       _delay_ms(BLINK_DELAY_MS);

       /* set pin 5 low to turn led off */
       PORTB &= ~_BV(PORTB5);
       _delay_ms(BLINK_DELAY_MS);
     }
}

Rather than code that is riddled with the oh so convenient Arduino functions. I want to get under the hood and get dirty with Arduinos!

That being said, I'm looking for any great learning sources that you may be able to offer so that I can expand my knowledge!

So far, the only somewhat useful source that I've managed to find is this page: https://hekilledmywire.wordpress.com/2010/12/04/22/

However, the images are missing and it seems minimalistic, anyways.

2

There are 2 answers

3
MikeD On BEST ANSWER

Provided you are familiar with C, I recommend to

  • start with the AVR Libc reference
  • inspect iom328p.h for your processor specific definitions (located under ...\Atmel Toolchain\AVR8 GCC\Native\[#.#.####]\avr8-gnu-toolchain\avr\include\avr)
  • optionally, in Atmel Studio create a new ASF board project selecting device ATmega328p and inspect the sources loaded into your project folder from the "user_board" template (which anyway is a generic nearly empty set of *.h's providing space for things you may/may not need)
  • have the complete processor manual close to you at all times - register and bitnames found there match with the definitions in AVR libraries

Be aware that the libraries coming with Atmel Studio and the toolchain support the m328P, but the UNO board per se is not supported by the ASF. However, for basic programming you will be fine.

adding ... on PORTB

PORTB is defined in your processor's specific ...io.h (1st bullet above) which is automatically included by including <io.h> and choosing the correct processor in AVR Studio. In the library of your processor you find

#define PORTB _SFR_IO8(0x05)

Looking up the processor guide (4th bullet above) page 615 you see that PORTB is at I/O address 0x05 (q.e.d.). _SFR_IO8(..) by itself is a macro defined in <avr/sfr_defs.h> to convert from I/O to memory address (yes the lower registers are double mapped as I/O and Memory, whereby Memory address is by 0x20 higher because the lowest Memory addresses are occupied by R0 to R31).

By including <io.h> you get from the AVR library

#include <avr/io.h>
// included by io.h
//    #include <avr/sfr_defs.h>
//    #include <avr/portpins.h>
//    #include <avr/common.h>
//    #include <avr/version.h>
//    #include <avr/io(your_processor).h> via processor declaration ... fuses
//        #include <avr/(maybe some more).h>

All these ...h's (and some more) finally let you program in C using the register/port/pin names you find in the processor manual.

There are some more usefull libs like

#include <stdint.h>                     // Type definitions, e.g. uint8_t
//    #include "stdint-gcc.h"
#include <avr/power.h>                  // clock prescaler macro
#include <avr/interrupt.h>              // interrupt macros

you will find libs to support reading/writing from/to program and flash memory etc. etc.

2
PhillyNJ On

You can do what you want but you are going to need a programmer like the Atmel-ICE, AVR Dragon, STK 500 or a AVRISP mkII. There a a few more. Atmel has a number of programers depending on your needs. There a also some 3rd party programmers that are a lot cheaper. I have the STK500 and Dragon. Love them both and they play nice with Atmel Studio 6.X.

A good learning resources is this book:

Make: AVR Programming By Elliot Williams.