本人接触stm32f 时间不长,在一个项目上使用了stm32f302rc这个芯片,初始化代码用的是STM32CubeMX生成的。ADC1、ADC2各有4路模拟输入。目前遇到的问题是ADC1采样正常,但ADC2采样不正常,输入0V时ADC2转换始终不为零,折合电压大概有0.9V左右(3.3V基准)。这个问题已经困扰了几天了,还没有解决。求各位高人帮忙看看,如何解决。
谢谢!!
以下是初始化代码:
- ADC_HandleTypeDef hadc1;
- ADC_HandleTypeDef hadc2;
- ADC_MultiModeTypeDef multi;
- 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();
- }
- multi.Mode = ADC_MODE_INDEPENDENT;
- multi.DMAAccessMode = ADC_DMAACCESSMODE_DISABLED;
- if (HAL_ADCEx_MultiModeConfigChannel(&hadc2, &multi)!=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);
- }
- }
复制代码
|
另外你adc2为什么会有multimode的设置,如果是配置的independent模式的话,应该是没有双adc模式的配置的。
我没有302的片子 ,只是根据经验配置了一下,你可以试试我生成的代码;
评分
查看全部评分
如果不正常,就是ADC2未正常初始化。
评分
查看全部评分
评分
查看全部评分
评分
查看全部评分
/* USER CODE BEGIN ADC2_MspDeInit 1 */
/* USER CODE END ADC2_MspDeInit 1 */
这样的结构内部添加/修改你的代码,或者另外独立建立文件,如果没有使用代码管理,你会发现你所修改的内容,再次使用cube配置时被改的体无完肤,你会欲哭无泪的
配置过程自查,按照前面网友说的,单独配置每一个ADC,都正常了,再合在一起使用,调试。
评分
查看全部评分
评分
查看全部评分
抱歉,刚出差回来,没有及时回复。
按您给的代码试了,还是不行,和以前一样。
另:
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1
hadc2.Init.ClockPrescale r= ADC_CLOCK_ASYNC_DIV1
中ADC_CLOCK_ASYNC_DIV1用ADC_CLOCK_SYNC_PCLK_DIV1替代,否则
while(HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED) != HAL_OK)过不去。
关闭ADC1单独使用DAC2也不行。
目前只有将原来定义在ADC2的端子定义在ADC1里一切正常。
在EOCSelection中用ADC_EOC_SEQ_CONV或ADC_EOC_SINGLE_CONV都一样。