blog posts

AVR microcontroller programming Full guide !

A microcontroller is an electronic component with a CPU, input and output ports, RAM and ROM, and other components. Microcontrollers are usually design for small applications; Hence, unlike microprocessors (microprocessors), the most important issue is simplicity and low power consumption. One common type of microcontroller is called an AVR microcontroller. We then answer what an AVR microcontroller is and what its features are and get acquainted with its applications in electricity and electronics. Finally, we take a deeper look at the internal components of a microcontroller and how they work.

What is the difference between a microprocessor and a microcontroller?

The two concepts of microprocessor (microprocessor) and microcontroller (microcontroller) are often confusable. Microcontrollers, while having many similarities to microprocessors, also have important differences. Microcontrollers are an advanced type of microprocessor.

Microprocessors are integrated circuits that have only a CPU. In addition to the CPU, microcontrollers have RAM and ROMs, input and output units, and some other internal components. Microcontrollers are sometimes callable by minicomputers or single-chip computers. But does this mean that microcontroller is another name for computer?

What is the difference between a computer and an AVR microcontroller?

The computer is designed to be used for many general tasks on a single device; For example, it can perform mathematical calculations, store information, or access the Internet. On the other hand, microcontrollers are designed to execute only one command; For example, turn on an air conditioner when the temperature is too high and turn it off when the temperature is too low.

Computer processors are at least 32-bit; they process data in batches of 32 digits or more. But the AVR microcontroller is much simpler and usually processes data in eight bits. Of course, there are newer AVR microcontrollers that are 16-bit or 32-bit.

Because if you do not install programs on your computer, the computer will be useless, so AVR microcontroller programs must also be install. This program is storable on the AVR internal memory. You can load AVR programs on it by a device called a programmer. In the continuation of this article, we will get to know AVR microcontroller programming methods.


What is AVR Microcontroller Electrical and Electronics

What is an AVR microcontroller? AVR is a type of microcontroller manufactured by Atmel. The AVR microcontroller was one of the first microcontrollers to use internal flash memory to store applications; Unlike one-time programmable ROMs, EPROMs and EEPROMs were used in other microcontrollers at the time.

History of AVR microcontroller

The AVR microcontroller is an electronic device that was first introduced in 1996. Alf-Egil Bogen and Vegard Wollan designed the AVR microcontroller architecture. Also, the AVR microcontroller is also named after its designers, A if-Egil Bogen Vegard Wollan R ISC, also known as Advanced Virtual R ISC. The AT90S8515 was the first microcontroller based on the AVR design; However, until the AT90S1200 microcontroller was introduced in 1997, the AVR microcontroller was not yet known.

Why choose an AVR microcontroller?

There are different types of microcontrollers. Electrical engineers are aware of the features of each microcontroller and use them in a variety of applications. But some examples of the most popular microcontrollers are the AVR, PIC, and 8051 microcontrollers. But what is the special feature of the AVR microcontroller?

Comparison of AVR with PIC – Difference of AVR with 8051

The main feature of the AVR microcontroller is its high speed. The AVR microcontroller executes many commands at high speed in each execution cycle. The AVR is about four times faster than the PIC microcontroller, consumes less power, and can operate in various power storage modes.

Explanation: The RISC or Reduced instruction Set Computing architecture is a CPU design that increases efficiency and execution speed by simplifying commands. In contrast, CISC or Complex Instruction Set Computing, means a complex set of commands.

Key Features of Electronic Electrical AVR Microcontroller

Therefore, the following can be considered the main features of the AVR microcontroller:

  • High speed
  • Excellent performance
  • High memory
  • Low power consumption
  • Optimized
  • low price
  • RISC architecture

What are the AVR microcontroller categories?

VAVR microcontrollers are divided into three categories based on their structural characteristics:

  • TinyAVR – has less memory and smaller size and is suitable for simpler applications.
  • MegaAVR – Mega microcontroller is the most popular type of AVR. It has a relatively good memory (up to 256 KB), and more peripherals and is suitable for medium to complex applications.
  • XmegaAVR – Used for complex applications that require more memory and higher speeds.

MegaAVR microcontroller family

Some of the most used microcontrollers are from the Mega family:

  • ATmega8 microcontroller
  • ATmega16 microcontroller
  • ATmega32 microcontroller
  • ATmega328 microcontroller

The following table shows a comparison of the specifications of these types of microcontrollers:

How to register an AVR microcontroller

On the ATmega16 microcontroller, the letters AT refer to the manufacturer of the AVR microcontroller, Atmel. Mega is the type of AVR, and 16 indicates the amount of memory, 16 KB. 

What is the application of the AVR microcontroller for electricity and electronics?

Microcontrollers are useful in many electrical and electronic devices that are automatically controlled. For example, car engine control systems, medical devices, remote controls, and many household appliances such as printers, washing machines, microwaves, toys, etc.

The AVR microcontroller is also useful in many applications, such as the Internet of Things and smart homes, touch screens, cars, etc. In the following, we will get acquaintation with some features and applications of each MegaAVR microcontroller.

ATmega8 microcontroller

ATmega8 microcontroller features

  • 28 pins
  • One kilobyte of internal RAM
  • Eight KB of flash memory
  • Two external interrupts

Applications of ATmega8 microcontroller

  • Use in electrical and electronics projects
  • Write text using LED matrices
  • Measure light intensity using LDR
  • Alarm bells

Making the clock using LED matrices and an AVR microcontroller

ATmega16 microcontroller

ATmega16 microcontroller features

  • 40 pins
  • One kilobyte of internal RAM
  • Eight KB of flash memory
  • Speed ​​one million commands per second
  • Different modes of power storage

Applications of ATmega16 microcontroller

  • Smart home systems
  • Medical equipment
  • Embedded systems
  • Electrical and electronics projects
  • Automotive and industrial automation
  • Temperature and pressure control devices

ATmega16 microcontroller

ATmega32 microcontroller

ATmega32 microcontroller features

  • 44 pins
  • Two KB of internal RAM
  • 32 KB of flash memory
  • Speed ​​16 million commands per second

Applications of ATmega32 microcontroller

  • Temperature and pressure control devices
  • Measurement of analog signals
  • Latent systems such as coffee machines or mobile phones
  • Engine control systems
  • Digital signal processing

ATmega328 microcontroller

ATmega328 microcontroller features

  • 28 or 32 pins
  • Two KB of internal RAM
  • 32 KB of flash memory
  • Speed ​​of 20 million commands per second

Applications of ATmega328 microcontroller

  • Use in making Arduino
  • robotic
  • Power control and management systems

Use of AVR microcontroller in Arduino

What is the AVR programming language?

What is the AVR programming language? To answer this question, we must first know what a program is. A program is a set of simple commands that receive data and change it. In most applications, when using an AVR microcontroller, such as a washing machine, data is receivable, their values ​​are read, and action is taken base on them. Sometimes, for example, to display data on an LCD screen, it is necessary to change the data and send it to another device.

Assembly, the most basic AVR programming language

To execute these instructions, they must be written in simple binary code. Each of these binary commands has an equivalent assembly language closer to our language. The most basic way to write AVR microcontroller programs is to write assembly commands; However, it is possible to write code in a binary language, but this does not make sense in common applications.

Using assembly language helps us better understand the structure of the AVR microcontroller and how its various components work. In addition, these codes will be short and fast. But the downside is that you, as a programmer, have to do everything. These include memory management and program structure, which can be very tedious.

What is the most common AVR microcontroller programming language?

To avoid this problem, programmers use high-level programming languages ​​to build AVR programs. Some of the most common AVR programming languages ​​include C, Java, and Basic. High level means that in languages ​​like C, each line of code is translated into many lines of assembly code.

Also, because the compiler itself manages the memory and structure of the program, the programmer’s task becomes easier. Common and frequently used commands are also readily available in libraries of these languages.

Take a deeper look at the AVR microcontroller for electronics and electronics.

There are many microcontrollers in the AVR family, and each is used in electrical and electronics for specific applications. Here we take a closer look at the ATmega16 microcontroller. The ATmega16 microcontroller has 1 KB of RAM and 40 pins at a speed of one million commands per second. Some of its features are:

  • 32 input / output pins
  • One kilobyte of EEPROM data storage memory
  • A 16-bit timer and an eight-bit timer
  • Analog to digital converter

Internal components of ATmega16 microcontroller

In this section, we examine each of the internal parts of the ATmega16 microcontroller.

Input / output ports: ATmega16 has four types (PORT A, PORT B, PORT C and PORT D) eight-bit input / output port.

Internal Oscillator: The ATmega16 microcontroller has a 1 MHz internal oscillator to adjust the internal clock. The maximum frequency of this oscillator is eight MHz. It is also possible to connect the ATmega16 microcontroller to an external oscillator and increase its frequency up to 16 MHz.

Analog to Digital Converter (ADC): The ATmega16 microcontroller has eight channels of 10-bit analog-to-digital conversion. The ADC reads the analog input of devices such as sensors and converts them into digital information intelligible to the microcontroller.

Digital to Analog Converter (DAC): The ATmega16 microcontroller has a digital to analog converter that does the opposite of ADC.

Timer / counter: The ATmega16 microcontroller has two timers / an eight-bit timer and a 16-bit timer / counter. Timers are useful for tasks such as spacing two commands.

Watchdog timer: The watchdog timer is located next to the internal oscillator. This timer continuously monitors the microcontroller, and if the microcontroller gets in a part of the code for more than a certain period, it resets the AVR microcontroller.

Interrupt: The interrupt in the AVR microcontroller is one of the most useful and practical units. If the command condition in the interrupt field is true while the program is running, the CPU stops executing the program commands and resumes after executing the interrupt commands. The ATmega16 microcontroller has 21 interrupts, four of which are external.

USART: The USART protocol is a transmitter-receiver communication protocol that allows the transfer of information between the AVR microcontroller and other systems.

SPI: The SPI or Serial Peripheral Interface exchanges information between two devices with the same clock source. SPI data transfer rate is higher than USART.

ISP: In the AVR microcontroller, the ISP can be loaded into the program using the programmer without removing the microcontroller from the circuit.

Memory: ATmega16 has three different types of memory.

  1. Flash memory: EEPROM flashes or flash memory stores data and can be easily erased electrically. This memory also holds information without an electrical connection. The ATmega16 microcontroller has 16 KB of EEPROM flash memory.
  2. Byte Addressable EEPROM: This memory, like flash memory, stores information without an electrical connection. ATmega16 has 512 bytes of this type of memory.
  3. SRAM: SRAM, or fixed memory, is a memory that is erased when a power outage occurs. The ATmega16 microcontroller has an internal kilobyte of SRAM used by the CPU and some other microcontroller components.

Basics of ATmega16

Here are some important basics of the ATmega16 microcontroller:

VCC: Digital power supply voltage

GND: Land

PORTA (PA0… PA7): PORT A pins can be analog inputs. If the analog-to-digital converter is not useful, it also acts like an eight-bit input/output port.

PORT B, PORT C, and PORT D: These pins act as eight-bit I / O ports.

Reset: This is the basis for performing a hardware reset. If the base is grounded, the microcontroller resets.

XTAL1 and XTAL2: These are the bases for connecting to an external crystal oscillator. The ATmega16 microcontroller can operate up to 16 MHz if an external crystal is useful.

Pins 30 to 32: We connect these pins to our reference voltage if we intend to use an analog-to-digital converter.