A Microcontroller (MCU) is a miniature computer which can perform certain operations just like your personal computer. MCUs are specifically distinguished by the fact that the processor core, RAM, ROM and input/output ports lie on the same chip. Microcontrollers are usually dedicated devices embedded within an application. For example, microcontrollers are used as engine controllers in automobiles and as exposure and focus controllers in cameras. Other applications include robotics, industrial automation, home appliances like washing machines, refrigerators and microwave ovens etc.
Microcontrollers can be classified on the basis of their bits processing power – like 8 bit MC, 16 bit MC. An 8-bit microcontroller means it can read, write and process 8-bit data directly. Basically, 8 bit specifies the size of the data bus.
NOTE: An 8-bit controller can always process arithmetic operations involving numbers having value over 8 bit i.e. value over 256(2^8). For this process, large numbers are broken in 8-bit parts and computed separately, making the process time consuming. So you can say, A 32-bit controller is faster than an 8 bit and 16 bit MC. This may not be true always, as many factors decide the speed of operation of a microcontroller.
Inside a microcontroller
To understand the different components inside a microcontroller let’s consider 8051 architecture. The 8051 is an 8 bit MC and it belongs to the MCS-51 family introduced by Intel Corporation at the end of the 1970s. Why 8051? To understand things, it is important to start simple. Though 8051 is considered a primitive technology as compared to the (now popular) 32-bit microcontrollers available in the market, the architecture is very easy and well documented. Moreover, most of the components are common for all microcontrollers, so understanding one will help you understand the other.
Central Processor Unit (CPU): CPU is the brain of any processing device. It monitors and controls all the operations and processes that are performed inside a microcontroller. CPU is independent of user control. It reads the instructions written in ROM memory and executes them to perform required tasks. It also contains the Arithmetic Logic Unit (ALU) which performs arithmetic operations inside the controller, like addition, subtract, multiply etc. The CPU also has a control over the RAM and accesses it when needed.
Memory: A microcontroller requires memory to perform tasks. There are mainly three types of memories: ROM, RAM, SFR.
ROM (Read Only memory)
The program (set of instructions or operations the microcontroller is instructed to perform) is stored in the ROM (aka Flash memory). This type of memory is read only i.e. you cannot change the content of this memory while the microcontroller is working. You can reprogram the content of ROM, by following a set of procedures mentioned in the microcontroller’s datasheet. When the system is powered down or during a power failure, the program memory is not erased.
RAM (Random access Memory)
This type of memory allows modification of its content at any time. This provides temporary storage of data required for performing certain operations. When the microcontroller is powered down, the content of this memory is lost. Meaning, this memory is volatile.
SFR (Special Function Registers)
SFR’s are special registers (small memory locations) which are closely coupled to the CPU. All the vital information needed for the synchronized working and control of the microcontroller is stored here.
The program you write is in ROM while the variables you create and the variables created during the course of execution of the program are stored in RAM.
Modern controllers also have a cache memory, which stores the frequently used data, reducing the time to fetch the variable from a relatively slower memory like RAM. Also, certain controllers make it possible to change certain section of the program code in ROM even when the instructions under other sections are being decoded.
Bus: Basically Bus is a communication channel or medium for transfer of Data. These buses consist of 8, 16 or more wires. Thus, these can carry 8 bits, 16 bits simultaneously. IN 8051, buses are of two types:
- Address Bus: Microcontroller 8051 has a 16-bit address bus. It is used to address memory locations. It is used to transfer the address from CPU to Memory.
- Data Bus: Microcontroller 8051 has 8 bits data bus. It is used to carry data.
The CPU calls for an address of a variable (from RAM) or instruction (from ROM) using the address bus. The required data having this address is put on the data bus so that the CPU can access it.
Oscillator: Microcontrollers require clock cycles to execute instructions. These clock cycles are generated by an oscillator circuit within the microcontroller. An external quartz crystal is connected to the oscillator circuit as a clock source. Small voltage is applied across the crystal, and with the help of the controller’s oscillator, this voltage starts oscillating. The oscillating voltage is converted to clock pulses by the oscillator. Oscillator is like the heart and the clock pulse is like its heartbeat.
Modern controllers like the STM32 have an internal RC circuit as a clock source. So you can work on a microcontroller without connecting a quartz crystal. But the internal RC is not very precisely calculated and may result in uneven or wrong clocking. So it is ideally recommended to add an external crystal to achieve timing accuracy.
Interrupts: As the name suggests, it interrupts the current operation or work and executes some other task which is more important at that moment. The feature of Interrupt is very useful as it helps in managing time critical processes. Interrupts give us a mechanism to put on hold the ongoing operation, execute a subroutine and then again resumes the normal program.
Input/Output Port: The main application of a microcontroller is to control operations, so it is generally connected to external machines, devices or peripherals. Input/outputs ports are provided so that the controller can trigger an event, detect an event or communicate with other peripherals and sensors. Microcontroller 8051 has 4 input output ports to connect it to other peripherals.
Timers/Counters: Timers are a vital component in every embedded system application. They help in keeping timed track of processes. Microcontroller 8051 has two 16 bit timers and counters. The timers are used for various applications like measurement of intervals, to determine pulse width, to generate PWM (Pulse Width Modulation), to generate delays etc.
Nowadays, microcontrollers include a lot more peripherals inside a single chip. Like the Cortex M4 series microcontrollers have many peripherals such as ADC (Analog to Digital Converter), DAC (Digital to Analog Converter), multiple timers and support communication with dedicated internal hardware for USART, SPI, CAN, I2C etc.
Taking a step further in understanding microcontrollers, you need to know about the different abstraction layers that generally exist in an embedded system. Click here to know more.