With the development of technology, electronic products are becoming more and more common, making our daily lives more convenient. Most electronic products have microcontrollers, which achieve functionality by executing software logic. While assembly language is the most suitable programming language for microcontroller programming, the most commonly used and widespread is C language. So, why should we use C language for microcontroller programming?
1. What is C Language?
C language was first run on the DEC PDP-11 computer in 1972, designed by American Dennis Ritchie at Bell Labs for the UNIX operating system. Later, he and his colleagues co-authored the C language programming standard – K&R, not expecting that C language would become a widely used programming language and sweep the globe. The reason it is called C language is that it was designed based on B language.
UNIX operating system, Linux operating system, MySQL, etc. are all implemented using C language programming.
2. What are the characteristics of microcontrollers?
A microcontroller is a programmable device, and its control logic is implemented through software. Before microcontrollers, control logic was implemented purely in hardware, constrained by hardware logic which could not achieve complex logic. Microcontrollers have instruction sets, originally programmed using assembly language. Although assembly has very high execution efficiency, its readability and portability are very poor, while C language can complement the shortcomings of assembly.
Example: MOVC A, @A+DPTR
Function: Take the value from DPTR+A as an address, find the content at that address and send it to accumulator A.
(Example in assembly language)
-
Configuring the registers of the microcontroller. Programming a microcontroller is essentially configuring its registers and port settings, which are time-sensitive and require precise control. -
Microcontrollers require Flash and RAM for storage and execution of programs, and these resources are very limited. Therefore, the space occupied by the code is very important. -
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;
}
(Bubble sort implementation in C language)
3. Why use C language for programming?
-
There is no need to memorize the microcontroller’s instruction set, just configuring the registers can make the microcontroller work properly; -
C language has various types such as pointers, arrays, structures, unions, and enums, greatly enhancing programming flexibility and program handling capabilities; -
C language facilitates modular programming, allowing for standardized code.
C language programming
Do you program in C language? Come to the comment section to discuss!

END
→Follow to Stay Updated←