Introduction: Getting Started With WinAVR
Dear All. I'm writing this tutorial/instructable to help you get started with the microcontroller programming.
I'm going to explain you the thing right from the scratch, so don't worry if your are not familiar with any sort of microcontroller programming i.e. embedded coding and if you don't able to get the exact meaning of what I exactly want to say, you can always put the questions in comment section. So, let's get started.
What is microcontroller & AVR by Atmel?
Wikipedia says: 'A microcontroller (sometimes abbreviated µC, uC or MCU) is a small computer on a single integrated circuit containing a processor core, memory, and programmable input/output peripherals.'
Yeah, that's what it is. It's a small computer, which can work for you.
Ohkay then, till now we got to know what's microcontroller. Now, we need to make them work for us. So, the answer is 'Programming' them.
About AVR atmel Wikipedia says: The AVR is a modified Harvard architecture 8-bit RISC single-chip microcontroller, which was developed by Atmel in 1996. The AVR was one of the first microcontroller families to use on-chip flash memory for program storage, as opposed to one-time programmable ROM, EPROM, or EEPROM used by other microcontrollers at the time.
Wikipedia says: Computer programming (often shortened to programming) is a process that leads from an original formulation of a computing problem to executable computer programs.
Oh yeah, pretty confusing, huh?
We can simply say, programming means telling the micro-controller to what to do next or what to do when certain things happen.
So, without further due, let's get started.
Step 1: Installing WinAVR
So, if you want to start with you should have two things 1. Courage and 2. Executable file.
Yeah I know as you are here you have the first thing.. ;-) for the second step you have to grab the executable file from official sit e of winAVR or directly go here to download from Sourceforge. The latest build that I've been following since couple of years is 20100110.
Installing process is very simple just click a couple of next buttons and changing the installation directory if required. By default, it goes to the root of the drive where you have your OS.
The important thing to note here in the screenshot is you have to check whether the "Add Directories to PATH" is checked. This option sets up the system variables.
Step 2: Step 2: Creating First Project
Here I'll be illustrating the simplest way to get through the WinAVR. The installation of WinAVR installs following program which we will be using to write/compile the code
- Programmer's Notepad - it's generic notepad with the additional capability of syntax highlighting.
- Mfile - it's the utility to create the Makefile. It's very important in-case of compiling and programming.
- The programmer notepad is the environment where you will be writing the code. You are free to choose your own language to write the code but compiling process or method will change. So, now let's start, the first thing first is you have to create your own folder where you can host your project. I have chosen to use keep it under a folder named AVR located at user home folder.
Creating your first project:
Now name the project whatever you like and hit "Save" to safeguard your project. Now, we have the folder and project both the things, it's time to add the code to it. To do so, go ahead and click on the file create a new file and then select the type of the language you will be using for the project in my case it is C/C++. The selection of the language helps editor to highlight the syntax.
Adding new file:
As the project is saved and ready to use now you can add the files to the project. You can create new file and dave it via "File menu". These are known as source files. The source files must include the "main.c" which is the file will get compiled. In addition, you can also include/create your own header files. Header files are the supportive to the main.c which fetches following advantages,
- It helps to organize your code/project.
- It helps to make the code readable and concise. So, do not end up writing huge codes.
- Also, it allows you to include the files from multiple projects. And these files can be portable.
Also, remember if you create a new file it won't get added to the project. You have to do it manually. By right clicking on the project and selecting add file option, locate the file and click open. Shown in the images.
Writing the code:
As this is the tutorial to setup AVR toolchain. I'll be not focusing on the how to write the code. You can download te whole project or just download the main.c from the attached files.
Step 3: Step 3: Creating Makefile
Creating Make file:
Make file is the file which assists the editor, compiler, and linker. It consists of the file name which is the source file, in this case, it's "main.c". Also, it holds the information of your microcontroller and programmer you are using and the commands and ports via it is connected to the computer. Precisely it contains the rules for your project to compile. Open the program Mfile located in the start menu under WinAVR folder.
After opening, you will see the window in which you can select the parameters from the drop down menu. For example, I'll be compiling my project for the atmega16a microcontroller. Here in the Makefile only you can select your source file e.g. "main.c". Once do the necessary changes you can then "Save As" the modified Makefile into the directory where the main source file is located.
Once you have the files ready you can go ahead and compile the program. To do so you can go to "Tools" option and choose following commands,
- Make All: Command to compile the code
- Make Clean: It's the command to delete all files created during the compiling and linking process
- Make Program: This is used to burn the program to the microcontroller target.
Happy programming!! That's all you need to get started with compiling the code for AVR controllers. `