博文

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

How does interrupts works exactly in microcontrollers?

图片
 Interrupts are how a microcontroller stops what it’s doing (briefly) to handle an important event right now , then returns to exactly where it left off. Here’s how it works “under the hood”, step by step. The core idea Your main code runs in a loop (or an RTOS task). Hardware events happen asynchronously: a timer hits zero, a UART byte arrives, a GPIO edge occurs, ADC completes, etc. Instead of polling (“are we there yet?”), the MCU uses an interrupt : a hardware signal that asks the CPU to run a specific function called an ISR (Interrupt Service Routine). What happens when an interrupt occurs (exact sequence) 1) An event sets an interrupt flag Example: a timer overflows → the timer peripheral sets a status bit like TIMERx_IF = 1 . 2) The interrupt controller decides if it should fire An interrupt triggers the CPU only if: The peripheral’s interrupt is enabled (local enable bit), The interrupt is unmasked/enabled in the interrupt controller (e.g., N...

How to use DMA in circular mode on STM32?

图片
 Using DMA in circular mode on STM32 basically means: “Keep filling the same buffer in RAM over and over, wrapping around when you reach the end.” This is perfect for continuous ADC sampling , UART RX streams , audio , etc. Here’s how to do it, in practical steps. 1. Basic idea Peripheral data register → DMA → RAM buffer. DMA writes: buf[0] … buf[N-1] then automatically wraps back to buf[0] and continues. Your code never restarts the DMA; it just reads from the buffer while DMA keeps filling it. 2. Create the buffer in RAM Example for an ADC (16-bit samples): # define ADC_BUF_LEN 256 uint16_t adc_buf[ADC_BUF_LEN]; Example for UART RX (8-bit): # define UART_RX_BUF_LEN 128 uint8_t uart_rx_buf[UART_RX_BUF_LEN]; Make sure the buffer is global or static so it stays allocated. 3. Configure DMA in circular mode (HAL example) Let’s say you’re using STM32Cube HAL. 3.1. In CubeMX / code, set: Direction : Peripheral → Memory Mode : DMA_...

STM32 pins VCC, VDD, VEE, VSS, VBAT

图片
  This is a fundamental and important concept when starting with STM32 (or any microcontroller ). Here’s a clear breakdown of what these pins mean and how to handle them on an STM32. The Quick Answer (TL;DR) VDD:   Positive supply voltage  for the  digital  internal logic. (e.g., 3.3V) VCC:   Positive supply voltage  for the  analog  peripherals (ADCs, DACs, etc.) and I/O pins. Often connected to the same 3.3V rail as VDD. VSS:   Ground reference  for the digital circuitry (0V). VSSA:   Ground reference  for the analog circuitry. (Must be connected to VSS but with a careful layout). VREF+ / VREF-:   Reference voltage  for the Analog-to-Digital Converter (ADC). Provides a cleaner reference than VCC/VSSA for accurate measurements. VBAT:  Backup power input for the Real-Time Clock (RTC), backup registers, and sometimes the low-power oscillator when main power is off. VEE:   Negative supply voltage . Rar...

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...

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...