IoT_SCV_CH584M/bsp/src/bsp_adc.c

62 lines
1.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* bsp_adc.c
*
* Created on: 2024年11月29日
* Author: 123
*/
#include "bsp_adc.h"
#include "CONFIG.h"
#include "log.h"
signed short RoughCalib_Value = 0; // ADC粗调偏差值
static tmosTaskID vbat_task_id = INVALID_TASK_ID;
void VBAT_ADC_Init(void)
{
// 功能引脚数字输入禁用寄存器
R32_PIN_IN_DIS |= (1U << 4); // 禁用数字功能
//ADC初始化
/* 单通道采样选择adc通道0做采样对应 PA4引脚 带数据校准功能 */
GPIOA_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_Floating);
// 采样率最高8M
ADC_ExtSingleChSampInit(SampleFreq_8_or_4, ADC_PGA_1_2);
RoughCalib_Value = ADC_DataCalib_Rough(); // 用于计算ADC内部偏差记录到全局变量 RoughCalib_Value中
logDebug("RoughCalib_Value =%d \n", RoughCalib_Value);
ADC_ChannelCfg(0);
ADC_ExcutSingleConver();//时间足够时建议再次转换并丢弃首次ADC数据
DelayMs(10);
}
void ADC_GPIO_Init(void)
{
GPIOA_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_Floating);
// 确保ADC电源已启用
R8_ADC_CFG |= RB_ADC_POWER_ON;
}
uint16_t VBAT_ProcessEvent(uint8_t task_id, uint16_t events)
{
if (events & VBAT_EVT_START)
{
uint16_t adc_vbat = 0;
float vbat = 0;
ADC_GPIO_Init();
ADC_ChannelCfg(0);
adc_vbat = ADC_ExcutSingleConver() + RoughCalib_Value;
logDebug("adc_vbat =%d \n", adc_vbat);
vbat = (adc_vbat/1024.0-1)*1.05;
logDebug("vbat =%f \n", vbat);
tmos_start_task(vbat_task_id, VBAT_EVT_START, MS1_TO_SYSTEM_TIME(1000*10)); //1000*60
return (events ^ VBAT_EVT_START);
}
return 0;
}
void BSP_VBAT_Init(void)
{
VBAT_ADC_Init();
vbat_task_id = TMOS_ProcessEventRegister(VBAT_ProcessEvent);
tmos_set_event(vbat_task_id, VBAT_EVT_START);
}