ADC — ADC_TempSensor(HAL 库)
固件?: PY32F0xx_Firmware V1.5.0
芯片: PUYA
源文?: PUYA/PY32F0xx_Firmware_V1.5.0/Projects/PY32F030-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 *)(0x1fff0f14)) /*!< Temperature Scale1 */
#define HAL_ADC_TSCAL2 (*(uint32_t *)(0x1fff0f18)) /*!< Temperature Scale2 */
#define Vcc_Power 3.3l /* VCC power supply voltage, modify according to actual situation */
#define TScal1 (float)((HAL_ADC_TSCAL1) * 3.3 / Vcc_Power) /* Voltage corresponding to calibration value */
#define TScal2 (float)((HAL_ADC_TSCAL2) * 3.3 / Vcc_Power) /* Voltage corresponding to calibration value */
#define TStem1 30l /* 30 ��? */
#define HighTemp_105
#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?