本帖最后由 luanzao 于 2021-4-3 18:01 编辑
问题描述:
1、ADC DMA转换不成功,得到的数据如下图:
实际应该是正弦波。
2、PA3采集电压时,单独设置如下模式可以用,但并到下面的初始化代码中就不能用了。
adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1);
adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE);
预期结果:PA1为uint16_t adc_buff[1740]的ADC采集通道采样的正弦波数据,循环模式12位ADC采集,需要使用DMA转换;PA3为采集外部电压,扫描模式,用的时候转换一下。
初始化代码如下:
void ADC_Config(void)
{
/* enable GPIOA clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* enable alternate function clock */
rcu_periph_clock_enable(RCU_AF);
/* enable ADC0 clock */
rcu_periph_clock_enable(RCU_ADC0);
/* config ADC clock */
rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV2);
/* enable DMA0 clock */
rcu_periph_clock_enable(RCU_DMA0);
/* config the GPIO as analog mode, for ADC */
gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
/* reset ADC */
adc_deinit(ADC0);
/* ADC mode config */
adc_mode_config(ADC_MODE_FREE);
/* ADC continous function enable */
adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
/* ADC data alignment config */
adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
/* ADC resolution 12B */
adc_resolution_config(ADC0,ADC_RESOLUTION_12B);
/* ADC channel length config */
adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 2);
/* ADC regular channel config */
adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_1, ADC_SAMPLETIME_7POINT5);
/* ADC trigger config */
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 DMA function enable */
adc_dma_mode_enable(ADC0);
/* enable ADC interface */
adc_enable(ADC0);
DelayMs(2);
/* ADC calibration and reset calibration */
adc_calibration_enable(ADC0);
}
void DMA_ADC0_Configuration(void)
{
/* ADC_DMA_channel configuration */
dma_parameter_struct dma_data_parameter;
/* ADC DMA_channel configuration */
dma_deinit(DMA0, DMA_CH0);
/* initialize DMA single data mode */
dma_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
dma_data_parameter.memory_addr = (uint32_t)(&adc_buff);
dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_8BIT;
dma_data_parameter.direction = DMA_PERIPHERAL_TO_MEMORY;
dma_data_parameter.number = DATA_LEN;
dma_data_parameter.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_init(DMA0, DMA_CH0, &dma_data_parameter);
/* enable DMA circulation mode */
dma_circulation_enable(DMA0, DMA_CH0);
dma_memory_to_memory_disable(DMA0, DMA_CH0);
/* enable DMA channel */
dma_channel_enable(DMA0, DMA_CH0);
}
uint32_t Adc_read_value(uint32_t adc_channel)//主要用于读取ADC通道3的 电压值
{
uint32_t ADC1ConvertedValue;
/* ADC regular channel config */
adc_regular_channel_config(ADC0, 0, adc_channel, ADC_SAMPLETIME_7POINT5);
adc_software_trigger_enable(ADC0,ADC_REGULAR_CHANNEL);
adc_flag_get(ADC0, ADC_FLAG_EOC);
ADC1ConvertedValue=adc_regular_data_read(ADC0);
adc_flag_clear(ADC0, ADC_FLAG_EOC);
// adc_software_trigger_enable(ADC0,ADC_INSERTED_CHANNEL);
//
// /* Get ADC1 converted data */
// ADC1ConvertedValue = ADC_IDATA0(ADC0);
return ADC1ConvertedValue;
}
主函数:
ADC_Config();
DMA_ADC0_Configuration();//配置ADC的DMA传输
adc_software_trigger_enable(ADC0,ADC_REGULAR_CHANNEL);//触发ADC转换
DelayMs(18);//18MS后,采集完成
|
|