以通用定时器TIM3产生脉冲信号,连接到高级定时器TIM1,捕获TIM1的PWM信号
PWM输入捕获只能使用通道1和通道2,因为触发从模式控制器的信号只有TI1和TI2的
#include "stm32f10x.h"
//PWM 输出模式 TIM2产生PWM信号 F=TIM_CLK/[(ARR+1)(PSC+1)] 令频率为1kHz,使得psc=71,arr=9
void TIM_Config()//Period=9 当Channel2Pulse=5,占空比为50%
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
//PWM输出引脚初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; //PA1 PA2 - PWM_OutPut
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//时基结构体初始化
TIM_TimeBaseStructure.TIM_Period = 9;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//输出比较结构体初始化
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_Cmd(TIM2, ENABLE);
TIM_CtrlPWMOutputs(TIM2, ENABLE);//使能TIM2 PWM输出模式
}
//令可以捕获的最小频率为1kHz,使得psc=71,arr=999。即,如果输出的频率小于1kHZ,即可捕获的最大周期为1us,就捕获不到了
void PMW_IN(void)//TIM3接收信号,CH2通道接收来自
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//输入引脚配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//CH2通道
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入 用以捕获方波
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//中断配置
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//时钟基础初始化
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//PWM输入基本配置
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; //选择通道2,对应输入引脚为PA7,
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(TIM3, &TIM_ICInitStructure); //PWM输入配置
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); //选择TIMx输入捕获的触发信号,因为选择的是CH2通道,直连映射,所以是TI2FP2作为触发信号
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); //选择从模式:复位模式
//复位模式:上升沿重新初始化计数器,并且产生一个更新寄存器的信号
//PWM输入模式时,从模式必须工作在复位模式,当捕获开始,计数器CNT复位
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
TIM_Cmd(TIM3, ENABLE); //使能计数器
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); //允许捕获/比较2中断
}
中断服务函数
void TIM3_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
IC1Value=TIM_GetCapture1(TIM3);//占空比
IC2Value=TIM_GetCapture2(TIM3);//周期
if(IC2Value!=0)
{
Dutycle=(float)((IC1Value+1)/(IC2Value+1));//占空比
frequency=(float)(72000000/1000/(IC2Value+1);//频率
}
else
{
Dutycle=0;
frequency=0;
}
}