发 帖  
原厂入驻New
[经验]

【"RV-STAR 开发板免费试用"免费试用】+ADC单通道、多通道 规则组 DMA 采样

2020-10-16 10:56:19  205 开发板 ADC
分享
0
今天在昨天的ADC最简配置例子上,进行DMA和多通道的采样配置。
下面例程均基于规则通道,注入通道与这个有些许差异,需自己查看手册配置。

单通道DMA软件触发采样配置:
  1. void gd_rvstar_adc_dma_init(void)
  2. {
  3.         /* ADC_DMA_channel configuration */
  4.         dma_parameter_struct dma_data_parameter;

  5.         /* enable GPIOC clock */
  6.         rcu_periph_clock_enable(RCU_GPIOC);
  7.         /* enable ADC0 clock */
  8.         rcu_periph_clock_enable(RCU_ADC0);
  9.         /* enable DMA0 clock */
  10.         rcu_periph_clock_enable(RCU_DMA0);
  11.         /* config ADC clock */
  12.         rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);

  13.     //GPIO 配置
  14.         /* config the GPIO as analog mode PC0 */
  15.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0);

  16.         //ADC配置
  17.         /* reset ADC */
  18.         adc_deinit(ADC0);

  19.         /* ADC mode config  配置转换速度*/
  20.         adc_mode_config(ADC0,ADC_MODE_FREE);
  21.         /* ADC continous function disable  不允许连续转换 */
  22.         adc_special_function_config(ADC0, ADC_SCAN_MODE, DISABLE);
  23.         /* ADC data alignment config 数据对其格式*/
  24.         adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
  25.         /* ADC channel length config */
  26.         adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1);

  27.         /* ADC regular channel config 配置转换通道 */
  28.         adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_10, ADC_SAMPLETIME_55POINT5);

  29.         /* ADC trigger config 软件触发*/
  30.         adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE);
  31.         /* ADC external trigger enable */
  32.         adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);

  33.         /* ADC discontinuous mode */
  34.         adc_discontinuous_mode_config(ADC0, ADC_REGULAR_CHANNEL, 3);

  35.         /* enable ADC inteRFace */
  36.         adc_enable(ADC0);
  37.         delay_1ms(1);
  38.         /* ADC calibration and reset calibration */
  39.         adc_calibration_enable(ADC0);
  40.         delay_1ms(1);

  41.         /* ADC DMA function enable */
  42.         adc_dma_mode_enable(ADC0);

  43.         //DMA配置
  44.          /* ADC DMA_channel configuration */
  45.         dma_deinit(DMA0, DMA_CH0);

  46.         /* initialize DMA single data mode */
  47.         dma_data_parameter.periph_addr  = (uint32_t)(&ADC_RDATA(ADC0));
  48.         dma_data_parameter.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;
  49.         dma_data_parameter.memory_addr  = (uint32_t)(&adc_value);
  50.         dma_data_parameter.memory_inc   = DMA_MEMORY_INCREASE_ENABLE;
  51.         //dma_data_parameter.memory_inc   = DMA_MEMORY_INCREASE_DISABLE;

  52.         dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  53.         dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;
  54.         dma_data_parameter.direction    = DMA_PERIPHERAL_TO_MEMORY;
  55.         dma_data_parameter.number       = 1;//仅开了一个通道
  56.         dma_data_parameter.priority     = DMA_PRIORITY_HIGH;
  57.         dma_init(DMA0, DMA_CH0, &dma_data_parameter);

  58.         dma_circulation_enable(DMA0, DMA_CH0);

  59.         /* enable DMA channel */
  60.         dma_channel_enable(DMA0, DMA_CH0);
  61. }
复制代码
main函数里面的修改:

gd_rvstar_adc_dma_init();
while 里面添加:
adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
//delay_1us(10);
printf("\r\n ADC0 regular channel 10 data = %d \r\n",adc_value);
delay_1ms(1000);
编译下载现象:
ADC5.png
///////////////////////////////////////////////////////////////////////////
下面进行多通道DMA   软件触发 配置
引脚选用PC0~PC5共6个,与ardunio扩展引脚对应。
PC0_3.png
PC4_5.png

  1. void gd_rvstar_adc_group_dma_init(void)
  2. {
  3.         /* ADC_DMA_channel configuration */
  4.         dma_parameter_struct dma_data_parameter;

  5.         /* enable GPIOC clock */
  6.         rcu_periph_clock_enable(RCU_GPIOC);
  7.         /* enable ADC0 clock */
  8.         rcu_periph_clock_enable(RCU_ADC0);
  9.         /* enable DMA0 clock */
  10.         rcu_periph_clock_enable(RCU_DMA0);
  11.         /* config ADC clock */
  12.         rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);

  13.     //GPIO 配置
  14.         /* config the GPIO as analog mode PC0 */
  15.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
  16.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
  17.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
  18.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
  19.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
  20.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_5);

  21.         //ADC配置
  22.         /* reset ADC */
  23.         adc_deinit(ADC0);

  24.         /* ADC mode config  配置转换速度*/
  25.         adc_mode_config(ADC0,ADC_MODE_FREE);
  26.         /* ADC continous function disable  不允许连续转换 */
  27.         adc_special_function_config(ADC0, ADC_SCAN_MODE, DISABLE);
  28.         /* ADC data alignment config 数据对其格式*/
  29.         adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
  30.         /* ADC channel length config */
  31.         adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 6);

  32.         /* ADC regular channel config 配置转换通道 */
  33.         adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_10, ADC_SAMPLETIME_55POINT5);
  34.         adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_11, ADC_SAMPLETIME_55POINT5);
  35.         adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_12, ADC_SAMPLETIME_55POINT5);
  36.         adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_13, ADC_SAMPLETIME_55POINT5);
  37.         adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_14, ADC_SAMPLETIME_55POINT5);
  38.         adc_regular_channel_config(ADC0, 5, ADC_CHANNEL_15, ADC_SAMPLETIME_55POINT5);

  39.         /* ADC trigger config 软件触发*/
  40.         adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE);
  41.         /* ADC external trigger enable */
  42.         adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);

  43.         /* ADC discontinuous mode */
  44.         adc_discontinuous_mode_config(ADC0, ADC_REGULAR_CHANNEL, 3);

  45.         /* enable ADC interface */
  46.         adc_enable(ADC0);
  47.         delay_1ms(1);
  48.         /* ADC calibration and reset calibration */
  49.         adc_calibration_enable(ADC0);
  50.         delay_1ms(1);

  51.         /* ADC DMA function enable */
  52.         adc_dma_mode_enable(ADC0);

  53.         //DMA配置
  54.          /* ADC DMA_channel configuration */
  55.         dma_deinit(DMA0, DMA_CH0);

  56.         /* initialize DMA single data mode */
  57.         dma_data_parameter.periph_addr  = (uint32_t)(&ADC_RDATA(ADC0));
  58.         dma_data_parameter.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;
  59.         dma_data_parameter.memory_addr  = (uint32_t)(&adc_group_value);
  60.         dma_data_parameter.memory_inc   = DMA_MEMORY_INCREASE_ENABLE;
  61.         //dma_data_parameter.memory_inc   = DMA_MEMORY_INCREASE_DISABLE;

  62.         dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  63.         dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;
  64.         dma_data_parameter.direction    = DMA_PERIPHERAL_TO_MEMORY;
  65.         dma_data_parameter.number       = 6;//
  66.         dma_data_parameter.priority     = DMA_PRIORITY_HIGH;
  67.         dma_init(DMA0, DMA_CH0, &dma_data_parameter);

  68.         dma_circulation_enable(DMA0, DMA_CH0);

  69.         /* enable DMA channel */
  70.         dma_channel_enable(DMA0, DMA_CH0);
  71. }
复制代码

mian函数里面:

gd_rvstar_adc_dma_init();
while 里面修改:
  1. // ADC 规则组 软件触发 DMA
  2.             adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
  3.             //delay_1us(10);
  4.             printf("\r\n ADC0 regular channel 10 data = %d \r\n",adc_group_value[0]);
  5.                 printf("\r\n ADC0 regular channel 11 data = %d \r\n",adc_group_value[1]);
  6.                 printf("\r\n ADC0 regular channel 12 data = %d \r\n",adc_group_value[2]);
  7.                 printf("\r\n ADC0 regular channel 13 data = %d \r\n",adc_group_value[3]);
  8.                 printf("\r\n ADC0 regular channel 14 data = %d \r\n",adc_group_value[4]);
  9.                 printf("\r\n ADC0 regular channel 15 data = %d \r\n",adc_group_value[5]);
  10.                 delay_1ms(1000);
复制代码
////////////////////////////////////////////////////////////////////////////////////////////
多通道DMA定时器PWM触发:
  1. void gd_rvstar_adc_group_dma_regular_timer1ch1_init(void)
  2. {
  3.         timer_oc_parameter_struct timer_ocintpara;
  4.         timer_parameter_struct timer_initpara;
  5.         /* ADC_DMA_channel configuration */
  6.         dma_parameter_struct dma_data_parameter;

  7.         /* enable GPIOC clock */
  8.         rcu_periph_clock_enable(RCU_GPIOC);
  9.         /* enable ADC0 clock */
  10.         rcu_periph_clock_enable(RCU_ADC0);
  11.         /* enable timer1 clock */
  12.         rcu_periph_clock_enable(RCU_TIMER1);
  13.         /* enable DMA0 clock */
  14.         rcu_periph_clock_enable(RCU_DMA0);
  15.         /* config ADC clock */
  16.         rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);

  17.         //GPIO 配置
  18.         /* config the GPIO as analog mode PC0 */
  19.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
  20.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
  21.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
  22.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
  23.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
  24.         gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_5);

  25.         //TIMER1_CH1 PWM设置
  26.         timer_deinit(TIMER1);

  27.         /* TIMER1 configuration */
  28.         timer_initpara.prescaler         = 5;
  29.         timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
  30.         timer_initpara.counterdirection  = TIMER_COUNTER_UP;
  31.         timer_initpara.period            = 199;
  32.         timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
  33.         timer_initpara.repetitioncounter = 0;
  34.         timer_init(TIMER1,&timer_initpara);

  35.         /* CH1 configuration in PWM mode1 */
  36.         timer_channel_output_struct_para_init(&timer_ocintpara);
  37.         timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_LOW;
  38.         timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  39.         timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara);

  40.         timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, 100);
  41.         timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM1);
  42.         timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);

  43.         /* auto-reload preload enable */
  44.         timer_auto_reload_shadow_enable(TIMER1);

  45.         timer_enable(TIMER1);
  46.         //ADC配置
  47.         /* reset ADC */
  48.         adc_deinit(ADC0);

  49.         /* ADC mode config  配置转换速度*/
  50.         adc_mode_config(ADC0,ADC_MODE_FREE);

  51.         /* ADC contineous function enable */
  52.         adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE);

  53.         /* ADC continous function disable  不允许连续转换 */
  54.         adc_special_function_config(ADC0, ADC_SCAN_MODE, DISABLE);
  55.         /* ADC data alignment config 数据对其格式*/
  56.         adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
  57.         /* ADC channel length config */
  58.         adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 6);

  59.         /* ADC regular channel config 配置转换通道 */
  60.         adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_10, ADC_SAMPLETIME_55POINT5);
  61.         adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_11, ADC_SAMPLETIME_55POINT5);
  62.         adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_12, ADC_SAMPLETIME_55POINT5);
  63.         adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_13, ADC_SAMPLETIME_55POINT5);
  64.         adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_14, ADC_SAMPLETIME_55POINT5);
  65.         adc_regular_channel_config(ADC0, 5, ADC_CHANNEL_15, ADC_SAMPLETIME_55POINT5);

  66.         /* ADC trigger config 软件触发*/
  67.         adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_T1_CH1);
  68.         /* ADC external trigger enable */
  69.         adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);

  70.         /* ADC discontinuous mode */
  71.         adc_discontinuous_mode_config(ADC0, ADC_REGULAR_CHANNEL, 3);

  72.         /* enable ADC interface */
  73.         adc_enable(ADC0);
  74.         delay_1ms(1);
  75.         /* ADC calibration and reset calibration */
  76.         adc_calibration_enable(ADC0);
  77.         delay_1ms(1);

  78.         /* ADC DMA function enable */
  79.         adc_dma_mode_enable(ADC0);

  80.         //DMA配置
  81.          /* ADC DMA_channel configuration */
  82.         dma_deinit(DMA0, DMA_CH0);

  83.         /* initialize DMA single data mode */
  84.         dma_data_parameter.periph_addr  = (uint32_t)(&ADC_RDATA(ADC0));
  85.         dma_data_parameter.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;
  86.         dma_data_parameter.memory_addr  = (uint32_t)(&adc_group_value);
  87.         dma_data_parameter.memory_inc   = DMA_MEMORY_INCREASE_ENABLE;
  88.         //dma_data_parameter.memory_inc   = DMA_MEMORY_INCREASE_DISABLE;

  89.         dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  90.         dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;
  91.         dma_data_parameter.direction    = DMA_PERIPHERAL_TO_MEMORY;
  92.         dma_data_parameter.number       = 6;//
  93.         dma_data_parameter.priority     = DMA_PRIORITY_HIGH;
  94.         dma_init(DMA0, DMA_CH0, &dma_data_parameter);

  95.         dma_circulation_enable(DMA0, DMA_CH0);

  96.         /* enable DMA channel */
  97.         dma_channel_enable(DMA0, DMA_CH0);
  98. }
复制代码
main函数里面:
gd_rvstar_adc_group_dma_regular_timer1ch1_init();
while 里面:
  1. //ADC 规则组 TI_C1触发 DMA
  2.                 while( !dma_flag_get(DMA0,DMA_CH0, DMA_FLAG_FTF));
  3.                 //clear channel1 transfer complete flag
  4.                 dma_flag_clear(DMA0,DMA_CH0, DMA_FLAG_FTF);

  5.             printf("\r\n ADC0 regular channel 10 data = %d \r\n",adc_group_value[0]);
  6.                 printf("\r\n ADC0 regular channel 11 data = %d \r\n",adc_group_value[1]);
  7.                 printf("\r\n ADC0 regular channel 12 data = %d \r\n",adc_group_value[2]);
  8.                 printf("\r\n ADC0 regular channel 13 data = %d \r\n",adc_group_value[3]);
  9.                 printf("\r\n ADC0 regular channel 14 data = %d \r\n",adc_group_value[4]);
  10.                 printf("\r\n ADC0 regular channel 15 data = %d \r\n",adc_group_value[5]);
  11.                 delay_1ms(1000);
复制代码
上述两种多通道,输出现象:
adc6.png








相关经验

评论

高级模式
您需要登录后才可以回帖 登录 | 注册

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
发经验
关闭

站长推荐 上一条 /9 下一条

快速回复 返回顶部 返回列表