[GD32F310G-START Review] Review 3: GD32F301 Development Board ADC Module
[Copy link]
This post was last edited by cjwmusic on 2022-6-21 04:09
Overview
- Successive approximation method
- 12-bit resolution (configurable to 12, 10, 8, 6 bits)
- Watchdog detection
- 16 external channels, 2 internal channels, battery voltage channel
- Conversion mode: single, continuous, scan, intermittent
- Conversion result: 16 bits, left-aligned/right-aligned
- Hardware oversampling mechanism
experiment
In this ADC experiment, a 10K 3296 packaged sliding resistor is used to simulate the external, variable analog signal input, which is input into the analog input pin of GD32. After internal conversion, the converted digital result is displayed on the LCD.
Analog circuit schematic diagram
RV1 is a sliding resistor with a maximum resistance of 100K. Pin 2 outputs a variable voltage to the analog pin of the GD32 microcontroller.
R1 is a 10K resistor that serves as a current limiter.
For the convenience of the experiment, the circuit is relatively simple. The actual ADC sampling circuit also needs to add filtering and amplification circuits.
And the analog power supply should be separated from the digital power supply as much as possible. The same 3.3v power supply is used in this experiment.
Experimental circuit built on breadboard
Verification of experimental circuit
After completing the construction of the experimental circuit, while turning the adjustment knob of the sliding rheostat, use an oscilloscope to measure the voltage of pin 2 of the sliding rheostat. It is found that the voltage waveform gradually changes with the adjustment, indicating that there is no problem with the construction of the analog part of the circuit.
Software Writing
- ADC Clock Configuration
- ADC GPIO Configuration
- ADC Configuration
ADC Clock Configuration
From the clock tree in the user manual, we can see that the ADC clock can come from three sources:
- The APB2 clock is divided into
- The AHB clock is divided into
- IRC 28M clock frequency is obtained
Select the ADC clock source by setting the ADCSEL bit in the configuration register 2 (RCU_CFG2).
When using library functions for programming, configure through the rcu_adc_clock_config method.
The specific ADC clock configuration code is as follows:
// ADC 时钟使能
rcu_periph_clock_enable(RCU_ADC);
//ADC 时钟通过APB2时钟6分频获得
rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6);
GPIO Configuration
Correspondence between ADC external input channels and GPIO pins
ADC Pins |
GPIO Pins |
ADC_IN0 - ADC_IN7 |
PA0 - PA7 |
ADC_IN8 - ADC_IN9 |
PB0 - PB1 |
Since the PA pin is used to drive the LCD, the PB0 pin is used as the analog input in this experiment.
The ADC pin needs to be configured as input mode without pull-up or pull-down resistors.
The specific configuration code is as follows:
gpio_mode_set(GPIOB, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_0);
ADC Configuration
The configuration of ADC mainly includes:
Conversion Mode |
single |
Alignment of conversion results |
Right Align |
Number of conversion channels |
1 |
External trigger configuration |
No external trigger required |
ADC sampling accuracy |
12-bit sampling accuracy |
ADC sampling time configuration |
55.5 clock cycles |
ADC Interrupt Configuration |
Conversion completion triggers interrupt |
The specific code is as follows:
// 单通道用连续转换模式
adc_special_function_config(ADC_CONTINUOUS_MODE, ENABLE);
// 转换结果转换右对齐
adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
// 转换通道1个
adc_channel_length_config(ADC_REGULAR_CHANNEL, 1);
// 不用外部触发转换,软件开启即可
adc_external_trigger_config(ADC_REGULAR_CHANNEL, ENABLE);
adc_external_trigger_source_config(ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_NONE);
// ADC 采样精度配置
adc_resolution_config(ADC_RESOLUTION_12B);
// 使能ADC
adc_enable();
delay_1ms(1);
// 使能ADC校准
adc_calibration_enable();
// ADC 采样通道、采样时间
adc_regular_channel_config(0, ADC_CHANNEL_7, ADC_SAMPLETIME_55POINT5);
// 使能 ADC 中断
nvic_irq_enable(ADC_CMP_IRQn, 1, 1);
// 清除 ADC 规则组转换结束中断标志
adc_interrupt_flag_clear(ADC_INT_FLAG_EOC);
// 使能 ADC 规则组转换结束中断
adc_interrupt_enable(ADC_INT_EOC);
// 软件触发ADC转换
adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
Interrupt code:
void ADC_CMP_IRQHandler(void)
{
// 清除中断标志位
adc_interrupt_flag_clear(ADC_INT_FLAG_EOC);
// 读取转换结果
adcValue = adc_regular_data_read();
LCD_write_english_string(0, 10, "ADC Value:");
LCD_write_number(20, 20, adcValue);
}
Final experimental results
I uploaded all the codes to github, and you can download and use them if you are interested:
https://github.com/jwkongkong/GD32-NOKIA-5110
Welcome everyone to communicate and correct ~
|