Back to Home

About microcontrollers

DIY · microcontrollers · educational program

About microcontrollers

    About 55% of the processors sold in the world account for 8-bit microcontrollers. More than 4 billion 8-bit microcontrollers were sold in 2006. They are installed in microwaves, washing machines, music centers ... Moreover, they are single-chip computers with their own processor, memory, input-output ports. In the article I will try to briefly explain what kind of animals they are and how they are tamed.

    The topic is extensive and rather suitable for a multi-volume book, if you disassemble everything, so the article will intentionally simplify and omit all sorts of subtleties. I ask you not to judge strictly, in this topic I myself am far from a professional, and this is my first article on the hub If posted to the wrong blog, please move to the correct one.

    The microcontroller is a single-chip computer. In 1971 they came up with the idea of ​​placing the entire processor harness (RAM, port controllers, ROMs, etc.) on one chip. In 1980 intel already released the first microcontroller. Today, the microcontroller chip contains a processor, flash memory for programs, 1K - 256K; 32 B - 8KB SRAM (RAM); 64 B - 4KB EEPROM. Clock frequency 1 - 16 MHz. Of course, the figures are only approximate, it all depends on the manufacturer and model. Thus, the microcontroller has a Harvard architecture, i.e. separate data and program memory.

    Naturally, it was not without holivar here either. Approximately 30% of the market is occupied by controllers of different manufacturers and architectures. The old MCS-51 family (8051), the PIC family from microchip, and the AVR family from Atmel. All sorts of specialized microcontrollers we do not consider. (By the way, there is even ours, the domestic microcontroller KR1878BE1 (An15E03) from the Angstrem company, only for it military and complete geeks write and develop devices on it, because there is neither developed support nor tools). In the forums that I inhabit, the vast majority use atmel microcontrollers, so the rest of the story will be mostly about them. Globally, they do not differ from PIC and others, the differences are only in the parameters (speed, number of teams, memory, presence of peripherals ...).

    Reasons for using microcontrollers.

    Despite the fact that the microcontroller device is quite complex, their use greatly simplifies the development of electronic devices. Hardware circuitry solutions are transferred to the plane of the program code. The relay in the automation cabinet is replaced by a string in C. This achieves tremendous flexibility, because most microcontrollers can be reflashed without removing from the board. An alarm clock with a function to turn on a load (for example, a kettle) at a given time can be absolutely the same hardware as a microwave control unit, only the programs will differ. It is precisely because of this, when the manufacturer cannot afford to develop a specialized chip, he sets the entire logic of the program and sews it into the microcontroller.

    What is the microcontroller programmed on?

    Historically, the main language is assembler. In particular, KR1878BE1 is programmed only in assembler. For other microcontrollers, there are compilers for the C language. For atmel microcontrollers there is an open source WinAVR compiler (gcc port). There is also a compiler for Basic Bascom AVR, and even for the Forth language. Initially, the architecture of the AVR microcontrollers was optimized so that programs written in C would run faster.

    Let's look at an example.
    Let's take an ATMEGA8 microcontroller and consider what it can with an example.
    For 28 rub. (wholesale) / 41 rub. (Retail) we get a chip that contains:
    • An 8-bit RISC processor, 130 instructions, most of which are executed in 1 clock cycle, 32 general-purpose registers. Performance up to 16 MIPS at 16 MHz.
    • 8K flash memory for programs, 512 bytes of EEPROM memory, 1K SRAM
    • Two 8-bit and one 16-bit timer (allow you to generate pulses of a given duration / count pulses autonomously, without distracting the CPU)
    • 3 PWM channels
    • 8-channel ADC
    • Two hardware module Wire Interface (I2C clone)
    • USART module
    • Analog comparator.
    • 23 I / O lines.
    Everything is described in detail in the datasheet for this microcontroller: www.atmel.com/dyn/resources/prod_documents/doc2486.pdf (308 pp. English). Among the manufactured by Atmel, this is not the weakest and not the coolest microcontroller.
    Since the number of legs of the microcircuit is limited, almost all have 2 functions that can be switched programmatically.
    I / O lines are connected in groups of 8 into ports called PORTA, PORTB, etc. Ports and integrated peripherals are managed by writing and reading data from special registers. So for example, to set a high logical level (+5 V) on the third line of port A, you need to set the third bit in the PORTA register like this:
    PORTA | = (1 << 3);
    And in order to, for example, measure the signal level on the 1st output of the ADC you need: Work with the rest of the microcontroller modules (USART, TWI, timers) is about the same - set the necessary bits to configure them, and pick up / write data in special registers.

    ADCSRA |= (1<<6); //устанавливаем бит ADSC который запускает процесс измерения
    //процесс преобразования занимает около 13 тактов
    Result = ADCW; //забираем результат из соответствующего регистра.




    The microcontroller has a developed interrupt system. In order not to load the processor with a constant status check (whether the ADC conversion has completed, whether the timer counter has reached the required value, whether a signal has arrived on the port), interrupts for the corresponding events are turned on and interrupt handlers are written.
    The microcontroller program is an endless cycle. Here is an example of a program that will blink with an LED connected in the 3rd leg of port A:

    #include
    void main (void)
    {
    while(1)
    {
    PORTA |= (1<<3); //основная программа
    delay_ms(100);
    PORTA &= ~(1<<3);
    delay_ms(100);
    };
    };


    Thus, it can be seen that there is nothing complicated in microcontrollers, and almost everyone can make gadgets necessary for themselves, such as a shelf watch, recently published on Habrahabr, if desired. I also recently saw how on the basis of the Arduino platform (the same microcontroller only soldered on the board and a bunch of terminals for connecting an external load) they made a doorbell working with twitter (http://rooreynolds.com/2008/05/14/hacking-the -doorbell /) Due to the fact that enthusiasts created libraries for working with common nodes (LCD screens, thermometers ...), the development time and complexity are greatly reduced. In the next article I will show how to make an electronic clock on a microcontroller in 1.5 days.

    Read Next