• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

AVR microcontroller programming

Red Squirrel

No Lifer
What's a good resource on learning how to code Atmel AVR microcontrollers? Like code syntax, functions, classes etc? I've found some small sniplets here and there online but a full blown reference would be nice. Like how to use i2c, SPI, the ADCs, etc...

I have a general understanding of how you write bits and such and from what I read most of it involves this, but what bit do I write where to do what, is not really explained all that well. You can set the pin mode, but what do I set a pin to to get a certain mode and so on.

Just looking for a good resource to get started. I ordered this programmer, think it will work well?

http://www.amazon.ca/gp/product/B00V35SVHA?psc=1&redirect=true&ref_=oh_aui_detailpage_o00_s00

I plan to code under Linux using avr-gcc and avrdude, and just a text editor.

Also is there some libraries/frameworks that I should be looking into and are they fairly chip independant? Or is it easier to just make my own functions based on the chip I'm writing for?

I've used Arduino before but I want to actually learn to program the chips directly. I ordered 10 Atmega 328's to play with along with various i2c sensors. Chose those knowing that I can still use them for arduino too if I want to.
 
If you want to program the chips directly, you are looking at programming at the register level. The datasheet will provide all the information you need. Any library you use would be an abstract layer on top of that, like the arduino.
 
Maybe a good tip : Programmers notepad 2 is a simple editor but allows to automate functions such as executing of a make file. You might want to look for winarm and how it is used there. Then you can make the changes for gcc for avr.
 
I'm more looking for actual code resources. For example say I want to use the ADC on a specific pin, and want to read the value, what specific code do I need to do. The datasheets will have lot of info but some actual code samples/tutorial would be a good start too. Also how does the i2c/SPI pins on micros differ from the other pins? Can't I just implement i2c on any digital pin, or is there more to it than that?

I just ordered this programmer:

http://www.amazon.ca/gp/product/B00V35SVHA?psc=1&redirect=true&ref_=oh_aui_detailpage_o00_s00

That should work well in Linux right?

As I progress through what I'll probably end up doing is writing my own mini libraries for lot of stuff, though I imagine there probably are some I can download too.

I may also cheat and just use Arduino boot loader, but I want to avoid that just because I want to learn to program them directly.
 
I'm not entirely sure what you're asking. Code for AVRs is typically written in C, AVR assembly, or a combination of the two. You can use C++, but I would not recommend it. Any kind of coding reference is going to be a reference for C or C++. The only other real reference I can think of would be the reference for AVR libc. Things like I2C and SPI are standards, if you want to learn about them in general wikipedia is a good place to start. If you want to learn about how to use them specific to your AVR the datasheet is the go to resource. The AVR Freaks forum is probably the best place to ask questions, and has quite a few tutorials IIRC.

I'm more looking for actual code resources. For example say I want to use the ADC on a specific pin, and want to read the value, what specific code do I need to do. The datasheets will have lot of info but some actual code samples/tutorial would be a good start too.

If you want something more detailed than the data sheet you'll just have to google for tutorials or code samples. AVR Freaks may have that kind of stuff. Typically a hardware feature has one or more control registers where you will enable the device and set it up, then registers where you read or write data. IIRC for an ADC on AVRs, you have to set some prescalar values to determine how the ADC samples the input, then set a flag bit asking the hardware to start a read of the analog signal, then watch a flag bit to see when it's completed because it takes multiple clock signals, then finally read the value from the output register(s).

Also how does the i2c/SPI pins on micros differ from the other pins? Can't I just implement i2c on any digital pin, or is there more to it than that?

The pins that the datasheet talks about using for serial (USART), I2C, SPI, etc., have hardware attached to them that simplifies operation for you. For example with serial you set a few registers indicating baud rate, frame format, etc, and then the hardware will generate an interrupt when a byte is received, which you just read from a register. Or for writing, you write a byte to the output buffer and the hardware will give you an interrupt when the buffer is empty and it's ready for the next byte.

You can try to implement any of those on any digital pin via bit banging, that is, manually controlling every aspect of the transmitting and receiving bit by bit. Obviously this is a lot more work, is less efficient (the main processor core has to waste cycles handling the communication that's normally done in dedicated hardware), and it may not be possible to implement some of the more complex aspects such has high baud rate serial.

Also if you've never done anything with MCUs or low level stuff before the first thing I would do is read up on interrupts and get a solid understanding of what they are and how they work. You especially need to understand how to write code when they are involved, mainly how and when to use atomic blocks. Otherwise you will end up getting bugs that appear to be absolutely unexplainable.
 
Dig into the header files for your chip. You can see all the port and register definitions. It also comes with tons of useful macros.

I generally don't like programming on Windows, but I really like Atmel Studio for AVR programming. I haven't been able to get anything comparable on Linux, although I can't wait to try JetBrains new C/C++ IDE CLion 🙂

That's a good programmer. Sounds like you're on the right track. There are lots of libraries for AVR, so don't get re-implementing things like LCD drivers unless you're doing it for fun.
 
Last edited:
Thanks for the info on i2c and such, I was just curious what the difference is, makes sense now. Definitely wont bit bang it myself if I don't have to.

Never thought of looking in headers, I'll have to check that. Come to think of it I can probably start playing with that even without my programmer as I can at least go through the process of compiling and such.

I was thinking somewhere I could find a tutorial, the same way a regular C/C++ tutorial will introduce you to various commands like cout<< and so on, but guess I'll just have to look for examples and piece stuff together as I go. Been finding some here and there through searching and it looks fairly basic in a sense as it seems it's mostly just the thing of writing a bit to a certain variable to do something. Kind of like assembly but with access to what C/C++ has to offer. So just the thing of knowing what each variable is for and I did find tutorials that explain that.

Come to think of it, in a datasheet when it talk about say, PORTC is that actually the name of the variable in code too?
 
Never thought of looking in headers, I'll have to check that. Come to think of it I can probably start playing with that even without my programmer as I can at least go through the process of compiling and such.

You're better off looking at the avr-libc documentation I linked than headers. Because the headers support all/most AVRs, so needless to say they can be pretty messy like most library code.

I was thinking somewhere I could find a tutorial, the same way a regular C/C++ tutorial will introduce you to various commands like cout<< and so on, but guess I'll just have to look for examples and piece stuff together as I go.

Most of the "entry level" tutorials on this stuff are aimed at people using Arduino boards and rely on the abstractions in the Arduino libraries.

Come to think of it, in a datasheet when it talk about say, PORTC is that actually the name of the variable in code too?

Yes, avr-libc should have #defines for all of the ports and registers using the same names as the datasheet.
 
Have you checked out mysensors.org? It's a site related to arduino sensors for use in various home automation controllers (e.g. Mi Casa Verde). Even its not a perfect fit for what you're doing, it might be worth looking at.
 
Back
Top