📜 ⬆️ ⬇️

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 ... At the same time, they are single-chip computers, with their processor, memory, I / O 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 multivolume book, if everything is disassembled, so the article will contain intentional simplifications and omissions of all sorts of subtleties. I ask you not to judge strictly, I am far from professional in this topic, and this is my first article on Habré. If posted to the wrong blog, please move to the desired one.

A microcontroller is a single-chip computer. In 1971, they invented to place the entire processor strapping (RAM, port controllers, ROM, etc.) on a single chip. In 1980 intel already released the first microcontroller. To date, the microcontroller chip contains a processor, flash memory for programs with a capacity of 1K - 256K; 32 B - 8KB SRAM (RAM); 64 B - 4KB EEPROM. Clock frequency 1 - 16 MHz. Of course, the numbers are only approximate, it all depends on the manufacturer and model. Thus, the microcontroller has a Harvard architecture, i.e. Separate memory data and programs.

Naturally, without holivar not done here. In the market, approximately 30% each is occupied by controllers from different manufacturers and architectures. The old MCS-51 (8051) family, the PIC family from microchip and the AVR family from Atmel. We do not consider any specialized microcontrollers. (By the way, there is even ours, the domestic microcontroller KR1878BE1 (An15E03) from the Angstrom company, only for it the military and completed geeks write and develop on it, since there is neither developed support nor tools). In those forums that I dwell, atmel microcontrollers are overwhelmingly used, so the further narration will be mostly about them. Globally, they do not differ from PIC and others, the differences are only in parameters (speed, number of commands, memory, presence of peripherals ...).
')
Reasons for using microcontrollers.

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

What is the microcontroller programmed on?

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

Consider an example.
Let's take the ATMEGA8 microcontroller and look at what he can with his example.
For 28 rubles. (wholesale) / 41 rub. (rozn) we get a chip that contains:
• 8-bit RISC processor, 130 commands, 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 specified duration / count the pulses autonomously, without distracting the CPU)
• 3 PWM channels
• 8 channel ADC
• Hardware module Two Wire Interface (I2C clone)
• USART module
• Analog comparator.
• 23 I / O lines
All details are described in the datasheet for this microcontroller: www.atmel.com/dyn/resources/prod_documents/doc2486.pdf (308 pages, Eng.). Among those produced by Atmel, this is not the weakest or the coolest microcontroller.
Since the number of legs in a microcircuit is limited, almost all have 2 functions each, which can be switched by software.
I / O lines are combined in groups of 8 into ports called PORTA, PORTB, etc. Port and embedded peripherals are controlled by writing and reading data from special registers. So for example, in order to set a high logic 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 for example to measure the signal level at 1 output of the ADC, you need:

ADCSRA |= (1<<6); // ADSC
// 13
Result = ADCW; // .


The 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.

The microcontroller has an advanced interrupt system. In order not to load the processor with a constant state check (whether the ADC conversion has completed, whether the timer counter has reached the required value, whether a signal has arrived at the port), interrupts are activated for the corresponding events and interrupt handlers are written.
A microcontroller program is an endless loop. For example, a program that will flash an LED connected to the 3 leg of port A:

#include <delay.h>
void main (void)
{
while(1)
{
PORTA |= (1<<3); //
delay_ms(100);
PORTA &= ~(1<<3);
delay_ms(100);
};
};


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

Source: https://habr.com/ru/post/37764/


All Articles