請問Timer要如何外部觸發才會計數???

2019-07-21 05:48发布

請問請問各位下面的程式碼,我使用的是STMF0的Discovery開發板
TIM_ETRCloc​​kMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_Inverted,0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0|GPIO_PinSource5|GPIO_PinSource15, GPIO_AF_2);
TIM_SetCounter(TIM2,0);

請問我規劃一個計數器想要外部接腳信號來觸發計數器計數,該怎麼做?????
我規劃的是Timer2,程式碼如下,請問哪邊設定錯誤了,為什麼我的PA0,PA5,PA15不管給甚麼信號
計數器都不會動作
          

void TIMER2_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  //TIM_ICInitTypeDef TIM_ICInitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

  /* TIM3 clock source enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* Enable the TIM3 Update Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
       
//------------------------------------------------ -------------------//
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_15|GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

//------------------------------------------------ -------------------//
  /* Timer configuration in Encoder mode */
  TIM_DeInit(TIM2);
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Prescaler =(uint16_t) (((SystemCoreClock / 1000000)) - 1); // Shooting for 1 MHz, (1us)
  TIM_TimeBaseStructure.TIM_Period = 40000 - 1;//20000 - 1; // 1 MHz / 20000 = 50 Hz (20ms)
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseStructure.TIM_RepetitionCounter=0;       
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//------------------------------------------------ -----------------------//

  TIM_ETRCloc​​kMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_Inverted,0);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource0|GPIO_PinSource5|GPIO_PinSource15, GPIO_AF_2);
TIM_SetCounter(TIM2,0);
//------------------------------------------------ ----------------------//

// Clear all pending interrupts
  TIM_ClearFlag(TIM2, TIM_FLAG_Update);
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

  TIM_Cmd(TIM2, ENABLE);         

}



0条回答

一周热门 更多>