AVR programming. Programming AVR Microcontrollers for Beginners

Microcontrollers are small, but at the same time very convenient devices for those who want to create various amazing robotic or automated things at home. As part of this article, AVR programming for beginners, various aspects and nuances of this process will be considered.

general information

avr programming
Microcontrollers can be found everywhere. They are in refrigerators, washing machines, telephones, machine tools in production, smart homes, and even in many different technical devices. Their widespread use is due to the possibility of replacing more complex and large-scale analog device circuits. Programming MK AVR allows you to provide autonomous control over electronic devices. These microcontrollers can be thought of as the simplest computer that can interact with external technology. So, they can open / close transistors, receive data from sensors and display them on screens. Also, microcontrollers can perform various processing of input information like a personal computer. If you master the programming of AVR from scratch and reach the professional level, you will have almost unlimited possibilities for controlling various devices using I / O ports, as well as changing their code.

A bit about AVR

c programming
The article will consider a family of microcontrollers manufactured by Atmel. They have pretty good performance, which allows you to use them in many amateur devices. Widely used in industry. You can find in this technique:

  1. Domestic. Washing machines, refrigerators, microwave ovens and more.
  2. Mobile Robots, communications, and so on.
  3. Computational. Peripheral device control systems, motherboards.
  4. Entertaining. Jewelry and children's toys.
  5. Transport. Security and engine management systems.
  6. Industrial equipment. Machine control systems.

These, of course, are not all areas. They are used where it is advantageous to use not a set of control chips, but one microcontroller. This is possible due to low energy consumption and simplified production. To write programs, the C and Assembler languages ​​are used, slightly modified for the microcontroller family. Such a change is necessary due to weak computing capabilities, which are usually calculated in tens of kilobytes. AVR programming without learning these languages ​​is not possible.

How to get your first microcontroller?

AVR programming requires:

  1. The presence of the necessary development environment.
  2. The microcontrollers themselves.

The second item will be considered in more detail. There are three possibilities to get the required device:

  1. Buy directly the microcontroller itself.
  2. Get a device as part of the designer (for example - Arduino).
  3. Assemble the microcontroller yourself.

In the first paragraph there is nothing complicated, so immediately go to the second and third.

Get a device as part of the designer

programming courses
The famous Arduino will be chosen as an example. This is an electronic designer, in combination a convenient platform for the rapid and high-quality development of various electronic devices. The Arduino board includes a certain set of components for operation (there are various configurations). It necessarily includes an AVR controller. This approach allows you to quickly start developing the device, does not require special skills, has significant opportunities in terms of connecting additional boards, and also on the Internet you can find a lot of information on questions of interest. But there were some cons. Buying an Arduino, a person deprives himself of the opportunity to plunge deeper into AVR programming, to better know the microcontroller, the specifics of its work. A relatively narrow line of models also adds negativity, which is why you often have to buy boards for specific tasks. The peculiarity is also that the programming in "SI" here differs quite strongly from the standard form. Despite all its flaws, the Arduino is suitable for beginners to explore. But do not abuse.

Self assembly

It should be noted that the AVR microcontrollers are quite friendly to beginners. You can assemble them yourself with affordable, simple and cheap components. If we talk about the pros, this approach allows you to better familiarize yourself with the device, independently choose the necessary components, customizing the final result to the requirements put forward, the use of standard programming languages ​​and low cost. Of the minuses, we can only note the complexity of self-assembly when it is carried out for the first time, and there is no necessary knowledge and skills.

How to work?

programming software avr
So, let's say that the issue with the microcontroller has been resolved. Further, it will be considered that it was acquired or purchased independently. What else do you need to master AVR programming? For this purpose, you need a development environment (a regular notepad is also suitable as a basis, but I recommend stopping at Notepad ++). Although there are other programs for programming AVR, the above software will be able to cope with all the requirements. A programmer is also needed. It can be purchased at a nearby store, ordered online, or assembled on your own. The circuit board does not hurt. It is not required, but its use allows you to save your nerves and time. Also bought / created independently. And the last one is the power source. For AVR, it is necessary to provide a voltage supply of 5V.

Where and how to study?

Creating masterpieces from scratch does not work. It requires knowledge, experience and practice. But where to get them? There are several ways. Initially, you can independently seek out the necessary information in the global network. You can sign up for programming courses (distance or full-time) to obtain basic working skills. Each approach has its advantages. So, remote programming courses will be cheaper, and maybe free. But if something does not work out, then in full-time classes an experienced developer will be able to quickly find the cause of the problem. Also, it will not be superfluous to familiarize yourself with the literature that is in the public domain. Of course, you won’t be able to go out on books alone, but you can get basic knowledge about the device, programming with SI, Assembler, and other working points.

Input / output ports

mk avr programming
This is an extremely important topic. Without an understanding of how I / O ports work, in-circuit AVR programming in general is not possible. After all, the interaction of the microcontroller with external devices is carried out precisely with their mediation. At first glance, a beginner might think that the port is a rather confusing mechanism. To avoid such an impression, we will not consider in detail the scheme of its work, but only get a general idea about it. Consider a software implementation. As an example of the device, the AtMega8 microcontroller was chosen - one of the most popular of the entire AVR family. The input / output port is three registers that are responsible for its operation. At the physical level, they are realized as legs. Each of them corresponds to a specific bit in the control registry. Each leg can work both for inputting information, and for its output. For example, you can hang the function of ignition of the LED on it or the processing of pressing a button. By the way, the three registers that were mentioned are: PORTx, PINx and DDRx. Each of them is eight-bit (do not forget that we are considering AtMega8). That is, one bit is engaged in a certain leg.

Work registers

The most significant in terms of orientation is the managing DDRx. It is also eight-bit. Values ​​for it can be written 0 or 1. How does the controller change when using zeros and ones? If you set 0 in a certain bit, then the corresponding leg will be switched to the input mode. And from it it will be possible to read data that comes from external devices. If you set 1, then the microcontroller will be able to control something (for example, give an order to the transistor to skip the voltage and light up the LED). The second most important is PORTx. He is involved in managing the condition of the legs. Let's look at an example. Let's say we have an output port. If we set the logical unit to PORTx, then a signal is sent from the microcontroller to the control unit to start work. For example, light an LED. When zeroing, it will be extinguished. That is, to work with the DDRx control register constantly, there is no need. And finally, let's talk about PINx. This register is responsible for displaying the status of the controller foot when it is configured for input status. It should be noted that PINx can only work in read mode. Write to it does not work. But to read the current state of the leg is no problem.

Work with analogues

AVRs are not the only microcontrollers. This market is divided between several large manufacturers, as well as between numerous Chinese imitation devices and homemade products. In many ways they are similar. For example, PIC / AVR programming is not much different. And if there is an understanding of one thing, then it will be easy to understand everything else. But we still recommend starting the path with AVR due to its competent structure, friendliness to the developer and the presence of a large number of supporting materials, because of which the development process can be significantly accelerated.

Safety precautions

in-circuit avr programming
When programming the AVR microcontrollers on "SI" or on "Assembler", it is necessary to work very carefully. The fact is that by setting a certain combination of registers and changing the internal settings, you can safely lock the microcontroller. This is especially true for fuses. If you are not sure of the correctness of your actions, then it is better to abandon their use. The same applies to programmers. If you buy factory equipment, it will flash microcontrollers without problems. When assembling with your own hands, a sad situation may arise in which the programmer locks the device. This can happen either due to an error in the program code, or through a malfunction in it. By the way, about another (this time positive) point, which was previously mentioned in passing, but was never fully disclosed. Now almost all modern microcontrollers have the function of in-circuit programming. What does it mean? Let's say that the device was soldered on the board. And to change its firmware, now you do not need to evaporate it, because such an intervention can damage the microcontroller itself. It is enough to connect to the corresponding conclusions and reprogram it through them.

Which model to choose?

AtMega8 was reviewed as part of the article. This is a rather mediocre microcontroller for its characteristics, which, nevertheless, is enough for most crafts. If you want to create something large-scale, then you can take some kind of monsters like Atmega128. But they are designed for more experienced developers. Therefore, if there is not enough experience, it is best to start with small and simple devices. In addition, they are much cheaper. Agree, it’s one thing to accidentally block a microcontroller for one hundred rubles, and quite another for five hundred. It is better to get your hand in and figure out the various aspects of functioning so that you do not lose significant amounts in the future. Initially, you can start with AtMega8, and then orient yourself according to your needs.

Conclusion

pic avr programming
So the topic of AVR programming in the most general terms was considered. Of course, much more can be said. So, for example, marking of microcontrollers was not considered. And it can say a lot. So, basically microcontrollers operate at a voltage of 5V. Whereas the presence, for example, of the letter L can say that only 2.7 V is enough for the device to work. As you can see, sometimes knowledge about marking can play a very important role in terms of the correct and durable operation of the devices. The operating time of microcontrollers is also an interesting topic. Each device is designed for a certain period. So, some can work out a thousand hours. Others have a guarantee stock of 10,000!

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


All Articles