Before we dive into programming, we need to learn microcontroller basics first!
What is a microcontroller? It is basically a computer in a single chip.
A computer, technically, is any device that is instructed to do math or logical operations through programs. Your computer and mine run thanks to a microprocessor, which is different from a microcontroller. A microprocessor needs other chips - RAM, ROM, etc - to function properly. A microcontroller, meanwhile, already has the microprocessor, RAM and ROM inside it.
A microcontroller is a computer and can be instructed through programs. But it's not used like your own computer as it's mostly utilized to control products and devices automatically. They are used in medical devices, appliances, toys, automobiles, and more! The low cost of microcontrollers is favorable in these areas over a microprocessor (which is already expensive, by itself) that still needs other components called peripherals.
Inside a Microcontroller
Microcontrollers come in different brands and each brand has a different way of wiring the insides of their microcontrollers (which we'll call architecture). A typical microcontroller contains these parts:
The CPU or central processing unit is the core of the microcontroller. It is the part of the microcontroller that carries out the instructions from the user's program.
The memory represents two parts: program memory and random access memory (RAM). Program or Flash memory is where the program is stored. RAM is the location of registers that are needed to create applications for the microcontroller. In other microcontrollers, RAM is part of the CPU and is called a Register file.
In CISC (complex instruction set computer, also called Von Neumann architecture) devices, the program memory and RAM is on the same location. On the other hand, a RISC (reduced instruction set computer, also called Harvard architecture) device separates program memory from RAM. Microcontrollers are mostly RISC devices.
Another type of memory is read-only memory (ROM) which retains data even if power is not applied to the microcontroller. This is in contrast with random-access memory (RAM) where data is erased when the microcontroller is turned off. Read-only memory, however, is not directly interfaced with the CPU and is usually part of the peripheral.
The input/output unit is the interface between the outside world and the insides of a microcontroller. An I/O unit or port can accept data, send data or be left hanging. A pin on a port can also have other functions assigned by a register on RAM.
All microcontrollers have timers which can serve a number of purposes. A microcontroller timer can be free-running (runs independent of the program) or a watchdog (resets in a specific time interval). A free-running timer is used, for example, to count button presses or create a clock signal.
The peripherals present inside a microcontroller varies from device to device. Most have analog to digital converters and a serial receiver/transmitter. A peripheral gives additional function to a microcontroller. We will learn more about peripherals as we go on.
A program is a set of instructions to be executed by the CPU. It can be low level (assembly) or high level(C/C++, Basic, Python). They are converted to a combination of 1's and 0's or machine code.
A low level program or assembly program uses mnemonics that are human readable. No assembly language is acceptable to all devices: they are created for a particular machine.
An assembly program can be directly converted to machine code and so takes up less space inside a microcontroller's program memory. Time critical applications are also better coded through assembly because of instruction cycle awareness.
However, assembly language programming is much harder for the programmer as you are essentially working with numbers. Assembly language programming involves moving numbers and applying arithmetic and logic operations.
A high level program is less taxing to the programmer and is generally cross-platform (can work across multiple devices). The most popular high level programming language is C/C++. The Arduino platform uses C/C++ for their IDE (integrated development environment). The PICs also has a programming environment using C. Most of the tutorials in this site will be using C/C++ as the programming language.
Loading a Program to a Microcontroller
A program is written using an IDE and is then assembled (if assembly language) or compiled (if high level language). The machine code generated after compilation and assembly is encoded in what is known as a binary file. A HEX file is the ASCII representation of binary files. Some microcontrollers use BIN files instead of HEX files. The HEX or BIN file is read and interpreted by a programmer software. The decoded machine code is then burned to the microcontroller flash memory.
Programmer hardware is needed to load the program into the microcontroller's flash memory. The hardware may use different ways to program the device. When computers were older, programs are loaded to the microcontroller via the parallel port. The size of parallel ports is a disadvantage to modern computers and thus have been removed completely. Nowadays, microcontrollers are programmed via USB using In-Circuit Serial Programmers (ICSP). For PICs, the ICSP hardware is known as PicKit. For AVRs, it's Atmel-ICE. You can also create your own ICSP circuit.
Bootloading is another method of programming a microcontroller. A bootloader is a program loaded to a microcontroller that can change the program on the flash memory. With it, a standalone hardware programmer will not be needed to update the program of the microcontroller -- a personal computer can send the program via serial (USB).
The Arduino has a bootloader which accepts the program sent by the user through USB. PICs and AVRs can also use bootloaders. The ICSP is still needed for loading the bootloader the first time.
Microcontrollers typically don't consume much power. Most devices run on 5 V while some run on 3.3 V and draws in around 200 mA. Note that microcontrollers must be run on voltage as specified on their datasheets. Doing otherwise means damage to the device.
The power supply must also be smooth or regulated. An unregulated power supply produces voltage spikes that may damage the microcontroller. A smoothing capacitor is added across the power supply + and - terminals to filter out voltage spikes.
- A microcontroller is a device that can accept programs to do particular tasks
- A typical microcontroller has a CPU, memory, input/output unit, timer and other peripherals
- A microcontroller program can be low level or high level. For program space and time critical applications, low level assembly language is preferred. High level language like C/C++ is easier to use.
- A program is loaded to a microcontroller through a ICSP or via a bootloader
- Microcontrollers are sensitive to voltage variations and must be operated as specified
That's the microcontroller basics. Next up, we'll build our very first microcontroller program.