Microcontroller (MCU) programming

Dinesh Niroshan, 17 July 2014

This is an interesting topic which combines Electronics and programming. I have started to do this few years ago, but couldn't continue due to lack of time. I thought I’d start this again and share some of the learning's.

What is a microcontroller
Microcontroller is a single chip which contains a processor, memory, ROM and ports such as serial, parallel, Ethernet or USB. Types of ports available will be different from microcontroller to microcontroller. Simple low cost microcontrollers may have serial ports and parallel ports only, but advanced microcontrollers will have all types of ports. Simply microcontrollers are similar to computer System Unit without any peripherals.

There are lot of microcontroller manufacturers, but I will focus on Microchip (http://www.microchip.com/) microcontrollers.

Basic microcontroller (PIC 16F84 from Microchip).


What do we need and how?
Mainly following items are required to program a microchip.

  •  Computer
  •  Compiler
  •  Programmer 
  •  Microprocessor
  •  Electronic circuit to test functionality of the program written

Computer
A computer is required to write microcontroller program, compile and upload it to the microcontroller using a programmer.

Compiler
A compiler is used to convert a program written in high level language to machine code. Here I will select MP Lab which is from Microchip Microcontroller manufacturer.  MP lab comes with a nice IDE, with debugging and simulation.

More commercial compilers and IDEs available with more developer friendly features.

Programmer

A programmer is a piece of electronic circuit to interface Microcontroller with Computer via Serial Port or USB. There are lot of microcontroller programmers available in the market. If you are looking for a low cost version, you can build your own one it is a very simple circuit with s single transistor. Following circuit is from https://www.olimex.com.

Circuit diagram to build your own microcontroller programmer.

 

Readymade microcontroller programmer from Olimex (https://www.olimex.com).

Microcontroller
This is the chip we are going to program with our own program.

Following example source code which will switch on and off all LEDs attached to RA0 to RA3 with a delay of 10 seconds. This is a very simple program to test a microcontroller.



MPLAB IDE

Test circuit
This is a simple circuit to test functionality of the program we written. May be connect few LEDs to each output (RA0 to RA3 according to example source).

Applications
Basic application of microcontroller is to get some input and do some activity according to the input. For example we say we have a 2000W electric heater. If we attach a temperature sensor as an input and as an output we can turn on the electric heater if the temperature goes below 20° and turn off the heater when temperature goes above 22°.

There are lot of sensors, and peripherals to connect with microcontrollers, few of them are shown below.



Humidity and temperature sensor

Matrix display the output. E.g. showing the temperature read from a temperature sensor.

It is up to you to create creative useful applications. Happy Microcontroller Programming!