Books about the C language describe everything that exists in this language. Literally every smallest detail. Do you need all of it? Of course not! I selected 6 basic elements that, in my opinion, you must learn in order to start writing in C with microcontrollers in mind.

1. Conditional statements

You should master the if statement well. Together with expanding it using else and else if. Conditional checking in programming is an everyday thing. In many places, for example, we receive something. Even from external sensors. It’s worth checking what came in and reacting to it.

You also need to practice creating complex conditions in an if statement. For such constructs, you’ll need logical operators. There is a certain danger associated with them that you need to be aware of… Would you like to learn it? Write in the comments!

2. Loops

You must know all loops that are in C. There are 3 of them.

  • for
  • while
  • do…while

The third one is, in my opinion, often forgotten—and that’s a shame. When I write in a program, e.g., receiving a few bytes from a sensor or from any interface, I always use loops. They help avoid repeating code in such repetitive activities.

They incredibly make filling and reading arrays easier. One for loop with an iterator and you go through the entire array from start to finish.

What about do…while? In a microcontroller, we often need to query a status, e.g., from an external sensor. Whether it has finished measuring or processing, for example, temperature into a digital value. Then at least once we have to establish communication. The do…while loop does exactly that. No matter what loop condition we have, it will still execute its action at least once (the so-called loop iteration).

3. Logical and bitwise operations

In a microcontroller, we operate a lot on registers. Not only those in the microcontroller, but we also configure external chips this way. Options in registers are packed, and often we need to change one or two bits. For example, in the middle of that register, not at the edge.

We must read a full 8-, 16- or 32-bit register. So we must treat that value in the right way. This is where bitwise operations in C come in.

We must know operations such as bit shifting, masking. All of this so that we change only the things we need to modify. Without touching unrelated bits.

4. Pointers

The biggest pain for beginners. The best thing about them is that once someone understands how they work, later they are surprised that it was actually easy 😀

Why do we need them? First, access to registers is access by address, i.e., via a pointer. The same rules apply here.

Pointers are also very useful when dividing a program into logical layers and “objects”.

When passing data between subsequent layers, we do it using pointers. We avoid copying data from place to place in microcontroller programming.

When writing pseudo-object-oriented C, we build “objects” based on structures. To handle them, we build universal functions that take pointers to those “objects”. Thanks to this, we have a very organized program or library.

5. Functions

You need to be able to build functions. This may sound strange to someone who has been programming for a long time. Unfortunately, beginners often don’t use functions. They write everything in main, which is wrong.

We also can’t create one giant function. We divide the program into smaller pieces. So as not to duplicate code. If you write some code sequence again, you should wrap it in a function. We avoid repetition.

People say that a function shouldn’t be longer than what you can see on the screen. That’s true, because later it can be analyzed quickly and easily. The only problem is that everyone has a different code editor and a different screen. So such advice is more of an approximation than something strict.

6. Data types

Of course the simple ones like int, float, char, which are built into the C standard. It’s also worth knowing their “fixed” counterparts from the stdint.h header. These are, for example, uint8_t, uint16_t uint32_t. They have a strictly defined size and you can see it at first glance. That helps tremendously.

You must know the limitations of floating-point types (float and double). We are not physically able to represent all real numbers in a binary system. I wrote about this in another article.

And of course composite types. These are structures, unions, and the enumerated type enum is also included. The minimum you must be able to do is use structures.

So that you can create “objects” from these structures. An object describes exactly, e.g., a sensor. It contains its address, which interface it is connected to, its specific parameters. Later we create a few “objects” for a few sensors that will differ by address or also by the I2C interface number.

A function that operates on these objects takes a pointer to the object and already knows how to work with that specific one sensor we passed. It itself extracts from the information contained in the “object” at what address to talk to it and on which interface.

Where to learn all of this?

I created a course dedicated to microcontrollers (the online course content is conducted in Polish). I teach C from scratch in it. Everything I discussed in this post is included in the course curriculum.

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 it in between explaining successive aspects of the C language.

An additional advantage is also that I show how to run a project well. I will show you how to deal with building abstraction layers. We will use structures, pointers and callbacks. And of course splitting into files. It helps a lot.

Such separated layers are much easier to перенос between projects, and even between different microcontroller families.

Join the waiting list for the course (the newsletter and course-related emails are conducted in Polish) and start learning together with the materials I prepared. After signing up, you will receive weekly emails about the C language: https://cdlamikrokontrolerow.pl

Podobne artykuły

.
Categories: STM32

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *