- static void gpio_config(void)
- {
- gpio_init_type gpio_initstructure;
- crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
- crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
- gpio_default_para_init(&gpio_initstructure);
- /* config adc pin as analog input mode */
- gpio_initstructure.gpio_mode = GPIO_MODE_ANALOG;
- gpio_initstructure.gpio_pins = GPIO_PINS_4 | GPIO_PINS_5 | GPIO_PINS_6 | GPIO_PINS_7;
- gpio_init(GPIOA, &gpio_initstructure);
- gpio_initstructure.gpio_mode = GPIO_MODE_ANALOG;
- gpio_initstructure.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1;
- gpio_init(GPIOB, &gpio_initstructure);
- }
【ADC的配置】
首先使能ADC1的时钟源CRM_ADC1_PERIPH_CLOCK,选择时钟源为CRM_ADC_CLOCK_SOURCE_HCLK;分频为ADC_HCLK_DIV_4。使能DMA,转换结果右对齐,连续转换。配置6个通道。具体代码如下:
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] adc configuration.
- * @param none
- * @retval none
- */
- static void adc_config(void)
- {
- adc_common_config_type adc_common_struct;
- adc_base_config_type adc_base_struct;
- crm_periph_clock_enable(CRM_ADC1_PERIPH_CLOCK, TRUE);
- nvic_irq_enable(ADC1_IRQn, 0, 0);
- crm_adc_clock_select(CRM_ADC_CLOCK_SOURCE_HCLK);
-
- adc_common_default_para_init(&adc_common_struct);
- /* config division,adcclk is division by hclk */
- adc_common_struct.div = ADC_HCLK_DIV_4;
- /* config inner temperature sensor and vintrv */
- adc_common_struct.tempervintrv_state = FALSE;
- adc_common_config(&adc_common_struct);
- adc_base_default_para_init(&adc_base_struct);
- adc_base_struct.sequence_mode = TRUE;
- adc_base_struct.repeat_mode = FALSE;
- adc_base_struct.data_align = ADC_RIGHT_ALIGNMENT;
- adc_base_struct.ordinary_channel_length = 6;
- adc_base_config(ADC1, &adc_base_struct);
- adc_resolution_set(ADC1, ADC_RESOLUTION_12B);
- /* config ordinary channel */
- adc_ordinary_channel_set(ADC1, ADC_CHANNEL_4, 1, ADC_SAMPLETIME_6_5);
- adc_ordinary_channel_set(ADC1, ADC_CHANNEL_5, 2, ADC_SAMPLETIME_6_5);
- adc_ordinary_channel_set(ADC1, ADC_CHANNEL_6, 3, ADC_SAMPLETIME_6_5);
- adc_ordinary_channel_set(ADC1, ADC_CHANNEL_7, 4, ADC_SAMPLETIME_6_5);
- adc_ordinary_channel_set(ADC1, ADC_CHANNEL_8, 5, ADC_SAMPLETIME_6_5);
- adc_ordinary_channel_set(ADC1, ADC_CHANNEL_9, 6, ADC_SAMPLETIME_6_5);
- /* config ordinary trigger source and trigger edge */
- adc_ordinary_conversion_trigger_set(ADC1, ADC_ORDINARY_TRIG_TMR1CH1, ADC_ORDINARY_TRIG_EDGE_NONE);
- /* config dma mode,it's not useful when common dma mode is use */
- adc_dma_mode_enable(ADC1, TRUE);
- /* config dma request repeat,it's not useful when common dma mode is use */
- adc_dma_request_repeat_enable(ADC1, TRUE);
- /* enable adc overflow interrupt */
- adc_interrupt_enable(ADC1, ADC_OCCO_INT, TRUE);
- /* set oversampling ratio and shift */
- adc_oversample_ratio_shift_set(ADC1, ADC_OVERSAMPLE_RATIO_8, ADC_OVERSAMPLE_SHIFT_3);
- /* disable ordinary oversampling trigger mode */
- adc_ordinary_oversample_trig_enable(ADC1, FALSE);
- /* set ordinary oversample restart mode */
- adc_ordinary_oversample_restart_set(ADC1, ADC_OVERSAMPLE_CONTINUE);
- /* enable ordinary oversampling */
- adc_ordinary_oversample_enable(ADC1, TRUE);
- /* adc enable */
- adc_enable(ADC1, TRUE);
- while(adc_flag_get(ADC1, ADC_RDY_FLAG) == RESET);
- /* adc calibration */
- adc_calibration_init(ADC1);
- while(adc_calibration_init_status_get(ADC1));
- adc_calibration_start(ADC1);
- while(adc_calibration_status_get(ADC1));
- }
【DMA配置】
使用能dma通道1的时钟,使能DMA1的中断,设置dma的缓冲为30,即是6个通道接收5次数据。设置地址为结构体adc1_ordinary_valuetab的地址,使用DMA1中断,具体代码如下:
- /**
- * @brief dma configuration.
- * @param none
- * @retval none
- */
- static void dma_config(void)
- {
- dma_init_type dma_init_struct;
- crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE);
- nvic_irq_enable(DMA1_Channel1_IRQn, 0, 0);
- dma_reset(DMA1_CHANNEL1);
- dma_default_para_init(&dma_init_struct);
- dma_init_struct.buffer_size = 30;
- dma_init_struct.direction = DMA_DIR_PERIPHERAL_TO_MEMORY;
- dma_init_struct.memory_base_addr = (uint32_t)adc1_ordinary_valuetab;
- dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_HALFWORD;
- dma_init_struct.memory_inc_enable = TRUE;
- dma_init_struct.peripheral_base_addr = (uint32_t)&(ADC1->odt);
- dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_HALFWORD;
- dma_init_struct.peripheral_inc_enable = FALSE;
- dma_init_struct.priority = DMA_PRIORITY_HIGH;
- dma_init_struct.loop_mode_enable = TRUE;
- dma_init(DMA1_CHANNEL1, &dma_init_struct);
- dmamux_enable(DMA1, TRUE);
- dmamux_init(DMA1MUX_CHANNEL1, DMAMUX_DMAREQ_ID_ADC1);
- /* enable dma transfer complete interrupt */
- dma_interrupt_enable(DMA1_CHANNEL1, DMA_FDT_INT, TRUE);
- dma_channel_enable(DMA1_CHANNEL1, TRUE);
- }
at32f423_int.c中,更新两个标志,同时清除中断标志:
- /**
- * @brief this function handles dma1_channel1 handler.
- * @param none
- * @retval none
- */
- void DMA1_Channel1_IRQHandler(void)
- {
- if(dma_flag_get(DMA1_FDT1_FLAG) != RESET)
- {
- dma_flag_clear(DMA1_FDT1_FLAG);
- dma_trans_complete_flag = 1;
- }
- }
- /**
- * @brief this function handles adc1_2_3 handler.
- * @param none
- * @retval none
- */
- void ADC1_IRQHandler(void)
- {
- if(adc_flag_get(ADC1, ADC_OCCO_FLAG) != RESET)
- {
- adc_flag_clear(ADC1, ADC_OCCO_FLAG);
- adc1_overflow_flag++;
- }
- }
5、采集处理:转换5次后,把结构体中的数据取出来,并取平均值,然后显示到OLED上,代码如下:
- int main(void)
- {
- __IO uint32_t index = 0;
- uint32_t val[6]={0};
- uint8_t show_str[20] = {0};
- nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
- /* config the system clock */
- system_clock_config();
- /* init at start board */
- at32_board_init();
- at32_led_off(LED2);
- at32_led_off(LED3);
- at32_led_off(LED4);
- uart_print_init(115200);
- gpio_config();
- dma_config();
- adc_config();
- printf("tmr_trigger_automatic_preempt \r\n");
-
- at32_led_on(LED2);
- OLED_Init();
- OLED_Clear(1);
- GUI_ShowCHinese(24,16,16,"雅特力科技",0);
- GUI_ShowString(30,32,"AT32F423",16,1);
- OLED_Display();
- while(1)
- {
- /* adc1 software trigger start conversion */
- for(index = 0; index < 5; index++)
- {
- adc_ordinary_software_trigger_enable(ADC1, TRUE);
-
- delay_ms(100);
- }
- if((dma_trans_complete_flag == 0) || (adc1_overflow_flag != 0))
- {
- /* printf flag when error occur */
- at32_led_on(LED3);
- at32_led_on(LED4);
- printf("error occur\r\n");
- printf("adc1_overflow_flag = %d\r\n",adc1_overflow_flag);
- printf("dma_trans_complete_flag = %d\r\n",dma_trans_complete_flag);
- }
- else
- {
- /* printf data when conversion end without error */
- printf("conversion end without error\r\n");
- for(index = 0; index < 5; index++)
- {
- printf("adc1_ordinary_valuetab[%d][0] = 0x%x\r\n", index, adc1_ordinary_valuetab[index][0]);
- printf("adc1_ordinary_valuetab[%d][1] = 0x%x\r\n", index, adc1_ordinary_valuetab[index][1]);
- printf("adc1_ordinary_valuetab[%d][2] = 0x%x\r\n", index, adc1_ordinary_valuetab[index][2]);
- printf("adc1_ordinary_valuetab[%d][3] = 0x%x\r\n", index, adc1_ordinary_valuetab[index][3]);
- printf("adc1_ordinary_valuetab[%d][4] = 0x%x\r\n", index, adc1_ordinary_valuetab[index][4]);
- printf("adc1_ordinary_valuetab[%d][5] = 0x%x\r\n", index, adc1_ordinary_valuetab[index][5]);
- printf("\r\n");
- val[0] += adc1_ordinary_valuetab[index][0];
- val[1] += adc1_ordinary_valuetab[index][1];
- val[2] += adc1_ordinary_valuetab[index][2];
- val[3] += adc1_ordinary_valuetab[index][3];
- val[4] += adc1_ordinary_valuetab[index][4];
- val[5] += adc1_ordinary_valuetab[index][5];
- }
- }
- OLED_Clear(1);
- GUI_ShowCHinese(24,0,16,"雅特力科技",1);
- sprintf(show_str,"1:%1.2fV",((float)(val[0]/5) * 3.3)/4095);
- GUI_ShowString(0,16,show_str,16,0);
- sprintf(show_str,"2:%1.2fV",((float)(val[1]/5) * 3.3)/4095);
- GUI_ShowString(0,32,show_str,16,0);
- sprintf(show_str,"3:%1.2fV",((float)(val[2]/5) * 3.3)/4095);
- GUI_ShowString(0,48,show_str,16,0);
- sprintf(show_str,"4:%1.2fV",((float)(val[3]/5) * 3.3)/4095);
- GUI_ShowString(63,16,show_str,16,0);
- sprintf(show_str,"5:%1.2fV",((float)(val[4]/5) * 3.3)/4095);
- GUI_ShowString(63,32,show_str,16,0);
- sprintf(show_str,"6:%1.2fV",((float)(val[5]/5) * 3.3)/4095);
- GUI_ShowString(63,48,show_str,16,0);
- OLED_Display();
- val[0] = 0;
- val[1] = 0;
- val[2] = 0;
- val[3] = 0;
- val[4] = 0;
- val[5] = 0;
- dma_trans_complete_flag = 0;
-
- }
- }
【实验效果】
https://www.bilibili.com/video/BV1Dc41197Ak/?vd_source=e1bd226340c8b87027d5dcfc6b0c3344