Nerd For Tech
Published in

Nerd For Tech

The other side: the Embedded C programming

The C language for most programmers in the world is a hated language, for others is the loved one, but, everyone that has a certain experience or a medium/experienced programming level will state that C is a hell of a language.

C is considered the “father of all languages” because is very mature, remains on the market after 50 years (I think), and because the natural approach of the language lets you build whatever you want. C has evolved over the years and set the foundations for many other languages around, the best example is the one and only: Java.

When you start programming (In my case with C) you start and end with basic programs that run and live on an IDE, and compiling a C code via CLI was a risky job. The main point here is that C as a low-level programming language presents a lot of software environments, one of those is the Embedded Programming with C.

Embedded Systems

The best to describe an Embedded system is by combining hardware and software at the same time in order to achieve a particular and specific task.

This can be found on Digital cameras, mobile phones, washing machines, anywhere that has a processor system inside of it, on this particular element we will be placing an embedded software.

This kind of solution, devices or whatever you want to call it, is split into three main parts, parts that I used to understand the particular and conceptual responsibility of each element, and where the software actually lives.

The hardware is considered the body of the system, totally, but then we set the embedded processor as the brain, think of that thing that moves the hardware. And finally and most important part, the software (The C program) can be considered as the soul of that composition.

The programming part cannot be a standalone system, most are part of larger systems.

Programming embedded systems are slightly different than programming on your personal computer because devices are limited to resource constraints in power, storage, processing, and other things. The hardware is typically smaller with less power-consuming components.

Basically, we need to program very intelligently because the capability is more tied to the hardware device. And to achieve this, we have to work on machine code, using a low-level language like Assembly, but many other languages can also work in order to build a fully working software with short resources.

The problem with Assembly relies on the hard labor to maintain code and a hard learning curve. The cons of using Assembly is that is the best language EVER, to convert to machine code, and is awesome with time accuracy.

So, what could be the specs for embedded systems programming?

In order to complete an “A+” code, we need to contemplate the following aspects: size, speed, and portability for language benefits.

The size aspects look at controlling the memory that a program occupies, for an embedded processor like micro-controllers the ROM capacity is very limited. Of course, the program should be fast, the hardware should not be slowed down because of not optimized software, and by portability, the same program needs to be compiled into different processors.

Other aspects like ease of implementation, maintenance, and simplicity can make the difference as well.

This is great, but this also can be done by many other languages, even high-level languages like Java, Visual Basic, and other C flavors like C++ or C#

So, Why C?

The C programming language is the most popular and widely used programing language.

C already has an Embedded extension that uses the same syntax and semantics. But also, takes advantage and features of the fully-featured language.

  1. C is small and easy to learn, understand, program and debug, the language consider a full development cycle in multiple ways
  2. C compilers are available for almost all embedded devices, is very versatile in a wide variety of microprocessors
  3. C has the advantage of being processor-independence.
  4. C combines the functionality of Assembly language and features of high-level languages.

By this point, we need to understand that is the same language, may appear different, but the thing is that is applied in different contexts. C is used for big computers, while Embedded C on micro-controllers-based applications. The key point is being forced to use the resources optimally, mostly memory usage.

Data types, arithmetic operations, control structures, custom functions, variable declarations, memory assignments, bit-level operations (very interesting concept btw) … number system, works the same, it has differences but is not that crucial, most elements may work better to ARM C compilers, other to 8051 or C51 compilers and so on.

Even the working compiler is pretty much similar. The compiler process generates a series of output files during the whole compilation.

Starting with a C source code to be compiled. The source code brings within the source text with macros expanded and the developer comment deleted.

The preprocessor works on that C source code and generates a .src file (which is Assembly code).

The Assembler generates an Object-Code file that contains a relocatable object code. Next, The Linker compiler takes that Object-Code and along with C libraries generates an Absolute Object File.

Finally, that .obj file is converted to the .hex (hexadecimal file) and then to a binary file used to program the processor.

It’s kinda tedious but simplifying all of this, the C source code goes to Assembly code, and with the help of C libraries, it converts into a binary file that can be executed by anyone.

Do you want to start developing for micro-controllers?. A must-need tool is the Keil IDE, which contains the compiler, the debugger, and the linker compiler in one place.

Cheer up!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store