各位大神好: 本人使用STM32F103C6T6,期望目标是:TIM2采用中心对齐模式3,同时ADC_ExternalTrigConv_T2_CC2触发ADC采样?按照手册说明:在中心对齐模式3中增加和减少的过程中都会产生比较事件,比较事件可以触发ADC,这样按照手册说明应该是触发两次采样,但是我测试情况发现无论如何都只触发一次采样?而且都是在增加过程中的比较点处触发?是哪里有问题? 已经明确的问题:1、不是两次采样中间时间不够,已经确保两次采样之间的时间足够完成,确保了两次之间的时间够长。2、确保了TIM2是中心对齐模式,3、确保了ADC能够触发采样,只是在一个周期内没有触发两次,在此恳请各位帮忙解答一下,谢谢, 如下式我的程序还请各位有空帮忙查看: //ADC void ADC_Config(void) { GPIO_InitTypeDef GPIO_InitStruct; ADC_InitTypeDef ADC_InitStruct; DMA_InitTypeDef DMA_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStruct); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); DMA_DeInit(DMA1_Channel1); DMA_InitStruct.DMA_PeripheralBaseAddr = ( Uint32 ) ( & ( ADC1->DR )); DMA_InitStruct.DMA_MemoryBaseAddr = (Uint32)ADC_ConvResult; DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStruct.DMA_BufferSize = ADC_SAMPLE_NUM; DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; DMA_InitStruct.DMA_Priority = DMA_Priority_High; DMA_InitStruct.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStruct); DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE); DMA_Cmd(DMA1_Channel1 , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); ADC_InitStruct.ADC_Mode = ADC_Mode_Independent; ADC_InitStruct.ADC_ScanConvMode = ENABLE; ADC_InitStruct.ADC_ContinuousConvMode = DISABLE; ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2; ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStruct.ADC_NbrOfChannel = ADC_SAMPLE_NUM; ADC_Init(ADC1, &ADC_InitStruct); RCC_ADCCLKConfig(RCC_PCLK2_Div6); ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 1, ADC_SampleTime_1Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_1Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 3, ADC_SampleTime_1Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 4, ADC_SampleTime_1Cycles5); ADC_DMACmd(ADC1, ENABLE); //ADC_ITConfig(ADC1,ADC_IT_EOC, ENABLE); ADC_Cmd(ADC1,ENABLE); ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)) { ; } ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1)) { ; } ADC_ExternalTrigConvCmd(ADC1,ENABLE); NVIC_InitTypeDef NVIC_InitTyStuct; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitTyStuct.NVIC_IRQChannel = DMA1_Channel1_IRQn; NVIC_InitTyStuct.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitTyStuct.NVIC_IRQChannelSubPriority = 1; NVIC_InitTyStuct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitTyStuct); } void TIM2_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE); /* GPIOA, GPIOB and GPIOE Clocks enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); #if 1 GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); #endif /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = EPWM_TIMER_TBPRD - 1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* Channel 1, 2,3 and 4 Configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = EPWM_TIMER_TBPRD - 600; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OC2Init(TIM2, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM2, DISABLE); /* TIM2 Main Output Enable */ TIM_CtrlPWMOutputs(TIM2, ENABLE); #if 0 TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Enable); TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0); TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Gated); TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); #endif TIM_Cmd(TIM2, ENABLE); //TIM_Cmd(TIM1, ENABLE); } |
程序如下:
void TIM2_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
/* GPIOA, GPIOB and GPIOE Clocks enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
#if 1
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#endif
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3;
TIM_TimeBaseStructure.TIM_Period = EPWM_TIMER_TBPRD;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = EPWM_TIMER_TBPRD - 600;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, DISABLE);
/* TIM2 Main Output Enable */
TIM_CtrlPWMOutputs(TIM2, ENABLE);
#if 1
TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Enable);
TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0);
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Gated);
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
#endif
TIM_Cmd(TIM2, ENABLE);
TIM_Cmd(TIM1, ENABLE);
}
评分
查看全部评分
首先要谢谢您的关注,可能你没有仔细看我的说明,你发的ST例程是边沿模式,TMI计数增加,采样TIM1-CC1触发我这里也没问题,不过还是要谢谢你。
根据官方手册,定时器触发ADC只在上升沿触发,楼主哪里看到下降沿也可以触发ADC?
评分
查看全部评分