C Language points in ARM Microcontrollers and Embedded programming

C language is easy!?

There are many different programming languages in the world today. Among them, C has a relatively long history and is still in use as a development language for embedded systems.

In addition to its advantages over other programming languages, such as its lightweight code and suitability for real-time control, the fact that it has a long history and a wealth of assets is probably the reason why it is still in use today.

C is an essential language for ARM MCUs. C and C++ are also standard in the integrated development environment, and because C is a mature programming language with a long history, it is easy to find a book that suits your needs. It's not just about learning abstract programming, but you learn by operating MCU, you will be able to master the language in a short time even if you are self-taught.

Once you become familiar with the minimum grammatical rules and language development environment tools, you can learn more and more on your own, not only in C but in any language. The question is how to take the first step, and this site recommends learning MCU and C at the same time in an embedded development environment at the earliest possible stage.

C is classified as a classic programming language, but once you get used to its grammatical peculiarities, it is rather simple and clean because you do not need to be aware of the latest knowledge compared to, for example, C#, a Windows application development programming language.

Although C is a high-level language, it is a general-purpose language and does not provide ready-made convenience functions for everything. One of the most exciting aspects of the C language is that you can improve your skills by using the basic standard functions and building up small functions with the concept of functions of your own making.

It is not at all necessary to be aware of C-like programming structures from the beginning, and for beginners, it is fine if only a combination of operators and control statements are used. However, variable-related concepts (local variables, global variables, static modifiers, etc.) are of utmost importance and should be studied thoroughly from the beginning.

Aside from complex programming that makes full use of pointers, structures, and unions unique to the C language, it does not take much time to become proficient in basic programming such as basic arithmetic (four arithmetic operations, logic, bitwise operations, etc.) and control structures (if and switch statements). It will not take much time. The most important part of MCU is to master the operators for handling digital values, so it is best to focus on understanding this area. If you deal with the C language in this way, it may be classified as an easy language.

As you get used to it, you will want to try more advanced things such as handling strings freely, etc. At that stage, you should be aware of pointers, structs, etc. and seek C-like programming.
C language is so deep that it can be made as esoteric as you want, but that is not desirable from a practical standpoint.

I think you should first try to keep your programming organized and easy to read.

Specifications in accordance with CMSIS interface standards

The C language has a common syntax, but the syntax used for embedded systems differs slightly depending on the specifications of MCU, so it is necessary to adapt the language to MCU. In other words, it is like a dialect.

The only difference in the specifications among the dialects is in the way variables are declared, and the compiler for the STM32 MCU follows the CMSIS interface standard standardized for the Cortex-M series by ARM.

normal variables
The point

Among variables, constants are declared as variables with the const modifier. In the case of built-in variables, they are placed in ROM and their values cannot be changed. Variables whose values do not need to be changed should be declared as constants and assigned to ROM with the const modifier to save RAM.

const modifier variable

Floating-point type is used when dealing with decimal points; the STM32 with Cortex-M3 performs operations in software, so it is not super-fast, but it is possible since it is a 32-bit MCU. The variable type is specified as float for single-precision 4-byte and double for double-precision 8-byte. These are not ARM-specific, but standard.

Embedded Specifics

One of the built-in peculiarities of the C language is the use of variables with the "volatile" modifier.

volatile modifier variable

Declaring variables with the volatile modifier suppresses compile-time optimization. In embedded systems, not all variables always involve the CPU, as in the following example.

  • Variables whose change in value cannot be predicted by the program (CPU)
  • Variables whose values can change due to interrupts, etc.
  • Variables that are closely related to the hardware and are updated independently of the CPU

When optimized at compile time, the code itself can be made leaner and faster, but Variables that are unique to embedded systems and closely related to hardware without being related to the CPU may behave unexpectedly and cause bugs due to optimization.

For this reason, variables are declared with the "volatile" modifier to avoid compile-time optimization; ARM-based cores use the "__IO" modifier.

The point

Variables used in interrupt processing in the application and variables processed by peripheral functions (peripherals) should be declared as "volatile" variables with the "__IO" modifier.

Summary

This chapter has described the C language, which is essential for microcontroller development programming. Although C language has a high degree of difficulty among programmers and is often avoided by some, it is not that C itself is difficult as a programming language, but only that it has aspects that are difficult for beginners.

As a beginner of embedded systems, it is recommended to get used to the microcontroller by operating it with relatively simple programming structures, leaving the difficult concepts specific to the C language for later. The following is an example of a case in point.

The C language specification developed for ARM microcontrollers follows the ARM standard, and although the variable notation differs slightly from that of the general-purpose C language, that is the only difference.

How to learn C language in a cross environment using the teaching material board

Monitor Output in C Language Learning

Follow me!