請問STM32F0 Timer2要用外部方波啟動計數要怎麼改程式碼

2019-07-21 06:30发布


請大請大家幫忙看一看這個程式碼有甚麼問題
為什麼我的PC8PC9甚麼信號都沒有輸出

我是用用STM32F0 Discovery的板子
照理說照理說PA0接上USER的按鈕,按下去Timer2就會開始計數, 一旦計數到2000的時候
就會產生中斷副程式,把PC8/PC9的LED Toggle
一開始程式碼是是可以執行的,但是我為了要讓PA0的按鈕按下去才會開始計數
我加上了
  TIM_ITRxExternalClockConfig(TIM2,TIM_TS_ETRF); //配置外部觸?,否?不???
  TIM_ETRCloc​​​​kMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);

  TIM_SetCounter(TIM2, 0);   

但是結果就掛掉了~~~甚麼LED都沒有輸出Toggle的信號
板子完全沒動沒動作





#include "stm32f0xx.h"
void Delay (uint32_t nCount);
void TIMER_Init(void);

int main(void)
{

        GPIO_InitTypeDef GPIO_InitStructure;
        /* Configure the LED_pin as output push-pull for LD3 & LD4 usage*/
        RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOA,ENABLE);
        GPIO_InitStructure.GPIO_Pin =GPIO_Pin_8|GPIO_Pin_9;                                                  
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_Init(GPIOC, &GPIO_InitStructure);
        /* Force a low level on LEDs*/
//         GPIO_ResetBits(GPIOB,GPIO_Pin_7);       
//         GPIO_SetBits(GPIOB,GPIO_Pin_6);       


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_ResetBits(GPIOA,GPIO_Pin_0);   
        TIMER_Init();
        while (1)
        {       

       
        }
}               


void TIMER_Init(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  //TIM_ICInitTypeDef TIM_ICInitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;


  /*
M3 clock source enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* Enable the TIM2 Update Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
       

  /* Timer configuration in Encoder mode */
  TIM_DeInit(TIM2);
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

  TIM_TimeBaseStructure.TIM_Prescaler =(uint16_t) (((SystemCoreClock / 10000000)) - 1); // Shooting for 1 MHz, (1us)

  TIM_TimeBaseStructure.TIM_Period = 20000 - 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_ITRxExternalClockConfig(TIM2,TIM_TS_ETRF); //配置外部觸?,否?不???
  TIM_ETRCloc​​kMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
  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);  


        }


void Delay (uint32_t nCount)
{
  for(; nCount != 0; nCount--);
}


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。