stm32控制直流电机
2 |4 `1 P4 _, X* r1 I8 t0 U. S! _1 Y& l
- #include "sys.h"
+ f+ Z* e1 h9 G. e# D8 N - #include "DCmotor.h"1 K5 i; c, a0 r4 t
- void DCmotor_Init(void)
{( M6 u/ @' S) ~! ?2 t - {
; ?" N+ x# j0 I2 o' |! p3 [1 M - GPIO_InitTypeDef GPIO_InitStructure;
9 D' _1 P+ g& N6 m+ F - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
L7 m' h7 N' ^' W - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10| GPIO_Pin_11 ;
- J4 Q4 ^" M* {+ h! q$ V+ ]0 F1 q - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
% t9 z! t; q* s: M7 S- w L - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+ S( e& K. U1 K: C v - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;+ Q( u' P5 n' P& [; I1 |5 i2 Q" B
- GPIO_Init(GPIOA, &GPIO_InitStructure);3 x+ p" r0 l! F! x1 {3 _( |
- GPIO_SetBits(GPIOA,GPIO_Pin_10);
0 B( w6 Y* D: a( j$ Q+ e6 { - GPIO_ResetBits(GPIOA,GPIO_Pin_11);
3 x: k& x& i. E3 G2 ]* b8 F1 ^# \ - k+ v+ N" z% i& G; {- b( Z1 K
- * @/ w/ ?( o5 b' i
-
+ H) y9 z; I6 ` - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
3 c- R9 E4 i' T }4 y+ \ - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;
" M3 p2 {/ \1 y$ P, s - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
. R) M$ R* ?5 `( k; j% ] - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;9 M4 B2 W4 q3 c* X6 v" i6 A, c1 v
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;, g) x& p6 t( }! T5 K& J3 z
- GPIO_Init(GPIOB, &GPIO_InitStructure);
3 z! l# ^ ^# f: S -
% f$ h/ s! m! I9 i; h# h -
; u+ k5 s& S! ^ - }
* J% Y. z. b" Z* i. A. ^ - 0 o7 O" w- w8 F2 u( o' G4 M
: ~- C1 }3 J# A2 Q+ W G. X+ A- n- void TIM4_PWM_Init(u32 arr,u32 psc)% u+ F' _& k3 Z
- {
0 H( S% h- c; h# E3 U( C - //此部分需手动修改IO口设置
5 ^4 t! l: D% k# F - " \- |/ x7 s" e& ~) I- c
- GPIO_InitTypeDef GPIO_InitStructure;+ j1 X* V' z0 G4 x+ _$ M6 V
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;6 \+ k% k- Q p7 } B3 ]: s
- TIM_OCInitTypeDef TIM_OCInitStructure;' N* \. _: `3 I+ ^& `/ r
-
; E2 \! T- a3 E+ P - RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //TIM14时钟使能
{1 Z& c" \- E1 t" b: c - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //使能PORTF时钟
$ ]" V( u% \' x) p7 R - % ^) l4 u4 j# u; ~! b
- GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_TIM4); //GPIOF9复用为定时器144 l8 t& o) }1 |( ` N1 \. b4 [
-
) q6 Y& @* A& C: z - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //GPIOF9
( o& }' y) J& K) J4 E: p, B - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
- f5 r3 Y0 F+ I( g+ D; V - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
- e6 d' ^# D: R - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
& L+ [: F6 v3 o B* f& i - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
- ]) z) Z- K2 x# r - GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化PF9& L) v3 }+ Q/ U- I6 ?) i
-
' t9 E8 H. N8 ~8 m" Y! q - TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频4 t5 q+ m/ d' p/ C
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
E3 Y* w; r! }& f0 o - TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值
5 o/ Q; Y( h& I2 U - TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
9 ?8 z# `$ |2 {9 h! \+ W -
* I0 e+ t) G* D9 c- w/ Q7 b0 b - TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);//初始化定时器14+ Z& \ d1 a3 ~! A2 ]" h
-
8 D0 t i1 F/ K - //初始化TIM14 Channel1 PWM模式 " {; S* O( Z7 g' |4 R* s( C# j
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
. N0 D/ u7 {& K' S' A" a4 i, { d - TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
6 r: Q' `2 ]3 }, J) N0 d - TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低; Y9 M% V" D* R
- TIM_OC1Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 4OC1
& P( T5 F3 Q- W+ q - / z: l" A, ~1 `) b, D7 L$ V
- TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM14在CCR1上的预装载寄存器. N3 |! W* l% L7 p; t* v0 O
- 7 j7 ?; x: s* f$ e' ^: X" v9 y
- TIM_ARRPreloadConfig(TIM4,ENABLE);//ARPE使能 & F+ K" V' ^! x; [) e( U
- ; c# Z( t, H5 n3 c% W) U
- TIM_Cmd(TIM4, ENABLE); //使能TIM14+ S$ k' `2 H: F( c. V
-
+ N+ E/ m r* k6 }' L: h - + t. W8 Z( L" p6 C U
- } ) H- b9 ` A S0 m2 g" ?
复制代码 ————————————————
: d: c2 Z$ q4 R5 x版权声明:地板不会说谎" g+ S! j. }- i7 w3 @8 |
1 h) c$ D& [( v. f9 O/ T
8 ~: x" t+ D& I3 z' B |