Programming AVR MCUs without libraries like the Arduino library? - Page 1
Forum section: electronics / beginners

author: Kold
author_rank: Newbie
timestamp: January 26, 2015, 06:48:08 pm
content: Hi everyone; first post here!I've been doing a fair bit of embedded design projects using the Arduino, and I would finally like to break free of the hand-holding libraries that Arduino provides. However, this also means the amount of online help resources is now vastly reduced.I would like to be able to program AVR microcontrollers with regular old C from Atmel Studio (or even just compiling the code and programming the device manually). I've already taken the basic steps to do this: I have implemented an ADC and simple I/O to create a 7-segment display, but my next project is much more complex. I will need to be doing ADC, external interrupts, and using I2C+SPI. I imagine, also, that my project after that will be another order of magnitude more complex.I suppose, then, that I have three questions:Does anybody know of an online resource to help one learn to maneuver the AVR microcontrollers?When you folks began learning your first microcontroller in all its guts and glory, did you have the aid of helpful resources?If the only answer is to just throw yourself in and start reading the 400-page data sheet, do you have any tips before I throw myself in?Thanks!Edit: I realize I should have, perhaps, posted this in the Microcontroller sub-forum... please move it there if you think it more appropriate.

author: dannyf
author_rank: Super Contributor
timestamp: January 26, 2015, 07:20:24 pm
content: QuoteSadly, there is no other way around it.

author: nuno
author_rank: Frequent Contributor
timestamp: January 26, 2015, 07:22:24 pm
content: Well, it does go through picking up the datasheet and reading it. However, it doesn't mean you have to read everything and in detail. You'll need to have a "general overview" about what it has and does so, in a first reading, you can fast-read through the details, and from there on you just need to use it as a reference. When you need to use the ADC, then go back to the ADC chapter and read it carefully, and so on. Some microcontrollers' datasheets are better written to be used like this, having a very good overview in the 1st chapter(s); in AVRs the datasheets spread the general part a bit throughout the chapters on the specific modules, but usable anyways.Winever's (GCC for AVR) libc documentation will show you what functions are there that you can use. If you have the Arduino IDE, you already have Winever there and this documentation is included.There's an AVR centric forum, avrfreaks.net (there are very good EE people over there, too). It also has a collection of AVR specific projects that you can use in several ways.

author: Maxlor
author_rank: Frequent Contributor
timestamp: January 26, 2015, 07:34:10 pm
content: Quote from: Kold on January 26, 2015, 06:48:08 pmIt's not, actually. You will soon find that the documentation that Atmel provides is much more thorough and reliable than whatever documentation you find on Arduinos. And it's not just the data sheet: if you look at thedocumentation page for the ATmega32u4for example, you'll find dozens of application notes that describe all kinds of use cases.It is true that this documentation is a bit harder to understand than your average Arduino tutorial. I think the initial effort is well worth it though, since pretty much everything you'll ever need to know about the chip is there, and you can be quite sure that the information given is accurate too.

author: Kold
author_rank: Newbie
timestamp: January 26, 2015, 09:05:58 pm
content: Thanks for the advice, guys! Your words are encouraging...Time to... *gulp* start reading...

author: Simon
author_rank: Unknown
timestamp: January 26, 2015, 09:21:01 pm
content: You will find it quite a jump but worth it. I did as you and just went through one periphery at a time although I have not done many. Still setting up and reading the ADC is quite a thing when you do it yourself compared to the Arduino. The chips generally are set up ready to go so you only need to enable and work with the registers of the peripheries you want to use. I'm not sure what the Atmel studio has to offer there is the Atmel software framework they keep banging on about I don't know if that has any support for lower end chips. It certainly seems to add a little bit of the Arduino touch to arm processors from the little I have seen in tutorials. Unfortunately at most video tutorials are abysmal and are mostly marketing wank.

author: Puffie40
author_rank: Regular Contributor
timestamp: January 26, 2015, 09:30:08 pm
content: You will find the data sheet will give you all you need to know, and then some (like the behavior of the pins and power characteristics). With the proper compilers and some practice, you could even code the uP in assembly.Learning to refer to the data sheet will also allow you to jump to other processors like the PIC.

author: hneve
author_rank: Regular Contributor
timestamp: January 26, 2015, 09:35:55 pm
content: I also made the jump from arduino ide to atmel studio. I wrote a few entry level guides too, only I wrote them in my native language (Norsk)You will find things hard and maybe confusing at first but that's how it is when doing something new.Sent fra min D6503 via Tapatalk

author: dannyf
author_rank: Super Contributor
timestamp: January 26, 2015, 09:40:46 pm
content: Not sure if you are mind-set on Atmel Studio - I tried AVR Studio when it first came out and thought that was extremely bloated.Otherwise, you can do the development in IAR AVR, CB AVR, or even emBlocks - the last two are based on Code::Blocks, a superb IDE in my view.

author: G0HZU
author_rank: Super Contributor
timestamp: January 26, 2015, 10:09:02 pm
content: QuoteI've only used a few AVR parts but in all of my cases there is a chapter in the datasheet that prints a warning about the code examples given in the datasheetThis is partly to do with an issue with devices that have an extended I/O map. Also, there's some stuff about c headers. It's worth reading this chapter first and try to understand why they include this chapter because otherwise you may find you will get frustrated with the code samples in the datasheet.

author: Wilksey
author_rank: Super Contributor
timestamp: January 26, 2015, 10:24:37 pm
content: A quick Google search results but to name a few:http://www.avrbeginners.net/https://www.newbiehack.com/MicrocontrollerIntroductionABeginnersGuidetotheAtmelAVRAtmega32.aspxhttp://www.instructables.com/id/Help%3A-An-Absolute-Beginner-s-Guide-to-8-Bit-AVR-Pr/http://www.ladyada.net/learn/avr/I would check the main hobby guru sites such as Sparkfun, Adafruit, even YouTube, after all, most of us are probably here because of YouTube.AVRFreaks used to be a good resource, not sure what they have now, people do tend to focus more on Arduino than actual AVR these days.First decide if you want to focus on AVR C or AVR Assembler, then just do a quick Google search for "AVR for Beginners" for example.

author: Kold
author_rank: Newbie
timestamp: January 26, 2015, 10:37:03 pm
content: Once again, thanks for the advice, everyone! I just read through the SPI section of the datasheet and it wasn't bad at all! I internalize wireless com (ZigBee) protocols at work that are much worse than that was...Quote from: dannyf on January 26, 2015, 09:40:46 pmYour Code::Blocks-inspired IDEs intrigue me... because I use Code::Blocks to do all my C++ programming. But I do, already, have AVR Studio all set up and have been using it... food for thought!Quote from: Wilksey on January 26, 2015, 10:24:37 pmThanks! I had already done a preliminary Google search (in fact this is how I was able to get the ADC working in the first place for the most part), but couldn't find anything that really had more than basic fluff. I'll take a look over the places you recommended later when I get off work.

author: nuno
author_rank: Frequent Contributor
timestamp: January 26, 2015, 10:50:02 pm
content: The ADC is actually a pretty simple peripheral, that you can put to work with half a dozen or less register writes. Even when using the interrupts, dead simple.

author: amyk
author_rank: Super Contributor
timestamp: January 26, 2015, 11:41:38 pm
content: Datasheets are not usually meant to be read like a novel - read about the CPU first, then find the peripheral(s) you're interested in and read those sections. However, you should at the least read about the GPIO pins since they are shared between peripherals. Don't forget about the errata too!... and you actually don't need anything other than some wires and pushbuttons to program an AVR:http://youtu.be/UJHeDvr_doM

author: Psi
author_rank: Super Contributor
timestamp: January 27, 2015, 12:00:23 am
content: Most of the avr datasheets contain c code examples for many thingsAlso make sure you checkout the atmel app notes. They are basically a step by step example of doing lots of more advanced stuff. They don't always give you code but always provide a flowchart of the steps you need.Oh and avr external interrupts are easy.Enable the int you want by setting its enable bitSet if you want rise fall or bothMake sure you have global interrupts enabledAdd the isr signal function with the name of the intDone, it should now work

author: zapta
author_rank: Super Contributor
timestamp: January 27, 2015, 12:26:17 am
content: Quote from: Kold on January 26, 2015, 06:48:08 pmDatasheet is most useful. For AVR you can find also books and youtbue tutorials.Quote from: Kold on January 26, 2015, 06:48:08 pmNo much help, I had to build hardware the populate the memory and could figure out if the load operation is to load a register from memory or vice versa. It takes some time and patient.Quote from: Kold on January 26, 2015, 06:48:08 pmYou don't need to read the entire datasheet, just focus on the peripheral you are interested in. They are pretty much independent.BTW, you can do low level register access in the Arduino IDE, you don't have to use the Arduino libraries. It's a choice, not a requirement. BTW, Atmel Studio is a monster but this is a different story.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: January 27, 2015, 12:38:23 am
content: As a masochist (and purist, and only infrequent programmer in software so it doesn't matter,), I do it in assembler, which works just fine. You can set up an AVR Studio assembly project, or do it in C (GCC accepts assembler files, formatted accordingly; giving you the power of the C preprocessor for macros, which is handy). If you get fed up, scratching away like that, you can go to plain old C for as much as you like, and use whatever standard libraries you please. Which is basically what Arduino does (everything is a library function), just formatted a little cleaner.Tim

author: zapta
author_rank: Super Contributor
timestamp: January 27, 2015, 12:50:26 am
content: Quote from: T3sl4co1l on January 27, 2015, 12:38:23 am.. or C++ which is what Arduino uses (e.g. serial.println(...)).

author: alank2
author_rank: Super Contributor
timestamp: January 27, 2015, 03:59:03 am
content: Quote from: Kold on January 26, 2015, 06:48:08 pmI've learned everything over at avrfreaks.net. I didn't know how to flash an LED and started there. Read, Try, Learn, Repeat. Studying the datasheet is critical.