Bootstrap

【STM32-学习笔记-4-】PWM、输入捕获(PWMI)

1、PWM

image-20241219143627929

PWM配置

  1. 配置时基单元
  2. 配置输出比较单元
  3. 配置输出PWM波的端口
#include "stm32f10x.h"                  // Device header

void PWM_Init(void)
{
//**配置输出PWM波的端口*****************************************************************
    RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);// 使能GPIOA端口的时钟,因为我们要使用这个端口来输出PWM信号
    
    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);
//*************************************************************************************
//**配置时基单元************************************************************************
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//使能时钟
    
    TIM_InternalClockConfig(TIM2);// 配置定时器的内部时钟模式
  //这段代码的作用是将指定的定时器配置为内部时钟模式,即将定时器的时钟源设置为内部时钟,而不是外部信号
        
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;//定义定时器基本时间配置结构体
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//设置定时器时钟分频(1分频)
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//计数模式(向上计数)
    TIM_TimeBaseInitStruct.TIM_Period = 100 - 1;//自动重装载寄存器的值ARR
    TIM_TimeBaseInitStruct.TIM_Prescaler = 720 - 1;//预分频器的值PSC//1khz    
    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;//输出比较模式
    TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;//输出比较极性
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//输出使能
    TIM_OCInitStruct.TIM_Pulse = 0;//设定CCR寄存器的值;更改CRR的值,以控制占空比(CCR/(ARR+1))
    TIM_OC1Init(TIM2, &TIM_OCInitStruct);
//*************************************************************************************
    TIM_Cmd(TIM2, ENABLE);//启用定时器,PB0口输出PWM波
}

void PWM_SetCompare1(uint16_t Compare)//设置CRR的值以控制占空比(CCR/(ARR+1))
{
    TIM_SetCompare1(TIM2, Compare);
}

2、输入捕获配置

#include "stm32f10x.h"                  // Device header
//PWMI模式
//输入捕获测频率 PA6口

void IC_Init(void)
{
	//配置输入捕获的io口
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;//PA6对应TIM3
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	//配置时基单元
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//开启时钟
	
	TIM_InternalClockConfig(TIM3);//配置定时器的内部时钟模式
	//这段代码的作用是将指定的定时器配置为内部时钟模式,
	//即将定时器的时钟源设置为内部时钟,而不是外部信号
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//1分频
	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;//选择通道1
	TIM_ICInitStruct.TIM_ICFilter = 0x10;//指定输入捕获过滤器
	TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿
	TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;//1分频
	TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;//直连通道
  
	TIM_ICInit(TIM3, &TIM_ICInitStruct);//自动配置通道2,下降沿
	
	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);
	//TIM_GetCapture1此函数可获取CCR1捕获/比较器中的值
}

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

image-20241225152632356

image-20241225152717082

3、编码器

#include "stm32f10x.h"                  // Device header

void Encoder2_Init(void)
{
	//配置GPIO
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	//配置时基单元
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//开启定时器时钟
	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 = 1 - 1;//PSC
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
	
	TIM_ICInitTypeDef TIM_ICInitStruct;
	TIM_ICStructInit(&TIM_ICInitStruct);
	//配置通道1的滤波器和极性
	TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
	TIM_ICInitStruct.TIM_ICFilter = 0xF;
	TIM_ICInit(TIM3, &TIM_ICInitStruct);
	//配置通道2的滤波器和极性	
	TIM_ICInitStruct.TIM_Channel = TIM_Channel_2;
	TIM_ICInitStruct.TIM_ICFilter = 0xF;
	TIM_ICInit(TIM3, &TIM_ICInitStruct);
	
	TIM_EncoderInterfaceConfig(	TIM3, 
								TIM_EncoderMode_TI12, 
								TIM_ICPolarity_Rising, //不反向
								TIM_ICPolarity_Rising);//不反向-可控制极性
	TIM_Cmd(TIM3, ENABLE);
}

int16_t Encoder_Get(void)
{
	return TIM_GetCounter(TIM3);
}

int16_t Encoder_GetSpeed(void)
{
	int16_t Temp_Count = TIM_GetCounter(TIM3);
	TIM_SetCounter(TIM3, 0);//计数器清零
	return Temp_Count;
	
}

image-20241225163925175

image-20241225163943410

image-20241225164028067

;