STM32-定时器输入捕获

目录

一、输入捕获简介

二、频率测量方法

三、输入捕获框图

四、输入捕获通道

五、输入捕获基本结构

六、PWMI基本结构

 七、开发步骤

八、输入捕获库函数

九、实验

9.1 输入捕获测频率

9.2 PWMI模式测频率和占空比


一、输入捕获简介

>IC(Input Capture)输入捕获
>输入捕获模式下,当通道输入引脚出现指定电平跳变时,当前CNT的值将被锁存到CCR       中,可用于测量PWM波形的频率、占空比、脉冲间隔、电平持续时间等参数
>每个高级定时器和通用定时器都拥有4个输入捕获通道
>可配置为PWMI模式,同时测量频率和占空比
>可配合主从触发模式,实现硬件全自动测量

二、频率测量方法

STM32-定时器输入捕获_第1张图片

三、输入捕获框图

STM32-定时器输入捕获_第2张图片

四、输入捕获通道

STM32-定时器输入捕获_第3张图片

五、输入捕获基本结构

STM32-定时器输入捕获_第4张图片

六、PWMI基本结构

STM32-定时器输入捕获_第5张图片

 七、开发步骤

①RCC开启时钟,GPIO和TIM时钟打开

②GPIO初始化,把GPIO配置成输入模式

③配置时基单元,让CNT计数器在内部时钟的驱动下自增运行

④配置输入捕获单元(滤波器,极性,直连/交叉通道,分频器)

⑤选择从模式的触发源

⑥选择触发后执行的操作

⑦调用TIM_Cmd函数,开启定时器

八、输入捕获库函数

void TIM_ICInit(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct);

//结构体配置输入捕获单元(单个通道)

void TIM_PWMIConfig(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct);

//配置两个通道

void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource);

//选择输入触发源TRGI

void TIM_SelectOutputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_TRGOSource);

//选择输出触发源TRGO

void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_SlaveMode);

//选择从模式

======================配置各个通道的预分频器============================

void TIM_SetIC1Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC);
void TIM_SetIC2Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC);
void TIM_SetIC3Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC);
void TIM_SetIC4Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC);

======================分别读取4个通道的CCR=============================

uint16_t TIM_GetCapture1(TIM_TypeDef* TIMx);
uint16_t TIM_GetCapture2(TIM_TypeDef* TIMx);
uint16_t TIM_GetCapture3(TIM_TypeDef* TIMx);
uint16_t TIM_GetCapture4(TIM_TypeDef* TIMx);

九、实验

9.1 输入捕获测频率

实验设置:PA0输出PWM波,PA6输入捕获

PWM.c

#include "stm32f10x.h"                  // Device header

/*PWM初始化*/
void PWM_Init(void)
{
	/*一、RCC开启时钟,TIM与GPIO时钟打开*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
//	/*使用重映射(*_*)*/
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE);//PA0->PA15
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//解除JATG调试
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//(*_*)GPIO_Pin_15
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	/*二、配置时基单元(时钟源选择和时基单元配置)*/
	TIM_InternalClockConfig(TIM2);//选择内部时钟
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 100 - 1;		//ARR自动重装器值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1;	//PSC预分频器值
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//重复计数器值
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
	
	/*三、配置输出比较单元(CCR的值(捕获/比较器),输出比较模式,极性选择,输出使能)*/
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);//给结构体所有成员赋初始值(有些用不到但是必须赋值)
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//输出比较模式
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//输出比较的极性
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出使能
	TIM_OCInitStructure.TIM_Pulse = 0;//设置CCR
	TIM_OC1Init(TIM2,&TIM_OCInitStructure);
	
	/*四、配置GPIO,复用推挽输出*/
	//一处已操作
	
	/*五、运行控制,启动计数器CNT*/
	TIM_Cmd(TIM2,ENABLE);
}
	
/*封装函数->更改CCR值来改变占空比*/
void PWM_SetCompare1(uint16_t Compare)
{
	TIM_SetCompare1(TIM2,Compare);
}

/*通过PSC的改变来修改频率*/
void PWM_SetPrescaler(uint16_t Prescaler)
{
	TIM_PrescalerConfig(TIM2,Prescaler,TIM_PSCReloadMode_Immediate);
}

PWM.h

#ifndef __PWM_H
#define __PWM_H

void PWM_Init(void);
void PWM_SetCompare1(uint16_t Compare);
void PWM_SetPrescaler(uint16_t Prescaler);

#endif

IC.c

#include "stm32f10x.h"                  // Device header

//输入捕获初始化
void IC_Init(void)
{
	/*一、RCC开启时钟,GPIO和TIM时钟打开*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	/*二、GPIO初始化,把GPIO配置成输入模式*/
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	/*三、配置时基单元,让CNT计数器在内部时钟的驱动下自增运行*/
	TIM_InternalClockConfig(TIM3);//选择内部时钟
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 65536 - 1;		//ARR自动重装器值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;	//PSC预分频器值
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//重复计数器值
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
	
	/*四、配置输入捕获单元(滤波器,极性,直连/交叉通道,分频器)*/
	TIM_ICInitTypeDef TIM_ICInitStructure;
	TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;//通道
	TIM_ICInitStructure.TIM_ICFilter = 0xF;//滤波器
	TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿触发 
	TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//不分频
	TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//直连通道
	TIM_ICInit(TIM3,&TIM_ICInitStructure);
	
	/*五、选择从模式的触发源(TI1FP1)*/
	TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
	
	/*六、选择触发后执行的操作,Reset重置CNT*/
	TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
	
	/*七、调用TIM_Cmd函数,开启定时器*/
	TIM_Cmd(TIM3,ENABLE);
}

uint32_t IC_GetFreq(void)
{
	return 1000000 / (TIM_GetCapture1(TIM3) + 1);
}

IC.h

#ifndef __IC_H
#define __IC_H

void IC_Init(void);
uint32_t IC_GetFreq(void);

#endif

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "PWM.h"
#include "IC.h"


int main(void)
{
	OLED_Init();
	PWM_Init();
	IC_Init();
	
	//(输出比较)输出一个频率1kHz,占空比50%的PWM信号  
	PWM_SetPrescaler(720-1);		//频率f = 72M / (PSC+1)(ARR+1)
	PWM_SetCompare1(50);			//Duty = CCR/(ARR+1)
	
	//输入捕获
	//IC_GetFreq();
	
	OLED_ShowString(1,1,"Freq:");
	OLED_ShowString(1,11,"Hz");
	
	while (1)
	{
		OLED_ShowNum(1,6,IC_GetFreq(),5);
	}
}

9.2 PWMI模式测频率和占空比

PWM.c

#include "stm32f10x.h"                  // Device header

/*PWM初始化*/
void PWM_Init(void)
{
	/*一、RCC开启时钟,TIM与GPIO时钟打开*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
//	/*使用重映射(*_*)*/
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE);//PA0->PA15
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//解除JATG调试
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//(*_*)GPIO_Pin_15
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	/*二、配置时基单元(时钟源选择和时基单元配置)*/
	TIM_InternalClockConfig(TIM2);//选择内部时钟
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 100 - 1;		//ARR自动重装器值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1;	//PSC预分频器值
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//重复计数器值
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
	
	/*三、配置输出比较单元(CCR的值(捕获/比较器),输出比较模式,极性选择,输出使能)*/
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);//给结构体所有成员赋初始值(有些用不到但是必须赋值)
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//输出比较模式
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//输出比较的极性
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出使能
	TIM_OCInitStructure.TIM_Pulse = 0;//设置CCR
	TIM_OC1Init(TIM2,&TIM_OCInitStructure);
	
	/*四、配置GPIO,复用推挽输出*/
	//一处已操作
	
	/*五、运行控制,启动计数器CNT*/
	TIM_Cmd(TIM2,ENABLE);
}
	
/*封装函数->更改CCR值来改变占空比*/
void PWM_SetCompare1(uint16_t Compare)
{
	TIM_SetCompare1(TIM2,Compare);
}

/*通过PSC的改变来修改频率*/
void PWM_SetPrescaler(uint16_t Prescaler)
{
	TIM_PrescalerConfig(TIM2,Prescaler,TIM_PSCReloadMode_Immediate);
}

PWM.h

#ifndef __PWM_H
#define __PWM_H

void PWM_Init(void);
void PWM_SetCompare1(uint16_t Compare);
void PWM_SetPrescaler(uint16_t Prescaler);

#endif

IC.c

#include "stm32f10x.h"                  // Device header

//输入捕获初始化
void IC_Init(void)
{
	/*一、RCC开启时钟,GPIO和TIM时钟打开*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	/*二、GPIO初始化,把GPIO配置成输入模式*/
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	/*三、配置时基单元,让CNT计数器在内部时钟的驱动下自增运行*/
	TIM_InternalClockConfig(TIM3);//选择内部时钟
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 65536 - 1;		//ARR自动重装器值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;	//PSC预分频器值
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//重复计数器值
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
	
	/*四、配置输入捕获单元(滤波器,极性,直连/交叉通道,分频器)*/
	TIM_ICInitTypeDef TIM_ICInitStructure;
	TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
	TIM_ICInitStructure.TIM_ICFilter = 0xF;//滤波器
	TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
	TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//不分频
	TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//直连通道
	TIM_PWMIConfig(TIM3,&TIM_ICInitStructure);//通道2,交叉,下降沿检测
	
	/*五、选择从模式的触发源(TI1FP1)*/
	TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
	
	/*六、选择触发后执行的操作,Reset重置CNT*/
	TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
	
	/*七、调用TIM_Cmd函数,开启定时器*/
	TIM_Cmd(TIM3,ENABLE);
}

/*频率*/
uint32_t IC_GetFreq(void)
{
	return 1000000 / (TIM_GetCapture1(TIM3) + 1);
}

/*占空比*/
uint32_t IC_GetDuty(void)
{
	return (TIM_GetCapture2(TIM3)+1) * 100 / (TIM_GetCapture1(TIM3)+1);
}

IC.h

#ifndef __IC_H
#define __IC_H

void IC_Init(void);
uint32_t IC_GetFreq(void);
uint32_t IC_GetDuty(void);

#endif

main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "PWM.h"
#include "IC.h"


int main(void)
{
	OLED_Init();
	PWM_Init();
	IC_Init();
	
	OLED_ShowString(1,1,"Freq:00000Hz");
	OLED_ShowString(2,1,"Duty:00%");
	
	//(输出比较)输出一个频率1kHz,占空比50%的PWM信号  
	PWM_SetPrescaler(720-1);		//f = 72M / (PSC+1)(ARR+1)
	PWM_SetCompare1(50);			//Duty = CCR/(ARR+1)
	
	//输入捕获
	//IC_GetFreq();
	//IC_GetDuty();
	
	while (1)
	{
		OLED_ShowNum(1,6,IC_GetFreq(),5);
		OLED_ShowNum(2,6,IC_GetDuty(),2);
	}
}

你可能感兴趣的:(STM32,stm32,前端,嵌入式硬件)