Microcontrollers
In modern engineering, electronics plays a very important role. Nearly all of the mechatronic systems are controlled by a microcontroller. In short, a microcontroller is a miniaturized computer which is programmed to perform several tasks. In automotive industry, microcontrollers are used in a massive proportion, nearly all the systems in a vehicle has a microcontroller.
Modern vehicle have between 30 and 100 or more microcontrollers. They are used in systems as:
- Braking: Anti-lock Braking System (ABS), Electronic Stability Program (ESP)
- Chassis: ride height control system
- Powertrain: Engine Management System (EMS)
- Automatic Transmissions: gear shift control
- Infotaiment: radio control
The trend is to have even more microcontroller in a vehicle due to a rise of the degree of autonomy of a vehicle (e.g. Lane control functions, adaptive cruise control, etc.)
Microcontrollers are also used masively in other area as: telecomunications, powerplants, home appliances, etc. The bottom line is that every area of engineering involves a system with a microcontroller.
Why C programming ?
Almost all the industry microcontollers are programmed in C language. That is why, for an engineer, is critical to have basic C programming skills. Even if you are not a software developer, is quite likely to interact with C programming. You might need to understand how a piece of code works, you might need to test a mechatronic system, you might need to write control requirements for a systems with microcontroller.
Not having basic C language programming skills, for an engineer, is almost like not speaking english!
Where do we start ?
First of all, before starting writing C code, we need one of the following:
– a text editor and a C compiler
– an Integrated Development Environments (IDE) for C language
To get a better understanding on how C language works, it’s better to use a normal text editor (e.g. Notepad, Notepad++, etc.) and a separate compiler. This way you’ll have to go through the basic steps of the compilation process. One disadvantage of this method is that the C code syntax is not highlighted, the same font color is used for the whole text and the code is harder to debug.
For a quicker start it’s better to use an IDE. On Wikipedia you can find an exhaustive list of of integrated development environments for different programming languages. Go to the C/C++ section and choose one which fits your needs.
Just for your reference, for our tutorials, we are using either Geany (under Linux Ubuntu) or Pelles C (under Windows). It should not make any difference if you use another programming environment since C code is portable and standardized.
It worth mentioning that there are a couple of difference between C and C++. You can find a lot of references on the internet about this topic. In short C is definitely used for microcontrollers programming so you should focus on it. C++ is more application oriented (Windows, Linux), is an object oriented programming language.
First C code/program
Now that you have installed your C programming environment, open a new file, and write the following code/instructions in it:
#include<stdio.h> int main(void) { printf("Engineers are awesome!\n"); return 0; }
Save the file as a *.c file. This file represents the source file or the program, it contains the instructions (code) which needs to be executed on the target application (microcontroller or computer).
C is a compiled programming language. This means that the source code needs to be converted into an object file by the compiler. The object file is the file which can be interpreted by the machine. Each *.c file will have an associate *.obj file.
After the compilation is done, object file generated, the next step is the linking. This is performed by a linker, which gathers all object files associated with the applications and converts them in executable file.
Depending on the target application (the device that runs the C code), the executable file can be:
– *.exe file (for Windows applications)
– *.hex or *.mot file (for microcontrollers, depending on type)
By running the executable generate above we are going to see printed into command prompt window (Windows) or a terminal window (Linux) the message:
After the executable file is produced, the target machine loads it into the memory and executes it. On microcontrollers the *.hex file is flashed/programmed into the Flash memory, in order to be executed.
Observation: The above example can not be used for a microcontroller application since the printf()
function is specific to PC/laptop, a standalone microcontroller can not display a text message.
For any questions or observations regarding this tutorial please use the comment form below.
Don’t forget to Like, Share and Subscribe!