AVR C Programming - Coding in Ubuntu Terminal



UpdatedUpdatedOctober 06 - 2017October 06 - 2017

Summary

In this tutorial we'll first create a Makefile. Next we'll write a small C file.
Finally we will create a .hex file with the help of our Makefile and we'll burn
the .hex file into the microcontroller.

What is a Makefile

A Makefile is a special type of file. It has no extension. So when you create this file, you just have to name it
Makefile or makefile (depending upon the system) and have to make sure that it has no file extension.

Makefile contains shell commands. So every time you do not need to write several lines of commands on the terminal. All you have to do is to give a single command make and voila! Makefile will do the rest for you.

One Makefile for all projects

I usually used to write separate Makefiles for different projects. Recently while I was studying some sample projects of Elliot Williams, I got the impression of using just one Makefile for all projects. Later I edited his Makefile to make it more simple. I also added additional comments for better understanding.
Here is the full codes of that Makefile. You can use it for any project. It will translate any C file into .hex file and will burn the .hex file into the microcontroller's memory.

Please double-check line 7,8,9,10 and 18. If you use the same microcontroller like mine, then you might need to check only line 10.


Test our first project

You may follow the way I did or you may use your own preferred style.


  • Open a terminal ( Shortcut: Ctrl+Alt+T or Strg+Alt+T )
  • cd Templates
  • mkdir avr-C
  • cd avr-C
  • mkdir Lab1
  • cd Lab1
  • nano lab1.c
  • Now copy-paste the following codes and save the file.

  • Also create a Makefile inside Lab1 directory nano Makefile. You may use the Makefile which I've added before.
  • Write command make and hit Enter. It will compile the C file and will create all necessary files to generate the .hex file.
  • Connect the microcontroller mk2 board to your computer.
  • Write command make program and hit Enter. It will generate .hex file and will burn it into our microcontroller memory.

Now we'll connect any pin of PORT-D with any of the three ACTIVE-HIGH LEDs (Red or Green or Yellow) and connect any pin of PORT-C with the ACTIVE-LOW LED (Blue).

If you have done everything perfectly, you will see the result like the following image (the photo is underexposed so you can see the blue and red LEDs).

lab1.JPG