Day 8: Basics of C Language (Part 1): Making Microcontrollers Understand You

First Network

Reading time required

4minutes

Quick read only takes 2 minutes

A microcontroller is like a foreign friend; you need to learn its language to communicate with it.

I still remember the first time I got a microcontroller development board. I excitedly connected the power, and then… I didn’t know what to do next. It lay quietly on the table, unresponsive to my enthusiasm. At that moment, I realized that to make this little guy work, I had to learn the language to communicate with it.

This is what we are going to talk about today: the C language. Don’t worry, I won’t bombard you with those headache-inducing technical terms. We’ll get to know this language that allows microcontrollers to understand our commands, just like making friends.

1

Variables: Finding a Home for Data

Imagine your storage cabinet, where each compartment is labeled and holds different items. Variables in C language work the same way.

A variable is essentially a small room allocated for data in memory, where each room has a door number (variable name) and a resident (data).

int age = 25;float weight = 65.5;char grade = 'A';

Here, int, float, and char are data types that determine what kind of guests can stay in this room. int is suitable for integers, float can accommodate decimals, and char is specifically for single characters.

When I first learned, I often couldn’t remember which type to use. Later, I discovered a trick: think about what you want to store. If it’s age or quantity, which doesn’t have a decimal point, use int; if it’s temperature or weight, which needs to be precise to the decimal point, use float; if it’s a grade or score, which is a single letter or symbol, use char.

2

Basic Operators: The Microcontroller’s Calculator

With data in hand, we naturally want to operate on it. The C language provides a complete set of operators, like giving your microcontroller a calculator.

You should be familiar with basic arithmetic operators like addition, subtraction, multiplication, and division:

int a = 10 + 5;    // Additionint b = 10 - 5;    // Subtraction  int c = 10 * 5;    // Multiplicationint d = 10 / 5;    // Division

But C language also has some special operators, like the modulus operator %:

int remainder = 10 % 3; // The result is 1, because 10 divided by 3 leaves a remainder of 1

This is particularly useful for determining odd or even numbers:if(number % 2 == 0) checks if a number is even.

Comparison operators are also important; they are used to compare the size relationship between two values, returning true or false:

5 > 3   // True5 == 3  // False5 != 3  // True

I remember when I first started, I often confused the assignment operator = with the equality operator ==. I fell into that pit several times, and I hope you can avoid it.

3

The First Program: Lighting Up an LED Instead of Hello World

Traditional C language teaching always starts with “Hello World”, but in the microcontroller world, making an LED blink is our “Hello World”.

#include <reg52.h> // Include the microcontroller register definition filevoid main()        // Every C program has a main function, where the program starts executing{    while(1)       // Keep the program running in a loop    {        P1 = 0x00; // Turn on all LEDs at port P1        // A delay should be added here, otherwise it blinks too fast for you to see        P1 = 0xFF; // Turn off all LEDs at port P1        // A delay should be added here too    }}

Do you see that while(1)? It will make the code inside the braces repeat indefinitely, so the LED will keep blinking. In the microcontroller world, there is no concept of shutting down; as long as it is powered, it quietly works.

I still remember the sense of achievement when I successfully made the LED blink for the first time. Although it was just a small light flashing, at that moment, I felt like I could really communicate with the microcontroller.

4

Practical Advice for Beginners

The most effective way to learn C language is to write more code. Don’t just read without practicing; otherwise, you’ll never learn.

I suggest starting with simple tasks, like trying to modify the value of a variable and observing the changes. Change the frequency of the LED blinking, or try using different operators for calculations.

Don’t be discouraged by errors; every programmer grows through constant debugging. I remember in the beginning, a single semicolon could take me a long time to debug, but those setbacks ultimately became valuable experiences.

5

Moment of Reflection

If you already have a microcontroller development board, why not try to make an LED blink? If you don’t have the hardware yet, you can draw a flowchart on paper and think about how to use variables and operators to control the LED’s on and off.

Today, we took the first step in communicating with microcontrollers together. Variables allow us to store data, operators enable us to manipulate data, and the first blinking LED program shows us the results of our communication.

Next time, we will continue to delve deeper, learning how to make programs make decisions and repeat tasks—this is the flow control of programs. You will find that the world of microcontrollers will become even more exciting because of this.

Are you ready? See you on Day 9!

Leave a Comment