TIM-output compare (PWM) - STM32

TIM-Output Compare - STM32

Oc (Output Compare) Output compare The output
compare can set the output level to 1, set to 0 or reverse the operation by comparing the relationship between the CNT and CCR register values, and is used to output PWM waveforms with a certain frequency and duty cycle
. Both the advanced timer and the general-purpose timer have 4 output compare channels The first 3 channels of the advanced timer additionally have the functions of dead zone generation and complementary output

insert image description here
insert image description here
insert image description here

insert image description here
insert image description here
insert image description here
Step 1: RCC turns on the clock, and turns on the clocks of the TIM peripherals and GPIO peripherals we want to use.
Step 2: Configure the time base unit, including the previous clock source selection.
Step 3: Configure the output comparison unit, which includes The value of this CCR, output comparison mode, polarity selection, output enable these parameters.
The fourth step is to configure GPIO, and initialize the GPIO port corresponding to PWM to the configuration of multiplexed push-pull output.
The fifth step is to run the control. Start the counter so that the PWM can be output.
insert image description here

insert image description here
insert image description here
The pre-installed function is the shadow register (similar to the buffer register).
insert image description here
insert image description here
insert image description here
The one with N is the configuration of the complementary channel in the advanced timer
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
. If you don’t want to list all the members and assign values, you can first assign an initial value with Stiuctlnit, and then change your The value you want to change is fine,
insert image description here
only set the GPIO to multiplex push-pull output
insert image description here

insert image description here
insert image description here
insert image description here
insert image description here

#include "stm32f10x.h"                  // Device header

void PWM_Inint(void)
{
	//第一步开启时钟,初始化RCC,选择TIM2
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	//设置输出PWM的GPIO端口
		//使能GPIOA的时钟 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);	
	//GPIOA模式初始化
	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);
	
	//第二步选择时基单元的时钟,选择内部时钟,选择TIM2
	TIM_InternalClockConfig(TIM2);
	
	//第三步配置时基单元
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分频系数 1分频
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//计数的模式 向上计数
	//频率为1KHz,占空比为50%的PWM波形
	//计数器溢出频率:CK CNT_OV = CKCNT /(ARR + 1)= CK PSC /(PSC + 1)/(ARR +1)
	//配置定时为1s,则CK CNT_OV=1;CKCNT=72MHz=72000000;
	TIM_TimeBaseInitStruct.TIM_Period = 100 - 1;//周期,ARR自动重装器的值 范围0~65536
	TIM_TimeBaseInitStruct.TIM_Prescaler = 720 - 1;//PSC预分频器的值 范围0~65536
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0; //重复计数器的值 高级计数器才有
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
	
	//第四步配置输出中断
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//更新中断
	
	//清除中断标志位,能够避免刚初始化完就进中断的问题
	TIM_ClearFlag(TIM2,TIM_IT_Update);
	
	//第五步,配置输出比较单元,里面包括这个CCR的值、输出比较模式、极性选择、输出使能这些参数。
	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;//50;//用来设置CCR 
	TIM_OC1Init(TIM2,&TIM_OCInitStruct);

	//第六步,运行控制
	TIM_Cmd(TIM2,ENABLE);
}

//让LED呈现呼吸灯的效果,那就是不断更改CCR的值就行了
void PWM_SetCompare1(uint16_t Compare)
{
	TIM_SetCompare1(TIM2,Compare);

}

#ifndef _PWM_H
#define _PWM_H

void PWM_Inint(void);
void PWM_SetCompare1(uint16_t Compare);

#endif

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "KEY.h"
#include "PWM.h"

uint8_t i;//定义一个全局变量

int main(void)
{

	
	while(1)
	{
			for(i=0;i<100;i++)
			{
				PWM_SetCompare1(i);
				Delay_ms(10);
			}
			
			for(i=0;i<100;i++)
			{
				PWM_SetCompare1(100-i);
				Delay_ms(10);
			}
	}
}

pin remapping

insert image description here
Need to use AFIO.
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
If you want to use the three pins PA15, PB3, and PB4 as GPIO;
insert image description here
if you want to remap the multiplexed pins of timers or other peripherals
insert image description here

	//第一步开启时钟,初始化RCC,选择TIM2
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	
		//实现重映射,将TIM2_CH2通道重映射到PA15
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//打开AFIO时钟
	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE);
	//关闭PA15端口的复用
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
	
	//设置输出PWM的GPIO端口
		//使能GPIOA的时钟 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);	
	//GPIOA模式初始化
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;//GPIO_Pin_0;//将PA0改到PA15了
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);

Guess you like

Origin blog.csdn.net/qq_45159887/article/details/130464168