简易信号失真度测量装置的设计与实现(STM32单片机)
摘 要本文设计并实现了一种基于STM32G031微控制器的简易信号失真度测量装置。该装置利用STM32G031的PWM功能结合板上低通滤波器LPF电路生成频率可调DC∼20KHz且幅度可调10mV∼500mV的正弦波信号作为测试源。通过运算放大器将该信号输入至ADC进行量化处理采样过程采用DMA传输以提高效率。利用快速傅里叶变换FFT计算信号的频谱进而求得总谐波失真度THD。系统通过SPI接口的OLED显示屏实时展示采样波形、归一化幅值谱及失真度曲线支持线性与对数坐标显示。用户交互通过按键与光电旋转编码器实现支持频率与幅度的调节、采样率的变更以及显示模式的切换。实验结果表明该装置能够准确生成并测量低失真度正弦波信号但在小幅度输出时存在一定偏差。通过优化PWM占空比调节算法与FFT计算精度系统性能得到显著提升。本设计不仅展示了STM32G031在信号处理领域的强大能力也为便携式信号分析仪器的开发提供了有价值的参考。关键词STM32G031信号失真度PWMFFTOLED显示AbstractThis paper designs and implements a simple signal distortion measurement device based on the STM32G031 microcontroller. The device utilizes the PWM functionality of the STM32G031 combined with an on-board low-pass filter (LPF) circuit to generate a sine wave signal with adjustable frequency (DC∼20KHz) and amplitude (10mV∼500mV) as the test source. The signal is input to an ADC for quantization through an operational amplifier, with DMA transmission employed during the sampling process to enhance efficiency. The frequency spectrum of the signal is computed using the Fast Fourier Transform (FFT), enabling the determination of Total Harmonic Distortion (THD). The system displays the sampled waveform, normalized amplitude spectrum, and distortion curve (supporting both linear and logarithmic coordinate displays) in real-time on an SPI-interfaced OLED display. User interaction is facilitated through push buttons and a photoelectric rotary encoder, allowing for adjustments in frequency and amplitude, changes in sampling rate, and switches in display mode.Experimental results demonstrate the device’s capability to accurately generate and measure low-distortion sine wave signals, albeit with some deviations at small amplitude outputs. System performance is significantly enhanced by optimizing the PWM duty cycle adjustment algorithm and FFT computation accuracy. This design not only showcases the powerful signal processing capabilities of the STM32G031 but also provides valuable insights for the development of portable signal analysis instruments.KeywordsSTM32G031; Signal Distortion; PWM; FFT; OLED Display目 录摘 要 IAbstract II1 绪论 11.1 研究背景及意义 11.1.1 研究背景 11.1.2 研究意义 11.2 国内外研究现状及发展趋势 21.2.1 国内研究现状 21.2.2 国外研究现状 21.2.3 发展趋势 31.3 研究主要内容 32开发语言工具及相关选型 52.1 开发语言选择 52.2 集成开发环境IDE选型 52.3 图形化配置工具选型 52.4 硬件调试工具选型 63硬件设计 73.1总体设计 73.2设计思路 84 软件设计 104.1各功能代码及说明 104.1.1 SPWM波生成 104.1.2 ADC采样 134.1.3 FFT 154.1.4获取按键动作 184.2 OLED显示 204.3功能展示 264.3.1 OLED显示采样波形 264.3.2 OLED显示频谱/失真度曲线 275 总结与展望 295.1 项目总结 295.2 展望 29参考文献 31致 谢 32