gd32F303 adc多通道采集
时间: 2023-06-12 08:08:11 浏览: 166
GD32F30x_ADC_规则组查询法.rar_GD32 ADC程序_GD32F ADC_gd32 core_cminstr.
GD32F303芯片是一款具有多个ADC通道的微控制器,可以使用多种方式进行ADC多通道采集。以下是一个基本的ADC多通道采集代码示例,供您参考:
```c
#include "gd32f30x.h"
#include <stdio.h>
#define ADC_GPIO_PORT GPIOB
#define ADC_GPIO_PIN GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
uint16_t adc_value[4];
void adc_config(void)
{
rcu_periph_clock_enable(RCU_ADC0);
rcu_periph_clock_enable(RCU_GPIOB);
gpio_mode_set(ADC_GPIO_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, ADC_GPIO_PIN);
adc_sync_mode_config(ADC_SYNC_MODE_INDEPENDENT);
adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE);
adc_special_function_config(ADC0, ADC_DISCONTINUOUS_MODE, DISABLE);
adc_special_function_config(ADC0, ADC_DATA_ALIGN_RIGHT, ENABLE);
adc_special_function_config(ADC0, ADC_TSVREFE_VBAT_DISABLE, ENABLE);
adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC_EXTTRIG_INSERTED_NONE);
adc_external_trigger_config(ADC0, ADC_INSERTED_CHANNEL, ENABLE);
adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_0, ADC_SAMPLETIME_55POINT5);
adc_inserted_channel_config(ADC0, 2, ADC_CHANNEL_1, ADC_SAMPLETIME_55POINT5);
adc_inserted_channel_config(ADC0, 3, ADC_CHANNEL_2, ADC_SAMPLETIME_55POINT5);
adc_inserted_channel_config(ADC0, 4, ADC_CHANNEL_3, ADC_SAMPLETIME_55POINT5);
adc_mode_config(ADC_MODE_FREE);
adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_55POINT5);
adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_NONE);
adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
adc_enable(ADC0);
}
int main(void)
{
adc_config();
while(1)
{
if(adc_flag_get(ADC0, ADC_FLAG_INSERT_EOC))
{
adc_value[0] = adc_inserted_data_read(ADC0, ADC_INSERTED_CHANNEL_1);
adc_value[1] = adc_inserted_data_read(ADC0, ADC_INSERTED_CHANNEL_2);
adc_value[2] = adc_inserted_data_read(ADC0, ADC_INSERTED_CHANNEL_3);
adc_value[3] = adc_inserted_data_read(ADC0, ADC_INSERTED_CHANNEL_4);
adc_flag_clear(ADC0, ADC_FLAG_INSERT_EOC);
}
}
}
```
在此代码示例中,我们使用了ADC0,在PB0,PB1,PB2和PB3引脚上连接了4个ADC通道。我们使用了插入模式进行ADC采样,我们可以通过`adc_inserted_data_read()`函数来读取4个通道的值。我们还开启了ADC的外部触发功能,并使用了软件触发。您可以根据需要对此代码进行修改。
阅读全文