本帖最后由 zhjb1 于 2024-11-18 08:45 编辑
问题如题。AT32F407VGT7的ADC实验中的问题
一、采用官网AT32F403A_407_Firmware_Library_V2.1.4工程测试ADC,里面分别由各种ADC的实例,有几个运行后printf有输出但数值***不变——八成不对,悬空数值应该乱蹦,手触应该数值变化,但数值***稳定。
二、采用官网AT32F403_Firmware_Library_V2.1.1工程测试ADC,里面的实例与“AT32F403A_407_Firmware_Library_V2.1.4”一样,结果也一样。
三、由ATWorkbench生成的工程,其中的ADC初始化代码:
void wk_adc1_init(void){
/* add user code begin adc1_init 0 */
/* add user code end adc1_init 0 */
gpio_init_type gpio_init_struct;
adc_base_config_type adc_base_struct;
gpio_default_para_init(&gpio_init_struct);
/* add user code begin adc1_init 1 */
/* add user code end adc1_init 1 */
/*gpio--------------------------------------------------------------------*/
/* configure the IN1 pin */
gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
gpio_init_struct.gpio_pins = ADC0_PIN;
gpio_init(ADC0_GPIO_PORT, &gpio_init_struct);
/* configure the IN2 pin */
gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
gpio_init_struct.gpio_pins = ADC1_PIN;
gpio_init(ADC1_GPIO_PORT, &gpio_init_struct);
/* configure the IN3 pin */
gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
gpio_init_struct.gpio_pins = ADC2_PIN;
gpio_init(ADC2_GPIO_PORT, &gpio_init_struct);
/* configure the IN6 pin */
gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
gpio_init_struct.gpio_pins = ADC3_PIN;
gpio_init(ADC3_GPIO_PORT, &gpio_init_struct);
/* configure the IN7 pin */
gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
gpio_init_struct.gpio_pins = ADC4_PIN;
gpio_init(ADC4_GPIO_PORT, &gpio_init_struct);
crm_adc_clock_div_set(CRM_ADC_DIV_6);
adc_tempersensor_vintrv_enable(TRUE);
/*adc_common_settings-------------------------------------------------------------*/
adc_combine_mode_select(ADC_INDEPENDENT_MODE);
/*adc_settings--------------------------------------------------------------------*/
adc_base_default_para_init(&adc_base_struct);
adc_base_struct.sequence_mode = TRUE;
adc_base_struct.repeat_mode = FALSE;//TRUE;
adc_base_struct.data_align = ADC_RIGHT_ALIGNMENT;
adc_base_struct.ordinary_channel_length = 5;
adc_base_config(ADC1, &adc_base_struct);
//adc_resolution_set(ADC1, ADC_RESOLUTION_12B);
/* adc_ordinary_conversionmode-------------------------------------------- */
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_1, 1, ADC_SAMPLETIME_1_5);
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_2, 2, ADC_SAMPLETIME_1_5);
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_3, 3, ADC_SAMPLETIME_1_5);
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_6, 4, ADC_SAMPLETIME_1_5);
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_16, 5, ADC_SAMPLETIME_1_5);
adc_ordinary_conversion_trigger_set(ADC1, ADC12_ORDINARY_TRIG_SOFTWARE, TRUE);
adc_ordinary_part_mode_enable(ADC1, FALSE);
adc_dma_mode_enable(ADC1, TRUE);
/**
* Users need to configure ADC1 interrupt functions according to the actual application.
* 1. Call the below function to enable the corresponding ADC1 interrupt.
* --adc_interrupt_enable(...)
* 2. Add the user's interrupt handler code into the below function in the at32f403a_407_int.c file.
* --void ADC1_2_IRQHandler(void)
*/
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));
adc_calibration_start(ADC1);
while(adc_calibration_status_get(ADC1));
/* add user code begin adc1_init 2 */
/* add user code end adc1_init 2 */
}
设置了ADC,通道1-3,6,7(代码里显示的是通道16-温度传感器),自己写了一个软件触发acdTest()代码:
void adcTest(void){
//int i;
/*adc_ordinary_software_trigger_enable(ADC1, TRUE);
adc_preempt_software_trigger_enable(ADC1, TRUE);
delayer(100);
if((dma_trans_complete_flag)||adc_calibration_status_get(ADC1)){//(adc1_overflow_flag != 1)){
adcx[0]=adc_preempt_conversion_data_get(ADC1,ADC_PREEMPT_CHANNEL_1);
adcx[1]=adc_preempt_conversion_data_get(ADC1,ADC_PREEMPT_CHANNEL_2);
adcx[2]=adc_preempt_conversion_data_get(ADC1,ADC_PREEMPT_CHANNEL_3);
adcx[3]=adc_preempt_conversion_data_get(ADC1,ADC_PREEMPT_CHANNEL_4);
//adcx[4]=adc_preempt_conversion_data_get(ADC1,ADC_PREEMPT_CHANNEL_7);
}*/
adc_calibration_init(ADC1);
adc_ordinary_part_mode_enable(ADC1, TRUE);
adc_ordinary_software_trigger_enable(ADC1, TRUE);
adc_enable(ADC1, TRUE);
adc_calibration_start(ADC1);
delayer(1000);
while(adc_calibration_status_get(ADC1));
//while(dma_trans_complete_flag);
//for(i=0;i<5;i++){adcx=ADC1ConvertedValue<<16;}
adcx[0]=adc_preempt_conversion_data_get(ADC1,ADC_PREEMPT_CHANNEL_1);
adcx[1]=adc_preempt_conversion_data_get(ADC1,ADC_PREEMPT_CHANNEL_2);
adcx[2]=adc_preempt_conversion_data_get(ADC1,ADC_PREEMPT_CHANNEL_3);
adcx[3]=adc_combine_ordinary_conversion_data_get();;//adc_preempt_conversion_data_get(ADC1,ADC_PREEMPT_CHANNEL_4);
//adcx[4]=adc_combine_ordinary_conversion_data_get();
}
代码中忽略了第5个通道数据,查看at32f403a_407_conf.c文档,里面所有的通道默认为4,结果可以扩充到最大,实验吗,出来数据再说,结果与官网的工程一样没有数据。
遍历at32f403a_407_conf.c由以下获得数据的方法:
adc_ordinary_conversion_data_get(adc_type *adc_x)
adc_combine_ordinary_conversion_data_get(void)
adc_preempt_conversion_data_get(adc_type *adc_x, adc_preempt_channel_type adc_preempt_channel)
对应这三种方法按照文头提示都不能成功获取数据,最好的结果是有一个可以获得一样的固定数值,与官工程结果一样,不是实际的结果。实际上本论坛里不下3篇以上关于ADC的文档或例程代码都试过,没有成功的。也许我的工程比较复杂,AWB生成后的结果是DMA 通道4_5,常规案例是DMA1 通道1.
码代码是无奈之举,看代码费力费神。
非常希望得到解答。
有答有会交流学习共同进步,用好国产芯片!
先谢谢了
|