STM32: TIMER门控模式控制PWM输出长度

搞了两天单脉冲没搞定,无意中发现,这个利用主从模式的门控方式来控制一路PWM的输出长度很有效。

 1 //TIM2 PWM输出,由TIM4来控制其输出与停止
 2 //frequency_tim2:TIM2 PWM输出周期:KHz
 3 //duty_tim2:TIM2 PWM占空比  0-100
 4 //period_tim4: TIM4控制TIM2总周期,单位0.1ms
 5 //duty_tim4: TIM4控制TIM2输出时间,单位0.1ms
 6 
 7 void TIM2_CH3_PA2_PWM_TIM4_CH4_GATE(unsigned short int frequency_tim2,char duty_tim2,unsigned short int period_tim4,unsigned short int duty_tim4)
 8 {  
 9 
10     GPIO_InitTypeDef          GPIO_InitStructure;
11     TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
12     TIM_OCInitTypeDef         TIM_OCInitStructure;
13     
14     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM4, ENABLE);    //使能定时器2时钟
15      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA  | RCC_APB2Periph_AFIO, ENABLE);  //使能GPIO外设和AFIO复用功能模块时钟
16         
17     //设置该引脚为复用输出功能,输出TIM2 CH3的PWM脉冲波形    GPIOA.2
18     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //TIM2_CH3
19     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //复用推挽输出
20     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
21     GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
22  
23     //初始化TIM2
24     TIM_TimeBaseStructure.TIM_Period = (1000/frequency_tim2); //设置在下一个更新事件装入活动的自动重装载寄存器周期的值,本身频率为1MHz,又分频器设置的下面一行
25     TIM_TimeBaseStructure.TIM_Prescaler =(72-1); //设置用来作为TIMx时钟频率除数的预分频值,72MHz/72=1MHz 
26     TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
27     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
28     TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
29     
30     //初始化TIM2 Channel3 PWM模式     
31     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
32     TIM_OCInitStructure.TIM_Pulse=(10*duty_tim2/frequency_tim2);       //(TIM_Period)*(duty/100) --> (1000/period)*(duty/100)
33      TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
34     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
35     TIM_OC3Init(TIM2, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM2 OC3
36 
37     TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);  //使能TIM2在CCR3上的预装载寄存器 
38     TIM_Cmd(TIM2, ENABLE);  //使能TIM2
39 
40     //初始化TIM4
41     TIM_TimeBaseStructure.TIM_Period = period_tim4; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值,本身频率为1MHz,又分频器设置的下面一行
42     TIM_TimeBaseStructure.TIM_Prescaler =(7200-1); //设置用来作为TIMx时钟频率除数的预分频值,72MHz/72=1MHz 
43     TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
44     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
45     TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
46     
47     //初始化TIM4 Channel4 PWM模式     
48     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
49     TIM_OCInitStructure.TIM_Pulse=duty_tim4;       //(TIM_Period)*(duty/100) --> (1000/period)*(duty/100)
50      TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
51     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
52     TIM_OC4Init(TIM4, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM4 OC4
53 
54     TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);  //使能TIM4在CCR4上的预装载寄存器 
55     TIM_Cmd(TIM4, ENABLE);  //使能TIM4
56 
57     
58     TIM_SelectInputTrigger(TIM2, TIM_TS_ITR3);     //选择有效输入端 ,查表得知TIM4对于TIM2是ITR3       
59     TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Gated);  //配置为主从复位模式
60     TIM_SelectOutputTrigger(TIM4,TIM_TRGOSource_OC4Ref ); 
61     /* Enable timer's master/slave work mode */ 
62     TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);   //Slave
63     TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);   //Master
64 
65 }
TIM2_CH3_PA2_PWM_TIM4_CH4_GATE

参照网上朋友代码加了一点修改直接看函数就OK了,蛮简单的

posted @ 2013-11-25 13:22  wwjdwy  阅读(1986)  评论(0编辑  收藏  举报