ADC — ADC_TempSensor
固件库: PY32L020_Firmware V1.2.1
芯片: PUYA
源文件: PUYA/PY32L020_Firmware_V1.2.1/Projects/PY32L020-STK/Example/ADC/ADC_TempSensor/Src/main.c
功能简介
本示例利用 MCU 内置的温度传感器,通过 ADC 采集并计算芯片当前温度。无需外部传感器,非常适合学习 ADC 的基本用法。
硬件准备
- 电位器(可调电阻)连接到 ADC 输入引脚
- 或使用内部温度传感器(无需额外硬件)
完整代码
1
2
3
4
5
6
7
8
9
10
11
12
|
#include "main.h"
#define HAL_ADC_TSCAL1 (*(uint32_t *)(0x1fff0114)) /*!< Temperature Scale1 */
#define HAL_ADC_TSCAL2 (*(uint32_t *)(0x1fff0118)) /*!< Temperature Scale2 */
#define Vcc_Power 3.30 /* VCC power supply voltage, modify according to actual situation */
#define TScal1 (float)((HAL_ADC_TSCAL1) * 3.3 / Vcc_Power) /* Voltage corresponding to calibration value at 30℃ */
#define TScal2 (float)((HAL_ADC_TSCAL2) * 3.3 / Vcc_Power) /* Voltage corresponding to calibration value at 85℃ or 105℃ */
#define TStem1 30 /* 30℃ */
#define HighTemp_85
#define TStem2_85 85
#define TStem2_105 105
#define Temp_k_85 ((float)(TStem2_85-TStem1)/(float)(TScal2-TScal1))
#define Temp_k_105 ((float)(TStem2_105-TStem1)/(float)(TScal2-TScal1))
|
代码讲解
ADC 配置:选择采样通道、采样时间、触发方式、数据对齐方式。采样时间越长精度越高。
校准:ADC 使用前必须校准,消除工艺偏差。校准期间不要操作 ADC。
实验现象
- 串口助手打印 ADC 采样值
- 转动电位器时数值变化
- 温度传感器示例会显示芯片温度
注意事项
- ADC 参考电压影响测量精度
- 采样时间要根据信号源阻抗选择
- 使用前务必校准(Calibration)