F7自带的ADC_DMA和TIM例子,分别都通了,现在把它们和一起怎么也不灵,网上搜了好多资料也看不出差别,特来本坛一问,还请多多指教。用TIM2的TRGO来定时采样,T2工作了但是不能TRGO ADC,ADC如果换成CONT形式也能工作,但是采样时间就不按要求T2了。下面是代码。 /* Includes ------------------------------------------------------------------*/ #include "main.h" //TIM设置 #define TIMx TIM2 #define TIMx_CLK_ENABLE() __HAL_RCC_TIM2_CLK_ENABLE() #define TIMx_IRQn TIM2_IRQn #define TIMx_IRQHandler TIM2_IRQHandler TIM_HandleTypeDef TimHandle; void TIM_Config(void) { TIM_MasterConfigTypeDef TimxMasterConfigHandle; uint32_t uwPrescalerValue; uwPrescalerValue = (uint32_t)((SystemCoreClock / 2) / 10000) - 1; /* Set TIMx instance */ TimHandle.Instance = TIMx; /* Initialize TIMx peripheral as follows: + Period = 10000 - 1 + Prescaler = ((SystemCoreClock / 2)/10000) - 1 + ClockDivision = 0 + Counter direction = Up */ TimHandle.Init.Period = 10000 - 1; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; //TimHandle.Init.RepetitionCounter = 0; if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ //Error_Handler(); } // /* TIM TRGO selection */ TimxMasterConfigHandle.MasterOutputTrigger=TIM_TRGO_UPDATE;//TIM_TRGO_ENABLE; // Tim3MasterConfigHandle.MasterOutputTrigger=TIM_TRGO_RESET; TimxMasterConfigHandle.MasterSlaveMode=TIM_MASTERSLAVEMODE_DISABLE; if(HAL_TIMEx_MasterConfigSynchronization(&TimHandle, &TimxMasterConfigHandle)!=HAL_OK) { //Error_Handler(); } } void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim) { /*##-1- Enable peripheral clock #################################*/ /* TIMx Peripheral clock enable */ TIMx_CLK_ENABLE(); /*##-2- Configure the NVIC for TIMx ########################################*/ /* Set the TIMx priority */ HAL_NVIC_SetPriority(TIMx_IRQn, 3, 0); /* Enable the TIMx global Interrupt */ HAL_NVIC_EnableIRQ(TIMx_IRQn); } void TIMx_IRQHandler(void) { HAL_TIM_IRQHandler(&TimHandle); } ADC_HandleTypeDef AdcHandle; void ADC_Config() { ADC_ChannelConfTypeDef sConfig; /*##-1- Configure the ADC peripheral #######################################*/ AdcHandle.Instance = ADCx; AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;//ADC clock = APB2/4 AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; AdcHandle.Init.ScanConvMode = DISABLE; //单通道 /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */ AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous mode disabled to have only 1 conversion at each conversion trig */ AdcHandle.Init.DiscontinuousConvMode = DISABLE; AdcHandle.Init.NbrOfDiscConversion = 0; AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; /* Conversion start trigged at each external event */ AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T2_TRGO; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.NbrOfConversion = 1; AdcHandle.Init.DMAContinuousRequests = ENABLE; AdcHandle.Init.EOCSelection = DISABLE; if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { /* ADC initialization Error */ //Error_Handler(); } /*##-2- Configure ADC regular channel ######################################*/ sConfig.Channel = ADC_CHANNEL_8; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; sConfig.Offset = 0; if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { /* Channel Configuration Error */ //Error_Handler(); } } /** @addtogroup STM32F7xx_HAL_Examples * @{ */ /** @addtogroup ADC_RegularConversion_DMA * @{ */ /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* ADC handler declaration */ /* Variable used to get converted value */ __IO uint16_t uhADCxConvertedValue = 0,dat[512]; /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void Error_Handler(void); static void MPU_Config(void); static void CPU_CACHE_Enable(void); /* Private functions ---------------------------------------------------------*/ /** * @brief Main program. * @param None * @retval None */ int main(void) { /* Configure the MPU attributes as Write Through */ MPU_Config(); /* Enable the CPU Cache */ CPU_CACHE_Enable(); /* STM32F7xx HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 216 MHz */ SystemClock_Config(); /* Configure LED1 */ BSP_LED_Init(LED1); ADC_Config(); TIM_Config(); if(HAL_ADC_Start_DMA(&AdcHandle, (uint32_t*)dat, 40) != HAL_OK) { /* Start Conversation Error */ Error_Handler(); } if (HAL_TIM_Base_Start(&TimHandle) != HAL_OK) { /* Starting Error */ //Error_Handler(); } /*##-3- Start the conversion process #######################################*/ /*if(HAL_ADC_Start_DMA(&AdcHandle, (uint32_t*)&uhADCxConvertedValue, 1) != HAL_OK) { Error_Handler(); }*/ /* Infinite loop */ while (1) { } } /** * @brief This function is executed in case of error occurrence. * @param None * @retval None */ static void Error_Handler(void) { while (1) { /* LED1 blinks */ BSP_LED_Toggle(LED1); HAL_Delay(20); } } /** * @brief Conversion complete callback in non blocking mode * @param AdcHandle : AdcHandle handle * @note This example shows a simple way to report end of conversion, and * you can add your own implementation. * @retval None */ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle) { /* Turn LED1 on: Transfer process is correct */ BSP_LED_On(LED1); } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */ while (1) { } } void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { BSP_LED_Toggle(LED1); } |
楼主的问题解决了吗,我也遇到了同样的问题,如果解决了能不能分享一下,谢谢 |