Programming Tutorials For Atmel Studio

Go To Last Post
26 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello there. As a noob in Atmel Studio and having a been familiar with the arduino sketches i would like to step my game up.Problem is that i cannot find a tutorial or anything to read the way i am looking for it so i can get started. It would be awesome if i could find something like the arduino references.

 

Anyway that is a giant step up for me so i would love if you could be patient with me :P Thank you in advance.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David (aka frog_jr)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh ... well thank you very much for your help but i would not like to spam for basic things... my knowledge of the language used in atmel studio to talk to the registers and stuff is alien writing to me for now. Thats why i asked for something basic but i will start reading on the tutorials here. Thank you 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

my knowledge of the language

And what language is that? Studio supports C and assembler. If you are using C/C++ (I guess, same as Arduino) then you need to get a book on the language of your choice regardless on what platform it runs on, then you need to learn the "dialect" for some of the platform specific stuff.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Let me correct myself what i really mean is that i am quite familiar with analogWrite , pinmode , functions and voids but not with something like this #define LED_PORT PORTB

I know a bit about the timers and the registers on the microcontrollers but everything is a bit fuzzy to me. I am sorry about my crude explanation but this is the best way i found describing my problem to you :D hope you understand

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For getting started, take a look at this post (Link).

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i am quite familiar with analogWrite , pinmode

But that's is not C or C++ it's an Arduino dialect to do some stuff. (written in C or C++)

 

but not with something like this #define LED_PORT PORTB

That is REAL C, so get yourself a C book (the C gurus here recommend "Programming Language Ansi C" by Brian Kernighan and Dennis Ritchie) and start reading, it will even help you with Arduino stuff.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Tue. Nov 7, 2017 - 12:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

js wrote:
Studio supports C and assembler

To be more complete: Studio supports C, C++ and assembler (two "dialects").

 


@jackaros: While there has been hints and tidbits above, let's try to give a fuller picture:

 

The Arduino "language" that you know is actually C++ plus a collection of "objects and functions" to make life easier for you. I will refer to the latter as the Arduino framework. This framework hides a lot of the gory details of the language and the hardware from you, instead putting up an easier to use "facade".

 

If you want to start using Atmel Studio, you can

  • stay within the realms of the Arduino framework. Atmel Studio can handle the Arduino framework and taking this route is more or less just a change of editor (or "IDE" to be more correct with terminology),
  •  "break out" of the Arduino framework, in which case the full complexity and details that the framework hides will hit you. What follows is a breakdown of these things.

 

Programming language - general

Ignoring assembler, the choice of language in Atmel Studio is between C and C++. These are standardized languages used in a lot of environments, from large server paks and mainframes down to the small microcontrollers we talk about here. There are no AVR specifics defined in any of those standardized languages. (See the next section for how this is solved.)

 

If you want to start programming in one of these languages you are strongly advised to start learning the generic language, and I would recommend actually reading a book (or several). Since C is a much smaller and simpler programming language it might be smart to start with that.

 

C++ is (with a few exceptions) a superset of C. But it is a very large and complex superset of C! Unless you are ready for a big challenge I would not recommend starting with C++ unless you've programmed in other object-oriented languages earlier (e.g. Java or C#).

 

Programming language and tool chain - AVR specific

So how is C or C++ used for programming AVRs? Well... There are special compilers that generate machine code for AVRs. Staying with those that come with Atmel Studio they are avr-gcc and avr-g++ respectively. (In fact, the Arduino IDE also uses avr-g++ but you never knew that...).

 

Specifics of AVR hardware

Another thing the Arduibo environment hides from you is the hardware specific things re AVRs. All peripherals (digital I/O pins, timers, UARTs etc...) are controlled through "registers" that is written to or read from. This is what happens behind the scenes when you call e.g. digitalWrite() in your Arduino environment. When coding in plain C or C++ it will be you yourself that writes to, and reads from, those registers.

 

With the avr-gcc tool chain comes a huge set of source files with definitions to make this somewhat easier, but it will still be a big leap coming from Arduino. Instead of calling a simple function to set up a UART, you will need to write to several different things to several control registers (what you write there depends on what transmission speed, parity bits etc you want). Even something that you might find trivial using the Arduino framework, e.g.

digitalWrite(13, HIGH);

will be more contrived in plain C or C++. To begin with, in plain C/C++ the pin is not known as 13 (that is something specific for Arduino boards, and the avr-gcc/g++ tool chain does not constrain itself only to those boards) - instead you will refer to it by the port name and pin number within the port (e.g. port B pin 4). You will need to configure the pin to be an output (writing a specific value to one of the control registers of  port B). And then you will write a value to another control register of port B (yes, it has several) to actually set it high. Here's an example of how it might look:

 

// Configure port B pin 4 as an output
DDRB |= 1<<4;

   .
   .
   .
   
// Set port B pin 4 high
PORTB |= 1<<4;
   .
   .
   .
// Set port B pin 4 low
PORTB &= ~(1<<4);

In that, DDRB and PORTB are things specific for the avr-gcc tool chain. The rest ( all those |= , &=, << and ~ ) are standard C.

 

And here's the thing: There is no one source to these different things. You will have to go to different sources to find out the different things:

  • Generic C/C++ will be described and explained in e.g. textbooks, or general C/C++ tutorials somewhere on the net. They will not contain any AVR-specific stuff.
  • Things specific to the avr-gcc tool chain (including its "run-time library) is documented in the avrlibc documentation. But this will not contain the details of e.g. the DDRB or the PORTB registers (or the UART control registers, or any other AVR hardware specific stuff). That documentation is in...
  • the data sheet for the specific AVR.

 

Yes, that's the way it is. (And it might become even worse when you are after the advanced stuff!) And it will be largely up to you to put these different parts of documentation together.

 

The greatness and success story of Arduino is largely due to them hiding this complexity to the developer. If you want to break out of Arduino then you will get hit by the complexity and details.

 

So, is there no "middle ground"?

Yes, there is. Since the Arduino framework is in itself written in C++, and Arduino is compiled with the avr-gcc tool chain, nothing stops you from inserting a bit of low level C or C++ into your Arduino sketch. You might need to do this if you have a specific piece of hardware not supported by the Arduino framework. But you can actually do that using the Arduino IDE - there is no need for switching to Atmel Studio to do this.

 

On the other hand, Atmel Studio can import Arduino sketches and you can then continue working on them in Studio. One of the advantages here is that you will be able to do real on-chip debugging (using something like the Atmel-ICE). Side note: A possible drawback is that Atmel Studio does not support the flash programming method you're used to in Arduino IDE. If you want to keep your bootloader in place in your arduino hardware then you will need some tricks in  Studio to program the flash.

 

Which leads us to the big question...

 

Why do you want to switch to Atmel Studio?

There might be good reasons for it. But it might just as well be a misunderstanding about what can be done in the Arduino IDE, or what Atmel Studio can offer. So, tell us why you want to switch, and we could give better advice regarding if it is a wise move.

 

I'm not advising against switching, neither in general or categorically. It's just that the reason needs to be thought through and justified.

 

If the reason is "I'm just curious" then that's just fine with me. Curiosity is something positive! But again, be prepared for a situation much more complex than what you encountered in "pure Arduino".

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There are lots of tutorials - including videos - linked from the Atmel Studio pages on the Atmel & Microchip sites:

 

http://www.atmel.com/microsite/atmel-studio/

 

http://www.microchip.com/avr-support/atmel-studio-7

 

(they aren't quite the same -  so probably worth looking at both)

 

Here, for example, is Introducing Atmel Studio 7:  https://www.youtube.com/watch?v=...

 

Atmel's YouTube channel is now called Microchip Makeshttps://www.youtube.com/user/AtmelCorporation

 

Here are some general 'C' learning & reference resources: http://blog.antronics.co.uk/2011...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:

i am quite familiar with analogWrite , pinmode

But that's is not C or C++ it's an Arduino dialect 

Actually not: they are just function calls; exactly the same as in standard 'C' or C++ - nothing special or specific to Arduino.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wow i really have nothing more to say than thank you . I started working with arduino microcontrollers a long time ago and the reason i want this change is to get something more out of it.I knew for a long time that it was a pretty powerful platform but i never got the change of using all of its resources. Thank you very much all of you for your answers honestly i am happy to know what i have to do and from where to start. I shall now get reading.

Thank you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sounds like you simply want to learn C or C++. As we always say here the best place to learn C (or C++) is not using an AVR. You are far better off learning C using an IDE on a PC such as Visual Studio or Eclipse or Code::Blocks or NetBeans because the difference with PC programs is that you really can write:

#include <stdio.h>

int main() {
    printf("Hello, world\n");
}

compile that and see an immediate result when you run it. This is not true for an AVR. Similarly the debugging facilities for PC .exe programs are far more extensive than for AVR.

 

So if you are just trying to learn the basics of the C(++) language I'd start there.

 

Having said that, if you know Ardhuino you already kind of know C(++) anyway. The fact is that there's very little difference between:

setup() {
    pinmode(13, OUT);
}

loop() {
    digitalWrite(13, HIGH);
}

and doing it as:

setup() {
    DDRB |= (1 << 3);
}

loop() {
    PORTB |= (1 << 3);
}

The latter will achieve much the same (I probably got the wrong port and pin number though!) but is done using "standard C" while the former is obviously making use of Arduino library calls.

 

So you could just start to use "real C" in the Arduino IDE as a start and when you have learned more branch out to writing complete, standalone C programs in AS7

(and I keep saying "C" but I really mean "C" or "C++" - your choice).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
As we always say here the best place to learn C (or C++) is not using an AVR.

Absolutely!

 

You are far better off learning C using an IDE on a PC such as Visual Studio or ...

The advantage of choosing Visual Studio in this particular case is that Atmel Studio is really just Visual Studio in disguise!

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have already downloaded visual studio and gotten started learning C it is going to be along way since i start programming AVR microcontrollers using C but its going to be fun :D Thanks everyone for your help.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You don't have to use pinmode, digitalwrite, etc, in the Arduino IDE, you can write directly to port pins, after all it is the same compiler as in AS7...

 

Here is blinky using Arduino IDE but talking directly to port pin:

 

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

 */


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  DDRB = 0x20; //pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  PORTB |= (1 << PB5); //digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  PORTB &= ~(1 << PB5); //digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

 

Have fun!

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

You don't have to use pinmode, digitalwrite, etc, in the Arduino IDE, you can write directly to port pins, after all it is the same compiler as in AS7...

 

Here is blinky using Arduino IDE but talking directly to port pin:

 

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

 */


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  DDRB = 0x20; //pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  PORTB |= (1 << PB5); //digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  PORTB &= ~(1 << PB5); //digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

 

Have fun!

 

Jim

 

ki0bk wrote:

You don't have to use pinmode, digitalwrite, etc, in the Arduino IDE, you can write directly to port pins, after all it is the same compiler as in AS7...

 

Here is blinky using Arduino IDE but talking directly to port pin:

 

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

 */


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  DDRB = 0x20; //pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  PORTB |= (1 << PB5); //digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  PORTB &= ~(1 << PB5); //digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

 

Have fun!

 

Jim

 

Thanks for the answer ill get to the AVR stuff after i am a bit familiar with C . I have tried many examples that write to the registers and to the ports without the extra "background" code but there is no point if i do not understand every single thing so i can create my own code later on.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One of the keys to understanding all this is the use of "|= (1<<n)" and "&= ~(1 << n)". Look in the tutorial form for a thread called "bit manipulation 101" which will explain more. The key to the skill is to be able to set, reset or read one ore more single bits in a group of 8.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
 Look in the tutorial form for a thread called "bit manipulation 101" 

It's actually called 'Bit manipulation (AKA "Programming 101")' - here: http://www.avrfreaks.net/forum/tut-c-bit-manipulation-aka-programming-101

 

It's still all Standard 'C': just a part of the language that is particularly heavily used in this context - and less commonly in "application" programming.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note: the code I posted above was tested on an Arduino pro-mini, digital pin 13 may be mapped to a different port/pin on another platform. 

That is one of the advantages of using the Arduino functions, they know what port/pin combo makes up D13 on each platform, UNO/MEGA/mini/micro.....etc...   

I had to look at the schematic of the board to find what port/pin was D13 to write the code above, so it may not be portable to other platforms,  as they say YMMV.

 

 

Jim

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

One of the keys to understanding all this is the use of "|= (1<<n)" and "&= ~(1 << n)". Look in the tutorial form for a thread called "bit manipulation 101" which will explain more. The key to the skill is to be able to set, reset or read one ore more single bits in a group of 8.

That is where i struggle. I am pretty familiar with C programming from AVR mcus and other applications but writing and reading individual bits from registers is a problem that needs to be addressed.

Thank you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

clawson wrote:
 Look in the tutorial form for a thread called "bit manipulation 101" 

It's actually called 'Bit manipulation (AKA "Programming 101")' - here: http://www.avrfreaks.net/forum/tut-c-bit-manipulation-aka-programming-101

 

It's still all Standard 'C': just a part of the language that is particularly heavily used in this context - and less commonly in "application" programming.

Thanks for the link :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

Note: the code I posted above was tested on an Arduino pro-mini, digital pin 13 may be mapped to a different port/pin on another platform. 

That is one of the advantages of using the Arduino functions, they know what port/pin combo makes up D13 on each platform, UNO/MEGA/mini/micro.....etc...   

I had to look at the schematic of the board to find what port/pin was D13 to write the code above, so it may not be portable to other platforms,  as they say YMMV.

 

 

Jim

 

 

 

 

No problem mapping pins for different mcus just a quick peak on the datasheet and you are set.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that you don't have quote the entire post every time - just include enough to give context.

 

Or use the 'Reply' button - then the forum will automatically identify which post you're replying to.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your feedback i did not know that was possible without quoting the full post :P

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Google+

Nov 13 2017

https://plus.google.com/+MicrochipTech/posts/Ejbg2nn5JJj

...

Watch this first video in our “Getting Started with Atmel Studio 7” series to get a quick overview of the various development tools that we offer to get your design up and running in no time: http://mchp.us/2icNmhf

 

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

videos 2 through 15 are up as of about a week ago.

https://www.youtube.com/user/MicrochipTechnology/videos

 

"Dare to be naïve." - Buckminster Fuller