Search This Blog

Wednesday, June 20, 2012

INTRODUCTION TO MICROCONTROLLER


Introduction

                                    It was electricity in the beginning....The people were happy because they did not know that it was existing all around them and that it could be utilized. It was fine. And then Faraday came and the stone has started rolling slowly...
                                     During the time, the first machines using a new sort of energy appeared accompanied with people who knew something about electricity. A long time has passed since then and just when civilization got used to this innovation and stopped paying attention to what a new generation of specialists were doing, someone came up with an idea that electrons could be a very convenient toy being closed in a glass pipe. At first sight, it was only a good idea, but there was no return, electonics was born and the stone continued rolling down the hill faster and faster...
                                         A new science - new specialists. The blue coats were replaced with white ones and people who knew something about electronics appeared on stage. While the rest of the humanity were passively watching in disbelief what was going on, within plotters two fractions appeared- “software-oriented” and “hardware-oriented”. A bit younger than their teachers, very enthusiastic and full of ideas, both of them kept on working but separate ways. While the first group had a stable development, hardware-oriented people, driven by success, soon threw caution to the wind and invented transistor.
                                           Up till that moment, the things could be more or less kept under control, but broad publicity was not aware of seriousness of the situation and it soon led to a fatal mistake! Being naive in belief that using cheap tricks could slow down technology development as well as developing of the world, mass market opened its door to the products of Electronics Industry, closing a magic circle therefore. Components’ prices fell rapidly becoming available for use to younger population. The stone was falling freely...
                                            The first integrated circuits and processors have soon appeared, which enabled for computers to drop down in price. The computers have started to develop their own production. The prices droped down again and Electonics got new adherents. It appeared everywhere. Another circle has been closed! Ordinary people got hold of computers and computer era has begun...
                                            While this drama was going on, hobbyists and professionals, protected by anonymity, were working hard on their projects , although split in two big groups. Then, someone has again remembered: Why should not we make an universal component? It would be a cheap and universal integrated circuit that could be programmed and used in any field of electronics, device and wherever needed. Technology has been developed enough, market exists, why not? So it happened, body and spirit are united, the circuit is created and called MICROCONTROLLER.

1.1 What are microcontrollers and what are they used for?

As all other good things, this powerful component is basically very simple and is obtained by uniting tested and high- quality "ingredients" (components) as per following receipt:
  1. The simplest computer’s processor is used as a "brain" of the future system.
  2. Depending on the taste of the producer, it is added : a bit of memory, a few A/D converters, timers, input/output lines etc.
  3. It is all placed in one of standard packages.
  4. A simple software that will be able to control it all and about which everyone will be able to learn has been developed.
Three things have had a crucial impact on such a success of the microcontrollers:
  • Powerful and intelligently chosen electronics embedded in the microcontrollers can via input/output devices ( switches, push buttons, sensors, LCD displays, relays…) control various processes and devices such as: industrial automatics, electric current, temperature, engine performance etc.
  • A very low price enables them to be embedded in such devices in which, until recent time it was not worth embedding anything. Thanks to that, the world is overwhelmed today with cheap automatic devices and various “intelligent” appliences.
  • Prior knowledge is hardly needed for programming. It is sufficient to have any kind of PC (software in use is not demanding at all and it is easy to learn to work on it) and one simple device (programmer) used for “transffering” completed programs into the microcontroller.
Therefore, if you are infected with a virus called electronics, there is nothing left for you to do but to learn how to control its power and how to direct it at the right course.

How does microcontroller operate?

Even though there is a great number of various microcontrollers and even greater number of programs designed for the microcontrollers’ use only, all of them have many things in common. That means that if you learn to handle one of them you will be able to handle them all. A typical scenario on whose basis it all functions is as follows:

  1. Power supply is turned off and everything is so still…chip is programmed, every thing is in place, nothing indicates what is to come…
  2. Power supply connectors are connected to the power supply source and every thing starts to happen at high speed! The control logic registers what is going on first. It enables only quartz oscillator to operate. While the first preparations are in progress and parasite capacities are being charged, the first milliseconds go by.
  3. Power supply connectors are connected to the power supply source and every thing starts to happen at high speed! The control logic registers what is going on first. It enables only quartz oscillator to work. While the first preparations are in progress and parasite capacities are being charged, the first milliseconds go by.
  4. Voltage level has reached its full value and frequency of oscillator has became stable. The bits are being written to the SFRs, showing the state of all periph erals and all pins are configured as outputs. Everything occurs in harmony to the pulses’ rhythm and the overall electronis starts operating. Since this moment the time is measured in micro and nanoseconds.
  5. Program Counter is reset to zero address of the program memory. Instruction from that address is sent to instruction decoder where its meaning is recognised and it is executed with immediate effect.
  6. The value of the Program Counter is being incremented by 1 and the whole process is being repeated...several million times per second. 

    1.2 What is what in microcontroller?

    Obviously, everything that occurs in the microcontroller occurs at high speed and quite simple, but it would not be so useful if there are no special interfaces which make it complete. Text below refers to that (in short).

    Program Memory (ROM)

    The Program Memory is a type of memory which permanently stores a program being executed. Obviously, the maximal length of the program that can be written to depends on the size of the memory. Program memory can be built in the microcontroller or added from outside as a separate chip, which depends on type of the microcontroller. Both variants have advantages and disadvantages: if added from outside, the microcontroller is cheaper and program can be considerably longer. At the same time, a number of available pins is decremented as the microcontroller uses its own input/output ports to be connected to the memory. The capacity of Internal Program Memory is usually smaller and more expensive but such a chip has more possibilities of connecting to peripheral environment. Program memory size ranges from 512B to 64KB.

    Data Memory (RAM)

    Data Memory is a type of memory used for temporary storing and keeping different data and constants created and used during operating process . The content of this memory is erased once the power is off. For example: when the program performes addition, it is necessary to have a register presenting what in everyday life is called “a sum” . For that purpose one of the registers in RAM is named as such and serves for storing results of addition. Data memory size goes up to a few KBs.

    EEPROM Memory

    The EEPROM Memory is a special type of memory which not all the types of the microcontrollers have.Its content can be changed during program execution (similar to RAM ), but it is permanently saved even after the power goes off (similar to ROM). It is used for storing different values created and used during operating process and which must be saved upon turning off the device ( calibration values, codes, values to count up to etc.). A disadvantage of this memory is that programming is relatively slow- measured in miliseconds.

    SFRs ( Special Function Registers )

    SFRs are a particular part of memory whose purpose is defined in advance by the producer. Each of these registers have its name and control some of interfaces within the microcontroller. For example: by writing zero or one to the SFR controlling some input/output port, each of the port pins can be configured as input or output (each bit in this register controls the purpose of one single pin).

    Program Counter

    Program Counter is an engine which starts the program and indicates the address in memory where next instruction to execute is found. Immediately after its execution, the value of the counter is incremented by 1. For this automatic increment, the program executes one instruction at a time as it is written. However…the program counter value could be changed at any moment, which will cause “jump” to a new location in the program memory. This is how subroutines or branch instructions are executed. When finding its new place in the program, the counter resumes even automatic counting +1, +1, +1…

    CPU (Central Processor Unit)

    As its name tells, this is "Big Brother" who monitors and controls all operations being performed within the microcontroller and the user cannot affect its work. It consists of several smaller units. The most important are:
    • Instruction decoder - a part of electronics which recognizes program instructions and on the basis of which runs other circuits.
    • Arithmetical Logical Unit (ALU) - performs all mathematical and logical operations with data. The features of this circuit are described in the "instruction set" which differs for each type of the microcontroller..
    • Accumulator - is a special type of the SFR closely related to operating mode of the ALU. It is a kind of desk on which all data needed to perform some operation on are set (addition, shift etc.). It also contains a result, ready to be used further in operation. One of the SFRs, called the Status Register, is closely related to the accumulator, showing at any time the "status" of a number being in the accumulator (the number is greater than or less than zero etc.).

      Input/output ports (I/O Ports)

      The microcontroller cannot be of any use without being connected to peripheral devices. For that reason each microcontroller has one or more registers connected to its pins (called ports in this case). 
    Why input/output? Because the user can change pin’s role according to his/her own needs. These are, in fact, the only registers in the microcontroller whose state can be checked by voltmeter !

    Oscillator

    The oscillator can be compared with rhythm section of a mini orchestra. Equalized pulses coming from this circuit enable harmonious and synchronic operating of all other parts of the microcontroller. It is commonly configured so as to use quartz-crystal or ceramics resonator for frequency stabilization. Besides, it can often operate without elements for frequency stabilization ( like RC oscillator). It is important to know that instructions are not executed at the rate ordered by oscillator but several times slower. The reason for this is that each instruction is executed in several steps (In some microcontrollers execution time of all instructions is equal, while in others microcontrollers execution time differs for different instructions). Consequently, if your system uses quartz-crystal of 20MHz, execution time of a program instruction is not 50nS but 200, 400 or even 800 nS!

    Timers/Counters

    Most programs use in some way these miniature electronic "stopwatches". They are mostly 8- or 16-bit SFRs whose value is automatically incremented with each coming pulse. Once the register is completely "filled up"- an interrupt is generated !
    If the registers use internal oscillator for its operating then it is possible to measure the time between two events ( if the register value is T1 at the moment measuring has started, and T2 at the moment measuring has finished, then the time that has passed is equal to the value gained by their subtruction T2-T1 ). If the registers for its operating use pulses coming from external source then such a timer is converted to counter.
    This is a very simple explanation used to describe the essence of the operating. It’s a bit more complicated in practice.

    Register is another name for a memory cell. Beside 8 bits available to the user, each register has also addressing part usually not visible to the user. It is important to know:
    • All registers in ROM as well as those in RAM memory identified as general-purpose registers are mutually equal and nameless. During programming, each register can be assigned a name, which makes operating much easier.
    • All SFRs have their own names which are different for different types of the microcontrollers and each of them has a particular role.

    Watchdog timer

    Its name tells a lot about its purpose. Watchdog Timer is a timer connected to a particular and totally independent RC oscillator within the microcontroller.
    If enabled to operate, every time it "counts up to end", the microcontroller is reset and program execution starts from the first instruction. The to keep this from happening by using particular command. The whole idea is based on the fact that every program circulates, in other words, the program is executed in several longer or shorter loops.
    If the instructions which resets the value of the watchdog timer are set at some important program locations, besides commands being regularly executed, then the operation of the watchdog timer will not be noticed.
    If for any reason (usually electrical disturbances in industry), the program counter "gets stuck" at memory location from where there is no return, the register’s value being steadily incremented by the watchdog timer will reach the maximum et voila ! Reset occurs!

    Power Supply Circuit

    Two things within the circuit that take care of the microcontroller power supply are worth attention :




    Brown out is potentially dangerous state coming up at the moment the microcontroller is being turned off or in situations when due to powerful disturbances, voltage supply comes to the lowest limit. As the microcontroller consists of several circuits which have different operating voltage levels, this can cause its "out of control" performance. In order to prevent that, a circuit for brown out reset is usually embedded. When the voltage level drops below the lower limit then this circuit immediately resets the whole electronics.
    Reset pin is usually identified as MCLR (Master Clear Reset) and serves for "external" reset of the microcontroller by applying logical zero or one depending on type of the microcontroller. In case the brown out is not embedded a simple external circuit for brown out reset can be connected to this pin.

    Serial communication



    Connection between the microcontroller and peripheral devices established through I/O ports is an ideal solution for shorter distances- up to several meters. But, when it is needed to enable communication between two devices on longer distances or when for any other reason it is not possible to use "parallel" connection ( for example remote control of the aircraft ) it is obvious that something so simple cannot be taken into account. In such and similar situations, communication through pulses, called serial communication is the most appropriate to use.
    Serial communication problem has been resolved a long time ago and nowadays several different systems enabling this kind of connection are embedded as a standard equipment into most microcontroller. Which of them will be used in very situation depends on several factors. The most important are the following:
    • With how many devices the microcontroller must exchange data ?
    • How fast the serial communication must be?
    • What is the distance between devices?
    • Is there any need to transmit and receive data simultaneously ?
    One of the most important things concerning the use of serial communication is to strictly observe the Protocol. It is a set of rules which must be applied in order to enable devices to recognize the data being exchanged. Fortunately, the microcontrollers automatically take care of it, which leads to a reduction of the programmer’s work to simple writing and reading data.

No comments:

Post a Comment