我使用TIM3在单脉冲模式下工作,控制TIM2输出精确个数的pwm脉冲,PB10为TIM2的重映射功能。(同时另外两个引脚输出高电平,但这个不影响其它应该) 软件仿真,PB10一直没有脉冲输出,请大家帮忙看看代码,指导一下有什么问题?提前谢谢了。
#include "stm32f10x.h" #include "tim.h" void TIM2_TIM3_PWM(u16 Cycle, u16Pulse_Num); void Motor_Init(u16 TIM3per, u16 TIM2per,u16 TIM2Compare1); int main(void) { Motor_Init(1000, 1000, 1000); TIM2_TIM3_PWM(1000, 1500); GPIO_SetBits(GPIOB,GPIO_Pin_1); GPIO_SetBits(GPIOB,GPIO_Pin_11); while (1); // add your code; } #include "tim.h" void Motor_Init(u16 TIM3per, u16 TIM2per,u16 TIM2Compare1) { TIM_TimeBaseInitTypeDefTIM_TimeBaseInitStruct; TIM_OCInitTypeDef TIM_OCInitStruct; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 |RCC_APB1Periph_TIM3,ENABLE);// 打开时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB,ENABLE);//打开时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//打开复用时钟 GPIO_PinRemapConfig(GPIO_FullRemap_TIM2,ENABLE); //重映射时钟 GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;//复用推挽输出 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;// GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin= GPIO_Pin_1 | GPIO_Pin_11;// I/O输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinRemapConfig(GPIO_FullRemap_TIM2,ENABLE); //完全重映射 //TIM3工作在单脉冲模式下 TIM_TimeBaseInitStruct.TIM_ClockDivision =TIM_CKD_DIV1; TIM_TimeBaseInitStruct.TIM_CounterMode =TIM_CounterMode_Up; TIM_TimeBaseInitStruct.TIM_Prescaler= 7200; TIM_TimeBaseInitStruct.TIM_Period = TIM3per; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct); TIM_SelectOnePulseMode(TIM3,TIM_OPMode_Single);//单脉冲配置 TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);//使能预装载 TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC1Ref);//作为触发输出 TIM_OCInitStruct.TIM_OCMode =TIM_OCMode_PWM2; TIM_OCInitStruct.TIM_OutputState =TIM_OutputState_Enable; TIM_OCInitStruct.TIM_OCPolarity =TIM_OCPolarity_High; TIM_OCInitStruct.TIM_Pulse = 1; TIM_OC1Init(TIM3,&TIM_OCInitStruct); TIM_Cmd(TIM3,DISABLE);//先不使能 //TIM2 TIM_TimeBaseInitStruct.TIM_ClockDivision =TIM_CKD_DIV1; TIM_TimeBaseInitStruct.TIM_CounterMode =TIM_CounterMode_Up; TIM_TimeBaseInitStruct.TIM_Prescaler = 720; TIM_TimeBaseInitStruct.TIM_Period = TIM2per; TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct); TIM_SelectSlaveMode(TIM2,TIM_SlaveMode_Gated);//门控模式,高电平使能 TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);//主从模式使能 TIM_SelectInputTrigger(TIM2,TIM_TS_ITR2);//选择触发源 TIM_OCInitStruct.TIM_OCMode =TIM_OCMode_PWM2; TIM_OCInitStruct.TIM_OutputState =TIM_OutputState_Enable; TIM_OCInitStruct.TIM_OCPolarity =TIM_OCPolarity_High; TIM_OCInitStruct.TIM_Pulse = TIM2Compare1;//比较捕获的预装载值 TIM_OC3Init(TIM2,&TIM_OCInitStruct); TIM_Cmd(TIM2,ENABLE); } voidTIM2_TIM3_PWM(u16 Cycle, u16 Pulse_Num) //Cycle周期,Pulse_Num脉冲个数 { u16 TIM2per = 0; u32 Time = 0; Time = Cycle * Pulse_Num; Time /= 100; //预分频7200,100us计数一次 TIM2per = Cycle/10; //预分频720,10us计数一次 TIM_SetAutoreload(TIM3, Time+1); //设置TIM3重装值 TIM_SetAutoreload(TIM2, TIM2per-1); //设置TIM2重装值 TIM_SetCompare1(TIM2,TIM2per/2); //设置占空比50% TIM_Cmd(TIM3,ENABLE); //使能 }
|