博文

目前显示的是标签为“MCU”的博文

How to include a file in an stm32cubeide project?

图片
 In STM32CubeIDE, “including a file” usually means adding it to the project so it gets built , and/or making headers discoverable by the compiler . Here are the common cases. 1) Add an existing .c/.h file into your project (copy into project) In Project Explorer , choose the target folder: C files → Core/Src Header files → Core/Inc Right-click the folder → Import… Choose General → File System → Next Browse to your file(s) → check them Make sure “Copy files into project” is enabled Finish Then in your code: # include "myfile.h" 2) Add a new source/header file (create inside the project) Right-click Core/Src → New → Source File Name it myfile.c → Finish Right-click Core/Inc → New → Header File Name it myfile.h → Finish 3) Use a file that lives outside the project (link, don’t copy) Useful if you share code across multiple projects. Right-click your target folder (e.g., Core/Src ) → Import… General → File System ...

How to optimize MCU interrupt response time?

图片
 Optimizing MCU interrupt response time is crucial for real-time performance and accurate signal processing. Below are practical, layered strategies to reduce latency and improve the efficiency of your interrupt handling:  1. Minimize Interrupt Latency (Time Until ISR Starts)  What causes latency? Instruction execution delay Stack saving (context switch) Nested interrupts disabled Peripheral delay (e.g., slow flag clearing)  Optimization Techniques: Tip Description Enable fast interrupt mode (if supported) Some MCUs (e.g. ARM Cortex-M3+) support tail-chaining or fast ISR entry Use high-priority interrupts Assign highest priority to critical ISRs Avoid global interrupt disable ( __disable_irq() ) Keep interrupts globally enabled as much as possible Use vectorized interrupt handling Avoid shared interrupt vectors; direct vector → faster Place ISR in RAM (if allowed) Running code from RAM can be faster than Flash on some MCUs  2. Write Ef...

Why is my MCU resetting unexpectedly?

图片
 If your MCU ( Microcontroller Unit) is resetting unexpectedly , it's likely due to faults, environmental issues, or software bugs . Here's a structured breakdown of the most common causes and how to troubleshoot them:  1. Power Supply Issues Low voltage or unstable power can trigger brown-out detection or watchdog resets. Check for: Power dips or noise Insufficient current supply Poor decoupling (add capacitors close to Vcc/GND)  Fix: Use a regulated power supply, add capacitors (e.g. 0.1 µF + 10 µF), verify with oscilloscope if possible.  2. Watchdog Timer (WDT) Reset If the watchdog( What is a Watchdog? ) timer is enabled but not reset ("fed") properly , the MCU will reset.  Fix: Ensure wdt_reset() (or equivalent) is called regularly, or disable WDT in code if not needed.  3. Brown-Out Reset (BOR) Happens when supply voltage drops below a safe level . Some MCUs reset to protect memory or ensure stable operation....

What is interrupt in microcontroller?

图片
  An interrupt in a microcontroller is a signal or event that temporarily halts the normal execution flow of your program so the microcontroller can respond to an urgent task or event immediately . Simple Definition: An interrupt is like someone tapping your shoulder while you're working — you pause what you're doing , handle the urgent matter, then go back to your original task . How It Works: The MCU is running your main program (called the main loop). An interrupt event occurs (e.g., a button press, timer overflow, data received). The MCU pauses the main program. It jumps to a special function called an Interrupt Service Routine (ISR) or interrupt handler . Once the ISR is done, it returns to where it left off in the main program. Types of Interrupts: External Interrupts – Triggered by changes on input pins (e.g. button press). Timer Interrupts – Triggered by internal timers reaching a value. Peripheral Interrupts – Triggered by e...

How do I choose an RTOS for my MCU?

图片
Choosing the right Real-Time Operating System (RTOS) for your microcontroller ( MCU ) involves evaluating technical constraints, project requirements, and ecosystem support. Here’s a step-by-step guide with actionable criteria: 1. Assess Hardware Constraints Factor Questions to Ask Common Options MCU Flash/RAM Does the RTOS fit within available memory? ≤32KB: FreeRTOS, Zephyr (minimal config) ≥128KB: RT-Thread, NuttX CPU Architecture Is the RTOS ported to your MCU core? ARM Cortex: All RTOSes RISC-V: FreeRTOS, Zephyr 8-bit (e.g., AVR): FreeRTOS (limited) Peripheral Support Does it include drivers for your hardware? Zephyr: Broad driver support FreeRTOS: Relies on vendor HALs 2. Evaluate RTOS Features Requirement Solutions Best Matches Hard Real-Time Needs deterministic scheduling FreeRTOS (with preemption), QNX, VxWorks Low Latency Fast context switching (<10µs) Zephyr, RT-Thread Power Management Sleep modes tickless idle Amazon FreeRTOS (with low-power extensions) Networking TCP/I...

How to calibrate the error of ADC module inside MCU?

图片
  Understanding ADC Error Sources Before calibration, it's important to understand the common error sources in microcontroller ADCs: Offset Error : Non-zero output when input is zero Gain Error : Deviation from ideal slope in transfer function Non-linearity : Deviation from straight-line transfer function Noise : Random variations in readings Reference Voltage Errors : Inaccuracies in voltage reference Hardware Preparation for Calibration Precision voltage source  (or at least a stable known voltage) High-quality multimeter  (for reference measurements) Stable power supply  (clean power to MCU ) Temperature-controlled environment  (if temperature compensation needed) Basic Calibration Methods 1. Offset Calibration c // Measure with grounded input # define NUM_OFFSET_SAMPLES 100 float adc_calibrate_offset ( ) { uint32_t sum = 0 ; for ( int i = 0 ; i < NUM_OFFSET_SAMPLES ; i ++ ) { sum += adc_read ( ) ; // Replace with your ADC...