博文

目前显示的是 八月, 2025的博文

How to implement the design of odd frequency dividers in Verilog?

图片
 There are two common, FPGA -friendly ways to “divide by an odd number” in Verilog: Stay in one clock domain and generate a clock-enable (CE) pulse every N cycles. (Best practice.) If you truly need a new clock with ~50% duty, use both clock edges (posedge + negedge) to alternate half-period lengths. (Use sparingly; route through a clock buffer.) 1) Preferred: odd divider as a clock-enable pulse This avoids creating a new clock domain and all the CDC/timing pain. Your logic runs on the original clock, gated by a CE that goes high every N cycles. // Odd (or even) divider -> 1-cycle clock-enable pulse each N cycles module ce_divider #( parameter integer N = 5 // N >= 2 (odd or even) ) ( input wire clk, input wire rst_n, // active-low synchronous reset output reg ce // 1-cycle pulse every N cycles ); localparam W = $clog2(N); reg [W-1:0] cnt; always @(posedge clk) begin if (!rst_n) begin ...

How does PWM control LED brightness or motor speed?

图片
 Let’s dive into how PWM (Pulse Width Modulation) actually makes an LED look dimmer or a motor run slower — even though the voltage is always “full ON” or “full OFF.”  Principle of PWM PWM rapidly switches voltage ON and OFF at a fixed frequency. The duty cycle (%) controls the ratio of ON-time to OFF-time in each cycle. The load (LED, motor, etc.) responds to the average power delivered, not the instantaneous ON/OFF states.  So, instead of lowering voltage directly, PWM delivers “packets of full power” and lets the load integrate them.  LED Brightness Control Human eyes cannot perceive fast flickering above ~100 Hz → instead, they see the average light output . Example: 10% duty cycle → LED is ON only 10% of the time → appears dim. 50% duty cycle → LED is ON half the time → medium brightness. 90% duty cycle → LED almost always ON → near full brightness.  Advantage: LED always gets correct forward voltage → no color shift ...

A drop-in solution for STM32F4 + HAL that receives variable-length UART data reliably

图片
Here’s a drop-in solution for STM32F4 + HAL that receives variable-length UART data reliably. I’m giving you two production-proven patterns: Delimiter framing (e.g., \n ) using an ISR ring buffer (simple, great for text/AT commands) DMA circular + IDLE line (best for bursts/binary streams; minimal CPU) Both work on F4 (F401/F405/F407/F411, etc.) with CubeMX/HAL. A) Delimiter framing ( \n ) with ISR ring buffer 1) CubeMX setup Enable USARTx (e.g., USART1 @ 115200 8N1) Enable RXNE interrupt Configure GPIO pins (Tx/Rx AF) NVIC: set a sensible priority (e.g., preempt 5, sub 0 if using FreeRTOS) 2) Ring buffer + IRQ handler // uart_rx_isr_rb.c # include "main.h" # include <string.h> # include <stdarg.h> # define RB_SIZE 512 typedef struct { volatile uint8_t buf[RB_SIZE]; volatile uint16_t head, tail; // modulo RB_SIZE } ringbuf_t ; extern UART_HandleTypeDef huart1; // adjust to your instance static ringbuf_t r...

A specific optimization guide for a particular sensor type (ultrasonic, temperature, gas, or optical)

图片
  Ultrasonic ( HC-SR04 / waterproof transducers) Accuracy Add RC low-pass on echo (e.g., 1–4.7 kΩ + 100–470 pF) before the MCU to tame ringing. Time with a hardware timer input-capture ; average N readings, reject outliers with a median filter . Temperature-compensate sound speed: d = t ⋅ c ( T ) 2 ,    c ( T ) ≈ 331.3 + 0.606 T ° C   m/s d = \frac{t \cdot c(T)}{2},\; c(T)\approx331.3+0.606T_{°C}\,\text{m/s} d = 2 t ⋅ c ( T ) ​ , c ( T ) ≈ 331.3 + 0.606 T ° C ​ m/s Range Use a module with separate TX/RX transducers; add LNA (low-noise op-amp) on RX. Use narrow beam horns or foam baffles to reduce multipath. Slow down update rate and burst 8–16 pings , then correlate (coherent averaging) to dig out weak returns. Temperature (NTC, PT100 / PT1000 , IC like TMP117 ) Accuracy Excite NTC with constant-current (100–500 µA) to linearize ADC; use 4-wire for RTDs. Use a precision reference (≤0.05%) and 16–24-bit ΔΣ ADC for RTDs. Calibrate two-poi...

STM32 drives INA226 to measure current, voltage, and power

图片
  INA226 + STM32 is a rock-solid combo for precision DC current, bus voltage, and power. Here’s a clean, copy-ready guide with wiring, calibration math, and working HAL code. STM32 + INA226 (I²C) — Measure Current, Bus Voltage & Power 1) What the INA226 gives you Bus voltage (VBUS): up to 36 V, LSB = 1.25 mV Shunt voltage (VSHUNT) across your sense resistor , LSB = 2.5 µV Current (after you program a calibration) Power (after calibration), Power_LSB = 25 × Current_LSB Programmable averaging , conversion times , and alert (over-current, over-voltage, power, etc.) 2) Typical wiring (single-supply) Shunt resistor (RSHUNT) in series with the load on the low side or high side ( INA226 supports both; high side is common). Connect: VIN+ to load side (after shunt toward load) VIN− to source side (before shunt toward supply) VBUS internally sensed from VIN+ (no extra wire for bus voltage) SDA / SCL → STM32 I²C pins (e.g., PB9/PB8 o...

Raspberry Pi Environment Construction Guide

图片
  1. Hardware Preparation Raspberry Pi Model → Pi 4 (recommended), Pi 5 (newest), or Pi Zero (compact projects) MicroSD Card → 16GB+ (Class 10 or UHS-I), or SSD for better speed Power Supply → Official Pi adapter (5V 3A for Pi 4, 5V 5A for Pi 5) Cables & Peripherals : HDMI cable + monitor (optional, for first setup) USB keyboard/mouse (optional, if not headless) Ethernet cable (preferred) or Wi-Fi (Optional) Case + heatsink/fan for cooling 2. Install the Operating System Download Raspberry Pi Imager : https://www.raspberrypi.com/software Flash OS to microSD: Choose OS: Raspberry Pi OS Lite → headless, server projects Raspberry Pi OS Desktop → GUI for beginners Enable SSH , set Wi-Fi SSID/PW, and hostname in advanced options. Insert SD card → power up Pi. 3. First Boot & Basic Setup Login (default): Username: pi Password: raspberry Then: sudo raspi-config Change password Set timezone & locale ...