本帖最后由 zhjb1 于 2024-11-13 17:42 编辑
用Work_Bench创建了一个AT的工程,在做实验室能遇到一些问题,以后慢慢提问并解决。如果解决了,会将代码放上一起学习。二、多路ADC采样
在AT_WORK_BENCH中设置了ADC1的IN1,IN2,IN3,IN6,IN7,其中IN7连接内部温度传感器。
AWB生成的代码看着挺好也非常漂亮,初始化代码如下:
/**
* @brief init adc1 function.
* @param none
* @retval none
*/
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 = TRUE;
adc_base_struct.data_align = ADC_RIGHT_ALIGNMENT;
adc_base_struct.ordinary_channel_length = 5;
adc_base_config(ADC1, &adc_base_struct);
/* 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);
/* adc calibration-------------------------------------------------------- */
adc_calibration_init(ADC1);
while(adc_calibration_init_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 */
}
初始化之后在main中定时调用下边的ADC函数:
//Test ADCs
void adcTest(void){
int i;
adc_calibration_start(ADC1);//dma_trans_complete_flag = 0;这条语句也不行
adc_ordinary_software_trigger_enable(ADC1, TRUE);
//while(!dma_trans_complete_flag);
while(!adc_calibration_status_get(ADC1));//adc_calibration_status_get(ADC1)
for(i=0;i<5;i++){
adcx=adc_combine_ordinary_conversion_data_get();//adc_preempt_conversion_data_get(ADC1,i);//adc1_ordinary_valuetab;//(double)adc1_ordinary_value;
}
}
在STM例程中获得组数据不用循环赋值,只需要:
HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&ADC_Result,ADC_NUMOFCHANNEL);
而在WB中用那条语句也不通,上述语句倒是显示数据了,前4个通道为固定值1236(等效0~4095),第5个听到位0064稍有变化,在1个bit之间,基本稳定。所有5个通道数值***不变,无论你加电位器与否。
暂时没有辙了,发帖求助。
声明:用STM代码移植过来可以成功,但希望WB能成功。
先谢谢了
|