2016-06-13 55 views
0

我正在stm32f407上工作。我想要做的是用timer1 CC1事件觸發ADC1和ADC2,但它不起作用。我試圖用timer3 trgo或cc1事件觸發ADC1和ADC2,它工作正常。所以我不知道爲什麼timer1 cc1事件不起作用,任何人都可以幫助我?謝謝,以下是我的代碼。爲什麼不能用timer1 CC1事件觸發ADC?

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 interupt。

有沒有人有關於此的一些想法?


最反感的狀態是我發現,如果我讓CC1中斷,則CC1事件可以通過靶向ADC1,現在我的問題是,爲什麼我必須啓用CC1中斷? CC1是否中斷是否重要? 如果我用timer3的CC1事件觸發ADC1,我真的不需要啓用CC1中斷。

+1

是它確實很重要。您需要啓用中斷! – SamR

+0

感謝SamR,但爲什麼我不需要啓用中斷,如果我使用timer3呢? – roMoon

回答

0

確保寄存器TIMx_BDTR中的MOE位被設置爲

相關問題