44 lines
1.6 KiB
C++
44 lines
1.6 KiB
C++
#ifndef DEVICE_ADC_H
|
||||
|
|
#define DEVICE_ADC_H
|
|||
|
|
|
|||
|
|
#include <stdint.h>
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief ADC 采样模块。
|
|||
|
|
* @note 负责 VIN_ADC、内部温度与内部参考电压的 DMA 扫描采样与工程值换算。
|
|||
|
|
*/
|
|||
|
|
class Adc {
|
|||
|
|
public:
|
|||
|
|
/**
|
|||
|
|
* @brief ADC 采样结果结构体。
|
|||
|
|
* @note 保存外部 VIN_ADC、内部温度与内部参考电压的原始值,以及可直接使用的工程值。
|
|||
|
|
*/
|
|||
|
|
struct Sample {
|
|||
|
|
uint16_t vin_raw; /**< @brief 外部 VIN_ADC(PA6)原始 ADC 数值。 */
|
|||
|
|
uint16_t temp_raw; /**< @brief 内部温度通道原始 ADC 数值。 */
|
|||
|
|
uint16_t vref_raw; /**< @brief 内部参考电压通道原始 ADC 数值。 */
|
|||
|
|
float vin_adc_v; /**< @brief VIN_ADC 引脚处电压,单位 V(仅为分压后的 ADC 端电压)。 */
|
|||
|
|
float vdda; /**< @brief 根据内部参考电压换算得到的 MCU 供电电压,单位 V。 */
|
|||
|
|
float temperature; /**< @brief 根据温度通道换算得到的芯片温度,单位 ℃。 */
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
static void init();
|
|||
|
|
static bool dmaReady();
|
|||
|
|
static const uint16_t *dmaBuffer();
|
|||
|
|
static uint16_t readRaw(uint8_t channel);
|
|||
|
|
static uint16_t readVinRaw();
|
|||
|
|
static uint16_t readVrefRaw();
|
|||
|
|
static uint16_t readTempRaw();
|
|||
|
|
static float calculateVinAdcVoltage(uint16_t vinRaw, float vdda);
|
|||
|
|
static float calculateVdda(uint16_t vrefRaw);
|
|||
|
|
static float calculateTemperature(uint16_t tempRaw, float vdda);
|
|||
|
|
static void collectSample(Sample *sample);
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
static void initDma();
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
using adc_sample_t = Adc::Sample;
|
|||
|
|
|
|||
|
|
#endif /* DEVICE_ADC_H */
|