STM32的PWM控制4个舵机

本人虽然接触STM32快半年了,但是最近才开始系统的学习STM32,建议一边学,一边做东西,能够更快的提升自己

我用的定时器是TIM3,所以我会把我出现的问题,分享给大家,希望大佬多多指教!

因为我先进行部分映射,但是控制某个舵机的PB4引脚却没有反应,所以百度了好久…

应该注意的是PB4在单片机复位后,它是作为JTAG引脚来使用的,也就是说,要使用PB4你需要自己将其配置成普通GPIO,GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);

配置成普通GPIO后,需要进行部分重映射,GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3 , ENABLE);

timer.c

void TIM3_PWM_Init(u16 arr,u16 psc)
{
       
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);	//使能定时器3
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);  //使能GPIO外设和AFIO复用功能模块时钟
	
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); 
	GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3部分重映射 TIM3_CH1->PB4 TIM3_CH2->PB5
 
   //设置该引脚为复用输出模式,输出TIM3 CH2的PWM脉冲波形 GPIOB.5 GPIOB.0 GPIOB.1 GPIOB.4
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_4; //TIM_CH1 TIM_CH2 TIM_CH3 TIM_CH4
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO
 
   //初始化TIM3
	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
	
	//初始化TIM3 Channe12 PWM模式 
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1
 	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输出比较极性高
//	TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
//	TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
//	TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
	TIM_OC1Init(TIM3, &TIM_OCInitStructure);//根据T指定的参数初始化TIM3 OC1
	TIM_OC2Init(TIM3, &TIM_OCInitStructure);  //根据T指定的参数初始化TIM3 OC2
	TIM_OC3Init(TIM3, &TIM_OCInitStructure);//根据T指定的参数初始化TIM3 OC3
	TIM_OC4Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化TIM3 OC4

	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);  //使能TIM3在CCR2上的预装载寄存器
	TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
	 
	TIM_Cmd(TIM3, ENABLE);  //使能TIM3
//	TIM_CtrlPWMOutputs(TIM3, ENABLE);
	TIM_ARRPreloadConfig(TIM3, ENABLE);
 
}

main.c

int main(void)
 {
     		
 	u16 led0pwmval=0;
	u8 dir=1;	
	delay_init();	    	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 	 
	uart_init(115200);	
 	LED_Init();			  
 	TIM3_PWM_Init(199,7199);	 //不分频,PWM=72000000/900=80Khz
   	while(1)
	{
     
		delay_ms(1000);
		TIM_SetCompare1(TIM3,5);
delay_ms(1000);
		TIM_SetCompare1(TIM3,10);
delay_ms(1000);
		TIM_SetCompare2(TIM3,15);
delay_ms(1000);
		TIM_SetCompare2(TIM3,20);
delay_ms(1000);
		TIM_SetCompare2(TIM3,25);
delay_ms(1000);
		TIM_SetCompare3(TIM3,15);
delay_ms(1000);
		TIM_SetCompare3(TIM3,20);
delay_ms(1000);
		TIM_SetCompare3(TIM3,25);
delay_ms(1000);
		TIM_SetCompare4(TIM3,15);
delay_ms(1000);
		TIM_SetCompare4(TIM3,20);
delay_ms(1000);
		TIM_SetCompare4(TIM3,25);
	}	
 }

STM32重映射引脚

STM32的PWM控制4个舵机_第1张图片

舵机1先转,再是舵机2,然后舵机3,最后舵机4

你可能感兴趣的:(STM32,stm32,PWM,舵机)