ADC1 以“连续转换”模式运行,无 DMA。它有一个常规通道 3 和一个注入通道温度传感器。我想每隔几秒使用
tiM2 TRGO 激活注入通道。但是,它不起作用。
当我将 ADC 注入触发源设置为软件触发时,然后调用函数 LL_ADC_INJ_StartConversion(ADC1);注入通道的 ADC 值正确更新。
然后我尝试用中断调试 TIM2,但似乎从未调用过 TIM2_IRQHandler。
- void adc1_uac_init(void){
- LL_ADC_InitTypeDef ADC_InitStruct = { 0 };
- LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = { 0 };
- LL_ADC_CommonInitTypeDef ADC_CommonInitStruct = { 0 };
- LL_ADC_INJ_InitTypeDef ADC_INJ_InitStruct = { 0 };
- LL_GPIO_InitTypeDef GPIO_InitStruct = { 0 };
- /* Peripheral clock enable */
- LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC12);
- LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
- /**ADC1 GPIO Configuration
- PA2 ------> ADC1_IN3
- */
- GPIO_InitStruct.Pin = LL_GPIO_PIN_2;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
- LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /** Common config
- */
- ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B;
- ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
- ADC_InitStruct.LowPowerMode = LL_ADC_LP_MODE_NONE;
- LL_ADC_Init(ADC1, &ADC_InitStruct);
- ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
- ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
- ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
- ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_CONTINUOUS;
- ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
- ADC_REG_InitStruct.Overrun = LL_ADC_REG_OVR_DATA_OVERWRITTEN;
- LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
- LL_ADC_SetGainCompensation(ADC1, 0);
- LL_ADC_SetOverSamplingScope(ADC1, LL_ADC_OVS_GRP_REGULAR_CONTINUED);
- LL_ADC_ConfigOverSamplingRatioShift(ADC1, LL_ADC_OVS_RATIO_16, LL_ADC_OVS_SHIFT_NONE);
- LL_ADC_SetOverSamplingDiscont(ADC1, LL_ADC_OVS_REG_CONT);
- /* Disable ADC deep power down (enabled by default after reset state) */
- LL_ADC_DisableDeepPowerDown(ADC1);
- /* Enable ADC internal voltage regulator */
- LL_ADC_EnableInternalRegulator(ADC1);
- /* Delay for ADC internal voltage regulator stabilization. */
- /* Compute number of CPU cycles to wait for, from delay in us. */
- /* Note: Variable divided by 2 to compensate partially */
- /* CPU processing cycles (depends on compilation optimization). */
- /* Note: If system core clock frequency is below 200kHz, wait time */
- /* is only a few CPU processing cycles. */
- uint32_t wait_loop_index;
- wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US * (SystemCoreClock / (100000 * 2))) / 10);
- while (wait_loop_index != 0) {
- wait_loop_index--;
- }
- ADC_CommonInitStruct.CommonClock = LL_ADC_CLOCK_SYNC_PCLK_DIV4;
- ADC_CommonInitStruct.Multimode = LL_ADC_MULTI_INDEPENDENT;
- LL_ADC_CommonInit(__LL_ADC_COMMON_INSTANCE(ADC1), &ADC_CommonInitStruct);
- /** Configure Injected ADC
- */
- // ADC_INJ_InitStruct.TriggerSource = LL_ADC_INJ_TRIG_SOFTWARE;
- // ADC_INJ_InitStruct.SequencerLength = LL_ADC_INJ_SEQ_SCAN_DISABLE;
- // ADC_INJ_InitStruct.SequencerDiscont = LL_ADC_INJ_SEQ_DISCONT_DISABLE;
- // ADC_INJ_InitStruct.TrigAuto = LL_ADC_INJ_TRIG_INDEPENDENT;
- // LL_ADC_INJ_Init(ADC1, &ADC_INJ_InitStruct);
- // LL_ADC_INJ_SetQueueMode(ADC1, LL_ADC_INJ_QUEUE_DISABLE);
- // LL_ADC_SetOverSamplingScope(ADC1, LL_ADC_OVS_GRP_REGULAR_CONTINUED);
- // LL_ADC_SetCommonPathInternalCh(ADC12_COMMON,LL_ADC_PATH_INTERNAL_TEMPSENSOR);
- ADC_INJ_InitStruct.TriggerSource = LL_ADC_INJ_TRIG_EXT_TIM2_TRGO;
- ADC_INJ_InitStruct.SequencerLength = LL_ADC_INJ_SEQ_SCAN_DISABLE;
- ADC_INJ_InitStruct.SequencerDiscont = LL_ADC_INJ_SEQ_DISCONT_DISABLE;
- ADC_INJ_InitStruct.TrigAuto = LL_ADC_INJ_TRIG_INDEPENDENT;
- LL_ADC_INJ_Init(ADC1, &ADC_INJ_InitStruct);
- LL_ADC_INJ_SetQueueMode(ADC1, LL_ADC_INJ_QUEUE_DISABLE);
- LL_ADC_SetOverSamplingScope(ADC1, LL_ADC_OVS_GRP_REGULAR_CONTINUED);
- LL_ADC_INJ_SetTriggerEdge(ADC1, LL_ADC_INJ_TRIG_EXT_RISING);
- /** Configure Analog WatchDog 1
- */
- LL_ADC_SetAnalogWDMonitChannels(ADC1, LL_ADC_AWD1, LL_ADC_AWD_CHANNEL_3_REG);
- LL_ADC_ConfigAnalogWDThresholds(ADC1, LL_ADC_AWD1, ADC1_AWD_H, ADC1_AWD_L);
- LL_ADC_SetAWDFilteringConfiguration(ADC1, LL_ADC_AWD1, LL_ADC_AWD_FILTERING_NONE);
- LL_ADC_DisableIT_AWD1(ADC1);
- /** Configure Regular Channel
- */
- LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_3);
- LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_3, LL_ADC_SAMPLINGTIME_2CYCLES_5);
- LL_ADC_SetChannelSingleDiff(ADC1, LL_ADC_CHANNEL_3, LL_ADC_SINGLE_ENDED);
- /** Configure Injected Channel
- */
- LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_TEMPSENSOR_ADC1, LL_ADC_SAMPLINGTIME_92CYCLES_5);
- LL_ADC_SetChannelSingleDiff(ADC1, LL_ADC_CHANNEL_TEMPSENSOR_ADC1, LL_ADC_SINGLE_ENDED);
- LL_ADC_INJ_SetSequencerRanks(ADC1, LL_ADC_INJ_RANK_1, LL_ADC_CHANNEL_TEMPSENSOR_ADC1);
- /* Disable ADC deep power down (enabled by default after reset state) */
- LL_ADC_DisableDeepPowerDown(ADC1);
- /* Enable ADC internal voltage regulator */
- LL_ADC_EnableInternalRegulator(ADC1);
- /* Delay for ADC internal voltage regulator stabilization. */
- /* Compute number of CPU cycles to wait for, from delay in us. */
- /* Note: Variable divided by 2 to compensate partially */
- /* CPU processing cycles (depends on compilation optimization). */
- /* Note: If system core clock frequency is below 200kHz, wait time */
- /* is only a few CPU processing cycles. */
- wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US * (SystemCoreClock / (100000 * 2))) / 10);
- while(wait_loop_index != 0)
- {
- wait_loop_index--;
- }
- /* Run ADC self calibration */
- LL_ADC_StartCalibration(ADC1, LL_ADC_SINGLE_ENDED);
- while( LL_ADC_IsCalibrationOnGoing(ADC1));
- /* Delay between ADC end of calibration and ADC enable. */
- /* Note: Variable divided by 2 to compensate partially */
- /* CPU processing cycles (depends on compilation optimization). */
- wait_loop_index = (5 >> 1);
- while(wait_loop_index != 0)
- {
- wait_loop_index--;
- }
- LL_ADC_Enable(ADC1);
- while (LL_ADC_IsActiveFlag_ADRDY(ADC1) == 0);
- LL_ADC_REG_StartConversion(ADC1);
- LL_ADC_INJ_StartConversion(ADC1);
- }
- /* TIM2 init function */
- void MX_TIM2_Init(void)
- {
- LL_TIM_InitTypeDef TIM_InitStruct = {0};
- /* Peripheral clock enable */
- LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
- /* TIM2 interrupt Init */
- NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
- NVIC_EnableIRQ(TIM2_IRQn);
- TIM_InitStruct.Prescaler = 1700;
- TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
- TIM_InitStruct.Autoreload = 10000;
- TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
- LL_TIM_Init(TIM2, &TIM_InitStruct);
- LL_TIM_EnableARRPreload(TIM2);
- LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
- LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_UPDATE);
- LL_TIM_EnableMasterSlaveMode(TIM2);
- LL_TIM_EnableIT_TRIG(TIM2); //only for debug
- LL_TIM_EnableCounter(TIM2);
- }
的
- void TIM2_IRQHandler(void)
- {
- // only for debug
- if(LL_TIM_IsActiveFlag_TRIG(TIM2))
- {
- LED3_toggle();
- LL_TIM_ClearFlag_TRIG(TIM2);
- }
- }
0
|