• Design and programming of AVR microcontrollers. Microcontroller and how to defeat it. What is a memory chip and how to program microcircuits

    I am categorically against this approach. Usually it all ends - either with nothing, or with forums clogged with pleas for help. Even if someone is helped, 90% of the time it will never appear on electronics sites again. In the remaining 10%, he continues to flood the forums with pleas, they will first kick him, then throw mud at him. Of these 10%, another 9% are eliminated. Then there are two options: either it comes to a stupid head and still goes to the beginning, or in particularly neglected variants, it is his lot to copy other people’s designs, without a single thought about how it works. Arduinists are often born from the latter.

    The way from scratch, in my opinion, is to study the peripherals and features, if it is a microcontroller. It’s more correct to first figure out how to jerk your legs, then with timers, then with interfaces. And only then try to raise your FAT. Yes, this is not fast, and it will require time and effort, but practice shows that no matter how you try to shorten this path, problems will still emerge that will have to be solved and you will spend much more time without this base.

    Just don’t confuse warm and soft. First, there are exceptions to all rules, I personally saw people who had never held microcontrollers in their hands before, but in an extremely short period of time they were able to outpace experienced experienced radio amateurs, we don’t take them into account. Secondly, I came across individuals who started by copying diagrams and figured it out right away, but this is most likely an exception to the rule. Thirdly, there are also experienced programmers among the Arduino developers; this is just a platform, but this is rather an exception.

    If we talk about the general mass, then things are exactly as I described at the beginning: reluctance to understand the basics, in best case scenario delays the moment when we have to return to these issues. In the worst case, you will quickly hit the ceiling of your knowledge and always blame someone else for your problems.

    2. Before solving a problem, break it down to the point of absurdity, down to “soldering a resistor”, this helps, it’s been tested. Small problems are much easier to solve. When a large task is broken down into a bunch of small actions, all that remains is to complete them. I can give you one more piece of good advice, even though it may seem crazy to you - get a notebook and write in it everything you are going to do. You think I’ll remember that, but no. Let’s say today I’m in a good mood and I’m thinking about how to collect the board. Write down an action plan: go buy a resistor, prepare the wires, mount the display. Then you’ll forget everything, open your notebook and look - yeah, today I’m in the mood to saw and plan, I’ll make a fastening. Or you are assembling a board and the last component is left to be soldered, but that’s not the case, the resistors have run out, so I should have written it down before soldering, then I remembered.

    3. Do not use code generators, non-standard features and other simplifications, at least in the first stages. I can bring mine personal example. During times active use AVR I used the CAVR codegen. I was completely satisfied with him, although everyone said he was crap. The bells were constantly ringing, there were problems with libraries, with syntax, with porting, but it was hard to give up on it. I didn’t understand how it worked, I just knew where and how to check the boxes.

    The stake was driven into my coffin with the advent of STM32, it was necessary to crawl onto them, and that’s when the problems appeared. Problems are putting it mildly; in fact, I had to master microcontrollers and the C language from scratch. I didn't repeat past mistakes again. I must say this has already come in handy more than once. Since then, I’ve had the opportunity to work with other platforms and I don’t experience any difficulties; the approach justifies itself.

    Regarding all the improvements and simplifications, there was one very good comparison that they are like wheelchairs that ride on rails, you can ride and enjoy, but you can’t get up, where they take you, you will arrive there.

    4. Learn C language. Oh, how often do I hear novice radio amateurs boast that they know the radio well. This became food for me; I always like to consult with such interlocutors. It usually becomes clear right away that they don’t know the language at all. I can say that despite the apparent simplicity, I haven’t met many people who really knew him well. Basically, everyone knows him as much as is required to solve problems.

    However, the problem in my opinion is that without knowing the possibilities, you severely limit yourself. On the one hand, non-optimal solutions that will require more powerful iron, on the other hand not readable code, which is difficult to maintain. In my opinion, readability and maintainability of code is one of the most important places and it is difficult for me to imagine how this can be achieved without using all the capabilities of the C language.

    Many beginners are disdainful of learning the language, so if you are not like everyone else, you will immediately become two steps above other beginners. It also makes no difference where to study the language. In my opinion, a microcontroller is not very suitable for this. It's much easier to put some Visual studio or Qt Creator and solve problems on the command line.

    It will also be a good help to study any language tests that are given during interviews. If you dig around, you can learn a lot of new things.

    5. Learning assembler? There is no need to be afraid of him, nor to idolize him. You shouldn’t think that if you know how to write a program in assembly language, you will immediately become a microcontroller guru, for some reason this is a common misconception. First of all, it is a tool. Even if you don't plan to use it, I would still strongly recommend writing at least a couple of programs. This will greatly simplify the understanding of the microcontroller and internal structure programs.

    6. Read the datasheet. Many developers neglect this. By studying the datasheet you will be two steps higher than those developers. Doing this is extremely useful, firstly, this is the primary source, no matter what sites you read, in most cases they repeat information from the datasheet, often with errors and omissions. There may also be information there that you don't think about now, but that may be useful in the future. It may happen that some kind of error pops up and you remember that yes, it was said about this in the datasheet. If your goal is to become a good developer, then this stage cannot be avoided; you will have to read datasheets; the sooner you start doing this, the faster your growth will go.

    7. People often ask to send datasheets in Russian. A datasheet is something that should be perceived as the truth, the most accurate information. Even there, mistakes are possible. If the translator’s mistakes are added to this, he is also a human being, maybe not even on purpose, just misspelled. Or he has his own vision, he may miss something that, in his opinion, is not important, but perhaps extremely important for you. The situation becomes especially funny when you need to find documentation for not very popular components.

    In my opinion, it is much easier to exclude the entire layer of these problems in advance than to catch them later. Therefore, I am categorically against translations, the only true advice is to study English language to read datasheets and manuals in the original. You can understand the meaning of a phrase with the help of translator programs, even if your language level is completely zero.

    I conducted an experiment: I had a student, a datasheet and Google translator. Experiment No. 1: the student was given a datasheet and given the task to independently find the required values, the result is “how can I”, “I don’t know English”, “I didn’t find anything/I didn’t understand” are typical phrases indicating that he didn't even try. Experiment No. 2: the same student was given the same datasheet and the same task, with the difference that I sat next to him. The result is that after 5 minutes he found all the necessary values ​​himself, absolutely without my participation, without knowing English.

    8. Reinvent the wheel. For example, if you are studying some new thing, say a transistor, Uncle Horowitz authoritatively declares from the pages of his book that the transistor amplifies, always say - I DON’T BELIEVE. We take the transistor in our hands, plug it into the circuit and make sure that this is really the case. There is a whole layer of problems and subtleties that are not described in books. You can only feel them when you pick them up and try to assemble them. At the same time, we get a bunch of related knowledge and learn the subtleties. In addition, any theory without practice will be forgotten much faster.

    At the initial stage, one method helped me a lot - first you assemble a circuit and see how it works, and then try to find a rationale in a book. It's the same with software part, when there is a ready-made program, it is easier to understand it and correlate the pieces of code which is responsible for what.

    It is also important to go beyond what is permitted, apply more/less voltage, make larger/smaller resistors and monitor changes in the operation of the circuit. All this remains in the brain and will be useful in the future. Yes, this is fraught with consumption of components, but I consider this inevitable. At first, I sat and fired everything, but now, before betting this or that denomination, I always remember those fun times and the consequences of putting the wrong denomination.

    9. How would I do this if I were the developers? Can I do better? Ask yourself these questions every time, it really helps you progress in your learning. For example, study the 1wire, i2c, spi, uart interfaces, and then think about how they differ, whether it could have been done better, this will help you understand why everything is this way and not otherwise. You will also be aware of when and which one is best to use.

    10. Don't be limited by technology. It is important that this advice is very fine line. There was a stage in life when from every doorway you could hear “you should know FPGAs”, “but you can do something with FPGAs”. Formally, I had no goals to study PLISins, but there was no way I could ignore it. A little time was allocated for familiarization with this issue. Time was not wasted, I had a number of questions regarding the internal structure of microcontrollers, and it was after communicating with the Plisins that I received answers to them. Similar examples a lot, all the knowledge that I acquired in one form or another came in handy sooner or later. I don't have a single useless example.

    But as has been said, the issue of technology has a fine line. There is no need to grab everything. There are many areas in electronics. Maybe you like analog, maybe digital, maybe you’re a power supply specialist. If it’s not clear, then try yourself everywhere, but practice shows that at first it’s better to concentrate on something specific. Even if you need to press in several directions, it is better to do it in steps, first pushing through one thing.

    11. If you ask a novice radio amateur what he likes more: programming or circuit design, then with a 99% probability the answer will be programming. At the same time, these programmers spend most of their time on making boards with LUT/photoresist. The reasons are generally clear, but quite often this turns into a kind of insanity, which consists of making boards for the sake of making boards.

    On the Internet, almost the only true path to programming is to become a Jedi of making printed circuit boards. I also went through this path, but every time I ask myself the question why? Ever since I bought myself a couple of boards, for all occasions, every time I think that I could easily live all this time without homemade boards. My advice is, if there is even a drop of doubt, it is better not to bother and take a ready-made debugging board, and it would be better to spend time and money on programming.

    12. The next piece of advice is especially painful; I really don’t want to discuss it, but I have to. They often write to me, saying that xxx rubles are too expensive, where can I get them cheaper. It seems like a common question, but usually I immediately get tensed by it, since it often turns into endless complaints about the lack of money. I always have a question: why not tear off the butt and go to work? Even at the same time, even for construction, you have to wait a month, but then you can buy a couple of boards that will last for the next year. Yes, I know that it is difficult to find work in small towns and villages, move to a big city. Work remotely, in general you need to spin. There is simply no point in complaining, there is a way out of the situation, whoever looks for it finds it.

    13. In the same piggy bank I will add the very painful issue of the instrument. The tool should allow you to develop devices as quickly as possible. For some reason, many developers do not value their time. A typical example is a cheap crimp for terminals, which many employers like to save on. The problem is that it doesn't even crimp correctly, causing the wires to fall out. You have to perform a bunch of additional manipulations, wasting time accordingly. But as you know, a fool pays three times, so the low price of the crimper will increase many times due to the time spent and poor quality crimping

    I'm not saying that cheap = bad, no - it all depends on the situation. Let me return to the example of the crimper; there was a time when I crimped it with anything, so problems often arose. It’s especially unpleasant when you start a board and it doesn’t work, after a long search for an error you realize that it’s because of a poorly crimped wire, it’s a shame. Since normal crimping appeared, there have been no such problems. Yes, the inner toad was croaking and choked by its cost, but I never regretted this decision. All I want to say is that after working with a normal tool, you don’t want to go back to the bad one, you don’t even want to discuss it. As practice shows, it is better not to skimp on tools; if in doubt, test it from someone, read reviews, reviews.

    14. Start a website, you can write whatever you want on it, just like notes. Practice shows that employers still don’t read it, but the fact itself has a great effect.

    15. A delicate question: specialized higher education, is it necessary? I know of more than one case where people worked with absolutely no education and, based on their experience and knowledge, they could give a light to any certified specialist. Actually, I don’t have a specialized education, do I feel discomfort from this? To a certain extent, yes.

    At the very beginning, when microcontrollers were a hobby for me, I helped a lot with coursework and diplomas from different universities, just to assess my level. I can say with confidence that the level is generally low, regardless of the name of the university. It is not necessary to study for several years in order to write such a diploma. You can achieve this yourself in a very short period of time. And yet, there were often times when students knew some subject that they took in the 2-3rd year, but I did not know it. Although all this knowledge was compensated by self-education, it would still be better not to waste time on this.

    University for the sake of a piece of paper. I can say that there were also situations when they were offered a job that required a mandatory education and it was a shame that at that moment there was no piece of paper. But in general, history shows that most employers don't care about your paperwork.

    The next point is quite often not taken into account, this is the environment. Don’t forget that the people you study with are your generation, and it is possible that you will work with them. The number of firms operating in one industry is very limited. Practice shows that even in big cities, everyone knows about each other, down to intimate details.

    Another point is opportunities. Often, universities have their own capabilities - equipment, maybe some sections, maybe some programs for working abroad, this should be used if there is even the slightest opportunity. If you don’t see any prospects at a university, go to another, the world doesn’t end at just one.

    To summarize, the advice is this: if there is even the slightest opportunity, you need to go study, definitely according to your profile; if there is at least some chance, then climb everywhere, and not sit in the back seat. Make acquaintances, at the same time practice and develop yourself at home.

    16. Is it too late to start programming at 20, 30, 40, 50 years old? The practice of other people shows that age is not a hindrance at all. For some reason, many do not take into account the fact that there is a whole layer of work that young people, due to their ambitions, do not want to do. Therefore, employers prefer to hire those who will carry it. This is your chance to get hooked, and then everything depends only on you.

    And one last piece of advice. Many radio amateurs are uncommunicative, angry and irritable - consider this a job specificity. Radiate kindness and positivity, be a good person.

    9


    Modern amateur radio cannot be imagined without microcontrollers, and this is obvious. In recent decades, microcontrollers from various manufacturers have become widespread in various fields of human activity. They can often be found in the most unexpected devices and designs. You and I are witnessing the computerization and automation of the processes around us. The truth is that without knowledge of the basics of programming, it has become almost impossible to create modern competitive devices...

    If you are reading this article, you probably have a desire to understand how microcontrollers work, and most likely have questions:

    4. What literature should I study?

    Let's try to answer these questions.

    1. Which microcontroller should I choose for the job?

    8-bit microcontrollers are very popular among radio amateurs. PIC Microchip Technology and AVR Atmel, 16-bit MSP430 from TI, as well as 32-bit microcontrollers, architectures ARM.

    In industry, somewhat differently, the first place by a large margin is occupied by Renesas Electronics on the second Freescale, on the third Samsung, then go Microchip And T.I., then all the rest.
    Popularity is determined by price and availability, availability plays a significant role technical information and the cost of software support.

    We will study 8-bit AVR microcontrollers, families ATMEGA 8 and 16 series. The choice was determined, again, by accessibility, the presence of many amateur developments, and a huge amount of educational material. The presence of a variety of built-in components and functionality of this family.

    2. Which development environment should I use to program the selected microcontroller?

    Various integrated development environments (IDE, Integrated development environment) have been created for AVR.
    IDE is a system software, used by programmers for development software(software), which includes:
    text editor,
    compiler and/or interpreter,
    assembly automation tools,
    debugger

    The most common ones AVRStudio, ATmelStudio, WINAVR, CodeVision, IAR Embedded Workbench.
    In order to write programs, we will use the free IDE ATmelStudio version 6 and higher.
    You can download Atmel Studio from the official website after registration (registration is absolutely free and does not oblige you to anything!)

    ATmelStudio allows you to create projects and write programs both in assembler and in SI.

    Initially, the question is always: what programming language to choose to write effective programs?

    My answer is simple: you need to be able to write in at least two languages: assembly and SI. Assembly language is simply necessary when you need to write fast and compact subroutines and macros, various drivers devices. But when you need to create extensive project, built on complex algorithms, without knowledge of SI, a lot of time can be spent, especially during the debugging process, and if there is a desire to transfer it to another platform, for example PIC18, or STM, it can become an insoluble problem.
    In addition, hardware computing platforms have now appeared Arduino, work with which requires knowledge of the SI++ language.
    Therefore, we will write programs both in assembler and in SI.

    To clearly see the result of your work without using a soldering iron or breadboard, just install the program Proteus.

    3. How to flash the controller, and what additional devices and accessories are needed for convenient work with them?

    We use Datagorian. In addition, you will need to purchase development boards, power supply with an output voltage of 5 Volts. You can use it as a power supply with low ripples by using a 5 Volt zener diode.
    Perhaps, over time, Igor and I will propose a project for assembling a debug board.

    4. What literature should I study?

    But, for example:
    Practical programming of AVR in assembler. Revich, 2011
    1000 and one microcontroller circuit Vol. 1-2. Ryumik, 2010-2011
    10 practical devices on MK AVR Book 1-2. Kravchenko, 2008-2009
    A tutorial for a device developer using the AVR MK. Belov, 2008
    MK AVR families Tiny and Atmega. Efstifeev, 2008
    CodeVisionAVR. A guide for beginners. Lebedev, 2008
    Microprocessor control of devices, thyristors, relays. Belov, 2008
    Analog interfaces MK. Steward, Ball, 2007
    We create devices on the AVR MK. Belov, 2007
    MK AVR in amateur radio practice. Full analysis ATTINY2313. Belov, 2007
    Network and internetwork data exchange with MK. Go, 2007
    MK AVR. workshop for beginners. Hartov, 2007
    Application of AVR Schemes, algorithms, programs. Baranov, 2006
    AVR microcontrollers. Introductory course. Morton, 2006
    Measurement, control and regulation using AVR. Trumpert, 2006
    Programming in C language for AVR and PIC MK. Shpak, 2006
    Design of devices on MK. Belov, 2005
    MK - it's simple, volumes 1-3. Frunze, 2002-2003
    The C Programming Language, 2nd edition. Kernighan, Ritchie, 2009
    Programming ATMEL microcontrollers in the language S. Prokopenko, 2012

    5. Where on the Internet can you ask questions and get specific answers?

    You can ask questions on our or any other forum, where topics on microcontrollers are touched upon in one way or another. The main thing on forums is to formulate questions correctly in order to clearly receive answers. Abstract questions are not welcome, and most likely you will receive harsh criticism instead of an answer, or your question will remain unanswered!

    Now let's take a closer look at our favorite, the ATMEGA 8 microcontroller

    8-bit, high performance, low power AVR microcontroller
    Progressive RISC architecture
    130 high-performance instructions, most instructions executed in one clock cycle
    32 8-bit general purpose working registers
    Completely static operation
    Approaching 16 MIPS (at 16 MHz) performance
    Built-in 2-cycle multiplier

    Non-volatile program and data memory
    8 KB in-system programmable Flash memory(In-System Self-Programmable Flash)
    Provides 1000 erase/write cycles
    Additional boot code sector with independent lock bits
    Provides simultaneous read/write mode (Read-While-Write)
    512 bytes EEPROM
    Provides 100,000 erase/write cycles
    1 KB on-chip SRAM
    Programmable locking to protect user software

    Embedded Peripherals
    Two 8-bit timer/counters with separate prescaler, one with compare mode
    One 16-bit timer/counter with separate prescaler and capture and compare modes
    Real time counter with separate generator
    Three PWM channels
    8-channel A/D converter (TQFP and MLF)
    6 channels with 10-bit precision
    6-channel analog-to-digital converter (in PDIP package)
    4 channels with 10-bit precision
    2 channels with 8-bit precision
    Byte-oriented 2-wire serial interface
    Programmable Serial USART
    Serial interface SPI (master/slave)
    Programmable watchdog timer with separate built-in oscillator
    Built-in analog comparator

    Special microcontroller functions
    Power-On Reset and Programmable Low Voltage Detector
    Built-in calibrated RC oscillator
    Internal and external interrupt sources
    Five low consumption modes: Idle, Power-save, Power-down, Standby and ADC noise reduction

    I/O pins and housings
    23 programmable I/O lines
    28-pin PDIP package, 32-pin TQFP package, and 32-pin MLF package

    Operating voltages
    2.7 - 5.5 V (ATmega8L)
    4.5 - 5.5 V (ATmega8)

    Operating frequency
    0 - 8 MHz (ATmega8L)
    0 - 16 MHz (ATmega8)

    differences between ATMEGA16 and 8
    16 KB of In-System Self-Programmable Flash memory

    JTAG interface (IEEE 1149.1 compliant)
    Peripheral scanning capability compliant with JTAG standard
    Expanded support for embedded debugging
    Programming via JTAG interface: Flash, EEPROM memory, jumpers and lock bits

    Four channels PWM/PWM

    8-channel 10-bit analog-to-digital converter
    8 unbalanced channels
    7 differential channels (TQFP package only)
    2 differential channels with programmable gain of 1x, 10x or 200x (TQFP package only)

    Six low consumption modes: Idle, Power-save, Power-down, Standby, Extended Standby and ADC noise reduction

    32 programmable I/O lines

    40-pin PDIP package and 44-pin TQFP package

    AtmelStudio

    If you are just starting out, then you need to download and install the AtmelStudio program from official page atmel.com
    After installing the AtmelStudio program, you can start creating a project.
    Project- this is your program that you will write, debug and flash, after compilation, into the memory of the microcontroller.

    To create a project, you need to open the program, the following screensaver will appear:

    and the project creation page will open

    To create a new project, you need to click on "New Project..."
    In this case, a new window will open where you can select the programming language, the name of the project, its location, the name of the package with the project files and the ability to create a directory for further use in other cross-projects. To create a project where we will program in assembler, we need to select - Assembler, after that we change the name of the project, its location, and select OK.

    The following window will appear

    Choose “megaAVR, 8-bit” and find the microcontroller we need, we chose ATmega8. On the right side of the screen saver, a list of devices that work with this microcontroller appears, one of which we can connect. Choose OK.

    The text editor page appears, which allows you to edit and debug the program. While the page is blank, the time and date of creation and the name of the project file and user name are indicated. Eat additional window input/output devices, program compilation report window. Now we


    We can program in assembler.
    A project for programming in SI language is created in the same way.

    Microcontroller programming

    Introduction

    Section 2. Programming environments. Microcontroller connection diagrams

    Section 3. Practical implementation of the program on a microcontroller

    List of sources used

    Introduction

    Relevance of the topic. Microcontrollers are used in all spheres of human life and in the devices that surround him. Easy to connect and large functionality. By programming a microcontroller, you can solve many practical hardware problems.

    Purpose of the work. Based on a practical example, show the advantageous characteristics of using microcontrollers, the need for their implementation in various devices.

    We can consider that a microcontroller (MCU) is a computer housed in a single chip. Hence its main attractive qualities: small dimensions; high performance, reliability and the ability to be adapted to perform a wide variety of tasks.

    Microcontroller besides central processor(CPU) contains memory and numerous input/output devices: analog-to-digital converters, serial and parallel information transmission channels, real-time timers, pulse-width modulators (PWM), programmable pulse generators, etc. Its main purpose is to use it in automatic control systems built into a wide variety of devices: credit cards, cameras, cell phones, stereo systems, televisions, VCRs and video cameras, washing machines, microwave ovens, systems burglar alarm, ignition systems for gasoline engines, electric drives for locomotives, nuclear reactors and much, much more. Embedded control systems have become such a widespread phenomenon that a new branch of the economy has actually emerged, called EmbeddedSystems.

    MKs from ATMEL, which have great functionality, are quite widespread.

    The use of MK can be divided into two stages: the first is programming, when the user develops a program and flashes it directly into the crystal, and the second is the coordination of the designed actuators with the programmable MK. At the first stage, program debugging is greatly facilitated by a simulator that visually simulates the operation of a microprocessor. At the second stage, an in-circuit emulator is used for debugging, which is a complex and expensive device, often inaccessible to the average user.

    At the same time, little attention has been paid in the literature to the issues of teaching programming of some inexpensive microcontrollers in combination with real actuators.

    The development of a programmer layout, characterized by simplicity, clarity and low cost, becomes necessary both for the programming of crystals itself and for visually teaching a wide range of users the basics of MK programming.

    Section 1. Purpose and scope, their architecture

    A microcontroller is a computer on a single chip. Designed to control various electronic devices and interaction between them in accordance with the program embedded in the microcontroller. Unlike microprocessors used in personal computers, microcontrollers contain built-in additional devices. These devices perform their tasks under the control of the microprocessor core of the microcontroller.

    The most common embedded devices include memory devices and input/output (I/O) ports, communication interfaces, timers, and system clocks. Memory devices include random access memory (RAM), read-only memory (ROM), programmable ROM (EPROM), electrically programmable ROM (EEPROM). Timers include both real-time clocks and interrupt timers. I/O facilities include serial ports communications, parallel ports (I/O lines), analog-to-digital converters (A/D), digital-to-analog converters (D/A), liquid crystal display (LCD) drivers or vacuum fluorescent display (VFD) drivers. Embedded devices have increased reliability because they do not require any external electrical circuits.

    In contrast to a microcontroller, a controller is usually called a board built on the basis of a microcontroller, but quite often when using the term “microcontroller” the abbreviated name for this device is used, discarding the prefix “micro” for simplicity. Also, when mentioning microcontrollers, you can come across the words “chip” or “microchip”, “crystal” (most microcontrollers are made on a single silicon crystal), abbreviations MK or from the English microcontroller - MC.

    microcontroller program chip electronic

    Microcontrollers can be found in a huge number of modern industrial and household appliances: machines, cars, telephones, televisions, refrigerators, washing machines. and even coffee makers. Microcontroller manufacturers include Intel, Motorola, Hitachi, Microchip, Atmel, Philips, Texas Instruments, Infineon Technologies (formerly Siemens Semiconductor Group) and many others. Modern chip manufacturing requires ultra-clean rooms.

    Main classification sign microcontrollers is the bit depth of the data processed by the arithmetic logic unit (ALU). Based on this feature, they are divided into 4-, 8-, 16-, 32- and 64-bit. Today, the largest share of the global microcontroller market belongs to eight-bit devices (about 50% in value terms). They are followed by 16-bit and DSP microcontrollers (DSP - Digital Signal Processor - digital signal processor), focused on use in signal processing systems (each group occupies approximately 20% of the market). Within each group, microcontrollers are divided into CISC and RISC devices. The largest group is CISC microcontrollers, but in recent years there has been a clear upward trend in the share of RISC architecture among new chips.

    The clock speed, or more precisely the bus speed, determines how many calculations can be performed per unit time. Basically, the performance of a microcontroller and its power consumption increase with increasing clock speed. Microcontroller performance is measured in MIPS (Million Instructions per Second - million instructions per second).

    The term controller is derived from the English word to control - to manage. These devices can be based on different operating principles from mechanical or optical devices to electronic analog or digital devices. Mechanical control devices have low reliability and high cost compared to electronic control units, so we will not consider such devices in the future. Electronic analog devices require constant adjustment during operation, which increases the cost of their operation. Therefore, such devices are almost never used today. The most common control circuits today are those based on digital microcircuits.

    Depending on the cost and dimensions of the device that needs to be controlled, the requirements for the controller are determined. If the control object occupies tens of meters in area, such as automatic telephone exchanges, base stations cellular systems communications or radio relay lines communication, then universal computers can be used as controllers. Control can be carried out via the built-in computer ports (LPT, COM, USB or Ethernet). When the power is turned on, such computers are loaded with control program, which turns a universal computer into a controller.

    Using a universal computer as a controller allows you to develop new communication systems in the shortest possible time, easily modernize them (by simply changing the program) and also use ready-made mass-produced (and therefore cheap) blocks.

    If special requirements are placed on the controller, such as operation in shaking conditions, an extended temperature range, or exposure to aggressive environments, then industrial options have to be used universal computers. Naturally, these computers are much more expensive than conventional universal computers, but they still allow you to save system development time due to the fact that there is no need to develop controller hardware.

    I decided to devote this article (or rather a series of articles...) entirely to Atmel microcontrollers. Of course, this is a hackneyed topic... BUT! I know from my own experience that to know the truth in the midst of this, excuse me, MESS, is very, very difficult! Therefore, I decided to try to bring at least some clarity to the heads of those eager to know this terrible beast called “Microcontroller”.

    So, the purpose of this article is to describe and, if possible, show the entire process of creating a device based on a microcontroller from scratch. That is, from the idea (for example, we decided to assemble a New Year's flasher, similar to that described by the respected alx32 in the article...) before being implemented in hardware. Of course, bypassing all the intermediate stages: statement of the problem, selection of MK, selection of harness, formulation of the algorithm, writing the program, debugging, creation of the board and, the most long-awaited thing - launch!!!

    Updated: files added. So, task: we need to create a device capable of lighting in a certain order (let it be one by one), the N number of LEDs (let there be 8 pieces).
    (this is for starters……..)


    Now you can start programming. You can write in anything, but I advise beginners to give preference to the language C, because programming is easier and more visual. Personally, I use a compiler CodeVision AVR(it is in the file archive), further program listings will be given specifically for this compiler.

    Let's decide on the algorithm. We need to take turns activating one of the MK outputs after a certain period of time.

    You can turn it on/off in different ways:
    - assign values ​​to each output separately;
    - write down the values ​​of all outputs at once.

    The values ​​(sequence) can be obtained:
    - typing all commands manually;
    - from an array;
    - mathematical method.

    The time interval can be set:
    - functions delay (delay);
    - through a timer.

    Let's experiment with all these methods. But first we need preparation...

    To create a program template, we will use the code generator built into CVAVR. To do this, launch the program, press File -> New, in the window that opens, select "Project" and click OK. To the question “Should I use the code generator?” We answer “Yes”.
    The code generator window appears. In it we select the type of MK and its clock frequency, we leave the rest as is:


    Next, go to the tab "Ports" and there in "PortB" and set the following:


    This is how we defined all port pins B as outputs, and the zeros mean that when the power is turned on, they will be set to logical " 0 ".
    We don't need the rest of the functions yet.

    Click “File -> Generate, Save and Exit”, choose where to save the project files and see a window with the code created by the generator.

    Now let's enter our code into the program.
    The simplest implementation option (although not the most beautiful from a programming point of view) is to record the values ​​of each pin, and make delays through the function delay.

    delay_ms( x); - delay by x milliseconds

    delay_us( x); - delay by x microseconds

    PORTB- the port we work with.

    PORTB. x - appeal to conclusion x port B

    We find the following lines at the end of the text


    This endless loop(i.e. executed all the time while the power is on) of our program. All that is in front of him is commands preset microcontroller. Lines starting with “//” – comments, they are also useful to read sometimes.

    Fragment excluded. Our magazine exists on donations from readers. The full version of this article is available only


    Press the button Make the project

    (in the toolbar).

    Swearing?
    And rightly so! The compiler doesn't know the function delay_ms(), so you need to point it to the file in which this function is described.
    To do this, at the very beginning of the program text you need to insert the line #include (here semicolon no need!)
    Something like this:

    Press the magic button again.
    Project created.
    Now in the folder in which we saved the project itself, a file has appeared project_name.hex– this is the microcontroller firmware!

    But wait, don’t rush to grab the soldering iron... We are learning to program, not solder!

    That is why I propose to check our program in virtual mode, namely in such a wonderful and favorite product from Labcenter Electronics - Proteus VSM There you can simulate absolutely any circuits (there are even Laplace primitives!). You can get it in the attached archive, along with the project files. True, the version is not completely cracked, so saving does not work. I’ll tell you what to do about this in a separate article.

    So let's launch ISIS(development environment circuit diagrams). In this window, press the “P” button.

    In line "Keywords" enter “attiny2313” and on the right we get:


    There’s not much to choose from, so double-click on this lonely line and see on the left in the main window:


    This means that the element has been added.

    Now enter in the field "Keywords" words “LED-RED” And "RES". Add a resistor and an LED to the project and close the element selection window.

    Let's try to assemble a circuit(conclusion RESET be sure to connect to +5V, otherwise nothing will work! This is also desirable in life!)

    Here's a little hint:

    And to edit the properties of elements, just double-click on them.

    Have you collected it? I hope you didn’t hurt yourself, your loved ones, or the surrounding objects.

    Sorry for the mockery, but if you figure it out yourself, you won’t forget, so, understand, the program is very powerful and it’s worth mastering!

    When the circuit is assembled, we can flash our virtual MK. To do this, double-click on it and see the window.

    Hi all. As promised, starting today we begin to study AVR programming microcontrollers (using the example of Atmega8). For those readers who are interested in programming an Arduino board, don’t worry, articles on this topic will continue :) .

    One can ask a logical question why, out of a number of other microcontrollers (hereinafter referred to as MK), it was chosen as the experimental one. MK AVR. There are several reasons for this:

    • MK AVR universally available;
    • They have a fairly low price;
    • You can find a lot on the Internet free programs, which will help when working with MK data.
    • In addition, there are a great many written articles and forums where you can ask questions about the data. MK AVR.

    As I said earlier, we will use MK as a test subject. Atmega8. Why him?

    This microcontroller boasts 3 I/O ports. Besides that, it's pretty cheap.

    By ports we mean data buses that can operate in two opposite directions (that is, output and input).

    U Atmega8 3 ports. Port B consists of 8 pins (numbered 0,1,2,3,4,5,6,7). Port C consists of 7 pin pins (numbered 0,1,2,3,4,5,6). Port D consists of 8 pin pins (numbered 0,1,2,3,4,5,6,7).

    The microcontroller can be powered from 3.3 and 5 V. With a supply voltage of 5 V maximum frequency clocking is 16 MHz, and with a supply voltage of 3.3 V, the maximum clocking frequency is 8 MHz. For now, let's not bother with clock frequencies.

    Power is supplied to pin 7, and ground is supplied to pin 8.

    Download for free. Downloaded, installed, launched :)

    The first place to start getting acquainted with Atmel Studio is creating a project.

    Select File -> new -> project.

    A selection window will open. Select the “Browse” folder in which we will save the written projects. I created a folder for projects in advance.

    We assign a name to the project, in my case lesson_avr_1

    Pay attention to the “create directory for solution” checkbox. If the checkbox is checked, then in the folder that we have chosen to save projects, a separate folder will be created for the current project.

    That's all - the project has been created.

    Let's start setting up the project we created. Click Projest -> lesson_avr_1 properties or (alt+F7)

    Go to the Tool tab. We choose - simulator. The actions we take will make it possible to debug the written code. Save the changes. You can save changes in one (current) file or in all project files at once. Close settings.