請問怎麼樣利用timer量測輸入脈波寬度????

2019-07-21 06:21发布


請問各位~~~

我想要利用timer量測hc-sr04的脈波寬度
請問我該怎麼樣改改寫下面的程式碼

因為我遇到一個問題,我參考網路的資料但是
只要我把程式碼加上
//--------------------------------設定外部觸發------------- -----------//
// TIM_ITRxExternalClockConfig(TIM2,TIM_TS_ETRF); //配置外部觸?,否?不???
// TIM_ETRCloc​​kMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
// TIM_SetCounter(TIM2, 0);      
//------------------------------------------------ --------------------//

結果我把PA0加上輸入信號
但是就不會動就不會動作??

該怎麼半???






#include "stm32f0xx.h"
#include "usart.h"
void Delay (uint32_t nCount);
void TIMER_Init(void);
void Delay(__IO uint32_t nTime);
void TimingDelay_Decrement(void);
void GPIO_Config(void);
static __IO uint32_t TimingDelay;
#define MESSAGE1 "*****Alarm Example"
#define MESSAGE2 " testing on"
#define MESSAGE3 " UART-1 *****"


int main(void)
{
  uint32_t COUNT1=0;
  uint32_t COUNT2=0;
   SystemCoreClockUpdate();
         if (SysTick_Config(SystemCoreClock /1000))
  {
    while (1);
  }       

        USART_Configuration();
  printf(" %s", MESSAGE1);
  printf(" %s", MESSAGE2);
  printf(" %s ", MESSAGE3);

        GPIO_Config();
        TIMER_Init();
        while (1)
        {       

      }


//               
// TIM_Cmd(TIM2, ENABLE);
// TIM2->CNT=0;
// Delay(20);
// COUNT= TIM_GetCounter(TIM2);
//                 printf(" %2d", COUNT);
//  
        }
}               

void TIMER_Init(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  //TIM_ICInitTypeDef TIM_ICInitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  /* TIM2 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 / 1000000)) - 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(__IO uint32_t nTime)
{
  TimingDelay = nTime;

  while(TimingDelay != 0);
}

/**
  * @brief Decrements the TimingDelay variable.
  * @param None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  {
    TimingDelay--;
  }
}

void GPIO_Config(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_All;                                                  
        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_50MHz;
        GPIO_Init(GPIOC, &GPIO_InitStructure);

  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);
}







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