You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
#include "adc.h"
static float prv_adc_get_value(); // uint16_t meanvalue;
ADC_InitStruType x; GPIO_InitSettingType y; bool adccapture_error = false; static void prv_adc_pa2_init() { y.Signal = GPIO_Pin_Signal_Analog; //模拟
y.Dir = GPIO_Direction_Input; //输入
y.Func = GPIO_Reuse_Func0; // x.CHS设置adc采集的通道每个引脚对应一个通道
GPIO_Init(GPIO_Pin_A2, &y);
//经过分频以后Tadclk=1/(PCLK/4)约等于0.083us
x.CLKS = ADC_CLKS_PCLK; x.CLKDIV = ADC_CLKDIV_1_32; /* ADC时钟源预分频 */
//采集到的模拟量*3.3/4096=现在的电压
x.VREF_SEL = ADC_VREF_SEL_0; /* 内部参考电压2.048v,仅设置内部参考电压为多少 */ x.VREFP = ADC_VREFP_VDD; /* 选择芯片的工作电压VDD,这个是设置adc具体是要哪个电压来做参考adc的参考电压为多少 */ x.VREFN = ADC_VREFN_VSS; /* 负向参考电压选择 */ x.CHS = ADC_CHS_AIN8; x.SMPS = ADC_SMPS_SOFT; /* AD采样模式为软件控制 */ //采样时间st*2+1(个Tadclk)=1.743us
x.ST = 10; /* AD硬件采样时间选择 */ x.BITSEL = ADC_BITSEL_12; /* AD分辨率12位 */ ADC_Init(&x); prv_adc_get_value(); } static void prv_adc_pb8_init() { y.Signal = GPIO_Pin_Signal_Analog; //模拟
y.Dir = GPIO_Direction_Input; //输入
y.Func = GPIO_Reuse_Func0; // x.CHS设置adc采集的通道每个引脚对应一个通道
GPIO_Init(GPIO_Pin_B8, &y);
//经过分频以后Tadclk=1/(PCLK/4)约等于0.083us
x.CLKS = ADC_CLKS_PCLK; x.CLKDIV = ADC_CLKDIV_1_32; /* ADC时钟源预分频 */
//采集到的模拟量*3.3/4096=现在的电压
x.VREF_SEL = ADC_VREF_SEL_0; /* 内部参考电压2.048v,仅设置内部参考电压为多少 */ x.VREFP = ADC_VREFP_VDD; /* 选择芯片的工作电压VDD,这个是设置adc具体是要哪个电压来做参考adc的参考电压为多少 */ x.VREFN = ADC_VREFN_VSS; /* 负向参考电压选择 */ x.CHS = ADC_CHS_AIN3; x.SMPS = ADC_SMPS_SOFT; /* AD采样模式为软件控制 */ //采样时间st*2+1(个Tadclk)=1.743us
x.ST = 10; /* AD硬件采样时间选择 */ x.BITSEL = ADC_BITSEL_12; /* AD分辨率12位 */ ADC_Init(&x); prv_adc_get_value(); }
static void prv_adc_pb9_init() { y.Signal = GPIO_Pin_Signal_Analog; //模拟
y.Dir = GPIO_Direction_Input; //输入
y.Func = GPIO_Reuse_Func0; // x.CHS设置adc采集的通道每个引脚对应一个通道
GPIO_Init(GPIO_Pin_B9, &y);
//经过分频以后Tadclk=1/(PCLK/32)
x.CLKS = ADC_CLKS_PCLK; x.CLKDIV = ADC_CLKDIV_1_32; /* ADC时钟源预分频 */
//采集到的模拟量*3.3/4096=现在的电压
x.VREF_SEL = ADC_VREF_SEL_0; /* 内部参考电压2.048v,仅设置内部参考电压为多少 */ x.VREFP = ADC_VREFP_VDD; /* 选择芯片的工作电压VDD,这个是设置adc具体是要哪个电压来做参考adc的参考电压为多少 */ x.VREFN = ADC_VREFN_VSS; /* 负向参考电压选择 */ x.CHS = ADC_CHS_AIN4; // x.CHS = ADC_CHS_AIN8;
x.SMPS = ADC_SMPS_SOFT; /* AD采样模式为软件控制 */ //采样时间st*2+1(个Tadclk)=1.743us
x.ST = 10; /* AD硬件采样时间选择 */ x.BITSEL = ADC_BITSEL_12; /* AD分辨率12位 */ ADC_Init(&x); prv_adc_get_value(); }
static float prv_adc_get_value() { ErrorStatus status = ADC_SoftStart(); adccapture_error = false; if (status != SUCCESS) { adccapture_error = true; return 0; } uint32_t inticket = get_sys_ticket();
while (ADC_GetConvStatus() != RESET) { if (port_haspassedms(inticket) > 10) { // printf("prv_adc_get_value ch:%d\n", x.CHS);
// printf("fail\n", x.CHS);
adccapture_error = true; return 0; } } // printf("success %d\n", port_haspassedms(inticket));
uint16_t adcv = ADC_GetConvValue(); ADC_SoftStop(); // printf("success\n", x.CHS);
// printf("%d\r\n", adcv);
return adcv * 3.3 / 4096; }
static float prv_adc_get_value_average(int average) { if (average == 0) { average = 1; } float value = 0; for (size_t i = 0; i < average; i++) { value += prv_adc_get_value(); } return value / average; }
float adc_get_value_pa2(void) { prv_adc_pa2_init(); return prv_adc_get_value_average(AVERAGE_TABLE_SIZE); } float adc_get_value_pb8(void) { prv_adc_pb8_init(); return prv_adc_get_value_average(AVERAGE_TABLE_SIZE); } float adc_get_value_pb9(void) { prv_adc_pb9_init(); return prv_adc_get_value_average(AVERAGE_TABLE_SIZE); }
float adc_get_value_pa2_filter(float factor) { static float last = 0; float now_raw = adc_get_value_pa2(); if (adccapture_error) { return last; } float now = ((float)last * (1.0f - factor)) + ((float)now_raw * factor); last = now; return now; } float adc_get_value_pb8_filter(float factor) { static float last = 0; float now_raw = adc_get_value_pb8(); if (adccapture_error) { return last; } float now = ((float)last * (1.0f - factor)) + ((float)now_raw * factor); last = now;
return now; } float adc_get_value_pb9_filter(float factor) { static float last = 0; float now_raw = adc_get_value_pb9(); if (adccapture_error) { return last; } float now = ((float)last * (1.0f - factor)) + ((float)now_raw * factor); last = now; return now; }
|