GD32系列ADC单通道采样配置过程

时间:2025-01-15 11:49:47
  • //-------------------------------ADC sigle channel test ADC0 : A0 A1 OK --------------------------------------------------------------
  • void ADC0_Init_Concig(void)
  • {
  • rcu_periph_clock_enable(RCU_GPIOA);
  • rcu_periph_clock_enable(RCU_GPIOB);
  • rcu_periph_clock_enable(RCU_ADC0);
  • rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV6);
  • gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_0);
  • gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, GPIO_PIN_1);
  • adc_mode_config(ADC_MODE_FREE); //????
  • adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT); //???
  • adc_resolution_config(ADC0,ADC_RESOLUTION_12B); //12????
  • adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1U); //????
  • adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL,
  • ADC0_1_2_EXTTRIG_REGULAR_NONE); //????
  • adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE); //??????
  • adc_enable(ADC0);
  • adc_calibration_enable(ADC0); //????
  • }
  • uint16_t ADC0_Channel_Sample(uint8_t channel)
  • {
  • adc_regular_channel_config(ADC0, 0U, channel, ADC_SAMPLETIME_7POINT5); // ??DAC???????
  • adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL); //??????
  • while(!adc_flag_get(ADC0, ADC_FLAG_EOC)); // ??ADC????
  • adc_flag_clear(ADC0, ADC_FLAG_EOC); //?????????
  • return (adc_regular_data_read(ADC0)); //??ADC?
  • }
  • uint16_t Get_Adc_Average(uint8_t ch,uint8_t times)
  • {
  • uint32_t temp_val=0;
  • uint8_t t;
  • for(t=0;t<times;t++)
  • {
  • temp_val+=ADC0_Channel_Sample(ch);
  • }
  • return temp_val/times;
  • }
  • uint32_t adc_bat=0;
  • uint16_t vbat =0;
  • uint16_t Get_Voltage(uint8_t ch)
  • {
  • switch(ch)
  • {
  • case 0: //
  • adc_bat = Get_Adc_Average(0,4);
  • vbat = adc_bat*363/4096;
  • break;
  • case 1: //
  • adc_bat = Get_Adc_Average(1,4);
  • vbat = adc_bat*363/4096;
  • break;
  • default:
  • break;
  • }
  • return vbat;
  • }