Step by Step Guide to Microcontroller Programming

03/05/2021, hardwarebee

This is a step by step guide for those who want to start with Microcontroller Programming. The article starts with introducing a few basic terms that will be used in the guide, a list of tools, programming languages and at end, an example of a downloadable code with a step by step guide that you could use to practice microcontroller programming.


But before diving into Microcontroller Programing. Let’s define first two basic terms: Hardware and Software.


Hardware consists of the physical components in the system such as a chips, keyboard, a mouse, a monitor, memories, I/O devices. Hardware is harder to develop compared to software, it has to be designed for very specific applications, and it is a very efficient and perfect solution in high-speed systems.


Software is the non-physical components or the applications that run on the hardware such as operating systems, computer games and network applications. Software is highly configurable, easy and faster to be developed, also very cheap compared to hardware.


What is a Microcontroller


To understand what a microcontroller is, we need to understand first what a microprocessor or a CPU is (Central Processing Unit). A CPU is a heart of any computer system, it is like the human brain when performing a task. We can basically divide it into two essential parts:

Control unit: This unit is used to fetch the data from the memory.

Execution unit: This unit is used to execute or run the data.


Types of Processors


There are many categories of processors such as general-purpose processors, application specific system processor, and multi-processor system. General purpose processor has many types:


▪ Microprocessor

▪ Microcontroller

▪ Embedded processor

▪ Digital signal processor


Microprocessor is a semiconductor device or a computer on a chip, but not a fully functional computer. Its central processor unit contains



▪ Program counters

▪ Registers

▪ Other circuits (clocking time, interrupt)



A microcontroller is an IC that contains a microprocessor and some peripherals to be a complete functional computer, peripherals e.g., ADC, RAM, ROM, DAC.



History of Microcontroller


Originally, Intel corporation was the first manufacture of microcontrollers. The first microcontroller was produced in 1971 in the USA. It was a 4-bit microcontroller with the name i4004. Latterly, Intel produced more sophisticated models in that time with an 8-bit microcontroller and after that a 12-bit microcontroller was developed by Toshiba.


Applications of Microcontrollers


Microcontrollers have many applications in many industries e.g., automotive, medical, industrial automation, aerospace, robotics.


In the automotive industry, for example, modern cars nowadays have more than 100 microprocessors for systems like air condition, ABS, EBD and also ADAS or advanced driving assistance systems such as adaptive curies controller and lane assist, the following is an image of an ECU or electronic control unit.


In the medical domain, there are medical devices that include microcontrollers such as an MRI, glucose test set, portable EKG. The following image, is a glucose test device.



In aerospace, most of the aircrafts have been designed with new avionics systems using microcontrollers. Additionally, there are flight control system, air traffic control system, navigation and weather systems that are all based on microcontrollers.  


Popular Microcontrollers


There are many microcontrollers’ types and platforms, but selecting a microcontroller depends on many factors:


▪ Application

▪ Budget

▪ Performance

▪ Number of GPIO pins

▪ Memory size

▪ Temperature of operating environment

▪ Power consumption


AVR Microcontroller


The following image contains a block diagram of AVE12DA, one of the most popular microcontroller famlily — the AVR microcontroller:




AVR microcontrollers is produced by Atmel corporation (today: Microchip), Atmel has 6 families of AVR microcontrollers, 4 families as general-purpose microcontrollers and 2 as specific purpose microcontrollers. General purpose microcontrollers are:


▪ 8 bit – mega AVR

▪ 8 bit – AT Tiny

▪ 8 & 16 bit – AVR Xmega

▪ 32 bit – AVR


Specific purpose microcontrollers are:


▪ Automotive AVR

▪ Battery Management


Automotive AVR, from its name, it has been developed for automotive applications. It can work under very high temperatures up to 150 Celsius, and it includes protection systems for short circuit problems.


Battery management microcontroller is developed for battery management systems to secure charging and discharging operations.


Examples of AVR Platforms


▪ Arduino UNO

▪ Adafruit Blue fruit Micro

▪ Raspduino

▪ Digispark Pro



ARM Microprocessor/Microcontroller


ARM microprocessor is the most popular processor in the world, especially, in the consumer applications. You may have an ARM processor-based device, but you don’t know! ARM is mostly a key component of any successful 32-bit embedded systems. The first ARM was developed in 1985.



RISC Technology


ARM is used RISC technology, RISC stands for reduced instruction set computer, it is a design philosophy to deliver simple instructions within a single cycle. It relies more on the software. On the other hand, CISC technology relies more on the hardware.



RISC technology focuses on reducing the instruction set of the processor. On the other hand, CISC processor has a non-fixed number of the instruction set.


ARM Features


Arm has many features for every embedded system designer is looking for, e.g. low power consuming due to its compact size, also high code density is another feature since we are developing embedded systems, we will have limited resources in the most cases.


Internal structure of ARM-based Microcontroller


This figure shows the internal structure of most ARM based devices:



The boxes represent functions, the lines represent buses. Let’s talk more about every component in details:


▪ The ARM processor is the core component it is responsible for the processing operations

▪ Controllers are used to coordinate the system functions as memory and interrupt controllers

▪ Peripherals are used to provide the system inputs and outputs

▪ A bus is used for exchanging data between the different components in the system



Examples of ARM platforms


There are many devices and embedded platforms that have arm microprocessor e.g.


▪ Arduino Due

▪ Raspberry Pi

▪ STM32F103C8T6

▪ NXP LPC1768



Other Microcontroller and Platforms


As we said before selecting a microcontroller or a platform depends on many factors (budget, number of pins, etc.…). There are also another microcontrollers and platforms e.g.


▪ PIC Microcontrollers

▪ 8051

▪ ESP32

▪ Motorola Microcontrollers


Microcontroller Software and Hardware Tools


Since the creation of the microcontroller, there have been many types of software and hardware development tools. Of course, some of the tools can be used across various microcontroller types but some are very specific per microcontroller. Before starting with microcontroller programming, you may want to get yourself familiar with the microcontroller programming tools and, last but not least, the development process.


Using embedded development tools, we may need tools as follows:




It is a software tool that converts your source code in assembly into machine code, for example (GNU assembler).




It is a software program that coverts your source code in a high-level programming language into assembly language or machine code. Compilation can be:


▪ Native: this means you run the compiler and the generated code on the same machine

▪ Cross: This means you run the compiler on a machine and the generated code runs on a different machine (processor architecture).


Linker and Locator


A linker is a program that collect and link the compilations and assemble operations and produce a single exe.


A locator is a program tool that can be used to change the memory map of the linker output.




It is a program that imitates real scenario, it produces an approximation of the real-time systems. A simulator is used:

▪ if the real hardware not available

▪ in dangerous test environments and scenarios





It a software program and hardware device that can be interfaced to my PC to my embedded target. A debugger is used:

▪ to set breakpoints

▪ to trace execution

▪ to dump memory


Hardware debugger can be

▪ in-circuit debugger

▪ debug agent software on board


Flash loader


It is a program and hardware device that can be used to:


▪ Program ROM/Flash

▪ Watch variables

▪ Erase ROM/Flash




Profiler is a tool to monitor performance of the software code


Integrated Development Environment


It is a software program that has most the tools all in one. It may include


▪ Text editors

▪ Compilers

▪ Debuggers

▪ Profilers

▪ Simulators

▪ Linkers


Microcontroller Software Languages

We can classify them into two types:



▪ C/C++

▪ Java

▪ Ada


Using this type will provide an abstraction from the hardware level





Machine code


It is 0’s and 1s and hard to be written by humans, but easy for computers to understand




It is a mnemonic code and pseudo instructions to improve readability


An instruction consists of mnemonic (opcode) + operands

Opcode is an operation taken by a machine processor

Operands is the final target; the opcode have to take an operation for.


Assembly language is readable and understandable more than machine code but you need to use it on a specific processor and have knowledge of its architecture.


C/C++ Programming Languages


High-level languages have the following features

▪ Easy to write

▪ Flexible

▪ Processor independent

▪ High productivity


Microcontroller Programming Example Using AVR ATMEGA16


ATmega 16 has the following features:


▪ 16 Kb of In-system Self programmable flash memory

▪ 512 Bytes EEPROM

▪ 1 Kbyte Internal SRAM

▪ Programming Lock for security


Peripheral Features


▪ Real-Time Counter with Separate Oscillator

▪ Programmable Serial USART

▪ Four PWM Channels

▪ 8-channel, 10-bit ADC

▪ On-chip Oscillator

▪ Power-on Rest and Brown-out Detection

▪ Internal RC Oscillator and Interrupt Sources

▪ Voltages (from 2.7v to 5.5v)



LED Blinking Program Example using ATmeg16 and Proteus


LED blinking program in embedded systems like the “hello world” program in application development.  In the following example, you will learn and use the Proteus simulator to run your first program. To download the source files, please go to this folder.


Write your program using Atmel studio


After downloading your IDE,


microcontroller programming AVR


open it and choose new project and choose the location of the hex file as in the following image


microcontroller programming AVR


The next step is to choose the microcontroller family


microcontroller programming AVR


After that you will get the following window with starter code


microcontroller programming AVR


And now please write the following code


microcontroller programming AVR


The next step is building your program by pressing F7


microcontroller programming AVR


After that you will find the hex file on the chosen location


microcontroller programming AVR



The next step is using the Proteus simulator



And now create a schematic from the selected a template



microcontroller programming AVR


And choose Do not create a PCB layout


microcontroller programming AVR


Finally, we are done but we need to add the hex file as in the following image


microcontroller programming AVR 



Now edit CKSEL Fuses


microcontroller programming AVR


Save and click on play



And you can see that the LED is off for 500ms (left hand side) and on for 500ms (right hand side)



Program Explanation line by line


To download the source files, please go to this folder.


#define f_CPU 100000UL // to create a constant and choose the processor speed

#include <avr/io.h> // to recall some files for inputs and outputs

#include <avr/delay.h> // recall this file to use delay functions

int main(void){   // the starting function and the main program

DDRA = 0b0000001; // to configure the pin a as an output / direction

While(1){ // to loop forever

PORTA = 0b00000001; // to make the PA0 = 1 and output 5v (LED is on)

_delay_ms(500); // delay for half a second

PORTA = 0b0000000; // to make the PA0 = 0 and output 0v (LED is off)

_delay_ms (500); //delay for half a second


return 0;

} //end of the program




Nowadays embedded systems are used in vital products and can be used to secure and save a lot of people, as in the medical domain and other applications. You should keep learning after getting an overview and introduction about microcontroller programming. Your next step should be learning embedded software design and real-time triggered systems.






AVR Microcontroller and Embedded Systems: Pearson New International Edition: Using Assembly and C, Muhammad Ali Mazidi, DeVry University

ARM System Developer’s Guide Designing and Optimizing System Software A volume in The Morgan Kaufmann Series in Computer Architecture and Design Book • 2004

Recent Stories