You can often come across big banners, slogans, or even course and book titles: “C for AVR”, “C course for STM32”. This can give the impression that the C language is different for each microcontroller. Even more so for a computer, right?
So let’s think about how C for microcontrollers differs from C for PCs.
Division into PC and MCU – does it make sense?
First, it would be worth considering whether such a division even exists at all.
In my opinion, this division isn’t entirely appropriate. C is C. Period. It contains several dozen keywords describing various statements, data types, specifiers, etc. We use exactly the same ones when writing programs for a PC and the same ones when writing programs for microcontrollers.
So where is the difference?
The differences we should be talking about are at the level of how we use the language. We’ll write programs a bit differently for an MCU, and a bit differently for a PC. We have the same statements available, but we’ll use some more often and others less often. That’s what this is about.
Bitwise operations are everyday life
Bitwise operations are the norm when you write code for a microcontroller. Seriously.
At university, when learning C programming, we studied on PCs. I don’t remember needing bitwise operations while writing various sorting algorithms, tree searches, Dijkstra, etc. Everything was done on “whole” bytes.
It’s different on a microcontroller. When accessing registers, if you want to set a parameter, then bit-level access is mandatory. Why? Because within one register, each bit can correspond to a different thing. In integrated circuits, silicon is saved, so everything is tightly packed.
So if you want to change something in such tightly packed data, you have to perform several bitwise operations. For example, mask other values you’re not interested in so you don’t accidentally touch them.
Pointers, pointers everywhere
In a microcontroller, pointers are everywhere. If you ever program directly on registers, then writing to a register is precisely writing to a specific address in the microcontroller’s memory.
Naturally, you have to treat such a register write as a write through a pointer, or more precisely, writing a value to the place the pointer points to.
Going further: there is a statement that if you want to write efficient C code, you must avoid copying data from place to place. This is exactly what pointers help with. You pass the address of the place where the data you care about is located and… you work on the original without the need to copy.
Standard library
Not everyone realizes this, but the C standard library for PCs differs from the one for microcontrollers. Not at the level of whether a function exists or not. Although I once was surprised that I was missing the itoa() function in avr-gcc…
The point is that the implementation of standard libraries is different. After all, you have to use different CPU instructions to achieve the same effect. Some topics need to be approached differently.
Unfortunately, implementations for microcontrollers are not great. By including such a printf, you also include a bunch of other libraries. That starts to weigh a lot. The code simply bloats and takes up a lot of space in the microcontroller’s memory.
Another example is dynamic memory allocation using, for example, the malloc function. The standard, and especially the implementation, is not clearly defined. We cannot rely on it because it is unpredictable. On a microcontroller, we can’t afford such a large uncertainty. Especially if we are building systems related to human safety.
Where to learn C for microcontrollers?
I created a course dedicated to microcontrollers. In it, I teach C from the ground up (the course content is conducted in Polish).
I gathered my experience from several years of embedded programming and I want to pass on the best possible knowledge to you. I participated in various projects: on my own, a start-up, a medium-sized company, and a huge corporation.
In addition to the basics and syntax, I share a ton of good practices. I weave this in between explaining subsequent aspects of the C language.
An additional advantage is also that I show how to run a project well. I’ll show you how to deal with building abstraction layers. We’ll use structures, pointers, and callbacks for that. And of course, splitting into files. That helps a lot.
Such separated layers are much easier to перенос between projects, and even between different microcontroller families.
How to join the course? See the full offer at https://cdlamikrokontrolerow.pl (the course content is conducted in Polish).
Don’t wait, because the offer is valid until Friday, October 29, 2021, until 20:00.
I hope we’ll see each other in the course!
P.S. Let me know in the comments whether this lighter topic on the blog is interesting!



0 Comments