TIM输入捕获
输入捕获简介
频率测量
输入捕获通道
主从触发模式
输入捕获基本结构
PWMI基本结构
输入捕获模式测频率
接线图
引脚选择
代码
IC.c
#include "stm32f10x.h" // Device header
void IC_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //使能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//直接使用PA0接口对应TIM2_CH1_ETR
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU; //上拉输入
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
TIM_InternalClockConfig(TIM3); //配置内部时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1; //时钟分频
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInitStruct.TIM_Period=65536-1; //自动重装器 ARR
TIM_TimeBaseInitStruct.TIM_Prescaler=72-1; //预分频器 PSC
TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct); //配置时基单元
TIM_ICInitTypeDef TIM_ICInitStruct;
TIM_ICInitStruct.TIM_Channel=TIM_Channel_1;
TIM_ICInitStruct.TIM_ICFilter=0xF;
TIM_ICInitStruct.TIM_ICPolarity=TIM_ICPolarity_Rising; //上升沿触发
TIM_ICInitStruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICSelection=TIM_ICSelection_DirectTI; //直连通道
TIM_ICInit(TIM3,&TIM_ICInitStruct); //输入捕获初始化
TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1); //触发源选择
TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset); //从模式选择
TIM_Cmd(TIM3,ENABLE); //启动定时器
}
uint32_t IC_GetFreq(void)
{
return 1000000/(TIM_GetCapture1(TIM3)+1); //Freq=Fc/N
}
IC.h
#ifndef __IC_H__
#define __IC_H__
void IC_Init(void);
uint32_t IC_GetFreq(void);
#endif
PWM.c
#include "stm32f10x.h" // Device header
void PWM_Init(void)
{
//产生一个频率为1KHz,占空比为50%,分辨率为1%的PWM波形
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //使能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//直接使用PA0接口对应TIM2_CH1_ETR
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
//使用重定义功能将PA15映射给TIM2_CH1_ETR
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //开启AFIO
// GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE); //引脚重映射配置 PA0->PA15
// GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); //JTAG-DP Disabled and SW-DP Enabled,让PA15解除JTDI,变为正常的GPIO口
// GPIO_InitTypeDef GPIO_InitStruct;
// GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
// GPIO_InitStruct.GPIO_Pin=GPIO_Pin_15;
// GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
// GPIO_Init(GPIOA,&GPIO_InitStruct);
TIM_InternalClockConfig(TIM2); //配置内部时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1; //时钟分频
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInitStruct.TIM_Period=100-1; //自动重装器 ARR
TIM_TimeBaseInitStruct.TIM_Prescaler=720-1; //预分频器 PSC
TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct); //配置时基单元
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_OCStructInit(&TIM_OCInitStruct); //给结构体默认初始值
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1; //输出比较模式:PWM1
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High; //输出比较极性:高电平
TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable; //输出使能
TIM_OCInitStruct.TIM_Pulse=0; //捕获比较寄存器 CCR
TIM_OC1Init(TIM2,&TIM_OCInitStruct); //TIM2的OC1通道初始化
TIM_Cmd(TIM2,ENABLE); //启动定时器
}
/**
* @brief 设置CCR的值(占空比)
* @param Compare: uint16_t
* @retval void
*/
void PWM_SetCompare1(uint16_t Compare)
{
TIM_SetCompare1(TIM2,Compare);
}
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
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "PWM.h"
#include "IC.h"
uint8_t i;
int main(void)
{
OLED_Init();
PWM_Init();
IC_Init();
PWM_SetPrescaler(720-1); //频率Freq=72M/(PSC+1)/100
PWM_SetCompare1(50); //Duty=CCR/100
OLED_ShowString(1,1,"Freq:00000Hz");
while(1)
{
OLED_ShowNum(1,6,IC_GetFreq(),5);
}
}
PWMI模式测频率占空比
IC.c
#include "stm32f10x.h" // Device header
void IC_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //使能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//直接使用PA0接口对应TIM2_CH1_ETR
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU; //上拉输入
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
TIM_InternalClockConfig(TIM3); //配置内部时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1; //时钟分频
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInitStruct.TIM_Period=65536-1; //自动重装器 ARR
TIM_TimeBaseInitStruct.TIM_Prescaler=72-1; //预分频器 PSC
TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct); //配置时基单元
TIM_ICInitTypeDef TIM_ICInitStruct;
TIM_ICInitStruct.TIM_Channel=TIM_Channel_1;
TIM_ICInitStruct.TIM_ICFilter=0xF;
TIM_ICInitStruct.TIM_ICPolarity=TIM_ICPolarity_Rising; //上升沿触发
TIM_ICInitStruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICSelection=TIM_ICSelection_DirectTI; //直连通道
TIM_PWMIConfig(TIM3,&TIM_ICInitStruct); //自动配置两个输入捕获初始化,输入一个结构体就行,另一个结构体自动相反配置
TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1); //触发源选择
TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset); //从模式选择
TIM_Cmd(TIM3,ENABLE); //启动定时器
}
uint32_t IC_GetFreq(void)
{
return 1000000/(TIM_GetCapture1(TIM3)+1); //Freq=Fc/N
}
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"
uint8_t i;
int main(void)
{
OLED_Init();
PWM_Init();
IC_Init();
PWM_SetPrescaler(720-1); //频率Freq=72M/(PSC+1)/100
PWM_SetCompare1(80); //Duty=CCR/100
OLED_ShowString(1,1,"Freq:00000Hz");
OLED_ShowString(2,1,"Duty:000%");
while(1)
{
OLED_ShowNum(1,6,IC_GetFreq(),5);
OLED_ShowNum(2,6,IC_GetDuty(),3);
}
}
标签:捕获,09,TIM,TimeBaseInitStruct,InitStruct,GPIO,IC,void
From: https://www.cnblogs.com/mzx233/p/18011766