Why Use C Language for Microcontroller Programming

Why Use C Language for Microcontroller Programming

With the development of technology, there are more and more electronic products, which make our daily lives easier. Most electronic products have microcontrollers, and microcontrollers achieve functionality by executing software logic. The most suitable programming language for microcontroller programming is assembly language, but the most commonly used and widespread is C language. Why should microcontrollers be programmed using C language?

1. What is C Language

C language was first run on the DEC PDP-11 computer in 1972, designed by American Dennis Ritchie, who developed it at Bell Labs for the UNIX operating system. Later, he and his colleagues co-authored the C language programming standard—K&R. Little did they know that C language would become a widely used programming language and sweep the globe. It is called C language because it was designed based on B language.

Why Use C Language for Microcontroller Programming

UNIX operating system, Linux operating system, MySQL, etc. are all implemented using C language programming.

2. What are the Characteristics of Microcontrollers

Microcontrollers are programmable devices, and their control logic is implemented through software. Before microcontrollers, control logic was implemented purely through hardware, which could not achieve complex logic due to hardware constraints. Microcontrollers have instruction sets, initially programmed using assembly language. Although assembly has very high execution efficiency, its readability and portability are very poor, while C language can just compensate for the shortcomings of assembly.

Example: MOVC  A, @A+DPTR
Function: Takes the value from DPTR+A as an address, finds the content at that address and sends it to accumulator A

Example of Assembly Language

  1. Microcontroller register configuration. Programming a microcontroller is essentially configuring the microcontroller’s registers and port configurations, which require precise control.

  2. Microcontrollers need Flash and RAM to store and execute programs, and these resources are very limited. Therefore, the space occupied by the code is very important.

  3. Microcontrollers all have instruction sets. Friends who have learned microcontroller assembly may remember that the 8051 microcontroller has 111 instruction sets and 7 different addressing modes.

#include <stdio.h>
void bubble_sort(int arr[], int len) {
        int i, j, temp;
        for (i = 0; i < len - 1; i++)
                for (j = 0; j < len - 1 - i; j++)
                        if (arr[j] > arr[j + 1]) {
                                temp = arr[j];
                                arr[j] = arr[j + 1];
                                arr[j + 1] = temp;
                        }
}
int main() {
        int arr[] = { 22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70 };
        int len = sizeof(arr) / sizeof(arr[0]);
        bubble_sort(arr, len);
        int i;
        for (i = 0; i < len; i++)
                printf("%d ", arr[i]);
        return 0;
}

Implementation of Bubble Sort in C Language

3. Why Use C Language for Programming

As mentioned earlier, the assembly instructions for microcontrollers are relatively difficult to remember, and there are two possible scenarios: 1) Changing hardware platforms requires re-adapting the instruction set and updating the register access addresses, which is quite troublesome; 2) Changing personnel for program maintenance, as assembly’s readability is poor, it takes a long time to understand someone else’s code.

C language is more flexible and can just compensate for the disadvantages of assembly.

  1. No need to memorize the microcontroller’s instruction set; you can make the microcontroller work normally just by configuring the registers;

  2. C language has various types such as pointers, arrays, structures, unions, and enumerations, greatly enhancing programming flexibility and program processing capabilities;

  3. C language facilitates modular programming, allowing for code standardization.

Why Use C Language for Microcontroller Programming

C Language Programming

Do you program in C language? Let’s discuss in the comments.

Why Use C Language for Microcontroller Programming

Current Detection, Always the Same Principle

Eliminate the Mess Created by if/else

From Today, Let’s Joyfully Design PCB Stamps

How Does a Charger Not Output Voltage Without a Battery Connected?

Leave a Comment