问题在于通道选择寄存器,下面的代码在轮询方法本身中工作正常。
- void MX_ADC1_Init(void)
- {
-
- /* USER CODE BEGIN ADC1_Init 0 */
-
- /* USER CODE END ADC1_Init 0 */
-
- ADC_ChannelConfTypeDef sConfig = {0};
-
- /* USER CODE BEGIN ADC1_Init 1 */
-
- /* USER CODE END ADC1_Init 1 */
- /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
- */
- hadc1.Instance = ADC1;
- hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
- hadc1.Init.Resolution = ADC_RESOLUTION_12B;
- hadc1.Init.ScanConvMode = ENABLE;
- hadc1.Init.ContinuousConvMode = ENABLE;
- hadc1.Init.DiscontinuousConvMode = DISABLE;
- hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
- hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- // hadc1.Init.NbrOfConversion = 1;
- hadc1.Init.DMAContinuousRequests = DISABLE;
- hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;//ADC_EOC_SINGLE_CONV;
- //naga
- hadc.Init.SamplingTime = ADC_SAMPLETIME_160CYCLES_5;
-
- if (HAL_ADC_Init(&hadc1) != HAL_OK)
- {
- Error_Handler();
- }
- // /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- // */
- // sConfig.Channel = ADC_CHANNEL_0;
- // sConfig.Rank = 1;
- // sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES;
- // if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- // {
- // Error_Handler();
- // }
- // /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- // */
- // sConfig.Channel = ADC_CHANNEL_1;
- // sConfig.Rank = 2;
- // sConfig.SamplingTime = ADC_SAMPLETIME_84CYCLES;
- // if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- // {
- // Error_Handler();
- // }
- // /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- // */
- // sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
- // sConfig.Rank = 3;
- // sConfig.SamplingTime = ADC_SAMPLETIME_112CYCLES;
- // if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- // {
- // Error_Handler();
- // }
- /* USER CODE BEGIN ADC1_Init 2 */
-
- /* USER CODE END ADC1_Init 2 */
- }
-
-
- void ADC_Select_CH0 (void)
- {
- ADC_ChannelConfTypeDef sConfig = {0};
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_0;
- sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
- //sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- }
-
- void ADC_Select_CH1 (void)
- {
- ADC_ChannelConfTypeDef sConfig = {0};
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_1;
- sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
- //sConfig.SamplingTime = ADC_SAMPLETIME_84CYCLES;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- }
-
- void ADC_Select_CH5 (void)
- {
- ADC_ChannelConfTypeDef sConfig = {0};
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_5;
- sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
- //sConfig.SamplingTime = ADC_SAMPLETIME_84CYCLES;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- }
-
- void ADC_Select_CH6 (void)
- {
- ADC_ChannelConfTypeDef sConfig = {0};
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_6;
- //
- sConfig.Rank = 6;
- //sConfig.SamplingTime = ADC_SAMPLETIME_84CYCLES;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- }
-
- void Adc_Read_Raw_Data(void)
- {
- ADC1->CHSELR = 0x01;
- ADC_Select_CH0();
- HAL_ADC_Start(&hadc1);
- HAL_ADC_PollForConversion(&hadc1, 1000);
- Adc_0_Data = HAL_ADC_GetValue(&hadc1);
- HAL_ADC_Stop(&hadc1);
- sprintf(Buf,"adc0 %u rn", Adc_0_Data);
- HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
- Channel_0_Volt = ((float)Adc_0_Data/4095)*3.3;
- sprintf(Buf,"adc0 %u rn", (unsigned int)Channel_0_Volt);
- HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
- //HAL_Delay(10);
-
- ADC1->CHSELR = 0x02;
- ADC_Select_CH1();
- HAL_ADC_Start(&hadc1);
- HAL_ADC_PollForConversion(&hadc1, 1000);
- Adc_1_Data = HAL_ADC_GetValue(&hadc1);
- HAL_ADC_Stop(&hadc1);
- sprintf(Buf," adc1 %u rn", Adc_1_Data);
- HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
- Channel_1_Volt = ((float)Adc_1_Data/4095)*3.3;
- sprintf(Buf,"adc0 %u rn", (unsigned int)Channel_1_Volt);
- HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
- //HAL_Delay(10);
-
- ADC1->CHSELR = 0x20;
- ADC_Select_CH5();
- HAL_ADC_Start(&hadc1);
- HAL_ADC_PollForConversion(&hadc1, 1000);
- Adc_5_Data = HAL_ADC_GetValue(&hadc1);
- HAL_ADC_Stop(&hadc1);
- sprintf(Buf," adc5 %u rn", Adc_5_Data);
- HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
- Channel_5_Volt = ((float)Adc_5_Data/4095)*3.3;
- sprintf(Buf,"adc0 %u rn", (unsigned int)Channel_5_Volt);
- HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
- //HAL_Delay(10);
-
- ADC1->CHSELR = 0x40;
- ADC_Select_CH6();
- HAL_ADC_Start(&hadc1);
- HAL_ADC_PollForConversion(&hadc1, 1000);
- Adc_6_Data = HAL_ADC_GetValue(&hadc1);
- HAL_ADC_Stop(&hadc1);
- sprintf(Buf," adc6 %u rn", Adc_6_Data);
- HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
- Channel_6_Volt = ((float)Adc_6_Data/4095)*3.3;
- sprintf(Buf,"adc0 %u rn", (unsigned int)Channel_6_Volt);
- HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
- //HAL_Delay(100);
- }
|
|
2022-12-6 16:16:06
评论
举报
|
|
|