Microcontrollers have many names: MCU (Microcontroller Unit), also called microcontroller. With the growth of the Internet of Things, demand for microcontrollers has increased. At the same time, as microcontroller performance and resources have improved, the number of languages used for development has also grown. Which mainstream development languages are currently supported for microcontrollers?
1. Assembly language
Overview: Assembly is a low-level programming language closely tied to hardware. It maps directly to machine code and allows programmers to control hardware resources directly.
Example: A 8051 microcontroller (AT89S52) LED blink program written in assembly.
ORG 0x0000 ; program start address
MOV P1, #0x00 ; initialize P1 port to low
MAIN:
MOV P1.0, #1 ; turn on LED, set P1.0 high
ACALL DELAY ; call delay subroutine
MOV P1.0, #0 ; turn off LED, set P1.0 low
ACALL DELAY ; call delay subroutine
SJMP MAIN ; infinite loop
DELAY:
; delay subroutine
MOV R2, #50
DELAY_LOOP:
DJNZ R2, DELAY_LOOP
RET
2. C language
Overview: C is the most commonly used language for microcontroller development. It is a high-level language with good portability and readability. For microcontrollers, embedded C is typically used.
Example: STM32 LED blink program in C.
#include "stm32f4xx_hal.h"
int main(void)
{
HAL_Init();
SystemClock_Config();
__HAL_RCC_GPIOA_CLK_ENABLE(); // enable GPIOA clock
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Configure GPIO pin
GPIO_InitStruct.Pin = GPIO_PIN_5; // assume LED is connected to pin 5 of GPIOA
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; // push-pull output
GPIO_InitStruct.Pull = GPIO_NOPULL; // no pull-up or pull-down
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; // low speed
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
while (1)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // turn on LED
HAL_Delay(1000); // delay 1 second
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // turn off LED
HAL_Delay(1000); // delay 1 second
}
}
3. C++ language
Overview: C++ is an object-oriented language based on C. Although used less often for microcontrollers, some modern microcontrollers support C++.
Example: STM32 LED blink program in C++.
int main(void)
{
LED_Class LED1(GPIOF, GPIO_Pin_7, RCC_APB2Periph_GPIOF);
LED_Class LED2(GPIOF, GPIO_Pin_8, RCC_APB2Periph_GPIOF);
LED1.Init();
LED2.Init();
while(1)
{
LED1.Open();
LED2.Open();
Delay(10);
LED1.Close();
LED2.Close();
Delay(10);
}
}
4. Python
Overview: Although Python is not common for traditional microcontroller programming, some microcontrollers support MicroPython and CircuitPython, simplifying development.
Example: LED blink program in MicroPython.
import machine
import time
led_pin = machine.Pin(2, machine.Pin.OUT) # assume LED is connected to pin 2
while True:
led_pin.on() # turn on LED
time.sleep(1) # delay 1 second
led_pin.off() # turn off LED
time.sleep(1) # delay 1 second
Note that this code uses MicroPython, not standard Python. Ensure your development board supports MicroPython and is configured accordingly. In embedded development, you may also need to consider hardware configuration, clock settings, and other low-level details.
5. Other languages
Beyond assembly, C, C++, and Python, a few projects use Java, but Java requires a virtual machine and thus runs on higher-performance microcontrollers. Ada is also used in some aerospace and military applications; it provides strong static typing and support for concurrent programming.
Besides the languages mentioned above, what others have you used? Which languages have you used for microcontroller development?