Tag Archive : microcontroller

/ microcontroller

Getting Started to Microcontroller

December 11, 2015 | Article | No Comments

In this article we will discuss things we need to get started to microcontroller, especially programming and engineering a microcontroller.

All the things we need can be categorized into two:

  • Hardware
  • Software

The Hardware Part

Mostly we are facing hardware. In typical we will need following hardware:

  • Computer
  • In-System Programmer (ISP)
  • Target Board
  • Microcontroller


We need computer / laptop to write, edit, compile, and debugging the code before we send the program to the microcontroller. The requirements are not too much, at least it has spare resource to do our job. Any decent PC / laptop will do.

In-System Programmer (ISP)

We have our PC at one end with binary file ready to be written and a target board at another end. How can we transfer the program? For this we need a hardware called In-System Programmer or abbreviated as ISP (not Internet Service Provider). This devices programs the Microcontroller by burning the file (typically in hex format) into flash memory of microcontroller.

There are three type of ISP, according to the connector shape: Parallel programmer, Serial programmer, USB programmer. However, most ISP are now USB-based.

Parallel programmer – data is transferred through the parallel port of the PC. Technically the parallel port is called DB25 port as it has 25 pins. Any old school computer has this, usually referred as the “printer port”.


Serial programmer – The data is transferred throught the serial port of the PC. Technically the serial port is called DB9 port as it has 9 pins.


USB programmer – the data is transferred through the USB port of the PC. Typically, it use USB 2.0 standards.


Target Board

Microcontroller cannot function on its own. If Microprocessor need motherboard to function, microcontroller need a board with an appropriate circuit to make it work. Sometimes we call it development board which have slot for the MCU to fit in, slots for other accessory drivers, ICs pins outlet of GPIO pins, etc.

There are many ready made development board out there. You can buy one of them or make them by yourself. It is always recommended for beginner to buy one or two different types of development board, but don’t go and buy random board. We need to consider the type of ISP and MCU and then choose the one according to our requirement and budget.


Selecting the microcontroller is the biggest dilemma. There are more than 150 IC manufactures which manufacture MCUs, and each one of them can have more than dozens of different models in their account. So which one to choose?

By the popularity, there are two most popular microcontrollers: PIC by Microchip and AVR by Atmel.

When we are choosing microcontroller, we should consider many factors. But at least, we can consider following factors to decide what MCU we need:

  • Cost – A cheap MCU is always preferred. But many chips might offers competitive price with competitive features. Also remember that sometimes price and capability always been a tradeoff.
  • Speed – Speed of execution also matters. Running faster means accomplish work faster.
  • Ease of use – How ease of use means how many effort we need to learn the chip, assemble the system, maintain it or even upgrade the system if necessary.
  • Architecture – In simple words: RISC or CISC? There are two different categories of architecture, the Reduced Instruction Set Computing (RISC) and Complex Instruction Set Computing (CISC). In RISC, the instructions are highly optimized set of instructions rather than a more specialized set of instruction. While the CISC is the counterpart. In RISC less amount of instructions will result in more output, whereas in CISC more amount of instructions will result in less output. To do a particular job, RISC require lesser number of instruction than CISC. AVR and PIC are examples of RISC, while Intel 8051 and Motorola 68000 are based on CISC.
  • Compiler – Availability of compiler means ease our access to learn and code the microcontroller. Some manufacturer such as Atmel gives free compiler for their AVR, while other might charge it for some cost. Practically, they are more specific domain or specific purpose.
  • Durability – How long the microcontroller will last? How is the maximum or minimum constraint which tolerated by the microcontroller? This should be proven.

Choose any MCU you want.

The Software Part

In softwares, we have several subcategories:

  • Compiler
  • Debugger
  • Programmer


We want to program the microcontroller. Like microprocessor, microcontroller only know machine code in some manners. They don’t understand human language. To bridge the gap, a programming language is created and a compiler is used to translate the programming language to the code known by the microcontroller. There are many different language and compilers available for different OS.

For programming language, C is preferred over other (such as BASIC), as it is more organized and gives us the power to do substantially more. Apart from this, C is a language which widely used in low level programming. It is powerful, like a sharp sword you should know what kind of weapon you are using.

Different microcontroller use different machine code and hence can have different compiler. You can use compiler for AVR to write a program for PIC microcontroller. You can however if there is a compiler which can translate our code to different target microcontroller.


Debugger is a tool used to test and debug other program. Typically, the debugger will execute the instruction one by one and stop to whichever instruction interest us. The target program is the program written specifically for microcontroller.

Debugging process can be done in two different location: local and remote.

Debugging in local means the target program is run on our computer. We don’t upload the program to the microcontroller to know the execution, instead we let the debugger to run it in our system.

Remote debugging means the target program is run on the microcontroller. We connect our computer to debugging line of microcontroller and control the execution flow from our debugger. We then can set some register values, change the behavior, and stop at specific instructions.


A programmer software is a bit confusing term. It is a special software used to utilize hardware programmer or ISP so we can transfer the program from our computer to the board. Most of the compilers have an inbuilt programmer. But sometimes people prefer to use a separate one because of greater flexibility it offers.

Some popular programmers are: PonyProg, avrdude, ProgISP.

Creating Simple AVR Program: “Running” LEDs

December 3, 2015 | Article | No Comments

In the previous article we have discussed about AVR microcontroller programming in Linux environment and Windows environment. The term program in this case is downloading / store a binary program to your microcontroller using usb programmer and avrdude. If you haven’t read the article I recommended you to read it. It’s not a long article so it would be fine to skim it.

Now, in this article we will discussed about writing a simple program for our AVR microcontroller. What we will need:

  1. Host Operating System, (you can use GNU/Linux or Microsoft Windows Operating System)
  2. AVR microcontroller, embedded as board. In this case I use atmega32
  3. avrdude, this tool is for writing our program to microcontroller
  4. avr-gcc, this tool is for compiling the code
  5. A USB programmer

Now, let’s configure the chip. I have assembly my AVR as follows:

  1. ATMega32 chip
  2. External clock with 16MHz frequency
  3. Eight active low LEDs connected to PORT A
  4. A port for ISP programming connected to PORT B

Well, actually there are more in my board but for this article we will just need those informations.

Please note that I use PORT A for LEDs. You may use other port but you must adjust your configuration with this article. The one I used are active-low LEDs which can only emitted light if we give low voltage. Also note that I use PORT B for ISP programmer and this is the only one PORT to program AVR microcontroller.

I assume we all have prepared the tools. See article for preparing tools in Windows and Linux.

Writing the Codes

When writing the codes, you can use any text editor. For simplicity, we use notepad in Windows and Geany in Linux (remember, any text editor will do).

What I want is some turn on and off some LEDs in a pattern. For every second, I will switch the condition for every LEDs and shift the series to the left. This will give impression like the LEDs are running :3

OK, let’s get to actual code. Write this codes and save it as avr-leds.c

#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>

/* our entry point */

int main() {
   /* we used PORT A for LEDs so we will give a little setup */
   DDRA 	= 0xFF;
   PORTA	= 0b11000101;
   /* used for shifting */

   int tmp;

   /* our main loop. The execution goes here */
   while(1) {
      /* setting the running speed. It's a simple delay in 800 miliseconds */

      /* get the least significant bit, in this case is the most right position
         on binary number but it is the most left position on leds */
      tmp = PORTA & 1;

      /* shift the number to the right */
      PORTA >>= 1;

      /* grab the saved bit and shift it to the most right position */
      PORTA |= tmp << 7;
   return 0;

It’s short and simple code. We only write what we need, not completely set other parts.

In this code, we only use port A as output. To do so, we set all the 8 bits to 1 (which has value of 0xFF or 255 in decimal). Port A is then connected to 8 LEDs and I give initial pattern of 0b11000101. Remember that I use active low LEDs, so it will turn on when we give low logic.

To access individual bit of a byte, we use bitwise operation.

The statement:

tmp = PORTA & 1

will give me the least significant byte of PORTA and store it to tmp variable.

Then the statement:

PORTA |= tmp << 7;

will write the most significant bit by the value of tmp variable.

Here you see some


OK, next we will compile the code. To do so open your terminal and invoke this command:

avr-gcc -g -Os -mmcu=atmega32 -c avr-leds.c

That command tell the compiler to compile a single file avr-leds.c but do not link it yet. It’s useful if you have bigger project you can compile each source code separately.

What you need to know is -mmcu switch. It tells compiler what chip we are using now. In this case we are using ATMega32.

The previous command will compile the file into an object file with filename avr-leds.o.

Now, invoke this command:

avr-gcc -g -mmcu=atmega32 -o avr-leds.elf avr-leds.o

This will produce a binary file called avr-leds.elf, which is a GNU executable file. The file format is ELF (Executable and Linkable Format) which is similiar to linux program. However, this is not what we need. We need hex file for AVR.

Now invoke this command:

avr-objcopy -j .text -j .data -O ihex avr-leds.elf avr-leds.hex

This will produce the file we want, an intel hex file. Let me explain what switches we gives

When we write -j switch we tells the avr-objcopy to only copy from that section. In this case we only copy data from section .text and .data. If you have program in assembly before, then you might know what is section.

A -O switch tells the avr-objcopy to produce file with following format. And in our case it is intel hex format (ihex).

The next arguments are the .elf file and .hex file respectively. It means that our input would be avr-leds.elf and will be processed to avr-leds.hex.

Burn the Program to the Device

We use avrdude to do so. Make sure you have connected the device with the downloader and connect it to PC. The downloader I use is USBasp.

avrdude -c usbasp -U flash:w:avr-leds.hex -p atmega32

And we are done!

AVR Microcontroller Programming in Windows

December 3, 2015 | Article | No Comments

AVR or Alf & Vegard RISC, is a single chip microcontroller developed by Atmel. AVR is a modified Harvard architecture 8-bit RISC (Reduced Instruction Set Computer) which is famous microcontroller.

AVR microcontrollers has many categories or we will say family. Family basically a group or category based on similiar characteristic and properties. There are six family and fortunately, AVR microcontrollers have same architecture so programming in any kind of family can be guaranteed work on other family. It is only limited to physical limitation such as different amount of flash storage, etc. This is also an advantage using AVR while variety options can be found while developing microcontroller project.

This article will be covering preparation and setting up environment in Linux to develop AVR program.

What you will need:

  1. Microsoft Windows Operating System
  2. avrdude
  3. avr-gcc
  4. avr-binutils
  5. A programmer (will be described later)
  6. AVR Microcontroller, with it’s development board if any, in this case I use AVR ATMega32

Program in Microcontroller

Before I start, let’s talk about program. What is program? A program is simply set of instructions written using machine language so it can be executed by machine. It is also called “binary” which mean to be not human-readable file (but can be understood by machine). For microcontrollers, binary files have type .hex (Intel Hex Format). You can imagine it as .exe in windows application. There are other forms, but in this article we will focus on .hex.

AVR has a small amount of memory in it. It is a storage where program is actually saved. And yes it is small compared by computer. Just some Kilobytes. When the chip starts up, microcontroller will start running whatever program written in flash.

Compiler and Cross Compiling

Compilation for microcontroller is basically similar to compiling program for PC. However, the difference between those two is in term of product of compilation. Compiler for AVR is run in our PC (developer machine), compile source code, and then produce machine language which is specific for the microcontroller (not our PC). This process is called cross compiling.

There are many cross compiler for AVR. I use AVR-GCC for this purpose (in this article and other similar articles, unless told otherwise).

Other tools we need are collectively known as avr-binutils. The binutils packages provide all the low-level utilities needed in building and manipulating object files. In this package, we have AVR assembler (avr-as), linker (avr-ld), and librarian (avr-ar and avr-ranlib). In addition, we get tools which extract data from object files (avr-objcopy), dissassemble object file information (avr-objdump), and strip information from object files (avr-strip).

The last thing to mention is the libc or standard c library. This is library designed for AVR microcontroller, or in other word it is different with our library used for PC programing.

Fortunately, avr-gcc, avr-binutils, avr-libc, and avrdude (later) are already ported into Windows. These Windows version tools are collectively called WinAVR. We can download WinAVR for free from sourceforge site (http://sourceforge.net/projects/winavr/). Download the latest version.

Download / Burn the Program to the Chip

To program microcontroller we need a downloader. It is a special chip that can connect your microcontroller with our computer and write our program into our chip. Every AVR has a set of pins that are programming pins. You have to verify you chip pins and connect the programmer to those pins in the right order.

So what programmer can be used? In this article I used USBasp. Of course you can have other programmers, but I will only talk about this programmer. For information about USBasp, you can visit this link http://www.fischl.de/usbasp/

Now we have know our programmer (USBasp I mean) but then, how to write our program to microcontroller? Relax! We will cover now.

We will use avrdude, a small application to do that. This tool is already included in WinAVR.

Let’s take a peek of avrdude by give this command on our terminal:


Let’s see what we have got. You will get something like this:

Usage: avrdude [options]
-p <partno>                Required. Specify AVR device.
-b <baudrate>              Override RS-232 baud rate.
-B <bitclock>              Specify JTAG/STK500v2 bit clock period (us).
-C <config-file>           Specify location of configuration file.
<strong>-c <programmer>            Specify programmer type.</strong>
-D                         Disable auto erase for flash memory
-i <delay>                 ISP Clock Delay [in microseconds]
-P <port>                  Specify connection port.
-F                         Override invalid signature check.
-e                         Perform a chip erase.
-O                         Perform RC oscillator calibration (see AVR053).
-U <memtype>:r|w|v:<filename>[:format]
 Memory operation specification.
 Multiple -U options are allowed, each request
 is performed in the order specified.
-n                         Do not write anything to the device.
-V                         Do not verify.
-u                         Disable safemode, default when running from a script.
-s                         Silent safemode operation, will not ask you if
fuses should be changed back.
-t                         Enter terminal mode.
-E <exitspec>[,<exitspec>] List programmer exit specifications.
-x <extended_param>        Pass <extended_param> to programmer.
-y                         Count # erase cycles in EEPROM.
-Y <number>                Initialize erase cycle # in EEPROM.
-v                         Verbose output. -v -v for more.
-q                         Quell progress output. -q -q for less.
-?                         Display this usage.

avrdude version 5.10, URL: <http://savannah.nongnu.org/projects/avrdude/>

Woah, that’s too much. But our interest is only in few options. I’ve bold the text which will get our attention. We only interests in -p, -U, and -c options as this is a minimum command we need to invoke along with avrdude.

  • -c : We specify the programmer we type. We are using usbasp so can be replaced with usbasp. Of course if you are using other programmer you must specify. For a complete list about supported programmer, invoke command: avrdude -c asdf. Because asdf is not a valid programmer, avrdude would complaint and print out list of supported programmer to you. Read it and verify your programmer is supported.
  • -U :r|w|v:[:format]: This is the most important command. is either flash or eeprom which is destination where our program will be stored. Next is r (read), w (write), or v (verify) option that you must choose one (only one). Next is which you have to replace with your program path and name. Next is optional format flag [:format] that specify our program format.
  • -p : Specify microcontroller series. Or simply what microcontroller we willl program. For example I will use ATMega32 so i will replace with atmega32.

With these three, we can program in minimal. In this case when I want to program ATMega32 using USBasp with filename avr_leds.hex, the command I use will be:

avrdude -c usbasp -U flash:w:avr_leds.hex -p atmega32

Wait until process is finish and then you can see you microcontroller is programmed now.

Next time I will write some actual writing program for AVR.

Social Share Buttons and Icons powered by Ultimatelysocial