How to get started with embedded C language? How do I program a microcontroller?

Introduction

In this C language course, we’ll learn how to program to control embedded systems.

This course is aimed at people who are not familiar with electronic board programming in C.

This course can also be very useful for those who have already made programs in C but want to focus more on hardware: knowing which types of variables take up the least space in memory, or how to switch on an LED.

Which interpreter should you use?

To program, we’ll use microchip’s MPLAB IDE software to simulate and download the program directly onto the circuit board.

If you’d like to program electronic boards easily, especially Arduino boards, take a look at our courses on the Arduino language.

C language basics

We’re now going to take a look at the basics of the C language to get you started.

a) Void main

La première chose que l’on voit quand on ouvre un programme est le void main :

#include <stdio.h>

void main(){
    //Your progam
}

The main function is the main function of your program. This is where you put all the program instructions or function calls you need.

The void main is mandatory, as failure to include it will result in an error.


b) Square brackets

Every function starts with an open bracket ({) and ends with a closed bracket (}):

#include <stdio.h>

void main(){
    my_function();
}
void my_function {
}

Failure to use the square brackets will result in an error.

c) Semicolon

The last thing we’re going to look at is the semicolon. In the C language, all instructions always end with a semicolon:

#include <stdio.h>

void main(){
    my_function(); // We call the my_function which is an intruction so semicolon
}
void my_function {
}

Failure to use a semicolon will result in an error.

Small quiz

We’ll now take a short quiz to test your knowledge of the C language.

1) What is a library?

A) A pre-written source file containing ready-to-use functions

B) A file containing function names, their signatures and the type of return code

C) A file containing a program

2) What is a console-mode program?

A) A program that runs only on the system console

B) A program that runs on a virtual terminal in a graphical window

C) A program that runs in a graphical environment with windows

3) Which function displays text on the screen in console mode?

A) Serial.println(“message”);

B) printf(“message”);

C) fprintf(“message”);


4) Which symbol is used to make a line break?

A) \a

B) \r

C) \n


5) What’s the name of the program that translates your C-language code into binary?

A) The programmer

B) The interpreter

C) The compiler


6) When declaring a variable, which memory is used?

A) The hard disk

B) Microprocessor

C) RAM


7) Which of these variables will cause an error?

A) juju

B) juju-5

C) juju_5


8) Which data type can store -89?

A) int

B) unsigned int

C) double


9) What is the correct line to retrieve a decimal number entered from the keyboard?

A) scanf(“%f”,decimal_number);

B) scanf(“%lf”, *decimal_number);

C) scanf (%f” ,&decimal_number);


10) Which function can be used to round 2.75 to 2?

A) Ceil

B) Round

C) Pow

Quiz correction

Here are the answers to the quiz: 1)A), 2)A) , 3)B), 4)c), 5)c), 6)C), 7)B), 8)A), 9)C), 10)B).

Don’t worry if you had trouble answering the questions, you can take a look at our Embedded C language courses, which will help you with your programming!