4

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.

Ryan T. Grimm
  • 1,307
  • 1
  • 9
  • 17
Joshua Granger
  • 152
  • 1
  • 2
  • 10
  • 1
    https://github.com/dwelch67/avr_samples Atmel is generally good about documentation. start with the board, figure out what flavor of avr you have (atmega328p for example) then search for that at atmel, and more docs than you care to read show up, look for datasheets, reference manual, user guide, family user guide, etc. Some vendors the datasheet has just pin stuff for the hardware folks, some it has all the programming info, some it has some addresses, but the family doc covers the details, etc. atmel may be one of those multi doc vendors. – old_timer Jun 19 '15 at 22:01
  • 1
    http://www.atmel.com/devices/atmega328p.aspx then get ATmega48A/PA/88A/PA/168A/PA/328/P Complete then in the IO ports chapter, Register Description section, eventually you find PORTB which shows the bits for the port with the address 0x05 (0x25) which has to do with two different instructions/addressing modes in the instruction set the 0x05 my guess is the one you find if you grep through the IDE or compilers headers for the tool suite you are using. and understand those addresses dont have to be universal for all avrs, can change from one to another – old_timer Jun 19 '15 at 22:04

2 Answers2

3

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.

MikeD
  • 8,861
  • 2
  • 28
  • 50
  • I've selected this as the answer, though perhaps it's things I've already inspected. That's hardly your fault, though -- I should have asked my question more carefully. I do know C programming (I write desktop applications in C# and I've had a Microprocessors class using a Nexys4 FPGA board). An instructor of mine recommended that I learn to program an Arduino in pure C with Atmel Studio rather than in "Arduino C" with the Arduino IDE. Thus, I've been looking for good sources to explain the bit registers (and the addresses in which they are located, as well as how to access those addresses). – Joshua Granger Jun 19 '15 at 18:51
  • I think my gap in understanding (thus far) is why/how/where the compiler determines what "PORTB",etc. means (especially since it isn't clearly defined anywhere). Overall, there doesn't appear to many helpful articles on getting started with microprocessors in Atmel Studio (with Pure C) – Joshua Granger Jun 19 '15 at 18:55
  • 1
    re bit registers the processor manual shall be your guide (my answer 4th bullet --> link), for quick reference see pages 611ff, 65ff, 17ff, re PORTB ... see edit – MikeD Jun 19 '15 at 21:15
1

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.

PhillyNJ
  • 3,859
  • 4
  • 38
  • 64
  • I think you misunderstood the question. I already have the hardware (Arduino Uno / ATmega328P) and software (Atmel Studio) required to program the chip on the Arduino Uno. My question, on the other hand, is geared towards locating learning material so that I may become more familiar with the actual coding of the chip. – Joshua Granger Jun 17 '15 at 18:09
  • @JoshuaGranger Sorry I misunderstood. I updated my answer with a great book that can help you learn AVR programming. – PhillyNJ Jun 17 '15 at 18:12