#|【蓝桥杯嵌入式】【STM32】13_PWM输入捕获模式


文章目录

  • 前言
  • 1、PWM输入模式
  • 2、硬件设计
  • 3、软件设计

前言 ??STM32的定时器输入捕获模式可以通过更改输入捕获极性实现PWM的输入捕获,但实际上STM32也包含有非常方便的PWM输入捕获模式用以测量矩形波的频率和占空比
??下载工程文件:
??https://gitee.com/Joseph_Cooper/blue-bridge-embedded
1、PWM输入模式 #|【蓝桥杯嵌入式】【STM32】13_PWM输入捕获模式
文章图片

▲PWM输入模式于STM32官方数据手册 ??简单来说就是在输入捕获的基础上对同一个端口使用两个IC进行捕获分别设置为上升沿有效和下降沿有效,当捕获开始时清空计数器CNT的值,在下降沿和第二次上升沿到来时分别读取两个比较寄存器的数据用以测量脉冲的频率和占空比。
#|【蓝桥杯嵌入式】【STM32】13_PWM输入捕获模式
文章图片

▲通用定时器框图于STM32官方数据手册 2、硬件设计 【#|【蓝桥杯嵌入式】【STM32】13_PWM输入捕获模式】??本次使用到了CT117_EXA上的PA1PLUS1端口,可以实现调节RP3改变矩形波的输出频率。
#|【蓝桥杯嵌入式】【STM32】13_PWM输入捕获模式
文章图片

▲脉冲发生于CT117E_EXA 3、软件设计
#include "Headfile.h"__IO uint16_t IC2Value = https://www.it610.com/article/0; __IO uint16_t DutyCycle = 0; __IO uint32_t Frequency = 0; void RCC_Configuration(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); }void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin =GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); }void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }void TIM2_CC2Config() { TIM_ICInitTypeDefTIM_ICInitStructure; TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure; RCC_Configuration(); NVIC_Configuration(); GPIO_Configuration(); /* TIM2 configuration: PWM Input mode ------------------------ The external signal is connected to TIM3 CH2 pin (PA.01), The Rising edge is used as active edge, The TIM2 CCR2 is used to compute the frequency value The TIM2 CCR1 is used to compute the duty cycle value ------------------------------------------------------------ */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = 72; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_PWMIConfig(TIM2, &TIM_ICInitStructure); //选择输入捕获的触发信号 TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); // PWM输入模式时,从模式须工作在复位模式,当捕获开始时,计数器CNT被复位清零 /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); /* Enable the Master/Slave Mode */ TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); /* TIM enable counter */ TIM_Cmd(TIM2, ENABLE); /* Enable the CC2 Interrupt Request */ TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); }void TIM2_IRQHandler(void) { /* Clear TIM3 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); /* Get the Input Capture value */ IC2Value = TIM_GetCapture2(TIM2); if (IC2Value != 0) { /* Duty cycle computation */ DutyCycle = (TIM_GetCapture1(TIM2) * 100) / IC2Value; /* Frequency computation */ Frequency = (SystemCoreClock/72) / IC2Value; } else { DutyCycle = 0; Frequency = 0; } }

    推荐阅读