|
求助 关于STM32F302RC ADC2的问题!!!
[复制链接]
本人接触stm32f 时间不长,在一个项目上使用了stm32f302rc这个芯片,初始化代码用的是STM32CubeMX生成的。ADC1、ADC2各有4路模拟输入。目前遇到的问题是ADC1采样正常,但ADC2采样不正常,输入0V时ADC2始终不为零,
大概有0.9V左右(3.3V基准)。这个问题已经困扰了几天了,还没有解决。求各位高人帮忙看看,如何解决。
以下是初始化代码
- ADC_HandleTypeDef hadc1;
- ADC_HandleTypeDef hadc2;
- DMA_HandleTypeDef hdma_adc1;
- DMA_HandleTypeDef hdma_adc2;
- /* ADC1 init function */
- void MX_ADC1_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig;
-
- /**Common config */
- hadc1.Instance = ADC1;
- hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
- hadc1.Init.Resolution = ADC_RESOLUTION_12B;
- hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
- hadc1.Init.ContinuousConvMode = DISABLE;
- hadc1.Init.DiscontinuousConvMode = DISABLE;
- hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
- hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T4_TRGO;
- hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc1.Init.NbrOfConversion = 4;
- hadc1.Init.DMAContinuousRequests =DISABLE;
- hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
- hadc1.Init.LowPowerAutoWait = DISABLE;
- hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
- if (HAL_ADC_Init(&hadc1) != HAL_OK)
- {
- Error_Handler();
- }
-
- /**Configure Regular Channel */
-
- sConfig.SingleDiff = ADC_SINGLE_ENDED;
- sConfig.SamplingTime = ADC_SAMPLETIME_19CYCLES_5;
- sConfig.OffsetNumber = ADC_OFFSET_NONE;
- sConfig.Offset = 0;
- sConfig.Channel = ADC_CHANNEL_1;
- sConfig.Rank = 1;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure Regular Channel*/
- sConfig.Channel = ADC_CHANNEL_2;
- sConfig.Rank = 2;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure Regular Channel*/
- sConfig.Channel = ADC_CHANNEL_3;
- sConfig.Rank = 3;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure Regular Channel*/
- sConfig.Channel = ADC_CHANNEL_4;
- sConfig.Rank = 4;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
-
- HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
-
- HAL_ADC_Start_DMA(&hadc1, (U32*)ADC1_ConvertedValue,ADC_SampleCount);
- }
- /* ADC2 init function */
- void MX_ADC2_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig;
-
- /**Common config */
- hadc2.Instance = ADC2;
- hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
- hadc2.Init.Resolution = ADC_RESOLUTION_12B;
- hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE;
- hadc2.Init.ContinuousConvMode = DISABLE;
- hadc2.Init.DiscontinuousConvMode = DISABLE;
- hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
- hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T4_TRGO;
- hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc2.Init.NbrOfConversion = 4;
- hadc2.Init.DMAContinuousRequests = DISABLE;
- hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
- hadc2.Init.LowPowerAutoWait = DISABLE;
- hadc2.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
- if (HAL_ADC_Init(&hadc2) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure Regular Channel*/
-
- sConfig.SingleDiff = ADC_SINGLE_ENDED;
- sConfig.SamplingTime = ADC_SAMPLETIME_19CYCLES_5;
- sConfig.OffsetNumber = ADC_OFFSET_NONE;
- sConfig.Offset = 0;
-
- sConfig.Channel = ADC_CHANNEL_6;
- sConfig.Rank = 1;
- if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
-
- /**Configure Regular Channel */
- sConfig.Channel = ADC_CHANNEL_7;
- sConfig.Rank = 2;
- if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure Regular Channel*/
- sConfig.Channel = ADC_CHANNEL_8;
- sConfig.Rank = 3;
- if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure Regular Channel*/
-
- sConfig.Channel = ADC_CHANNEL_9;
- sConfig.Rank = 4;
- if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
-
- HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED);
-
- HAL_ADC_Start_DMA(&hadc2, (U32*)ADC2_ConvertedValue, ADC_SampleCount);
- }
- static uint32_t HAL_RCC_ADC12_CLK_ENABLED=0;
- void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- if(adcHandle->Instance==ADC1)
- {
- /* Peripheral clock enable */
- HAL_RCC_ADC12_CLK_ENABLED++;
- if(HAL_RCC_ADC12_CLK_ENABLED==1){
- __HAL_RCC_ADC12_CLK_ENABLE();
- }
-
- /**ADC1 GPIO Configuration
- PA0 ------> ADC1_IN1
- PA1 ------> ADC1_IN2
- PA2 ------> ADC1_IN3
- PA3 ------> ADC1_IN4
- */
- GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* Peripheral DMA init*/
-
- hdma_adc1.Instance = DMA1_Channel1;
- hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
- hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
- hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
- hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
- hdma_adc1.Init.Mode = DMA_CIRCULAR;//DMA_NORMAL;
- hdma_adc1.Init.Priority = DMA_PRIORITY_MEDIUM;
- if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
- /* Peripheral interrupt init */
- //HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
- //HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
- }
- else if(adcHandle->Instance==ADC2)
- {
- /* Peripheral clock enable */
- HAL_RCC_ADC12_CLK_ENABLED++;
- if(HAL_RCC_ADC12_CLK_ENABLED==1){
- __HAL_RCC_ADC12_CLK_ENABLE();
- }
-
- /**ADC2 GPIO Configuration
- PC0 ------> ADC2_IN6
- PC1 ------> ADC2_IN7
- PC2 ------> ADC2_IN8
- PC3 ------> ADC2_IN9
- */
- GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
-
- /* Peripheral DMA init*/
-
- hdma_adc2.Instance = DMA2_Channel1;
- hdma_adc2.Init.Direction = DMA_PERIPH_TO_MEMORY;
- hdma_adc2.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_adc2.Init.MemInc = DMA_MINC_ENABLE;
- hdma_adc2.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
- hdma_adc2.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
- hdma_adc2.Init.Mode = DMA_CIRCULAR;//DMA_NORMAL;
- hdma_adc2.Init.Priority = DMA_PRIORITY_MEDIUM;
- if (HAL_DMA_Init(&hdma_adc2) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc2);
- /* Peripheral interrupt init */
- //HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
- //HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
- }
- }
复制代码
|
|