STM32F1 定时器 PWM输入捕获两路

__IO u32 TIM4CH3_CAPTURE_UPVAL = 0;//通道3捕获到高电平的时刻
__IO u32 TIM4CH3_CAPTURE_DOWNVAL = 0;//通道3捕获到低电平的时刻

__IO u32 TIM4CH4_CAPTURE_UPVAL = 0;//通道4捕获到高电平的时刻
__IO u32 TIM4CH4_CAPTURE_DOWNVAL = 0;//通道4捕获到低电平的时刻

__IO u32 TIM4CH3_HIGHTIME = 0; //通道3捕获总高电平的时间
__IO u32 TIM4CH3_LOWTIME = 0;  //通道3捕获总低电平的时间

__IO u32 TIM4CH4_HIGHTIME = 0; //通道4捕获总高电平的时间
__IO u32 TIM4CH4_LOWTIME = 0;  //通道4捕获总低电平的时间

__IO u32 tim4_T4_0 = 0;
__IO u32 tim4_T4_1 = 0;
__IO u32 tim4_T3_0 = 0;
__IO u32 tim4_T3_1 = 0;

__IO u8 TIM4CH3_Count = 0; //通道1捕获计数
__IO u8 TIM4CH4_Count = 0; //通道2捕获计数

__IO float Frequency1;
__IO float Frequency2; //频率
__IO float DutyCycle1;
__IO float DutyCycle2; //输出占空比

void TIM4_Cap_Init(u16 arr, u16 psc) {
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);  //使能TIM4时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //使能GPIOD时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//使能复用时钟

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Pin =
      GPIO_Pin_14 | GPIO_Pin_15;                // PB14,15 清除之前设置
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉输入
  GPIO_Init(GPIOD, &GPIO_InitStructure);
	
  GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);//开启TIM4重映射
  //初始化定时器4 TIM4
  TIM_TimeBaseStructure.TIM_Period = arr;    //设定计数器自动重装值
  TIM_TimeBaseStructure.TIM_Prescaler = psc; //预分频器
  TIM_TimeBaseStructure.TIM_ClockDivision =
      TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式
  TIM_TimeBaseInit(
      TIM4,
      &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位

  //初始化TIM4输入捕获参数 通道3
  TIM4_ICInitStructure.TIM_Channel =
      TIM_Channel_3; // CC3S=01 	选择输入端 IC3映射到TI3上
  TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //下降沿捕获
  TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI上
  TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
  TIM4_ICInitStructure.TIM_ICFilter = 0x00; // 配置输入滤波器 不滤波
  TIM_ICInit(TIM4, &TIM4_ICInitStructure);

  //初始化TIM4输入捕获参数 通道2
  TIM4_ICInitStructure.TIM_Channel =
      TIM_Channel_4; // CC3S=01 	选择输入端 IC4映射到TI4上
  TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //下降沿捕获
  TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI上
  TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
  TIM4_ICInitStructure.TIM_ICFilter = 0x00; // 配置输入滤波器 不滤波
  TIM_ICInit(TIM4, &TIM4_ICInitStructure);
  //中断分组初始化
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;           // TIM4中断
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级2
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;        //响应优先级1
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道被使能
  NVIC_Init(
      &NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器

//允许CC3IE,CC4IE捕获中断
  TIM_ITConfig(TIM4, TIM_IT_CC3, ENABLE);
  TIM_ITConfig(TIM4, TIM_IT_CC4, ENABLE);
  
  TIM_Cmd(TIM4, ENABLE); //使能定时器4
}

TIM4_Cap_Init(0xffff,144-1); //STM32F1  72M,计满65536约需131毫秒

void TIM4_IRQHandler(void) {
  //采用定时器溢出中断,不管定时器频率如何变化,总是会出现定时器溢出中断更新不及时的情况,即计数值已重新技术到比0大的值,但定时器溢出中断未产生
  if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET) { //捕获3发生捕获事件
    TIM_ClearITPendingBit(TIM4, TIM_IT_CC3);        //清除中断标志位
    if (TIM4CH3_Count == 0) {
      //捕获到下降沿,计算高电平时间
      TIM4CH3_CAPTURE_DOWNVAL = TIM_GetCapture3(TIM4);
      if (TIM4CH3_CAPTURE_DOWNVAL < TIM4CH3_CAPTURE_UPVAL) {
        tim4_T3_0 = 65536;
      } else {
        tim4_T3_0 = 0;
      }
      //计算高电平时间
      TIM4CH3_HIGHTIME =
          TIM4CH3_CAPTURE_DOWNVAL + tim4_T3_0 - TIM4CH4_CAPTURE_UPVAL;
      TIM4CH3_Count = 1;
      TIM_OC3PolarityConfig(TIM4, TIM_ICPolarity_Rising); //设置为上升沿捕获
    } else {
      //捕获到上升沿,计算低电平时间
      TIM4CH3_CAPTURE_UPVAL = TIM_GetCapture3(TIM4);
      if (TIM4CH3_CAPTURE_UPVAL < TIM4CH3_CAPTURE_DOWNVAL) {
        tim4_T3_1 = 65536;
      } else {
        tim4_T3_1 = 0;
      }

      //计算低电平时间
      TIM4CH3_LOWTIME =
          TIM4CH3_CAPTURE_UPVAL + tim4_T3_1 - TIM4CH3_CAPTURE_DOWNVAL;
      TIM4CH3_Count = 0;
      //计算占空比和频率
      DutyCycle1 = (float)(TIM4CH3_HIGHTIME * 100) /
                   (TIM4CH3_LOWTIME + TIM4CH3_HIGHTIME);
      Frequency1 = (float)72000000 / 144 / (TIM4CH3_LOWTIME + TIM4CH3_HIGHTIME);
      if (DutyCycle1 > 60 || DutyCycle1 < 40) {
        TIM4CH3_Count = 0;
      }
      TIM_OC3PolarityConfig(TIM4, TIM_ICPolarity_Falling); //设置为下降沿捕获
    }
  }

  if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET) { //捕获4发生捕获事件
    TIM_ClearITPendingBit(TIM4, TIM_IT_CC4);        //清除中断标志位
    if (TIM4CH4_Count == 0) {
      //捕获到下降沿,计算高电平时间
      TIM4CH4_CAPTURE_DOWNVAL = TIM_GetCapture4(TIM4);
      if (TIM4CH4_CAPTURE_DOWNVAL < TIM4CH4_CAPTURE_UPVAL) {
        tim4_T4_0 = 65536;
      } else {
        tim4_T4_0 = 0;
      }
      //计算高电平时间
      TIM4CH4_HIGHTIME =
          TIM4CH4_CAPTURE_DOWNVAL + tim4_T4_0 - TIM4CH4_CAPTURE_UPVAL;
      TIM4CH4_Count = 1;
      TIM_OC4PolarityConfig(TIM4, TIM_ICPolarity_Rising); //设置为上升沿捕获
    } else {
      //捕获到上升沿,计算低电平时间
      TIM4CH4_CAPTURE_UPVAL = TIM_GetCapture4(TIM4);
      if (TIM4CH4_CAPTURE_UPVAL < TIM4CH4_CAPTURE_DOWNVAL) {
        tim4_T4_1 = 65536;
      } else {
        tim4_T4_1 = 0;
      }

      //计算低电平时间
      TIM4CH4_LOWTIME =
          TIM4CH4_CAPTURE_UPVAL + tim4_T4_1 - TIM4CH4_CAPTURE_DOWNVAL;
      TIM4CH4_Count = 0;
      //计算占空比和频率
      DutyCycle2 = (float)(TIM4CH4_HIGHTIME * 100) /
                   (TIM4CH4_LOWTIME + TIM4CH4_HIGHTIME);
      Frequency2 = (float)72000000 / 144 / (TIM4CH4_LOWTIME + TIM4CH4_HIGHTIME);
      if (DutyCycle2 > 60 || DutyCycle2 < 40) {
        TIM4CH4_Count = 0;
      }
      TIM_OC4PolarityConfig(TIM4, TIM_ICPolarity_Falling); //设置为下降沿捕获
    }
  }
}

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页