正點原子STM32的量測脈波寬度太複雜我的比較簡單!!!!

2019-07-21 06:24发布



我研究正點原子的戰艦版的實驗10輸入捕獲實驗
我個人認為很難懂, 搞不懂為什麼這樣弄
我的比較簡單,我只是利用EXIT0 PA0 channel1的外部輸入中斷
而且我給10m的脈波,量測就是10000,給20m量測就是20000
很準的
#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)
{
  uint32_t COUNT=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])
     {      
      printf(" <<< %d  %d %d >>>",Hunk[0],Hunk[1],Hunk[1]-Hunk[0]);
     }
      else
     {
                        printf(" <<< %d  %d %d >>>",Hunk[0],Hunk[1],(Hunk[1]+65535)-Hunk[0]);
     }
      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);
}

//--------------------------------------下面是STM32XXXit.c中斷副程式的部分-----------------------------------------//
/**
  ************************************************** ****************************
  * @file stm32f0xx_it.c
  * @author MCD Application Team
  * @version V1.0.0
  * @date 23-March-2012
  * @brief Main Interrupt Service Routines.
  * This file provides template for all exceptions handler and
  * peripherals interrupt service routine.
  ************************************************** ****************************
  * @attention
  *
  * <h2><center>&#169; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  * http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_it.h"
//#include "main.h"
void TimingDelay_Decrement(void);
void Delay(__IO uint32_t nTime);
void delayX(uint32_t time);

//static __IO uint32_t TimingDelay;

/** @addtogroup STM32F0-Discovery_Demo
  * @{
  */

/** @addtogroup STM32F0XX_IT
  * @brief Interrupts driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M0 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
#include "stm32f0xx_it.h"
#include <stdio.h>
#include <string.h>
//#include "main.h"
void TimingDelay_Decrement(void);
extern __IO uint8_t USARTFLAG;


void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
  TimingDelay_Decrement();
}



/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */



/******************************************************************************/
/*                 STM32F0xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f0xx.s).                                               */
/******************************************************************************/

void TIM3_IRQHandler(void)
{
        if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
       
        GPIOD->ODR ^= 0x0100;
  GPIOD->ODR ^= 0x0200;  
  TIM_ClearFlag(TIM3, TIM_IT_Update);                               
        }
}

void delayX(uint32_t time)
{
  while(time--);
}


uint32_t        count[2];
//--------------------Timer2觸發中斷取得計數值-----------------------//
// void TIM2_IRQHandler(void)
//{
// static uint32_t temp=0;
//        if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
//{       
//  count[0]=temp;

//}
//}

//----------------------------------------------------------//

void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
//GPIOC->ODR ^= 0x0100;
//GPIOC->ODR ^= 0x0200;  
TIM_ClearFlag(TIM2, TIM_IT_Update);       
  }
}

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

  }
}







//----------------------有消除彈跳功能範例------------------------//
//void EXTI0_IRQHandler(void) {
//if(EXTI_GetITStatus(EXTI_Line0) != RESET) {
//Delay(0xFFFF); //debounce
//if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) { //debounce
//if(btn) {
//btn=0;
//GPIO_SetBits(GPIOB, GPIO_Pin_8);
//}
//else {
//btn++;
//GPIO_ResetBits(GPIOB, GPIO_Pin_8);
//}
//}
//EXTI_ClearITPendingBit(EXTI_Line0);
//}
//}
////-----------------------------------------------------------//





//------------------------------------------------------//
//uint8_t  TIM2CH1_CAPTURE_STA=0;        //輸入捕獲狀態                                                   
//uint16_t        TIM2CH1_CAPTURE_VAL;        //輸入捕獲值//

//隅奀&#63239;5笢剿督昢最唗         
//void TIM2_IRQHandler(void)
//{

//         if((TIM2CH1_CAPTURE_STA&0X80)==0)//遜帤傖髡眸鳳       
//        {          
//                if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
//                 
//                {            
//                        if(TIM2CH1_CAPTURE_STA&0X40)//眒冪眸鳳善詢萇&#63181;賸
//                        {
//                                if((TIM2CH1_CAPTURE_STA&0X3F)==0X3F)//詢萇&#63181;怮酗賸
//                                {
//                                        TIM2CH1_CAPTURE_STA|=0X80;//梓暮傖髡眸鳳賸珨棒
//                                        TIM2CH1_CAPTURE_VAL=0XFFFF;
//                                }else TIM2CH1_CAPTURE_STA++;
//                        }         
//                }
//        if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)//眸鳳1楷汜眸鳳岈璃
//                {       
//                        if(TIM2CH1_CAPTURE_STA&0X40)                //眸鳳善珨跺狟蔥朓                
//                        {                                 
//                                TIM2CH1_CAPTURE_STA|=0X80;                //梓暮傖髡眸鳳善珨棒詢萇&#63181;闕遵
//                                TIM2CH1_CAPTURE_VAL=TIM_GetCapture1(TIM2);
//                                   TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Rising); //CC1P=0 扢离峈奻汔朓眸鳳
//                        }else                                                                  //遜帤羲宎,菴珨棒眸鳳奻汔朓
//                        {
//                                TIM2CH1_CAPTURE_STA=0;                        //&#63378;諾
//                                TIM2CH1_CAPTURE_VAL=0;
//                                 TIM_SetCounter(TIM2,0);
//                                TIM2CH1_CAPTURE_STA|=0X40;                //梓暮眸鳳善賸奻汔朓
// TIM_OC1PolarityConfig(TIM2,TIM_ICPolarity_Falling); //CC1P=1扢離峈狟蔥朓眸鳳                                                  
//                         }                    
//                 }                                                                                   
//         }
//
// TIM_ClearITPendingBit(TIM2, TIM_IT_CC1|TIM_IT_Update); //&#63378;壺笢剿梓祩弇
//
//}


//------------------------------------------------ --------//






/**
  * @brief This function handles PPP interrupt request.
  * @param None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/






0条回答

一周热门 更多>