ADC单次转换例程:
#include "gd32f1x0.h"
#include <stdio.h>
#include "gd32f1x0_adc.h"
#define BOARD_ADC_CHANNEL ADC_CHANNEL_6
#define ADC_GPIO_PORT GPIOA
#define ADC_GPIO_PIN GPIO_PIN_6
uint16_t adc_value;
/**************************************************************
//将IO口配置成模拟输入口
***************************************************************/
void gpio_config1(void)
{
/* config the GPIO as analog mode */
gpio_mode_set(ADC_GPIO_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, ADC_GPIO_PIN);
}
/**************************************************************
//时钟配置
***************************************************************/
void rcu_config(void)
{
/* enable GPIOC clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* enable ADC clock */
rcu_periph_clock_enable(RCU_ADC);
/* config ADC clock */
rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6);
}
/**************************************************************
//时钟配置
***************************************************************/
void adc_config(void)
{
/* ADC channel length config */
adc_channel_length_config(ADC_REGULAR_CHANNEL,1);
/* ADC regular channel config */
adc_regular_channel_config(0,BOARD_ADC_CHANNEL,ADC_SAMPLETIME_239POINT5);
/* ADC external trigger enable */
adc_external_trigger_config(ADC_REGULAR_CHANNEL,ENABLE);
/* ADC external trigger source config */
adc_external_trigger_source_config(ADC_REGULAR_CHANNEL,ADC_EXTTRIG_REGULAR_SWRCST);
/* ADC data alignment config */
adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
/* enable ADC interface */
adc_enable();
/* ADC calibration and reset calibration */
adc_calibration_enable();
/* ADC software trigger enable */
adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
}
/**************************************************************
//主程序
***************************************************************/
int main(void)
{
/* system clocks configuration */
rcu_config();
/* GPIO configuration */
gpio_config1();
/* ADC configuration */
adc_config();
while(1){
adc_flag_clear(ADC_FLAG_EOC);
while(SET != adc_flag_get(ADC_FLAG_EOC)){
}
adc_value = ADC_RDATA;
adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
}
}