GD32E230 ADC注入通道
外部触发输入的上升沿可以触发规则组或注入组的转换。
用于 ADC 规则通道的 规则通道的 外部触发
,很多场景上我们可以使用外部触发读取ADC,比如控制无刷电机要采样电机每相的电流时就需要用到注入通道,在每个PWM的高电平期间读取电机ABC相电流。大概配置如下:
//各个时钟初始化,GPIO的初始化void rcu_config_inject(void){/* enable GPIOC clock */ rcu_periph_clock_enable(RCU_GPIOA); /* enable ADC clock */ rcu_periph_clock_enable(RCU_ADC); /* enable timer1 clock */ rcu_periph_clock_enable(RCU_TIMER2); /* config ADC clock */ rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6);}void gpio_config_inject(void){/* config the GPIO as analog mode */ gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);}//定时器初始化,做外部触发作用void timer_config_inject(void){timer_oc_parameter_struct timer_ocintpara; timer_parameter_struct timer_initpara; /* TIMER1 configuration */ timer_initpara.prescaler = 71; timer_initpara.alignedmode = TIMER_COUNTER_EDGE; timer_initpara.counterdirection = TIMER_COUNTER_UP; timer_initpara.period = 999U; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; timer_initpara.repetitioncounter = 0U; timer_init(TIMER2, &timer_initpara); /* CH0 configuration in PWM mode1 */ timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH; timer_ocintpara.outputstate = TIMER_CCX_ENABLE; timer_channel_output_config(TIMER2, TIMER_CH_3, &timer_ocintpara); timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_3, 1U); timer_channel_output_mode_config(TIMER2, TIMER_CH_3, TIMER_OC_MODE_PWM1); timer_channel_output_shadow_config(TIMER2, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);}//ADC注入通道的配置void adc_config_inject(void){/* ADC continous function enable */ adc_special_function_config(ADC_SCAN_MODE, ENABLE); /* ADC trigger config */ adc_external_trigger_source_config(ADC_INSERTED_CHANNEL, ADC_EXTTRIG_INSERTED_T2_CH3); /* ADC data alignment config */ adc_data_alignment_config(ADC_DATAALIGN_RIGHT); /* ADC channel length config */ adc_channel_length_config(ADC_INSERTED_CHANNEL, 4U); /* ADC inserted channel config */ adc_inserted_channel_config(0U, ADC_CHANNEL_0, ADC_SAMPLETIME_55POINT5); adc_inserted_channel_config(1U, ADC_CHANNEL_1, ADC_SAMPLETIME_55POINT5); adc_inserted_channel_config(2U, ADC_CHANNEL_2, ADC_SAMPLETIME_55POINT5); adc_inserted_channel_config(3U, ADC_CHANNEL_3, ADC_SAMPLETIME_55POINT5); /* ADC external trigger enable */ adc_external_trigger_config(ADC_INSERTED_CHANNEL, ENABLE); adc_interrupt_enable(ADC_INT_EOIC); /* enable ADC interface */ adc_enable(); delay_ms(1U); /* ADC calibration and reset calibration */ adc_calibration_enable(); nvic_irq_enable(ADC_CMP_IRQn, 1);}//ADC中断函数void ADC_CMP_IRQHandler(void){/* clear the ADC interrupt or status flag */ adc_interrupt_flag_clear(ADC_INT_EOIC); ADC_Value_Inject[0] = adc_inserted_data_read(ADC_INSERTED_CHANNEL_0); ADC_Value_Inject[1] = adc_inserted_data_read(ADC_INSERTED_CHANNEL_1); ADC_Value_Inject[2] = adc_inserted_data_read(ADC_INSERTED_CHANNEL_2); ADC_Value_Inject[3] = adc_inserted_data_read(ADC_INSERTED_CHANNEL_3);}//主函数配置void main(void){/* system clocks configuration */ rcu_config_inject(); /* GPIO configuration */ gpio_config_inject(); /* TIMER configuration */ timer_config_inject(); /* ADC configuration */ adc_config_inject(); /* enable TIMER2 */ timer_enable(TIMER2);}
目前这个例程做的是1ms触发ADC转换,转换完成后会触发ADC中断,在中断里面可直接读取ADC转换值。读取ADC也可节省mcu的效率
赞 (0)