今天在昨天的ADC最简配置例子上,进行DMA和多通道的采样配置。
下面例程均基于规则通道,注入通道与这个有些许差异,需自己查看手册配置。
单通道DMA软件触发采样配置:
- void gd_rvstar_adc_dma_init(void)
- {
- /* ADC_DMA_channel configuration */
- dma_parameter_struct dma_data_parameter;
- /* enable GPIOC clock */
- rcu_periph_clock_enable(RCU_GPIOC);
- /* enable ADC0 clock */
- rcu_periph_clock_enable(RCU_ADC0);
- /* enable DMA0 clock */
- rcu_periph_clock_enable(RCU_DMA0);
- /* config ADC clock */
- rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);
- //GPIO 配置
- /* config the GPIO as analog mode PC0 */
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
- //ADC配置
- /* reset ADC */
- adc_deinit(ADC0);
- /* ADC mode config 配置转换速度*/
- adc_mode_config(ADC0,ADC_MODE_FREE);
- /* ADC continous function disable 不允许连续转换 */
- adc_special_function_config(ADC0, ADC_SCAN_MODE, DISABLE);
- /* ADC data alignment config 数据对其格式*/
- adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
- /* ADC channel length config */
- adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1);
- /* ADC regular channel config 配置转换通道 */
- adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_10, ADC_SAMPLETIME_55POINT5);
- /* ADC trigger config 软件触发*/
- adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE);
- /* ADC external trigger enable */
- adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
- /* ADC discontinuous mode */
- adc_discontinuous_mode_config(ADC0, ADC_REGULAR_CHANNEL, 3);
- /* enable ADC interface */
- adc_enable(ADC0);
- delay_1ms(1);
- /* ADC calibration and reset calibration */
- adc_calibration_enable(ADC0);
- delay_1ms(1);
- /* ADC DMA function enable */
- adc_dma_mode_enable(ADC0);
- //DMA配置
- /* 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.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
- dma_data_parameter.memory_addr = (uint32_t)(&adc_value);
- dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
- //dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_DISABLE;
- dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
- dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;
- dma_data_parameter.direction = DMA_PERIPHERAL_TO_MEMORY;
- dma_data_parameter.number = 1;//仅开了一个通道
- dma_data_parameter.priority = DMA_PRIORITY_HIGH;
- dma_init(DMA0, DMA_CH0, &dma_data_parameter);
- dma_circulation_enable(DMA0, DMA_CH0);
- /* enable DMA channel */
- dma_channel_enable(DMA0, DMA_CH0);
- }
复制代码
main函数里面的修改:
gd_rvstar_adc_dma_init();
while 里面添加:
adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
//delay_1us(10);
printf("rn ADC0 regular channel 10 data = %d rn",adc_value);
delay_1ms(1000);
编译下载现象:
///////////////////////////////////////////////////////////////////////////
下面进行多通道DMA 软件触发 配置
引脚选用PC0~PC5共6个,与ardunio扩展引脚对应。
- void gd_rvstar_adc_group_dma_init(void)
- {
- /* ADC_DMA_channel configuration */
- dma_parameter_struct dma_data_parameter;
- /* enable GPIOC clock */
- rcu_periph_clock_enable(RCU_GPIOC);
- /* enable ADC0 clock */
- rcu_periph_clock_enable(RCU_ADC0);
- /* enable DMA0 clock */
- rcu_periph_clock_enable(RCU_DMA0);
- /* config ADC clock */
- rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);
- //GPIO 配置
- /* config the GPIO as analog mode PC0 */
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
- //ADC配置
- /* reset ADC */
- adc_deinit(ADC0);
- /* ADC mode config 配置转换速度*/
- adc_mode_config(ADC0,ADC_MODE_FREE);
- /* ADC continous function disable 不允许连续转换 */
- adc_special_function_config(ADC0, ADC_SCAN_MODE, DISABLE);
- /* ADC data alignment config 数据对其格式*/
- adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
- /* ADC channel length config */
- adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 6);
- /* ADC regular channel config 配置转换通道 */
- adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_10, ADC_SAMPLETIME_55POINT5);
- adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_11, ADC_SAMPLETIME_55POINT5);
- adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_12, ADC_SAMPLETIME_55POINT5);
- adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_13, ADC_SAMPLETIME_55POINT5);
- adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_14, ADC_SAMPLETIME_55POINT5);
- adc_regular_channel_config(ADC0, 5, ADC_CHANNEL_15, ADC_SAMPLETIME_55POINT5);
- /* ADC trigger config 软件触发*/
- adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE);
- /* ADC external trigger enable */
- adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
- /* ADC discontinuous mode */
- adc_discontinuous_mode_config(ADC0, ADC_REGULAR_CHANNEL, 3);
- /* enable ADC interface */
- adc_enable(ADC0);
- delay_1ms(1);
- /* ADC calibration and reset calibration */
- adc_calibration_enable(ADC0);
- delay_1ms(1);
- /* ADC DMA function enable */
- adc_dma_mode_enable(ADC0);
- //DMA配置
- /* 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.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
- dma_data_parameter.memory_addr = (uint32_t)(&adc_group_value);
- dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
- //dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_DISABLE;
- dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
- dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;
- dma_data_parameter.direction = DMA_PERIPHERAL_TO_MEMORY;
- dma_data_parameter.number = 6;//
- dma_data_parameter.priority = DMA_PRIORITY_HIGH;
- dma_init(DMA0, DMA_CH0, &dma_data_parameter);
- dma_circulation_enable(DMA0, DMA_CH0);
- /* enable DMA channel */
- dma_channel_enable(DMA0, DMA_CH0);
- }
复制代码
mian函数里面:
gd_rvstar_adc_dma_init();
while 里面修改:
- // ADC 规则组 软件触发 DMA
- adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
- //delay_1us(10);
- printf("rn ADC0 regular channel 10 data = %d rn",adc_group_value[0]);
- printf("rn ADC0 regular channel 11 data = %d rn",adc_group_value[1]);
- printf("rn ADC0 regular channel 12 data = %d rn",adc_group_value[2]);
- printf("rn ADC0 regular channel 13 data = %d rn",adc_group_value[3]);
- printf("rn ADC0 regular channel 14 data = %d rn",adc_group_value[4]);
- printf("rn ADC0 regular channel 15 data = %d rn",adc_group_value[5]);
- delay_1ms(1000);
复制代码
////////////////////////////////////////////////////////////////////////////////////////////
多通道DMA定时器PWM触发:
- void gd_rvstar_adc_group_dma_regular_timer1ch1_init(void)
- {
- timer_oc_parameter_struct timer_ocintpara;
- timer_parameter_struct timer_initpara;
- /* ADC_DMA_channel configuration */
- dma_parameter_struct dma_data_parameter;
- /* enable GPIOC clock */
- rcu_periph_clock_enable(RCU_GPIOC);
- /* enable ADC0 clock */
- rcu_periph_clock_enable(RCU_ADC0);
- /* enable timer1 clock */
- rcu_periph_clock_enable(RCU_TIMER1);
- /* enable DMA0 clock */
- rcu_periph_clock_enable(RCU_DMA0);
- /* config ADC clock */
- rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);
- //GPIO 配置
- /* config the GPIO as analog mode PC0 */
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
- gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
- //TIMER1_CH1 PWM设置
- timer_deinit(TIMER1);
- /* TIMER1 configuration */
- timer_initpara.prescaler = 5;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = 199;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER1,&timer_initpara);
- /* CH1 configuration in PWM mode1 */
- timer_channel_output_struct_para_init(&timer_ocintpara);
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_LOW;
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
- timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara);
- timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, 100);
- timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM1);
- timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER1);
- timer_enable(TIMER1);
- //ADC配置
- /* reset ADC */
- adc_deinit(ADC0);
- /* ADC mode config 配置转换速度*/
- adc_mode_config(ADC0,ADC_MODE_FREE);
- /* ADC contineous function enable */
- adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE);
- /* ADC continous function disable 不允许连续转换 */
- adc_special_function_config(ADC0, ADC_SCAN_MODE, DISABLE);
- /* ADC data alignment config 数据对其格式*/
- adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
- /* ADC channel length config */
- adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 6);
- /* ADC regular channel config 配置转换通道 */
- adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_10, ADC_SAMPLETIME_55POINT5);
- adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_11, ADC_SAMPLETIME_55POINT5);
- adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_12, ADC_SAMPLETIME_55POINT5);
- adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_13, ADC_SAMPLETIME_55POINT5);
- adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_14, ADC_SAMPLETIME_55POINT5);
- adc_regular_channel_config(ADC0, 5, ADC_CHANNEL_15, ADC_SAMPLETIME_55POINT5);
- /* ADC trigger config 软件触发*/
- adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_T1_CH1);
- /* ADC external trigger enable */
- adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
- /* ADC discontinuous mode */
- adc_discontinuous_mode_config(ADC0, ADC_REGULAR_CHANNEL, 3);
- /* enable ADC interface */
- adc_enable(ADC0);
- delay_1ms(1);
- /* ADC calibration and reset calibration */
- adc_calibration_enable(ADC0);
- delay_1ms(1);
- /* ADC DMA function enable */
- adc_dma_mode_enable(ADC0);
- //DMA配置
- /* 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.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
- dma_data_parameter.memory_addr = (uint32_t)(&adc_group_value);
- dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
- //dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_DISABLE;
- dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
- dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;
- dma_data_parameter.direction = DMA_PERIPHERAL_TO_MEMORY;
- dma_data_parameter.number = 6;//
- dma_data_parameter.priority = DMA_PRIORITY_HIGH;
- dma_init(DMA0, DMA_CH0, &dma_data_parameter);
- dma_circulation_enable(DMA0, DMA_CH0);
- /* enable DMA channel */
- dma_channel_enable(DMA0, DMA_CH0);
- }
复制代码
main函数里面:
gd_rvstar_adc_group_dma_regular_timer1ch1_init();
while 里面:
- //ADC 规则组 TI_C1触发 DMA
- while( !dma_flag_get(DMA0,DMA_CH0, DMA_FLAG_FTF));
- //clear channel1 transfer complete flag
- dma_flag_clear(DMA0,DMA_CH0, DMA_FLAG_FTF);
- printf("rn ADC0 regular channel 10 data = %d rn",adc_group_value[0]);
- printf("rn ADC0 regular channel 11 data = %d rn",adc_group_value[1]);
- printf("rn ADC0 regular channel 12 data = %d rn",adc_group_value[2]);
- printf("rn ADC0 regular channel 13 data = %d rn",adc_group_value[3]);
- printf("rn ADC0 regular channel 14 data = %d rn",adc_group_value[4]);
- printf("rn ADC0 regular channel 15 data = %d rn",adc_group_value[5]);
- delay_1ms(1000);
复制代码
上述两种多通道,输出现象:
|