Microcontroller Programming (2)
Published: 2023-05-06 12:56:42

In the dilemma regarding microcontrollers, we often use the term "processor," which is not even remotely accurate. Processor and microcontroller are two completely different concepts. If you take apart your personal computer and take a closer look at the motherboard, you can see that the processor is the best-cooled component of your computer and is completely separate from RAM memory and the hard disk. The operating system on the computer is loaded from the hard disk, partially processed in RAM memory, partially on the hard disk, and managed by the processor or CPU (Central Processing Unit). The processor does not have memory in which to store the entire program it performs. The role of the processor is only to perform machine instructions, and where exactly these instructions come from is a completely different story. It is important to note that all components in a personal computer (RAM, CPU, HDD, etc.) are scattered across the motherboard or connected to it and are not just one component of the computer.

A microcontroller is an entire small computer located in just one integrated circuit, of course, without a monitor. By the very statement that it is an entire computer, we come to the basic parts of the microcontroller. We do not have a hard disk, but we do have FLASH memory, whose role is to store the program that the microcontroller will perform. We have our own RAM memory, our own processor (CPU), and our own peripheral of the microcontroller. Most importantly, everything is located in just one integrated circuit, albeit with far smaller memory capacities and far lower operating speeds than a personal computer, but given its size, we cannot expect more. It is important that in one integrated circuit, we have all the most important components of any computer.

When we talk about microcontrollers, we don't have to look far to find them. In today's world, almost every electronic device contains a microcontroller, and the reasons couldn't be simpler. We certainly don't want a personal computer in the trunk of our car to control the ABS, GPS, and all other car systems because it's not practical. We don't want to carry a personal computer with us in search of a power outlet to send an SMS. We don't want to connect a washing machine to a personal computer to run a wash cycle. These are the main reasons why microcontrollers, as small computers, have become so widespread, and therefore very cheap, and why they are used in almost every device. Their low cost plays a big role in their prevalence, which, I dare say, is practically free for what they can do. When I tell you that you can buy a complete computer for 2 or 3 euros that can perform a specific task in any technology field, then it's truly free. The dimensions of such computers can be 5mm x 5mm x 1mm or even smaller, which means that we can integrate them into any device we design. Of course, we can't perform demanding tasks on such computers like we do on personal computers, mainly due to their limited memory and speed, but that's not their purpose. Personally, I don't need a washing machine that runs Windows 7. I'm not going to play games on it or do complex graphic processing.

However, learning to program microcontrollers is not easy. Hardware and software often combine into a logical unit, even though they are separated in all the books. Certain terms are related only to hardware, or only to software. Every technical documentation for a good microcontroller contains over 500 pages of text with strange terms, and every programming language has the same amount. If you only connect hardware and software books, even if we ignore the electrical engineering part, you still have 1000 pages of text in front of you talking about thousands of new and unknown terms. No beginner can easily navigate through such extensive documentation. It's crazy to mention how many programming languages and different microcontrollers and their architectures exist. Therefore, when we talk about learning programming, we talk about the path we take to learn.

The story about programming microcontrollers cannot begin peacefully because in today's world, there is a certain war between all microcontrollers and programming languages. Every beginner is forced to choose a microcontroller and programming language to program in, and that's where we all make a big mistake. I made it a long time ago, and I'm almost sure that there is no forum related to microcontrollers that doesn't contain a comparison of AVR and PIC microcontrollers. Groups of programmers have formed to support one or the other microcontroller, and in that war, beginners suffer the most. The sad truth about these dilemmas is that it doesn't matter which microcontroller we choose. They are all good and work perfectly if we know how to properly start them and write good programs. In the professional world of microcontrollers, we often cannot choose the microcontroller we want to use for a project. Certain companies prefer certain microcontrollers, certain projects require certain microcontrollers. We dare to say "We know them all!" but... there is always a but! The microcontroller we first encounter and write our first programs for often becomes dear to us and takes a special place in the world of microcontrollers. Its architecture becomes the first one we get to know, we unconsciously remember the names of registers, configuration methods, and programming methods.

I understand that it may seem that choosing a microcontroller is not a big deal, but we often make mistakes in selecting programming languages or even make such a big mistake that we don't realize it until many years of programming. We might choose BASIC because we believe it is the best and easiest for beginners, but over the years of programming in BASIC, we become so accustomed to it that the mere thought of a better programming language seems like science fiction. As time passes, it becomes increasingly difficult to decide to learn C or assembler. I assume that certain authors or readers who program in BASIC will not agree with my way of thinking. I cannot help them, but I can point out the mistakes they have made by choosing BASIC through my texts. Through experience, I could compare any two microcontrollers, but I would write so many technical terms that no beginner would understand what I mean. For that reason, I will currently write about AVR microcontrollers. All my programming texts will be focused exclusively on programming AVR microcontrollers. Only when we understand certain concepts through these texts will I take the liberty to compare them not only with other microcontrollers, but also with all programming languages. The programming texts I will write for you will be complicated, difficult to understand, but they will be the truth. Quality knowledge has never been easy to achieve. It is often the harder path that I will guide you through in my programming texts for microcontrollers.

As I wrote a lot of text trying to explain to readers the way of professional programming, often finding myself in situations where I had to explain the smallest details of architecture, I decided to take the only logical path of learning, which makes it very easy to switch to professional programming. Although I am not a fan of development tools such as ARDUINO, primarily because I am extremely limited in such an environment that only has 5 buttons, I must admit that such a tool is certainly the best for learning programming. There are several well-established reasons that lead me to this conclusion. ARDUINO is based on the C/C++ programming language. The entire program and all libraries are open-source, meaning that we can use them but we don't have to. The fact that we don't have to use ready-made libraries leads us to advanced programming, where we can influence even the smallest detail of the microcontroller. At the same time, we can be both amateurs and professionals, depending on how we look at ARDUINO. Each ARDUINO development board has a bootloader, which means that we don't need a programmer and for the start, we don't have to struggle with FUSE settings of the microcontroller, and we can upload the program with just one click on the "Upload" button.

Readers often do not know what ARDUINO is and what it has to do with Microchip and AVR, so I will explain the basic differences between the three mentioned concepts. Microchip is a company that produces microcontrollers, but since there are a lot of microcontrollers, they are easiest to classify by architecture, so we have 8051, AVR, ARM, etc. Therefore, AVR is a microcontroller architecture and is produced only by Microchip. So, what about ARDUINO in that story? It is a completely new company that has made a development board and development environment for programming and has chosen an Microchip microcontroller with AVR architecture. ARDUINO is just a development environment and the name of the board, but don't be fooled, in the end, you are programming an Microchip AVR. People often ask me if ARDUINO can be programmed with Microchip development tools. Of course, it can, because on the board, there is nothing else but an Microchip microcontroller. That is why I will often use the term "Microchip company" and the term "AVR architecture", and I will call the development environment and the board "ARDUINO".

Buying any ready-made development board forgets the chapter "Designing Hardware", which for the sake of general culture, I cannot skip. Regardless of the fact that colleagues from ARDUINO nicely drew the board and took care of important hardware details for us, before we start writing software, we simply must know the basic requirements for the proper operation of any microcontroller. I don't expect anyone to start drawing boards in the first steps, but I also know that it is impossible to say "I program microcontrollers" and not know the basic 3 hardware requirements. Therefore, in the next text, we must go through the hardware, and only after that, we can start writing the first programs.