STM32F1 定时器TIM3 4路PWM波输出 ,部分复用IO口 ,由于TIM3PWM输出引脚与JTAG 引脚冲突
所以要先禁用JTAG 。才会有4路PWM输出。
main.c
/* 使用为正点原子精英板 *//* 接上LED等可以看到效果 */#include <stm32f10x.h>//#include "myusart.h"#include "timer.h"#include "oled.h"#include "delay.h"int main(){u16 pwm;delay_init();LED_init();OLED_init();//USART3_Init();MOTOR_TIME3_Init(999,719);OLED_ShowCharString(10,10,"XXXXXXXX:",16);OLED_RESET_Gram();while(1){pwm++;if(pwm>600) {pwm = 0;}TIM_SetCompare1(TIM3,pwm);TIM_SetCompare2(TIM3,pwm);TIM_SetCompare3(TIM3,pwm);TIM_SetCompare4(TIM3,pwm);OLED_ShowNum(48,45,pwm,3,16);OLED_RESET_Gram();};}
tim3.h
//输出通道设置PWM占空比// ************************************************************************************************* //// ************************************************************************************************* ////TIM3初始化设置 **此函数部分映射输出通道**// Period :自动重装在值// Prescaler:分频系数//CH1\CH2 ->> PB4\PB5;//CH3\CH4 ->> PB0\PB1;void MOTOR_TIME3_Init(u16 Period,u16 Prescaler) //此处设置为 *(999,71)*{GPIO_InitTypeDef GPIO_Inittim3;TIM_TimeBaseInitTypeDef TIM_TimeBaseInittim3;TIM_OCInitTypeDef TIM_OCInittim3;//NVIC_InitTypeDef NVIC_Initpwm;RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //使能GPIOB时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //定时器3时钟GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE); //TIM3部分冲映射GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); //关闭JTAG-DPTIM_DeInit(TIM3);GPIO_Inittim3.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_0 | GPIO_Pin_1;GPIO_Inittim3.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出GPIO_Inittim3.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_Inittim3);//初始化GPIOTIM_TimeBaseInittim3.TIM_ClockDivision=TIM_CKD_DIV1; //时钟分割TIM_TimeBaseInittim3.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式TIM_TimeBaseInittim3.TIM_Period=Period; //自动重装载值TIM_TimeBaseInittim3.TIM_Prescaler=Prescaler; //预分频值TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInittim3);TIM_OCInittim3.TIM_OCMode=TIM_OCMode_PWM2; //选择我PWM2模式输出TIM_OCInittim3.TIM_OCPolarity=TIM_OCPolarity_High; //高电平有效TIM_OCInittim3.TIM_Pulse = 0; //设置初始比较值为0TIM_OCInittim3.TIM_OutputState=TIM_OutputState_Enable; //使能输出状态TIM_OC1Init(TIM3,&TIM_OCInittim3); //初始化以上参数TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable); //使能输出比较TIM_OCInittim3.TIM_OCMode=TIM_OCMode_PWM2; //选择我PWM2模式输出TIM_OCInittim3.TIM_OCPolarity=TIM_OCPolarity_High; //高电平有效TIM_OCInittim3.TIM_Pulse = 0; //设置初始比较值为0TIM_OCInittim3.TIM_OutputState=TIM_OutputState_Enable; //使能输出状态TIM_OC2Init(TIM3,&TIM_OCInittim3); //初始化以上参数TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable); //使能输出比较TIM_OCInittim3.TIM_OCMode=TIM_OCMode_PWM2;TIM_OCInittim3.TIM_OCPolarity=TIM_OCPolarity_High;TIM_OCInittim3.TIM_Pulse = 0;TIM_OCInittim3.TIM_OutputState=TIM_OutputState_Enable;TIM_OC3Init(TIM3,&TIM_OCInittim3);TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);TIM_OCInittim3.TIM_OCMode=TIM_OCMode_PWM2;TIM_OCInittim3.TIM_OCPolarity=TIM_OCPolarity_High;TIM_OCInittim3.TIM_Pulse = 0;TIM_OCInittim3.TIM_OutputState=TIM_OutputState_Enable;TIM_OC4Init(TIM3,&TIM_OCInittim3);TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);TIM_ARRPreloadConfig(TIM3,ENABLE);TIM_Cmd(TIM3,ENABLE);}
个人见解,感谢阅读。