We love looking at hardcore electronics projects with a beefy microcontroller and hundreds, if not thousands, of lines of code at its center. But everyone needs to get there somehow.
This tutorial series aims to make you comfortable programming the Atmel AVR line of microcontrollers. Whether you’ve never touched a microcontroller before, or you’ve cut your teeth with dozens of Arduino projects, this will help you get right down to the hardware and give you the confidence to build anything.
Unlimited recording storage space. Live TV from 60+ channels. No cable box required. Cancel anytime. Microchip Technology Inc. Is a leading provider of microcontroller, mixed-signal, analog and Flash-IP solutions, providing low-risk product development, lower total system cost and faster time to market for thousands of diverse customer applications worldwide.
Series roadmap:
Prerequisite knowledge
Here’s the good news: I’ve set the bar quite low. You need basic knowledge of installing programs on your computer and using them. You should have some idea of how a solderless breadboard works and it is advisable that you have a multimeter and know how to measure voltage with it. And you shouldn’t be afraid of using Google to research questions that aren’t explicitly answered here.
What does a microcontroller actually do?
This is a loaded question. For the sake of understanding I’ll take this down to the most simple explanation:
- A microcontroller takes some type of input
- It makes a decision based on the software you have written
- The outputs are changed based on the decision in step 2.
A microcontroller does what you program it to do. It does so quickly, and reliably.
How does it work?
For this tutorial series I will be discussing digital logic. That is to say that all input and output pins will be judged based on a voltage of zero, or 5V. This produces our digital 1’s and 0’s, with 5 volts as a one, and zero volts as a zero.
So if you want to light up an LED just wire up the circuit to a pin, make that pin an output, and set it to a logic high (5 volts). If you want to add a button, connect it to a pin that is set as an input and program the chip to measure the voltage level of that pin. It really is that easy, once you learn how to write the correct commands so that the chip understands your wishes.
A look at he chip itself
I’ve decided to use an ATmega168 microcontroller. It’s a powerful chip but it’s no harder to start using than its younger brethren. It will leave plenty of room for you to grow into your projects, while remaining affordable (less than $4.50). Here’s a diagram of it:
This is often called the pinout as it shows what each of the 28 pins on the chip actually does. All of these pins have multiple functions and that’s why there’s long lines of text next to each, except for five which only have one name. These are the pins having do with voltage and ground (VCC, GND, AVCC, AREF, AGND), an important issue with microcontrollers.
Integrated circuits need a steady voltage source. This means as part of our project we’ll need to build a voltage regulator. This is an easy thing to do on a breadboard, and you should be able to get your hands on the parts locally. It is also worth noting that there is a semi-circular dimple on the top of the chip. This is something you’ll find in the plastic case of these dual-inline-package chips an it’s used to make sure you don’t plug it in backwards.
Take a look at the pinout once again and look for the pins whose names start with PD. You should see eight of them total, labeled PD0 through PD 7. This is a fantastic example of the 8-bit nature of these chips. PD stands for Port D, one of the input and output register. Everything in these chips centers around 8-bits. That’s a sequence of eight 1’s or zeros in different combination. If you want to turn on or off specific features, you change one or more bits in a 8-bit register. Every time you want to change one pin you must address all eight in the register. We’ll learn much more about this but not until the third part of the series.
Programming
The ATmega168 is a programmable microcontroller. But better yet, it’s reprogrammable. In fact, when you’re working on a project you’ll most likely reprogram it several times an hour.
This chip has a size limit of 16 kilobytes of programming space. In these modern times of 64 gigabyte iPods 16 kilobytes might sound minuscule. But in reality that’s 16 kilobytes of machine code. You can do a lot with that… trust me.
You do need some type of hardware to get the code onto these chips. Usually this comes in the form of an AVR programmer. In the second part of this tutorial we’ll look at several different programming options, then build and program a test circuit.
Do Your Homework
To get ready for the rest of this tutorial series I need you to gather some tools. You must have some type of computer, be it a Linux box, Mac, or Windows PC. This will run software that takes our code, compiles it into something the microcontroller can use, and then tells a programmer how to write it to our chip.
The compiler
We’re eventually going to be writing our own code for the AVR, which uses the RISC architecture. But we’re doing this on a computer with x86 architecture. The tool necessary to accomplish this is called a cross-compiler. This is quite possibly the best reason to choose AVR for development, there’s an excellent tool chain available that can easily be installed on multiple platforms.
- Mac users: Install CrossPack
- Windows users: Install WinAVR
- Linux users: Debian and Ubuntu users should install the GCC-AVR package which includes the entire toolchain. Others may want to look at the AVR-libc toolchain page for help compiling the packages.
This is not the only option. Many Windows users swear by Atmel’s free AVR Studio software. This is the only time I’ll reference it as I don’t have a Windows machine and have never tried that package.
Programming software
Our software-of-choice to run the hardware programmer is called AVRdude. If you installed one of the toolchains above you should already have this program. Go to a terminal window or the command prompt and type the following to make sure:
C Programming For Microcontrollers Pdf
This will show the help screen. If you get an error, you should check to make sure you properly installed the toolchain in the previous step, or go download AVRdude yourself.
What the future holds
That wraps up the introductory installment of this series.
Part 2: In the next installment of this series we’ll take a look at several pieces of hardware that you can use to program an AVR microcontroller. I’ve written a hello world program and will walk you through building the circuit on a breadboard, connecting the chip to a programmer, and using AVRdude to write this simple firmware to the device. I don’t want to get you too excited, but this does involve a flashing LED.
Part 3: A pre-compiled HEX file was used to program the AVR microcontroller in Part 2 of the series. In this portion we’ll look at the C language source code that made up that firmware. I’ll also talk in-depth about the peripherals available on the chip and detail how to use them. We’ll finish up by adding functionality to the original program, recompile it, and reprogram the chip with the upgraded version.
Part 4: Now that you’ve acquired AVR programming skills I’ll show you how to start building cool stuff with them.
How To Program Atmel Microcontroller
Follow me:
We are ready to go, all we need to do is make a little test. So click file/new/project and select 'AVR assembler project under 'Assembler' option. Click OK and select ATMEGA328P and click OK again. Now in the text editor copy and paste the following code:
This will make the built-in led of the Arduino blink. Now click in build/build solution and after that go to tools/Send to Arduino UNO. You should see the Rx/Tx leds flashing and after that the built-in led will start blinking. And that's all, I hope you found this information useful. Now you are free to use Atmel Studio to make your Arduino projects.
This is a non-invasive way to connect your Arduino board to Atmel Studio, so you can still using it as normal with the Arduino IDE. If you have a different board avrdude must be configured properly according the board you want to use.
If you like it, and want to support me give me a like in my store fanpage: DAFR ELECTRONICS and also vote for this instructable in the contest.
Thank you, I hope you find it helpful. If you have a question feel free to ask ;)
Microcontroller Tutorial Pdf
Written by:
How To Program Atmel Microcontroller
Daniel Fernandez R.