点击打开链接原出处 谢谢大家 一起学习
01 | /**************************** |
02 | **TIM2的通道1使用单脉冲模式 |
03 | **TIM3使用门控模式用来输出PWM |
04 | ** |
05 | ****************************/ |
06 | //TIM2per:重装值 |
07 | //Compare1:比较捕获1的预装载值 |
08 | void Motor_Init(u16 TIM2per, u16 TIM3per, u16 TIM3Compare1) |
09 | { |
10 | TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; |
11 | TIM_OCInitTypeDef TIM_OCInitStruct; |
12 | GPIO_InitTypeDef GPIO_InitStruct; |
13 | |
14 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //使能定时器2的时钟 |
15 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //使能定时器3的时钟 |
16 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能GPIOA时钟 |
17 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能复用IO时钟 |
18 | |
19 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 |
20 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_6; //PA0 |
21 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; |
22 | GPIO_Init(GPIOA,&GPIO_InitStruct); |
23 | |
24 | //TIM2工作在单脉冲下 |
25 | TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; //没有时钟分割 |
26 | TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 |
27 | TIM_TimeBaseInitStruct.TIM_Prescaler = 7200; //预分频值,每100us计数一次 |
28 | TIM_TimeBaseInitStruct.TIM_Period = TIM2per; //重装值 |
29 | TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct); |
30 | |
31 | TIM_SelectOnePulseMode(TIM2,TIM_OPMode_Single); //设置TIM2在单脉冲模式,且是单一的脉冲,在下一个更新事件停止 |
32 | TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable); //使能定时器2的通道1预装载寄存器 |
33 | TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_OC1Ref); |
34 | |
35 | TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2; //在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为有效电平 |
36 | TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //OC1输出使能 |
37 | TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; //有效电平为高 |
38 | TIM_OCInitStruct.TIM_Pulse = 1; //比较捕获1的预装载值 |
39 | TIM_OC1Init(TIM2,&TIM_OCInitStruct); |
40 | |
41 | TIM_Cmd(TIM2,DISABLE); //先不使能能TIM2 |
42 | |
43 | |
44 | //TIM3工作在从模式的门控模式下的PWM输出模式 |
45 | TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; //没有时钟分割 |
46 | TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 |
47 | TIM_TimeBaseInitStruct.TIM_Prescaler = 720; //预分频值,10us计数一次 |
48 | TIM_TimeBaseInitStruct.TIM_Period = TIM3per; //重装值 |
49 | TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct); |
50 | |
51 | TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Gated); //TIM3为门控模式 |
52 | TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable); //使能TIM3的主从模式 |
53 | TIM_SelectInputTrigger(TIM3,TIM_TS_ITR1); //内部触发,从TIM2触发 |
54 | |
55 | TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2; //在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为有效电平 |
56 | TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; //OC1输出使能 |
57 | TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; //有效电平为高 |
58 | TIM_OCInitStruct.TIM_Pulse = TIM3Compare1; //比较捕获1的预装载值 |
59 | TIM_OC1Init(TIM3,&TIM_OCInitStruct); |
60 | |
61 | TIM_Cmd(TIM3,ENABLE); //使能TIM3 |
62 | } |
63 |
64 | //设置PWM的输出 |
65 | //Cycle:为周期,单位(us) |
66 | //Pulse_Num:为脉冲个数(小于3200) |
67 | void TIM2_TIM3_PWM(u16 Cycle, u16 Pulse_Num) |
68 | { |
69 | u16 TIM3per = 0; |
70 | u32 Time = 0; |
71 | //改变TIM3的重装值改变脉冲频率这里设置脉冲占空比为50% |
72 | //改变TIM2的预分频器和重装值改变脉冲个数 |
73 | |
74 | Time = Cycle * Pulse_Num; |
75 | Time /= 100; //预分频为7200,100us计数一次 |
76 | TIM3per = Cycle/10; //预分频为720,10us计数一次 |
77 | |
78 | TIM_SetAutoreload(TIM2, Time+1); //设置TIM2的重装值 |
79 | TIM_SetAutoreload(TIM3, TIM3per-1); //设置TIM3的重装值 |
80 | TIM_SetCompare1(TIM3,TIM3per/2); //设置占空比为50% |
81 | TIM_Cmd(TIM2,ENABLE); //使能TIM2 |
82 | } |