This is a follow-up of the previous Tutorial as a lot of things remained unexplained like how to customize or use the Makefile suitewhy using an infinite loop inside main (it’s a common question from freshers)why we need a cpu clock (like we have used 1MHz for the hello_world)where did those DDRB,PORTB stuffs come from and so on. Let’s walk through them one by one.

Why we need a CPU clock ?

It’s the first requirement for any digital device. Clock is an electrical signal like this –


Here is the time period or 1 cycle. Every digital device needs to know when to execute an instruction. Here’s comes the idea of pulse train/clock. It act like an indicator for the device to let it know when it should fetch and execute an instruction. If there was no clock the device would stand still doing nothing. On AVR controllers, the cpu executes 1 instruction within 1/2/3/4 cycles. While most of the instructions are executed withing 1 cycle there are instructions like ADIW, RJMP, IJMP and some others which takes 2 cycles and instructions like RCALL, ICALL – who take 3 cycles and RET, RETI -who take 4 cycles. So you can consider The CPU Clock As The Heart Of The CPU.

Where did those DDRB,PORTB stuffs come from ?

They are built-in Macros who point to particular SFRs(Special Function Registers). SFRs are registers/memories who control different modules of the controller. These macros comes predefined with the C library for AVR microcontrollers(here avr-libc).

Reason for the infinite loop ( while(1){} )

We wanted to animate the LEDs as long as the power stays on. Hence the infinite loop. Usually we want the controller to work as long as the power stays on. So we do stuffs inside an infinite loop to keep the controller awake.

Why we are using Makefiles ?

It is to automate the compiling and building process. To learn more about Makefiles read this Wiki page and GNU Makefile Manual . Obviously if you want to create your own makefiles you will need to consider the operating system you using. But the things are made easier for you. Use this Makefile suite which works on both Linux and Windows OS.

How to use/customize the Makefile Suite ?

There are 6 files associated with the makefile suite –

  1. README – You can put notes, compilation guidelines and other stuffs the user/you/developers may need to know to use the project.
  2. Common – This file consists of common variables and commands used in the makefile suite.
  3. Makefile_Linux – This file consists of commands needed for Linux platform.
  4. Makefile_Windows – Like the previous, this one is intended for Windows platform.
  5. Makefile – This file the one needed by the make program. It detects the OS platform and supplies the suitable script to the make program.
  6. Config – This is where you put the configurations of your project.

As you can see all you may need to know is the meaning of the variables used in the Config file. Let’s walk through them –

  • TARGET – this is name of your project. The generated hex file will be named after this one. Must not be empty.
  • SRC_FOLDER – the name of the top level folder which contains all your source files with extensions like .c, .S, .cxx. Must not be empty.
  • BIN_FOLDER – the name of the binary folder. This folder will be automatically created. Must not be empty.
  • INCLUDE_FOLDER – the name of the top level include directory of the external header files. Can be empty.
  • C_SRC_FILES – a list of C source files along with relative path to the top level SRC_FOLDER. Say you have 2 source files – main.c and spi.c. The main.c is in src folder and the spi.c is in src/spi folder. So you enlist them as –
    C_SRC_FILES = main.c spi/spi.c

    Can be empty.

  • ASM_SRC_FILES – like the C_SRC_FILES it is a list of assembly files with extension .S. Can be empty.
  • DEVICE  – the target microcontroller( for our hello_world it was atmega8). Must not be empty.
  • F_CPU – cpu clock speed in Hz. Must not be empty.
  • LFUSE – low fuse byte in hex format. Can be empty if you use different burner tools.
  • HFUSE – high fuse byte in hex format.Can be empty if you use different burner tools.
  • EFUSE – extended fuse byte in hex format.Can be empty if you use different burner tools.
  • DEFS – extra definitions to be passed to the compiler. Enlist them without -D flag. Can be empty.
  •  LD_FLAGS – external library list to be linked with. Enlist them without -l flag. Can be empty. For example if you want to use math.h , you have to enlist the math library here like this –
    LD_FLAGS = m
  • Next few lines are self describing configuration options for printf type functions and floating point calculations.
  • SUDO – it is the sudo command of Linux. If you are using Windows remove the sudo from here.
  • DUDE – burner software. Essentially avrdude. Do not modify this unless you know what you are doing.
  • PROGRAMMER – burner name (compatible with avrdude) and other associated configuration flags for the burner goes here. Must not be empty.

Well this was a brief description of the variables here. If you have further question about them please ask me. If you find any bug in the suite please let me know.

That was a short list of the common questions. If you need to know something else at this stage let me know.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s