我自己想出來的脈波寬度量測~~~比較好理解

2019-07-21 06:23发布

這是我用EXTI0 外部觸發中斷做出做出來的脈波寬度量測~~
我試過1msec~1sec都可以精確量出有多少個CLOCK
還蠻精準的~~~
我自己是看不太懂正點的輸入捕獲裡面的程式碼
真的搞不懂!!
所以我自己想了一個可以用來量測超音波hc-sr04 senser的程式碼
#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);
void EXTI0_Config(void);
static __IO uint32_t TimingDelay;
extern uint32_t Hunk[2];
#define MESSAGE1 "*****Alarm Example"
#define MESSAGE2 " testing on"
#define MESSAGE3 "****** UART-1******"
extern uint8_t TIM5CH1_CAPTURE_STA;                 //懷眸鳳袨怓                                                   
extern uint16_t         TIM5CH1_CAPTURE_VAL;         //懷眸鳳硉       
extern uint32_t count[2];
int main(void)
{
  float DIST=0;
  uint32_t COUNT1=0;
  uint32_t COUNT2=0;
   SystemCoreClockUpdate();
         if (SysTick_Config(SystemCoreClock /1000))
  {
    while (1);
  }       
        EXTI0_Config();
        USART_Configuration();
  printf(" %s", MESSAGE1);
  printf(" %s", MESSAGE2);
  printf(" %s ", MESSAGE3);

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

//   while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1)
//     {
//                                TIM_Cmd(TIM2, ENABLE);
//                                COUNT1= TIM_GetCounter(TIM2);
//            while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0)
//            {
//                                                         COUNT2= TIM_GetCounter(TIM2);
//                                                          TIM2->CNT=0;
//            }   
//     }

//                printf(" %2d", COUNT2-COUNT1);

    if(Hunk[0]<Hunk[1])
     {     
      DIST=(Hunk[1]-Hunk[0])*0.034;  
      printf(" <<< %d    %d    %0.2f CM >>>",Hunk[0],Hunk[1],DIST);
     }
      else
     {
      DIST=(Hunk[1]+65535-Hunk[0])*0.034;
                         printf(" <<< %d   %d    %0.2f CM >>>",Hunk[0],Hunk[1],DIST);
     }
      Delay(2500);

        }
}               

void TIMER_Init(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_ICInitTypeDef  TIM2_ICInitStructure;   
  //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 = 0xFFFF  - 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_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_Inverted, 0);
// TIM_SetCounter(TIM2, 0);      
//--------------------------------------------------------------------//
        TIM2_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01         恁寁怀&#63541;傷 IC1茬扞善TI1奻
        TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;        //奻汔朓眸鳳
        TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //茬扞善TI1奻
        TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;         //饜离怀&#63541;煦&#63173;,祥煦&#63173;
        TIM2_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 饜离怀&#63541;薦疏&#63239; 祥薦疏
        TIM_ICInit(TIM2, &TIM2_ICInitStructure);
//---------------------------------------------------------------------//
// 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_9|GPIO_Pin_8|GPIO_Pin_7|GPIO_Pin_6;                                                  
        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_AF;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

}

void EXTI0_Config(void)
{
  EXTI_InitTypeDef   EXTI_InitStructure;
  GPIO_InitTypeDef   GPIO_InitStructure;
  NVIC_InitTypeDef   NVIC_InitStructure;

  /* Enable GPIOA clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  /* Configure PA0 pin as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Enable SYSCFG clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

  /* Connect EXTI0 Line to PA0 pin */
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);

  /* Configure EXTI0 line */
  EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  /* Enable and set EXTI0 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}





友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
1条回答
hunkchen2000
2019-07-21 06:44
//------------------------EXIT0 Interrupt----------------------------// uint32_t  Hunk[2]; void EXTI0_1_IRQHandler(void) { static int temp=0; static uint8_t X=0;  EXTI_InitTypeDef   EXTI_InitStructure;  NVIC_InitTypeDef   NVIC_InitStructure;   if(EXTI_GetITStatus(EXTI_Line0) != RESET)   {      Hunk[temp]=TIM2->CNT;      //printf(" --%d  %d  %d", X, temp,Hunk[temp]);      X++;       if(X==10)       X=0;      EXTI_ClearITPendingBit(EXTI_Line0);                 //----------------------------------------------//         /* Configure EXTI0 line */     EXTI_InitStructure.EXTI_Line = EXTI_Line0;     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;     EXTI_InitStructure.EXTI_LineCmd = ENABLE;     EXTI_Init(&EXTI_InitStructure);     /* Enable and set EXTI0 Interrupt */     NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;     NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;     NVIC_Init(&NVIC_InitStructure);     //-----------------------------------------------//      temp++;        if(temp==2)          {                        temp=0;            EXTI_InitStructure.EXTI_Line = EXTI_Line0;            EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;                                                 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;                                                 EXTI_InitStructure.EXTI_LineCmd = ENABLE;                                                 EXTI_Init(&EXTI_InitStructure);                                                 /* Enable and set EXTI0 Interrupt */                                                 NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;                                                 NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;                                                 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                                 NVIC_Init(&NVIC_InitStructure);          }             } }   

一周热门 更多>