The principle of working with Atmel microcontrollers (AVR)

Microcontroller - a device, a microchip, designed to control other devices. The microcontroller contains a processor, memory, as well as peripheral devices. In fact, the controller "toggles its pins" - sends pulses of different duration to the pins.

Microcontroller
atmega8 in DIP package

This is very important to understand when starting to study microcontrollers.

The list of controller's peripheral devices is very large.

Incomplete list:

  • universal digital ports - have adjustable operating mode both for input and output;
  • other input-output interfaces such as UART, USB, IEEE 1394, Ethernet;
  • analog-to-digital and digital-to-analog converters;
  • comparators;
  • pulse width modulators;
  • timers;
  • built-in flash memory arrays;
  • built-in clock generator and watchdog timer.
There are also more exotic devices - radio transmitters, drivers for various devices, etc.

In the notes, we will consider the principle of working with Atmel (Avr) microcontrollers

To start, it is sufficient to understand the universal digital ports.

Each Atmel controller has ports named PORTA, PORTB, .... depending on the type of microcontroller. Ports do not necessarily start with PORTA - for example, in DIP Atmega8 it does not exist. Depending on the type of controller, the ports have different bit widths (8, 16, 32, etc.). The bit width of the port may be included in the controller's name. For example, Atmega8, Atmega16, Atmega32. And it may not be included (Attiny2313 as an example - but it is also an 8-bit one). Hopefully, it is intuitively understood that the bit width of the port determines how many bits of information are transmitted simultaneously - and, therefore, how many pins the port has.

Let's consider working with a microcontroller in the C language.

First, you need to configure the port as input or output. This can be done using the register DDRx, where x is the port (A, B, C, etc.) 1 - the port is configured as output, 0 - as input.

Pin numbering of the port:

ports

For example, let's configure the first bit (pin 1) of port A as output:

DDRA = 0b00000001;

The leading 0b means that the number is represented in base-2 (binary) numeral system. You can represent numbers in any convenient form. But 2 forms are commonly used - binary and hexadecimal. The choice of which one to use is up to you.

Admittedly, such notation is not very convenient (but the most understandable and fast), usually bitwise operations are used for such and similar actions, but we will consider them in detail (applied to microcontrollers) in one of the next notes.

Now we can pass a value to the port (to the same pin 1):

PORTA = 0b00000001;

Well.... That's essentially all for familiarization! These basic knowledge plus a little imagination plus a little basic experience in programming is enough to, for example, create some kind of LED blinker (for example, like here: Подарок девушке своими руками (on attiny2313)).

For example, under gcc-avr the simplest program looks like this:

//Include input/output libraries
#include <avr/io.h>

void main() {

//Set first bit of port A as output DDRA = 0b00000001;

//Set high logic level to the first pin of port A PORTA = 0b00000001; }

And now we can turn on/off the LED =)