Saturday, February 12, 2011

Chapter 1: The Basics

What is a Microcontroller?
The obvious question should be answered first. The word microcontroller is similar to the word microprocessor which is the core of modern computers.

Micro means something very small or tiny. The integrated circuits are fabricated in very small chips which have made our modern computers smaller.

Controller means something which has the ability to control. For example, deciding how much amount of heat to maintain in a microwave oven when it is set to run on auto cook modes or a worker robot which has to decide which box to put in which truck.
Figure: Two robot characters in the movie Wall E by Pixer Animation Studios.
Both the oven and the robot need some sort of thing which can make these decisions for them. Otherwise both of them are going to be useless. Another example can be a cell phone. It shows its battery level, warns us if the battery is low. When the charge is full, it tells us to
remove the charger. Which ghost is responsible for that?

The answer is obvious, Microcontroller.

A Microcontroller is a chip which has the capability to decide and control situations according to the instructions provided by its user. The word Microcontroller is abbreviated as uC or MCU (Micro Controller Unit). We will sometimes refer to it as uC or MCU.

Figure: ATmega8 Microcontroller in a 28pin PDIP Package
  If you have a fair idea about computers, you should know that, a microprocessor or simply a processor which we call a CPU is actually a Microcontroller. Don’t be afraid, we are not going to buy a core 2 duo and put it on a breadboard! The things we are going to use are commonly in PDIP packages just like 74XX series ICs we used for gates, counters, registers etc. The uC chips available in the market are not very expensive. There are different models available, ranging from 50/60tk to 300/400/500tk.

Where and why we need a Microcontroller?
Any system that needs some sort of automated control uses Microcontrollers. Actually we are using uC everyday even though we are not aware of them. For example, Modern Televisions, Cell Phones, Microwave Ovens, Washing Machines, Cars, Toys, Airplanes, Battery Chargers, Inverters and specially Robots. If you want to give your equipment a sophisticated ‘Brain’ so that it can monitor situations, process calculations and take decisions according to your requirement, you need to use a uC. It is the “Brain” of a robot.

A Microcontroller is just like a computer. Why? Let’s find out.

Computer Architecture
To understand what is in a Microcontroller, we have to understand what is in a Computer. The diagram shown below is the Von Neumann computer architecture.
Figure: Von Neumann Computer Architecture.


I have described briefly the basic components of a computer below.

Input/Output (I/O)
Input Devices are the devices which are used to give inputs to the computer. For example, keyboard, mouse, camera, scanners, microphones etc
Output Devices are the devices which produce the outputs. Such as, monitor, printer, speaker.

Central Processing Unit (CPU): 

Figure: A Microprocessor

The Control Unit controls what happens in the processor. It is the circuitry that controls the flow of information through the processor, and coordinates the activities of the other units within it. It is the brain within the brain.

The Arithmetic Logic Unit (ALU) is the unit that performs arithmetic and logical operations within the processor. Such as addition, multiplication, AND, OR etc.

Memory is the part of a computer which stores data. There are two types of memory,

Read Only Memory (ROM): It is non-volatile memory. It can retain data even if there is no power in the computer. It stores data permanently.
Figure: A RAM module.

Random Access Memory (RAM): RAM is volatile. Data is erased when computer is turned off. It stores data temporarily.

External Storage
These are used for storing large amount of data permanently. Some examples are Hard Discs, CDs, and Flash Drives.

How the CPU works?
The internal structure of every computer can be broken into three parts: CPU, Memory and I/O as described earlier. The CPU executes (processes) information stored in the memory. I/O devices help us to communicate with the CPU. The CPU is connected to the memory and the I/O via strips of wire called system bus. The grouping of lines or wires is called bus. The bus carries information from one place to another. For example: from RAM to CPU, from I/O to RAM.

Figure: System Bus Structure

There are three types of buses: address bus, data bus and control bus.

The CPU recognizes a device by its address. The CPU puts the address of a specific device on the address bus and the circuitry finds the device. Data bus is used to transfer data between devices. The control bus indicates whether to read data from a device or to write to it. It controls the Read/Write operation.

The things a CPU does are the following, 
  • Fetch instruction: The CPU reads an instruction from memory.
  • Interpret instruction: The instruction is decoded to determine what action is required.
  • Fetch data: The execution of an instruction may require reading data from memory or an I/O module.
  • Process data: The execution of an instruction may require performing some arithmetic or logical operation on data.
  • Write data: The results of an execution may require writing data to memory or an I/O module.

Let’s explain it easily with an example. The CPU reads instructions from a program in the following order,

0             read a;                                                                 //fetch data a
1             read b;                                                                 //fetch data b  
2             c = a + b;                                                              //process data a+b
3             write c;                                                                //write data c

The CPU has a program counter/instruction pointer (IP). It points the lines of instructions one by one to be executed. For example, when the value of IP=0, the CPU executes the first line. When IP=2, the CPU executes the third line. The program counter increments by 1 after each line is executed. In the beginning, IP=0, which indicates the first instruction, in this code which is "read a;".

At the beginning, the Program Counter tells the CPU to fetch instruction 0 (First line). This instruction tells the CPU to fetch data ‘a’ from the memory. Instruction 0 also gives the address of ‘a’ to the CPU . The CPU will put that address on the address bus; give a read order on the control bus. The additional circuitry will locate and read ‘a’ from the memory and send to the CPU via the data bus.

Similarly the CPU will read ‘b’. Then the ALU will add a, b and the result will be stored in ‘c’ which will be located by the address bus. The CPU will issue a ‘write order’ on the control bus to write the resultant value in that memory location.

There are few more things we should know.

The CPU contains Registers to store information temporarily. From basic Digital Logic knowledge many of us know that Registers are arrays of latches which can store data. The values of ‘a’, ‘b’ are kept in the registers during the addition operation described earlier. The value of ‘c’ is also kept in another register before writing to the memory.

You may ask, why CPU needs registers as it can use memory locations to store data. The memory contains a huge amount of data and it takes some time to access them via the bus. The registers are quick source of data inside the CPU. The CPU have access to them directly. Before any logical/arithmetic operation, the CPU brings the data to the registers. This improves the speed of the operations significantly.

There are many devices other than CPU that also contain registers. Very soon you will no that a micrcontroller have many peripheral devices like Timers/Counters, USART, Interrupt Controllers etc. These also have some dedicated registers to store different settings and data of their operations. Say in a stopwatch we want our CPU to count elapsed time in seconds; we have to set the registers for counting seconds. If we want the time to be counted in minutes, we have to set the registers for counting minutes.

There are some special registers called Flags. These are the Condition Code Registers which indicate the condition of CPU operation. For example if there is an overflow in the addition operation described earlier, the Overflow Flag will indicate it. The CPU will write 1 in the overflow flag if overflow occurs. Otherwise it will contain 0. There are many flags which will help us to interpret the CPU operations and the results.

Difference between a Computer and a Microcontroller
After you have read up to this portion of the chapter, one question might come to your mind. What is the difference between the PCs we use in our daily life and the Microcontrollers? Well, there is more similarity then the differences. But there are significant differences which explain a uC being priced at as low as 60tk.

In a computer, we put on the processor on the motherboard. We also install RAM modules, Hard Discs etc. The motherboard contains the ROM that initiates the BIOS (Basic Input Output System).

Unlike Microprocessors, the Microcontrollers have everything that is required to perform its operation are built in a single package. That means a single MCU contains the processor, RAM, ROM, Flash Memory. All are included in a single chip. So a Microcontroller resembles a complete computer, well of course in a tiny format.

A Sample Microcontroller
A Sample Computer
Clock Speed
3.0 GHz
Program Storage
8 kilobyte Flash
500 Gigabyte Hard Disc
1 kilobyte
2 Gigabyte
The comparison reveals a major difference. Don’t underestimate the uC. It may not be adequate for encoding your HD videos but is great for our computing requirements in the circuits we might implement. For most applications we don’t need more than that. 

So, we are getting a computer in a tiny package. That’s why MCUs are simple to use and very cheap compared to a PC. Microcontrollers can process quite complex functions, data manipulations and DSP algorithms. The data shown here are just for an ordinary MCU; there are more powerful MCUs available in the markets.

This concludes the chapter. The next chapter will focus on the development process of the embedded systems.