
stm32控制直流电机) P$ P) Q: k' N, G( | #include "sys.h" #include "DCmotor.h"+ J& V' O* c; f void DCmotor_Init(void)5 [0 q5 P! B% M/ X { GPIO_InitTypeDef GPIO_InitStructure;# d. o2 X7 J# ?# ^ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);- n4 Z/ H7 l+ J( t1 w" m) u GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10| GPIO_Pin_11 ;. `8 R& F8 q i GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;8 Q5 I* V p" v7 G. ?3 n4 X. c GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA,GPIO_Pin_10);" N* [7 Q" {! a/ D8 e GPIO_ResetBits(GPIOA,GPIO_Pin_11); , K- r, X0 E# L* z9 M- y$ O+ t ' h7 [2 ]8 z5 C0 x; D RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;$ E. _ j* C% O. L; H GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;' ?- O' U" |7 d4 @ Z$ K, K GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); } : e8 B& J; n) X 0 b8 F) X) F0 _: f void TIM4_PWM_Init(u32 arr,u32 psc) { 8 k$ ? p% {; V8 s( t/ A+ @/ J //此部分需手动修改IO口设置3 ?" ~& _" @0 A8 e& n v B- O0 P+ t8 A5 E$ | GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure;! x6 }7 t2 N2 U2 M+ ]4 R RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //TIM14时钟使能 # {8 [, V4 K5 y, ^7 B, a RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //使能PORTF时钟 \# Q9 a7 `# n; r- U W GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_TIM4); //GPIOF9复用为定时器14- u$ r1 y. I1 |1 O+ T$ y& j; t" ] $ Y$ {; z' B9 [; P7 X GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //GPIOF9* B, ?) s$ S$ U GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz/ p6 s1 H: ~3 b( s+ U9 Y4 n GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉! w2 Y" ~2 \/ C0 p# F7 o) b" W GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化PF9 . H! y: t* E! Y' c TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值5 N+ M' ~" P5 a+ g. _4 I! b0 n TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; 4 a( ]4 Y3 H$ u/ h# M- q! k TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);//初始化定时器14 //初始化TIM14 Channel1 PWM模式 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式20 X! n0 F& i5 a6 u TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低 TIM_OC1Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 4OC1 5 l' E; }6 Z+ `- k% `) { TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM14在CCR1上的预装载寄存器 " M' T& C* M5 a( K+ B/ ?$ ` TIM_ARRPreloadConfig(TIM4,ENABLE);//ARPE使能 7 V: f; ?% I; r& D& B2 G) v ! B2 e( h) v4 L6 u$ j5 r* M TIM_Cmd(TIM4, ENABLE); //使能TIM140 b9 Z& r( H8 Z$ l 2 {9 a4 }7 E3 t. ? a- d } ————————————————0 P' l7 }4 O! Q) K/ S! J0 v' f 版权声明:地板不会说谎! N$ E# C1 k' |# e+ H) v2 r, [3 M! o- B : z. Q* @% n8 H4 t2 [ |