色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > STM32F107VCT6的通用定時器的輸入捕獲功能配置

          STM32F107VCT6的通用定時器的輸入捕獲功能配置

          作者: 時間:2016-11-10 來源:網(wǎng)絡 收藏
          將TIM4配置成輸入捕獲功能用于計算矩形波的周期。

          #define NVIC_Priority_Timer4 1

          本文引用地址:http://cafeforensic.com/article/201611/317514.htm

          void TIM4_Init_Config(void)
          {
          GPIO_InitTypeDef GPIO_InitStructure;
          NVIC_InitTypeDef NVIC_InitStructure;
          TIM_ICInitTypeDef TIM_ICInitStructure; //輸入捕獲結(jié)構(gòu)體
          TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //時基結(jié)構(gòu)體

          RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

          /*
          *PD15-TIM4_Ch4 矩形方波的輸入管腳
          */
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_Init(GPIOD, &GPIO_InitStructure);

          GPIO_PinRemapConfig(GPIO_Remap_TIM4 , ENABLE); //TIM4_CH4/PD15 硬件已經(jīng)映射好的,不能將TIM4_CH4用于其他管腳

          NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
          NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_Priority_Timer4;
          NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
          NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
          NVIC_Init(&NVIC_InitStructure);

          /*

          * 設置TIM4的時基:(1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+0xFFFF)/72M)*(1+71) = 65536/1M

          * (定時器計滿16位用時,則計一個數(shù)用時1/1M)

          */
          TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
          TIM_TimeBaseStructure.TIM_Prescaler = 71;
          TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
          TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
          TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

          TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
          TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //PD15管腳檢測下降沿
          TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
          TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
          TIM_ICInitStructure.TIM_ICFilter = 0xF;
          TIM_ICInit(TIM4, &TIM_ICInitStructure);

          TIM_Cmd(TIM4, ENABLE);
          TIM_ITConfig(TIM4, TIM_IT_CC4, ENABLE);
          }

          stm32f10x_it.c文件中

          unsigned int IC4ReadValue1 = 0;
          unsigned int IC4ReadValue2 = 0;
          unsigned int CaptureNumber_ch4 = 0;
          unsigned int Capture_ch4 = 0;

          void TIM4_IRQHandler(void)
          {
          //每當PD15管腳檢測到下降沿,就會進入到該中斷處理函數(shù)
          if (TIM_GetITStatus(TIM4, TIM_IT_CC4) == SET)
          {

          TIM_ClearITPendingBit(TIM4, TIM_IT_CC4);

          if (CaptureNumber_ch4 == 0)
          {

          CaptureNumber_ch4 = 1;
          IC4ReadValue1 = TIM_GetCapture4(TIM4);
          }
          else if (CaptureNumber_ch4 == 1)
          {

          CaptureNumber_ch4 = 0;
          IC4ReadValue2 = TIM_GetCapture4(TIM4);
          if (IC4ReadValue2 > IC4ReadValue1)
          {
          Capture_ch4 = (IC4ReadValue2 - IC4ReadValue1);
          }
          else if (IC4ReadValue2 < IC4ReadValue1)
          {
          Capture_ch4 = ((0xFFFF - IC4ReadValue1) + IC4ReadValue2);
          }
          else
          {
          Capture_ch4 = 0;
          }
          }

          }
          }




          評論


          技術(shù)專區(qū)

          關(guān)閉