stm32控制直流电机; T, \: t, C5 @8 B2 T5 O/ H #include "sys.h"6 l5 G9 T- d- @* m* }( R4 b/ b1 e #include "DCmotor.h"' P, Y/ Z/ l) s1 L void DCmotor_Init(void) { GPIO_InitTypeDef GPIO_InitStructure;1 N, [4 F+ v5 t$ f RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10| GPIO_Pin_11 ;; @# n1 P' t! p4 c( U GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;, ?( |6 l8 P# n) J5 Q3 s GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;# H0 D2 m' e6 P; E7 [9 r GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;1 D% s$ ], a2 |' A4 `! R$ k% |/ j GPIO_Init(GPIOA, &GPIO_InitStructure);6 G' B9 l, G; k) J9 N* D7 O! z0 Z) P GPIO_SetBits(GPIOA,GPIO_Pin_10); x) {1 _" {7 v( z- e( a GPIO_ResetBits(GPIOA,GPIO_Pin_11);/ x- o0 ?9 g- s( D& p" c8 i1 u ; M/ }# K3 ]* V& m 3 |4 [2 c p. T H RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;5 g. ^ W0 Z0 b GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;8 }6 w8 T/ E A4 M4 h1 g, z5 D GPIO_Init(GPIOB, &GPIO_InitStructure); , R7 X e$ w+ s+ ] }0 f2 j; Q% r: h 3 ~6 V3 u' w+ z. n! [' h 5 w( V" `. B( h" v0 f* f1 J4 n + ?1 h: o/ P( H6 |5 s: a void TIM4_PWM_Init(u32 arr,u32 psc): g2 v7 X2 W( x { 1 ]$ Y J' X1 o& F/ d8 q1 i //此部分需手动修改IO口设置0 y0 `: I/ n# U7 k5 z* F. P( B - a3 t+ C2 a* `) B# A) u GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;/ {- H8 j& e/ u- } x7 W TIM_OCInitTypeDef TIM_OCInitStructure; 5 i2 }2 j) i0 S RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //TIM14时钟使能 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //使能PORTF时钟 5 {3 c \ P' e# I ( H4 M1 K* m* J+ q& a3 B0 p1 u GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_TIM4); //GPIOF9复用为定时器14* [- s" @, Q5 |' Z4 Y 9 Z4 n' }% }3 m GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //GPIOF94 {: s k1 R. M7 n* J. g0 k8 x GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能 I/ H% k. N9 M GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉' }1 P$ y: q7 D: u GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化PF9 TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值0 ^% ^( v0 t3 `& P TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);//初始化定时器14# k: z5 G8 d, f A //初始化TIM14 Channel1 PWM模式 ' j" O( Y& l6 \0 S. {, u TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2) ^4 @6 v; V0 m$ D7 m( t" ^ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低 TIM_OC1Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 4OC1* R) P& g+ x$ @& @# P) U: n) E5 [ ' q/ \3 d8 w" D$ S TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM14在CCR1上的预装载寄存器. R8 h+ G8 O& {9 Z! C' C8 }! L ! S# i/ r$ v; ~0 g g TIM_ARRPreloadConfig(TIM4,ENABLE);//ARPE使能 TIM_Cmd(TIM4, ENABLE); //使能TIM145 p0 ]# {( Q' W$ m Y- _ } ————————————————. |. O9 G8 \- v6 s. w 版权声明:地板不会说谎/ p; z2 r3 J2 ~" B8 k( O ! Z* g$ ^% z' X |