STM32高级定时器TIM1相关功能配置

TIM1_CHx:PWM的主通道
TIM1_CHxN:PWM互补输出通道
STM32高级定时器TIM1相关功能配置_第1张图片
配置TIM1_CH3N-----PB1引脚,需要开启重映射,查阅《STM32中文参考手册》P119,PB1对应着TIM1_CH3N的部分重映射,综上:若要使PB1输出PWM波,需要配置TIM1开启重映射和PWM互补输出功能。代码如下:

void TIM1_init(u16 arr, u16 psc)
{
	GPIO_InitTypeDef         GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_OCInitTypeDef       TIM_OCInitStructure;
	TIM_BDTRInitTypeDef      TIM_BDTRInitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);  
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1,ENABLE);
	
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;  
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);


	TIM_DeInit(TIM1);
	TIM_TimeBaseInitStructure.TIM_Period = arr;     
	TIM_TimeBaseInitStructure.TIM_Prescaler = psc;  
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;    
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure); 

	TIM_OCInitStructure.TIM_OCMode 				= TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OutputState 	=	 TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_OutputNState 	= TIM_OutputNState_Enable;
	TIM_OCInitStructure.TIM_Pulse 				= 0;
	TIM_OCInitStructure.TIM_OCPolarity 		= TIM_OCPolarity_High;
	TIM_OCInitStructure.TIM_OCNPolarity 	= TIM_OCNPolarity_High;
	TIM_OCInitStructure.TIM_OCIdleState 	= TIM_OCIdleState_Reset;     
	TIM_OCInitStructure.TIM_OCNIdleState 	= TIM_OCIdleState_Reset;

	TIM_OC3Init(TIM1, &TIM_OCInitStructure);

	TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);

	//死区控制
	/*
		TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
		TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
		TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
		TIM_BDTRInitStructure.TIM_DeadTime = 0;
		TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;               
		TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
		TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
		TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
	*/

	TIM_Cmd(TIM1, ENABLE);
	TIM_CCPreloadControl(TIM1,ENABLE);
	TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

设置占空比函数
TIM_SetCompare3(TIM1,500);
//初始化
TIM1_init(899,0);
STM32高级定时器TIM1相关功能配置_第2张图片
周期80Khz,占空比44.4%
完美使用TIM互补输出PWM。

你可能感兴趣的:(STM32)