STM32 TIM输入捕获源码

int32 Motor1_CAPTURE_HValue;
int32 Motor1_CAPTURE_Duty;
int32 Motor1_CAPTURE_Period;
int32 Motor2_CAPTURE_HValue;
int32 Motor2_CAPTURE_Duty;
int32 Motor2_CAPTURE_Period;
int32 Motor3_CAPTURE_HValue;
int32 Motor3_CAPTURE_Duty;
int32 Motor3_CAPTURE_Period;

TIM_ICInitTypeDef  TIM3_ICInitStructure;
//arr设置捕获级数,psc预分频比
void TIM3_Cap_Init(u32 arr,u16 psc)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
	GPIO_Init(GPIOB,&GPIO_InitStructure);

	GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_TIM3);
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource1,GPIO_AF_TIM3);
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_TIM3);

	TIM_TimeBaseStructure.TIM_Prescaler=psc;
	TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseStructure.TIM_Period=arr;
	TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
	TIM3_ICInitStructure.TIM_Channel = TIM_Channel_2;
	TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
	TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
	TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
	TIM3_ICInitStructure.TIM_ICFilter = 0x00;
	TIM_ICInit(TIM3, &TIM3_ICInitStructure);
	TIM3_ICInitStructure.TIM_Channel = TIM_Channel_3;
	TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
	TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
	TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
	TIM3_ICInitStructure.TIM_ICFilter = 0x00;
	TIM_ICInit(TIM3, &TIM3_ICInitStructure);
	TIM3_ICInitStructure.TIM_Channel = TIM_Channel_4;
	TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
	TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
	TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
	TIM3_ICInitStructure.TIM_ICFilter = 0x00;
	TIM_ICInit(TIM3, &TIM3_ICInitStructure);
	TIM_ITConfig(TIM3, TIM_IT_CC2,ENABLE);
	TIM_ITConfig(TIM3, TIM_IT_CC3,ENABLE);
	TIM_ITConfig(TIM3, TIM_IT_CC4,ENABLE);
	TIM_Cmd(TIM3,ENABLE );
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	TIM_SetCounter(TIM3,0);
//以下为捕获中断,我只给出了一个通道的捕获处理,其他两通道同理。Motor1_CAPTURE_Period为捕获的周期,Motor1_CAPTURE_Duty为捕获的高电平时间
void TIM3_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
	{
		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5))
		{
			Motor1_CAPTURE_Period = TIM_GetCapture2(TIM3) - Motor1_CAPTURE_HValue;
			if (Motor1_CAPTURE_Period < 0) Motor1_CAPTURE_Period += 65535;
			Motor1_CAPTURE_HValue = TIM_GetCapture2(TIM3);
		}
		else
		{
		Motor1_CAPTURE_Duty = TIM_GetCapture2(TIM3) - Motor1_CAPTURE_HValue;
		if (Motor1_CAPTURE_Duty < 0) Motor1_CAPTURE_Duty += 65535;
		}
		TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
	}
}
}

先上代码如上,TIM3三通道捕获。关于捕获的概念我这里也说明一下:
输入捕获是用来检测外部事件和输入信号,比如监测一个类似PWM信号,想要知道这个PWM信号的周期和占空比,这就需要用到捕获去获取。每当信号产生边沿跳变时(可以指定是上升沿或下降沿或双边沿),定时器捕捉到特定的边沿跳变后,把计数寄存器当前的值锁存到通道寄存器。所以单片机可以从通道寄存器读取数值从而判断此信号的周期,占空比。

你可能感兴趣的:(STM32,TIM,捕获,多通道)