Microcontroller fundamentals in a nutshell


Article background

In this article I will cover various aspects of microcontroller components, programming, interfacing peripherals, design, develop software, testing and debugging the developed software features.

Required Components

Article Contents

Online courses related to this article

Course description

Learn to develop software solutions for linux environment, implement requirements through real time projects and get required practical skills for software jobs

Course description

Learn firmware development theory, applications, get practical skills from online tests, projects and earn certificate for software jobs

Course description

Learn to develop embedded systems, interfacing electronic peripherals through real time projects and get required practical skills for software jobs

Micrcontroller Basics

An embedded system is a combination of computer hardware, software and few additional mechanical or electronic parts designed to perform a dedicated function. Examples of embedded systems are microwave oven, MP3 player, or alarm clock. microcontroller production counts are in the billions per year, and are integrated into household appliances (microwave, washing machine, coffee machine, . . . ) telecommunication (mobile phones), automotive industry (fuel injection, ABS, . . . ), aerospace industry, industrial automation and so on...

A personal computer too is comprised of computer hardware and software and mechanical components like memory cards, disk drives etc. But personal computer is not designed to perform a specific function. It does many different things for you starting from playing games to talking with friends over voice and video.

A personal computer uses a microprocessor where as an embedded system uses a microcontroller.

Since there is a wide variety of microcontrollers to choose from and since costs are important we need to select the cheapest device that matches the application’s needs.

The basic layout of a microcontroller is shown below. We will discuss each of the component and its interfacing with the microcontroller in great detail in the following sections.

MCU layout
Basic layout of a microcontroller.

CPU, Architecture, Instruction set

CPU – Microcontrollers brain is named as CPU. CPU is the device which is employed to fetch data, decode it and at the end complete the assigned task successfully. With the help of CPU all the components of microcontroller is connected into a single system. Instruction fetched by the programmable memory is decoded by the CPU. CPU is a unit which monitors and controls all processes within the microcontroller and the user cannot affect its work. With the help of CPU all the components of microcontroller is connected into a single system. It consists of several smaller subunits, of which the most important are:

  • Instruction decoder is a part of the electronics which recognizes program instructions and runs other circuits on the basis of that. The abilities of this circuit are expressed in the "instruction set" which is different for each microcontroller family.
  • Arithmetical Logical Unit (ALU) performs all mathematical and logical operations upon data.
  • Accumulator is an SFR closely related to the operation of ALU. It is a kind of working desk used for storing all data upon which some operations should be executed (addition, shift etc.). It also stores the results ready for use in further processing. One of the SFRs, called the Status Register, is closely related to the accumulator, showing at any given time the "status" of a number stored in the accumulator (the number is greater or less than zero etc.).
  • The instruction set is an important characteristic of any CPU. It influences the code size, that is, how much memory space your program takes. Hence, you should choose the controller whose instruction set best fits your specific needs.

    Memory, Volatile, Non-volatile memory, Accessing memory

    Semiconductor memories can be broadly divided into volatile and non-volatile. This is shown in the following picture. Volatile memory retains its contents only so long as the system is powered on. Non-volatile memory retains its contents accross power cycles.

    MCU layout
    Different types of semiconductor memories.

    Software development, programming, downloading, debugging

    Embedded software development requires close interaction with the physical world—the hardware platform. In some cases there are very large embedded systems that require individuals to work solely on the application-layer software for the system. These application developers typically do not have any interaction with the hardware. When designed properly, the hardware device drivers are abstracted away from the actual hardware so that a developer writing software at the application level doesn't know how a string gets output to the display, just that it happens when a particular routine is called with the proper parameters.

    Once components are selected project, next important step is to go through the specifications and write some initialization software. Whatever programming language is selected for a given project, it is important to institute some basic coding guidelines or styles to be followed by all developers on a project. Coding guidelines can make reading code easier, both for you and for the next developer that has to inherit your code.

    Use the required IDE and compiler that is recommended for the microcontroller to compile the project.

    After a program has been compiled and linked, you need to download the executable to the microcontroller.

    Hardware, switches, Keypad, Potentiometer, LED, Motors

    Hardware can be classified as input or output. Inputs range from simple switches to complex analog sensors which measure physical values and convert them into a corresponding voltage. Outputs range from LEDs to actuators.

    A button is one of the simplest input elements. It consists of two contacts which are connected if the button is pressed. So if one of the contacts is connected to for example GND and the other is connected to the microcontroller input, then the controller will read HIGH as long as the button is not pressed, and will read LOW while the button is pressed.

    A switch works on the same principle like button. However, a switch remains in the position it is put. So if the switch is put into its ON position (which connects the contacts), then it will stay on until it is moved into the OFF position.

    For practical applications of a push button switch, go to project .

    The LED (light emitting diode) is the most basic output element. Its form and color vary widely to accommodate a wide variety of applications. The color of a LED is determined by the chemicals used for it. Common colors are red and green, but yellow, orange, blue and white LEDs are also readily available, as well as LEDs emitting light in the infrared or ultraviolet bands.

    For practical applications of a light emitting diode, go to projects , .

    Timer, counter, Pulse width modulation, watchdog timer

    Most microcontrollers provide one or more timers with 8 and/or 16 bit resolution. Timers are used for a variety of tasks ranging from simple delays over measurement of periods to waveform generation.

    The most basic use of the timer is in its function as a counter, but timers generally also allow the user to timestamp external events, to trigger interrupts after a certain number of clock cycles, and even to generate pulse-width modulated signals for motor control.

    Pulse width Modulation or PWM is one of the powerful techniques used in control systems today. It is used in wide range of application which includes: speed control, power control, measurement and communication.

    For detailed explanation about PWM theory of operation visit the article .

    For practical applications of pulse width modulation, go to project .

    A watchdog timer is a special hardware fail-safe mechanism that intervenes if the software stops functioning properly. The watchdog timer is periodically reset (sometimes called "kicking the dog") by software. If the software crashes or hangs, the watchdog timer soon expires, causing the entire system to be reset automatically.

    The inclusion and use of a watchdog timer is a common way to deal with unexpected software hangs or crashes that may occur after the system is deployed.

    Interrupt Control, handling, Interrupt service routine

    Microcontrollers are meant to be deployed in systems that have to react to events. To monitor the events we can simply poll the input signal, that is, to periodically check for state changes. Polling has its drawbacks: Not only does it unnecessarily waste processor time if the event only occurs infrequently, it is also hard to modify or extend. After all, a microcontroller generally has a lot more to do than just wait for a single event.

    The microcontroller itself offers a convenient way in the form of interrupts. Here, the microcontroller polls the signal and interrupts the main program only if a state change is detected. As long as there is no state change, the main program simply executes without any concerns about the event. As soon as the event occurs, the microcontroller calls an interrupt service routine (ISR) which handles the event. The ISR must be provided by the application programmer.

    Digital Input/Output, Analog Input/Output, ADCs, DACs

    To Read Full Article

    Login or Register to create a free Mbed Technologies account

    Register Login

    Want to contribute or ask a new article? Write and upload your article information .


     Embedded Systems

     C Programming

     Linux Software

     Python Programming

     Search Code Snippets

     Popular C search examples

     Popular C search MCQs

     Online certificate courses

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude

     Embedded System Software