Lighting Up Microcontrollers with 5 Programming Languages: Assembly, C, C++, Python, Rust – Let’s Explore Their Differences!

Follow+Star Public Account Number, don’t miss out on exciting contentAuthor | strongerHuangWeChat Public Account | strongerHuangWith the popularity of the Internet of Things,the demand for microcontrollers is increasing.At the same time, as the performance of microcontrollersimproves and resources increase, microcontroller development is no longer limited to assembly and C language.Today, we will light up LEDs using Assembly, C, C++, Python, Rust, and see their differences.1. Assembly LanguageDescription:Assembly language is a low-level programming language that is closely related to hardware. It directly maps to machine language, allowing programmers to control hardware resources directly.Example:Write a LED lighting program for the 51 microcontroller (AT89S52) in assembly language.

ORG0x0000  ; Program start address
MOVP1, #0x00  ; Initialize P1 port to low level
MAIN:MOVP1.0, #1  ; Light up LED, set P1.0 to high level
ACALLDELAY   ; Call delay subroutine
MOVP1.0, #0  ; Turn off LED, set P1.0 to low level
ACALLDELAY   ; Call delay subroutine
SJMPMAIN     ; Infinite loop
DELAY:; Delay subroutine
MOVR2, #50
DELAY_LOOP:DJNZR2, DELAY_LOOP    RET

2. C LanguageDescription:C is the most commonly used development language for microcontrollers and is a high-level programming language with good portability and readability. For microcontrollers, embedded C is typically used for development.Example:Write a LED lighting program for the STM32 microcontroller 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); // Light up 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++ LanguageDescription:C++ is an object-oriented programming language based on C. Although it is relatively less used in the microcontroller field, some modern microcontrollers have started to support C++.Example:Write a LED lighting program for the STM32 microcontroller 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);
  }
}

Of course, the classes and function interfaces defined here can be found in my previous article “Can Microcontrollers Be Developed with C++?“.4. Python LanguageDescription:Although Python is not commonly used for traditional microcontroller programming, some microcontrollers like MicroPython and CircuitPython support Python, simplifying development.Example:Write a LED lighting program for a microcontroller 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()   # Light up 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 correctly installed and configured. In actual embedded development, you may also need to consider hardware configuration, clock settings, and other low-level details.5. Rust LanguageDescription:Rust is not a common language for writing STM32 microcontroller programs, but you can achieve this through some tools and libraries.Example:Write a LED lighting program for the STM32 microcontroller in Rust.

#![no_std]
#![no_main]
#![feature(lang_items)]
extern crate cortex_m;
extern crate cortex_m_rt as rt;
extern crate stm32f1xx_hal as hal; // Note: Choose the appropriate HAL library based on your STM32 model
use core::panic::PanicInfo;
use hal::prelude::*;
use hal::stm32;
use rt::entry;
// Language items
#[lang = "start"]
fn start(_main: &() -> !, _argc: isize, _argv: *const *const u8) -> ! {
  // Initialize HAL and CPU
  let cp = cortex_m::Peripherals::take().unwrap();
  let dp = stm32::Peripherals::take().unwrap();
  let mut rcc = dp.RCC.constrain();
  let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
  // Assume LED is connected to PIN5 of GPIOA
  let mut led = gpioa.pa5.into_push_pull_output();
  loop {
    // Turn on LED
    led.set_high().unwrap();
    // Delay for a while (you need to implement a delay function here)
    delay_ms(1000);
    // Turn off LED
    led.set_low().unwrap();
    // Delay again
    delay_ms(1000);
  }
}

6. Other Programming LanguagesIn addition to the above Assembly, C, C++, Python, and Rust, you may also see some other languages (like Java) running on microcontrollers.Overall, C is still the most widely used language due to its efficiency and space-saving features!———— END ————

Lighting Up Microcontrollers with 5 Programming Languages: Assembly, C, C++, Python, Rust - Let's Explore Their Differences!

● Column “Embedded Tools”

● Column “Embedded Development”

● Column “Keil Tutorial”

● Selected Tutorials from the Embedded Column

Follow the public account and reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.

Click “Read the Original” to see more shares.

Leave a Comment