为什么不能用timer1 CC1事件触发ADC?

时间:2016-06-13 06:04:23

标签: stm32 cortex-m stm32f4

我正在研究stm32f407。我想要做的是用timer1 CC1事件触发ADC1和ADC2,但它不起作用。我试图用timer3 trgo或cc1事件触发ADC1和ADC2,它工作正常。所以我不知道为什么timer1 cc1事件不起作用,有人可以帮助我吗?谢谢,以下是我的代码。

int main(void) 
{
    GPIO_InitTypeDef GPIO_InitStructure;
    ADC_InitTypeDef  ADC_InitStructure;

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);    

    /* Config GPIO */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* Config ADC1 */

    /* ADC1 clock and reset*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);

    /* ADC1 config */
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStructure.ADC_ScanConvMode = ENABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 2;
    /* config trigger event */
    // ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_CC1;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;

    ADC_Init(ADC1, &ADC_InitStructure);

    /* ADC1 channel config */
    /* channel 4 for PA4 */
    ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_480Cycles);
    /* channel 5 for PA5 */
    ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_480Cycles);

    /* ADC Common config */
    ADC_CommonInitTypeDef  ADC_CommonInitStructure;
    ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; 
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
    ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
    ADC_CommonInit(&ADC_CommonInitStructure);

    /* Enable ADC1 */
    ADC_Cmd(ADC1, ENABLE);

    /* Configure TIMER1 */
    TIM1_Int_Init(5000-1,8400-1);

    while (1) {} 
}
void TIM1_Int_Init(u16 arr,u16 psc) 
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    TIM_OCInitTypeDef       TIM_OCInitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

    TIM_TimeBaseInitStructure.TIM_Period = arr;
    TIM_TimeBaseInitStructure.TIM_Prescaler=psc;
    TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
    TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 0xFF;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; 
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);

    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
    TIM_ARRPreloadConfig(TIM1, ENABLE);

    TIM_CtrlPWMOutputs(TIM1, ENABLE);       
    TIM_Cmd(TIM1, ENABLE); 
}

让我更困惑的是,如果我启用TIM1_CC_IRQn中断,我可以真正捕获TIM1_CC_IRQHandler中的CC1中断。

有没有人对此有所了解?

最重要的状态是我发现如果我启用CC1中断,则CC1事件可以触发ADC1,现在我的问题是为什么我必须启用CC1中断?我是否引用CC1中断是否重要? 如果我用定时器3的CC1事件触发ADC1,我真的不需要启用CC1中断。

1 个答案:

答案 0 :(得分:0)

确保寄存器TIMx_BDTR中的位MOE置位