STMCU小助手
发布时间:2023-1-17 21:00
|
stm32控制直流电机9 z( {1 o" ?: I# m k. B/ V #include "sys.h"4 V* f( g7 I+ G' i #include "DCmotor.h"/ P* J" J; Z. m, L void DCmotor_Init(void) {1 S- u' `* Y; J7 r& ?: d GPIO_InitTypeDef GPIO_InitStructure;% Z1 `* Z" c) U& }2 m RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10| GPIO_Pin_11 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;, M5 l$ z' ~ s# W- ^9 Z GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);3 w# G4 i4 ~; m3 ~4 `0 o5 a GPIO_SetBits(GPIOA,GPIO_Pin_10); GPIO_ResetBits(GPIOA,GPIO_Pin_11); - A+ b* P5 O# [9 `7 v 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;* g2 e7 o$ d7 _9 X# ]4 j+ q GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);% K% C) d8 x4 U+ U }/ P& G% C9 h' g; w: V' P" O1 W y 7 b! i( G# E& L5 I# ?2 Y void TIM4_PWM_Init(u32 arr,u32 psc)& _) O) b8 k: x: e { w l& ~! ^5 {: ~& t //此部分需手动修改IO口设置 + @! g$ w. d. K- A* f' Q6 | GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;0 v( H4 o! z- e2 g6 u TIM_OCInitTypeDef TIM_OCInitStructure;" X, j1 v- Z% c" L9 |, r % E7 Y8 R% R6 g5 F1 _$ n6 G' v" y RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //TIM14时钟使能 " w; T7 V, J6 ]! {, k1 ] RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //使能PORTF时钟 ) N8 S$ A+ g+ b; T 8 a; R, A( P$ Z# _7 n! [2 E GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_TIM4); //GPIOF9复用为定时器14 # [/ G2 F. n- G& }* | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //GPIOF9 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能- w5 ^# |5 A& j* V% M4 \+ R GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉 GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化PF9 B+ d& [/ Z4 H1 L8 o TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式/ I7 m- Q; I3 x7 I* _# ~7 n TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值 TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; * F G9 Y! l, W( a" T7 w TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);//初始化定时器14 . v! [; W: y% V B; E //初始化TIM14 Channel1 PWM模式 / ?3 b# }' K# g+ y0 D; z TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 l, x" g7 B6 ^! n TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低 TIM_OC1Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 4OC1 " H( `9 \/ t6 f& G5 Z- O8 e( j3 f + C! t/ H5 p$ l L' L1 E. o TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM14在CCR1上的预装载寄存器" B9 t* @& `. i$ |- `8 _ . G/ L0 C0 [! U* f& ~% i; | TIM_ARRPreloadConfig(TIM4,ENABLE);//ARPE使能 3 a3 `# s* F* j5 z9 ^ F TIM_Cmd(TIM4, ENABLE); //使能TIM148 B0 h2 |* P2 @% v4 j2 ] ) F5 D! |5 S* b ) w' C9 z& e9 m& f } 4 x6 f$ n9 D2 a2 [! S ———————————————— V# I+ [; L9 u* F9 F& e ? 版权声明:地板不会说谎 |
微信公众号
手机版