#include "gd32f30x.h" #include "systick.h" #define TempADC ADC1 #define RCU_TempADC RCU_ADC1 float temperature; float vref_value; /*! \brief configure the different system clocks \param[in] none \param[out] none \retval none */ void temp_adc_rcu_config(void) { /* enable ADC clock */ rcu_periph_clock_enable(RCU_TempADC); /* config ADC clock */ rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV4); } /*! \brief configure the ADC peripheral \param[in] none \param[out] none \retval none */ void temp_adc_config(void) { /* ADC SCAN function enable */ adc_special_function_config(TempADC, ADC_SCAN_MODE, ENABLE); adc_special_function_config(TempADC, ADC_CONTINUOUS_MODE, DISABLE); /* ADC trigger config */ adc_external_trigger_source_config(TempADC, ADC_INSERTED_CHANNEL, ADC0_1_2_EXTTRIG_INSERTED_NONE); /* ADC mode config */ adc_mode_config(ADC_MODE_FREE); /* ADC data alignment config */ adc_data_alignment_config(TempADC, ADC_DATAALIGN_RIGHT); /* ADC channel length config */ adc_channel_length_config(TempADC, ADC_INSERTED_CHANNEL, 2); /* ADC temperature sensor channel config */ adc_inserted_channel_config(TempADC, 0, ADC_CHANNEL_16, ADC_SAMPLETIME_239POINT5); /* ADC internal reference voltage channel config */ adc_inserted_channel_config(TempADC, 1, ADC_CHANNEL_17, ADC_SAMPLETIME_239POINT5); adc_external_trigger_config(TempADC, ADC_INSERTED_CHANNEL, ENABLE); /* ADC temperature and Vrefint enable */ adc_tempsensor_vrefint_enable(); /* enable ADC interface */ adc_enable(TempADC); delay_1ms(1); /* ADC calibration and reset calibration */ adc_calibration_enable(TempADC); } void temperature_adc_config() { /* system clocks configuration */ temp_adc_rcu_config(); /* ADC configuration */ temp_adc_config(); } void get_adc_temperature() { /* ADC software trigger enable */ adc_software_trigger_enable(TempADC, ADC_INSERTED_CHANNEL); /* delay a time in milliseconds */ // delay_1ms(2000); /* value convert */ temperature = (1.43 - ADC_IDATA0(TempADC)*3.3/4096) * 1000 / 4.3 + 25; vref_value = (ADC_IDATA1(TempADC) * 3.3 / 4096); /* value print */ // printf(" the temperature data is %2.0f degrees Celsius\r\n", temperature); // printf(" the reference voltage data is %5.3fV \r\n", vref_value); // printf(" \r\n"); }