Microcontroller Programming (7) - Arrival to Čučerje
Published: 2023-05-07 08:51:56

We often discuss our first microcontroller and we all remember it in detail. It was our entry into the world of imagination. Although we were young and inexperienced, there was always curiosity that led us through all these years of learning. Each new discovery aroused great satisfaction within us. Creativity became a part of our lives and every new day in the world of imagination brought about new excitement. Unfortunately, we all made a big mistake that we corrected for years. You will say that we made a lot of mistakes, and I agree. The identical thing you are doing now, we did too. As scavengers, we ran towards the outside world and started "carving" justice, without even organizing our own village. It's not your fault, nor were we wrong a long time ago, that's what we were taught, and that's what they teach us today. Maybe the idea of teaching you to "blink" an LED, read a keypad, and write on an LCD is a good trap to awaken your curiosity? Perhaps we need to limit you to the world outside of the microcontroller and teach you everything again one day?

I abandoned the Arduino IDE and the idea of writing about it for a simple reason: it hides our beautiful Čučerje. Controlling the inputs and outputs of the microcontroller is easy in all programming languages, and I don't want to teach you that. Unfortunately, our village inside the microcontroller is incredibly large, and we need several years of learning to master everything we have at our disposal. Even the simplest blinking of an LED, which we wrote in previous texts in the Arduino IDE using the delay() function, is not programmed in the same way. We often mention the ARM architecture as something powerful, and it really is, but we cannot teach you to manage a big city when you don't even know how to manage a big village. The AVR architecture will be our village, and we will call it "Čučerje". If you prefer another name, feel free to change it. I use our beautiful Čučerje as a metaphor, and I declare myself the supreme commander of Čučerje. If you prefer tribes, feel free to take the title of chief somewhere in the south of Africa. Don't worry, programming is imagination, and whatever you choose, you are right. Our Čučerje looks like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxuv8hswz8brj4z6y7jb.jpg)


As we can see, Čučerje is not so small after all. We will get to know it through texts about programming, but first we have to get comfortable, and where else but in the "FLASH" command center. You can find it on the map, and you will see that our command responsibility is a close collaboration with our colleague AVR CPU. Look at our village or tribe however you like, but the fact is that all instructions are written in FLASH memory and executed by AVR CPU. What about the others on the map? Those are our citizens and they make the microcontroller interesting and powerful. Arduino hides them from us and limits our control over our own village. The residents of Čučerje only understand the ancient language of assembler, but since we have a translator called "Compiler", we can establish a command center using c/c++.
Luckily, we are in an advantage. Most supreme commanders do not know about the existence of this magical village, they believe that FLASH and RAM make up the final microcontroller. We are not better off at the moment, but at least we know about the existence of the citizens of Čučerje, and we still have to learn how to communicate with them. To do this, we must understand the meaning of the word "register". The simplest description of a register is "special-purpose memory". Special-purpose memory...this description doesn't tell us much, especially since we have CPU registers, IO registers, this kind and that kind of register. We could think about the definition of registers for days, and it would be difficult to understand what they are. They are memory locations of special purpose, but in Čučerje, they are part of our colleague SRAM, with whom we will work closely in the future.
The only way to control our Čučerje is to control the registers of the SRAM memory. But beware, our Čučerje is not just beautiful, it is full of big secrets. Every door we open will give us the impression that we are opening a Russian Babushka doll that never ends. Perhaps it is best that we first get to know the closest collaborators of Čučerje, AVR CPU and SRAM. Due to the complexity of their personalities, we currently cannot know everything about them, trust me, they are complicated types. But at least we can learn what they mean for Čučerje and what role they play in our village. Here, the three of us have met:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3i8byv46x52wawkvd5k.jpg)

FLASH memory is the executive center of the AVR microcontroller, where all instructions are stored and where everything we program is located. Who executes them? Well, the AVR CPU, our processor, executes them. Do we have a processor in the microcontroller? Of course we do, that's our most valuable member of the team. SRAM is a somewhat complicated type that suffers from dementia and forgets everything if the "power" disappears for a moment. But we have invented a cure for it and called it "Initialization". The Arduino IDE calls this cure the setup() function.

Our command chain is clear: the AVR CPU executes instructions exclusively from FLASH memory. There are microcontrollers that can execute instructions from SRAM memory as well, but their program is still stored in FLASH memory and is copied to SRAM when the MCU is started and executed from there. Our command center must be in FLASH memory and we do not have the ability to execute instructions from SRAM memory. FLASH and SRAM are memories of the microcontroller, and if we want to simplify their purpose, we can say that FLASH permanently stores our program, while SRAM is a data storage that we have when the microcontroller is powered on.

As the saying goes: "SRAM is a data storage...". We can think of each memory having a beginning and an end, and everything in between is called the memory size or capacity. Using this principle, we can say that in a personal computer, we have 4096 MB of RAM memory, with a clearly defined beginning and end. For a simpler example, let's imagine a memory of 4 bytes and ask the logical question: what differentiates the beginning and end of the memory? The only difference is in the address and data. We can say that the data 10101010 is stored at address 0 and the data 00001111 is stored at address 3.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vuwwa0wa8sd6o9giqf2.jpg)

The more important question is: Can we store the data that we previously stored at address 3 to address 0, and store the data from address 0 to address 3? If you look at RAM from the perspective of a personal computer and if RAM is a data storage, of course we can. RAM is our storage and we decide what and where to store it. However, in Čučerje, SRAM is not just a data storage, and the above example of memory does not apply to the residents of Čučerje. Certain memory locations are connected to ancient magic and it would be a shame not to know more about them. Let's take a closer look at our address space in SRAM memory.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7agz9fphptnvibp49w28.jpg)

The data storage described above exists in Čučerje and is called "Internal SRAM", but its starting address is not 0x000, but 0x200. In the SRAM memory from address 0x000 - 0x1FF, Čučerje's registers are located, and that memory space cannot be called a data storage. Let's go back to the definition: "Registers are special purpose memory". We have proven that they are memory, and not just any memory but the address part of the SRAM memory, and not just any location but a specific address space 0x00 - 0x1FF. I know, I know. The question still stands: "What are registers?"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ih120t4p7pknux3xsd6x.jpg)

In order to manage our village in some way, we must collaborate with all of its inhabitants, and we do this by reading and writing registers (special memory locations designed solely for this purpose). Each register is responsible for a certain functionality of our village, and since our village is quite large, we have a lot of registers. Managing registers is no more complicated than the following example. Instead of having sun, rain, we have DDRA, PORTC, PIND, TCCR1A..., but the principle is identical, and the purpose is not far from the truth.

If we add up all the registers of Čučerje (32+64+416), we get a total of 512 bytes. We cannot use all of them since certain memory addresses of registers are reserved for possible expansion of Čučerje, but there are enough of them to keep us busy for several years and to make us wonder again: "How well do we know the AVR architecture?"

In the AVR Studio development environment (in C and assembler), there are no pinMode() and digitalWrite() functions as in the Arduino IDE, but there is something even better. Every time we access the registers of Čučerje, we call them by their real names.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o9cnhgmyfc6ko8teqwqx.jpg)

No wonder why all beginners cringe at such written programs. No wonder why they don't understand our close collaboration with architecture. We don't see any illogicalities in managing Čučerje, and you don't see the whole Čučerje.