项目背景' @, q5 D. ]/ v2 g( j) q
在各类项目开发中,我们会经常涉及到步进电机的控制。一般情况下,单片机都是通过操作步进电机驱动器或驱动芯片实现对步进电机的控制。步进电机驱动器(芯片)一般都是留有三个接口En,Dir,Pul。En是步进电机使能(或脱机)的信号,高电平时,步进电机会断电脱机。Dir是步进电机转向信号,高电平与低电平代表着正转与反转。Pul是步进电机运动的脉冲信号,每接受到一个脉冲,步进电机运行一步(或一个细分步)。' V+ E. b2 u, ` u& w& `
: g7 h) ?4 [) \. r3 E驱动库介绍
) [' d5 H8 N/ E本步进电机驱动库主要实现了对步进电机驱动器上述三种信号的控制,同时也封装好了电机点动、长动、往复三种运动模式。通过将步进电机封装抽象为一个结构体类型,我们可以轻松实现对多个不同步进电机的运动控制。同时,本库函数配套的应用指南中也给出了步进电机与光电对管的配套使用例程。: ]- R6 X w3 q( F2 q& n
, N+ o$ @7 K v$ v源码分析, J2 n) \/ A# O- d
头文件) c* o$ c5 w$ }6 {
头文件中定义了步进电机结构体类型,它是对步进电机(驱动器)控制的抽象,与平台无关。在代码注释中有对每个成员的描述。特别需要注意的是,void StepMotor_TryMove(StepMotor_Struct* StepMotor_P)是电机运动控制的核心函数,它主要负责实现了电机三种运动模式。需要在定时器的更新中断服务函数中周期性的调用此函数。
$ a4 s2 D! M) {: R- ]* X
) [4 e+ p& F. ^/ e! T- /*+ }7 L' v, ~7 h% \/ B/ D
- * stepmotor.h
, J5 o' r& t1 N, y, X - *. G0 u5 u' z( R: b
- * Created on: 2020年8月31日4 {2 K. c. a. A) T: u3 \. `" `
- * Author: Tao8 x, r. X! I! W0 Y7 c9 g
- */
# L8 m: v& W3 l+ `& c' {1 p8 _
/ s( m1 h3 Q. n* b- #ifndef SOURCE_ALWHALESLIB_HARDWARE_INC_STEPMOTOR_H_" C9 H# k# @6 w0 x( V
- #define SOURCE_ALWHALESLIB_HARDWARE_INC_STEPMOTOR_H_
; y" j" d1 {5 S) _5 w1 r - 8 \% y6 d o4 ^ _. `) Q7 f) b# L
- #define USE_STEPMOTOR
! V( ?+ C4 K! N0 m, V - 5 S5 x4 Q+ x- E* ]; @# i7 s
- #ifdef USE_STEPMOTOR
$ C9 o. y$ r, ]/ l% g( j - % u. r }0 z/ K; I' x4 D
- #include "stepmotor.h"! X" f' @4 t4 Y V- K7 ~
- #include "stm32f10x_conf.h"* R# ?, e# n j; z6 Y( v; v
8 [/ X8 S* M( x' }& Y- #define SETPMOTOR_NUM 4
5 n7 L; l' G9 C4 K- Q/ f' A2 P - . Y4 L, K* B; `6 U' j
- /***步进电机的控制引脚映射***/
/ c: E/ n7 ^4 t' y/ ^; m4 z - #define STEPMOTOR_1_EN GPIOB_OUT(15)+ U- S8 Q# U3 q8 [2 s- U7 Q
- #define STEPMOTOR_1_DIR GPIOB_OUT(13), e. }: n, T1 I3 U1 b+ E
- #define STEPMOTOR_1_PUL GPIOB_OUT(14)
! V8 m5 z P& F
l% ^: ?( u9 R- #define STEPMOTOR_2_EN GPIOD_OUT(10)
! Q8 _, A9 ?$ R4 V/ o2 E1 I - #define STEPMOTOR_2_DIR GPIOD_OUT(8)
7 l+ [" k$ N1 R - #define STEPMOTOR_2_PUL GPIOD_OUT(9)2 J2 h: B' {7 S5 W& z
1 `3 ~6 S d6 p S6 w+ z( _$ x- #define STEPMOTOR_3_EN GPIOD_OUT(13)" T! o- q$ v& Y3 @, `- }
- #define STEPMOTOR_3_DIR GPIOD_OUT(11)* Z: k, }8 J k% B! V/ N1 E: G9 G$ }
- #define STEPMOTOR_3_PUL GPIOD_OUT(12)8 @7 q+ V/ h [! Q, d
- 8 a1 @" R7 f4 w
- #define STEPMOTOR_4_EN GPIOC_OUT(6)3 J0 N; b8 _3 Q1 u- Z
- #define STEPMOTOR_4_DIR GPIOD_OUT(14)
1 \8 g* |* I5 Q& P4 _6 Z0 p; F - #define STEPMOTOR_4_PUL GPIOD_OUT(15)
' I% L, {; F, N! k) }* s2 s ^ - /*******************************/
6 E8 g$ \4 U+ v& M! _! ^* d5 l& H( d
8 B. \: M, }1 l- * s' u% {; L. Y. I: K) P
- typedef void (*SetState_FP)(uint8_t state);
4 L5 h- q) E2 n8 @ - typedef void (*SetParam_FP)(uint32_t param);$ ?& G2 _6 b$ q$ d: V' x2 Q
6 |9 n: K' e% A- /**) }. Q% R+ I# w3 ^" g
- * @brief 步进电机的结构体封装9 S+ |, L9 a4 I1 p
- * triggerCount是电机单步运动的边沿次数,1: 单次,即电平跳变, 2: 双次,即脉冲
: ?$ J# T9 A3 N; g! }) u" H - * SetPulse是驱动电机运动的函数指针,一般情况下,应该为它注册一个操作GPIO端口电平的指针
# w$ f! s1 ]6 e( }5 F - * 一般会在定时器的更新中断服务函数中调用SetPulse,每次调用都将端口电平翻转 T1 u; h7 Z" f
- * triggerCount = 1 时,定时器的更新频率 = 步进电机速度(步/s)) L6 G% a( c/ F8 }' |0 N
- * triggerCount = 2 时,定时器的更新频率/2 = 步进电机速度(步/s)
[2 r# k" U9 N$ s! W' p2 [ - *9 Z6 `' `6 @* R8 Z
- * 一般通过定时器驱动步进电机的运动,定时器的启停即为电机的启停,定时器的频率即为电机的速度
2 t, O) q3 x0 ]# R0 M - * SetExec设置电机运动启停的函数指针,一般情况下,应该为它注册一个可以打开、关闭驱动定时器的函数; T" l8 A$ S1 _; ~4 X8 w8 d
- * SetSpeed是设置电机运动速度的函数指针,一般情况下,应该为它注册一个可以设置驱动定时器的更新周期的函数3 Z' a X8 T( T- r* t4 o
- */6 C) h' p& m$ L% a, e; h }+ Z3 I
- typedef struct
! U U z+ r% U9 J7 b' s0 T - {
' C4 E/ `( e+ ?7 O0 ?3 w+ _ - //电机是否正在运动& Q8 m+ B, C, k4 p5 P. c
- uint8_t IsRun;
: E6 t1 T3 O4 h+ U! {
8 v4 U- b, {; x. `0 w/ A3 m# P( d1 [- //电机单步运动的边沿次数(1: 单次,即电平跳变, 2: 双次,即脉冲)/ r! I; h6 n0 `9 X* H
- uint8_t triggerNum;
/ C, O0 ?8 |5 b' `& o
9 `) |5 `1 I: Q% d; y2 a/ x- //电机运行的步数计数0 q/ Q: W8 e! [: p$ A* u( E
- uint32_t StepCount;' I6 L% @/ |6 q8 C3 H' x+ [
5 f1 Y9 w q0 @* g5 ]- //电机循环计数(仅在工作模式为往复时生效)+ I* U1 l" J+ x% U! B
- uint8_t LoopCount;8 R1 j! i, ]0 h9 g U9 N3 S+ v
% X/ r! h0 m$ R- //电机设置运动步数
# s# ^6 H S- ~: |' u1 V3 I - uint32_t StepSV;
2 z5 A, D. X: k% a y% c - 4 L I5 G- v* s3 k. ~
- //电机的工作模式(0:距离, 1:往复, 2:长动)
$ S a; R9 @7 q$ q - uint8_t WorkMode;
8 R: c1 w- S8 b, b) ]
}8 _9 }; r: V7 ?/ M k2 m- //驱动步进电机的硬件定时器指针,
8 f6 G; w; {" U1 _6 P - //此成员与硬件相关,设置它的主要目的是为了编写各处理函数时的方便
" x3 T1 g1 o2 t7 _8 E/ k - // TIM_TypeDef* DriveTimer;
# B0 v4 o$ w7 {3 d/ h
' A+ }/ i, P" A* X, I- //设置激活电机的函数指针(激活电机后,电机将处于锁定状态,相当于通电)
: O' w. k3 b8 J# h+ P [4 i7 @, J - //只有处于激活状态的电机才能被控制
* B2 W9 P% w3 E9 R% E5 r - SetState_FP SetEn;
$ h& Q# A/ E/ v' _" q
" { h R) C" T) y% T- //设置电机运动方向的函数指针(0:正方向, 1:逆方向, 2:翻转方向)
K. r$ s- I) f x! L: m - SetState_FP SetDir;/ c: X) }4 a2 \# S9 B
- . c+ F0 T X* E! e! Y& U% S+ p
- //电机单步运动的函数指针(0:低电平, 1:高电平, 2:翻转电平)# h- E8 m1 { I& N8 |- ]
- SetState_FP SetPulse;
: J% e* T# J. L$ L6 H1 D4 a& g4 D
) d2 N2 }: F$ p( g, ~- //设置电机运动速度的函数指针; q% G5 n6 {% U& i/ B' H( i
- SetParam_FP SetSpeed; }9 K0 ]. M! M8 \
& H; H/ Y( U, d5 ?- Z4 H- //设置电机运动启停的函数指针
! e7 v( _9 r2 R - SetState_FP SetExec;
& B0 u- h( Y% X - } StepMotor_Struct;" ~) A7 V- y# K7 O/ N8 {
- - V t) I2 A5 \/ m+ `% Y
- extern StepMotor_Struct StepMotor[SETPMOTOR_NUM];
) ~7 r' b/ m% b! y - # {! E1 ~3 R9 {9 k4 _4 n- G( A4 z
- void StepMotor_Init();
, _& Z: N' [7 Z/ M" {5 E# A2 t" p5 v - void StepMotor_SetWorkMode(StepMotor_Struct* StepMotor_P, uint8_t mode);( B1 Z, d# J- H8 ` P
- void StepMotor_SetLoopCount(StepMotor_Struct* StepMotor_P, uint32_t loopCount);1 v. U7 S6 e7 d- u1 J' b, t" S
- void StepMotor_SetStep(StepMotor_Struct* StepMotor_P, uint32_t step);4 a" n2 L' r/ y# l& v/ D7 ?" u' N
- void StepMotor_TryMove(StepMotor_Struct* StepMotor_P);
: \( c8 `4 I8 ]7 W+ v4 d2 I
: L4 J4 p8 Z$ Y3 Q U- #endif0 Q: y& ?1 R2 D
- #endif /* SOURCE_ALWHALESLIB_HARDWARE_INC_STEPMOTOR_H_ */
复制代码
+ I: J; X$ S! F* T源文件
s+ o) a0 e+ |* I" C在源文件中,静态函数static void StepMotor_1_SetEn_Handler(uint8_t state),…,static void StepMotor_4_SetExec_Handler(uint8_t state)是与平台相关的代码,它们用来注册步进电机结构体成员的各功能函数指针。. f) L/ `' {: o1 \/ G& w# W- E8 Z
3 W# e+ \) I$ |9 b' P1 j1 n
StepMotor_Struct StepMotor[SETPMOTOR_NUM]结构体数组在声明时就直接进行了初始化。当然也可以利用void StepMotor_Init()函数(本源码中没有实现)实现上述结构体数组的初始化。与平台相关的代码主要在静态函数中,它们是抽象电机控制的具体实现。0 H5 W. p5 [$ y8 n7 u8 m
+ [2 B4 p5 _" J" X4 p, R
初始化结构体数组之后,所有对步进电机的控制都是围绕操作结构体成员展开。当然可以直接操作结构体成员,但笔者推荐使用封装好的常用步进电机操作函数来控制步进电机,例如void StepMotor_SetWorkMode(StepMotor_Struct* StepMotor_P, uint8_t mode),void StepMotor_SetStep(StepMotor_Struct* StepMotor_P, uint32_t step)等。, R7 Q7 Z. q1 M3 r* Z$ }- y9 G/ v
! b7 Q" P- R" \, X2 W* z" Y: o) c
- /*& D9 g; U! A4 A5 E( \, U( X% z
- * stepmotor.c! t3 L R. B& F3 P U0 H) x) f
- *& o8 [" ^1 X l
- * Created on: 2020年8月31日+ @% D( h: t# ?" v, z
- * Author: Tao3 f0 t( u: M. T' v ], h
- */4 ^! Q0 r4 v; O9 o1 h: E( O
- 8 Q0 g; J. o9 h5 N9 E" P- @+ i
- #include "stepmotor.h"
) Y: X0 l3 g" s% G" u: N
/ V" N# u. \! z. y/ [0 z4 k- #ifdef USE_STEPMOTOR
6 }, d( h, e3 @ - 7 T9 c# e) t/ \" D. k, L3 G) q
- /**********************************/
+ d" Q% Q) T0 h3 ]2 S. j4 | - static void StepMotor_1_SetEn_Handler(uint8_t state)
1 D/ x ]/ d B, c7 w! v - {$ q& E" w) [" h* F% s9 g) x
- STEPMOTOR_1_EN = state;: ?( |9 c- y- N7 ~; Z* O
- }; E- }; \& O+ }4 r0 p
- ^( O" H9 ?& D( H+ [- static void StepMotor_2_SetEn_Handler(uint8_t state)1 w5 G! F1 `5 Q+ F
- {( v! Y7 n# g$ c
- STEPMOTOR_2_EN = state;; l; Q4 s% M9 O, s2 I$ m5 F1 I; P
- }/ o! W+ t' _/ g& T$ f2 _
0 k) C; v# K* s2 ~& s, s4 x- static void StepMotor_3_SetEn_Handler(uint8_t state)
- L. l1 o+ C+ O) }9 y - {
8 K0 N' e; @4 s& Z+ a1 s9 O5 d4 y7 M& c8 d - STEPMOTOR_3_EN = state;% @2 C* \9 y. h$ E9 U# G
- }2 {$ k3 Z( T4 n/ M& F* h( V
- S/ K! Y, `0 A2 g+ T1 \" s A- static void StepMotor_4_SetEn_Handler(uint8_t state)/ a6 ~" \9 A/ y5 h Y8 G
- {
# F* y1 u- I; } ?# B - STEPMOTOR_4_EN = state;
5 y. L$ y) U( W: V" R - }
. c; |1 j! B& t# N/ u' T c
5 ]# ?- ?# g: J- /**********************************/1 O1 ]0 m) A; j5 Q5 \" ~
- static void StepMotor_1_SetDir_Handler(uint8_t state)2 Y* h5 I$ W$ r# {3 h! u9 Q7 _
- {* H0 U0 S% Y" U: S: {
- if(state < 2), n- x- g0 d8 I# T) E: z
- {% _# w8 ~0 V; y% L9 }+ \! }9 X; T' e
- STEPMOTOR_1_DIR = state;7 r% D7 w7 e( {& w8 W2 C
- }/ S, E' N. B- l8 F: O: N" n8 G
- else2 ^7 h5 C# v/ Q% @8 m7 M# s
- {
# T3 G+ S6 Y4 f! A9 x - STEPMOTOR_1_DIR = ~STEPMOTOR_1_DIR;
2 q9 u7 a2 S/ S4 M( ?6 @1 o. H - }
, Z; D" Z) L$ }. t. m - }6 v. t* [5 z: s, I7 w! N
- ' I7 ]8 C( u9 v2 W
- static void StepMotor_2_SetDir_Handler(uint8_t state)
) s: u U( \) A" d) @ - {4 Z+ P& ^6 c0 z$ ?4 T
- if(state < 2)
% N7 _$ W( E" n8 ?* I* o8 t% \( O - {, m7 A5 s, a( }7 B- U7 ?
- STEPMOTOR_2_DIR = state;
8 N7 |; Z8 u3 U4 b - }2 X! F, M/ b+ Q% f. U8 e
- else
; ?2 \1 l9 I+ U E - {4 c: ~" i+ C2 m
- STEPMOTOR_2_DIR = ~STEPMOTOR_2_DIR;4 |- h' F+ w6 O1 O
- }! \% a. B; k0 C, Y
- }' Z P+ g3 K5 T# y) W& `' G' B# C" L
4 L `1 n" z7 M% A6 H- static void StepMotor_3_SetDir_Handler(uint8_t state)9 {0 _/ X0 D' \# a$ O
- {0 i. `6 Y( i/ O+ ^3 w$ @6 _
- if(state < 2)
6 m0 s2 V# g! p) J - {% A; G( d3 w/ _1 z2 a
- STEPMOTOR_3_DIR = state;
' P$ \# W% Z7 P1 b% m% F+ }8 B1 q" x; U - }- F2 S: s% A7 P0 i4 w9 k5 {' W7 @
- else
. T- e& [1 H2 _6 m7 E2 q6 J2 {, M - {
2 Q' K' ]- L$ Q0 [7 Y - STEPMOTOR_3_DIR = ~STEPMOTOR_3_DIR;
' W' y$ x( U/ Q1 A - }- L/ v8 D( c/ G! v7 k
- }
# D/ Q9 q+ n9 w t! H
8 @, N' ?' a! G( j, w- static void StepMotor_4_SetDir_Handler(uint8_t state)
7 H, |3 E7 H: G5 ^4 O; Y3 x) [ - {
e9 \6 i K2 l5 v; v - if(state < 2) i$ L# S5 |, V) W# ?
- {; Y2 [3 }: L$ F0 c4 ~. [
- STEPMOTOR_4_DIR = state;/ A7 c5 E# |' i$ p& \2 A: ~
- }: U7 p2 M v o8 L
- else
# Z0 e7 b0 f; q( N - {
- [0 Y; L8 h+ \6 c* c - STEPMOTOR_4_DIR = ~STEPMOTOR_4_DIR;+ h) c( E( I/ P; p# X
- }
, Q; l- j' [, q8 x: g - }
- c+ @0 l+ C q( c5 y5 w - ) h2 D2 L( }6 y( v4 l0 e3 |7 j
- /**********************************/$ q8 n7 \4 M* n. Y
- static void StepMotor_1_SetPulse_Handler(uint8_t state)
! I7 u$ [; n. k5 d- D - {" G' g$ t9 C9 H0 P
- if(state < 2)% h5 b% _; u6 X, X; Q. o0 Z0 x: {
- {
& ?5 {( _# e3 @& k& S - STEPMOTOR_1_PUL = state;- A- _) y1 |& f# ]7 W5 M
- }0 Z$ x/ O* k) `7 \( U" G
- else" _. X; P, O% I* U# F1 g
- {
' `3 q1 |: R$ j+ v% Y - STEPMOTOR_1_PUL = ~STEPMOTOR_1_PUL;
3 w3 c' D# ?& {) V - }
1 C6 p A$ R6 |0 r7 t6 x* n* o6 r8 k2 z - }
9 l i3 E: `# U4 z+ i - - n1 L6 o4 [0 n1 n: h. Z( K
- static void StepMotor_2_SetPulse_Handler(uint8_t state)
4 V4 X) G- }7 \* ^: |2 N \( y - {5 }8 c- [1 \: V' d# A
- if(state < 2)
; v3 n8 e q7 ?' a' o - {
6 }4 [: q# f) F; T - STEPMOTOR_2_PUL = state;
$ y! Q; W- L8 O% C+ @! f# A$ b - }
- _% B. j1 }2 ]8 W& h& ] - else
1 j3 A$ z9 h Y% `2 b% |' \ - {
, \1 N" K I0 \ - STEPMOTOR_2_PUL = ~STEPMOTOR_2_PUL;" W8 F2 K- z! E0 Z
- }
/ P6 z3 a/ n Z, m$ O" k0 h+ h: U - } R8 I2 c# l" `! J. X3 b
% h, {6 K) d- v7 l, N* b3 K A- static void StepMotor_3_SetPulse_Handler(uint8_t state)/ i) a+ e) ]) [* R. ^4 |, v
- {
/ v6 p* \& A% O v, h - if(state < 2)& L1 O- x4 l, I4 H9 d3 @
- {! v4 l/ p! J: y+ b B
- STEPMOTOR_3_PUL = state;
5 Z" k$ z+ C2 x% v. `3 b+ Y - }
8 Y: u8 {8 G) p# D5 ?: O - else3 `$ Y! y, |6 j p
- {
4 X/ w H# d t8 D p$ Y - STEPMOTOR_3_PUL = ~STEPMOTOR_3_PUL;
# S& L/ Y$ [7 ~- }, o6 T, k - }
h6 C' Y( L3 J7 e, O, D: q - }0 i$ S7 |: M' t3 `4 V9 L
5 a, e1 |5 \* i3 z4 M1 X% g- static void StepMotor_4_SetPulse_Handler(uint8_t state)
! z, _9 `0 V, E6 y5 S+ k - {7 a( G1 n$ z7 u1 m8 t
- if(state < 2)! E1 F' O; e8 m; P" |
- {" }- v6 y: w2 x: V
- STEPMOTOR_4_PUL = state;) m- k# B. B- _5 j
- }
; o5 Y! ]) s9 o$ W& t, p, z' m - else
8 E* t6 N! }5 H3 K6 C' L - {6 F3 Q, y0 ]+ n, Q( v
- STEPMOTOR_4_PUL = ~STEPMOTOR_4_PUL;# a. c% o% P6 G
- }- o: E& q) [" P. e4 n
- }
+ W2 u, D; V: r% \ - $ ^8 @/ c! U- Q8 s# P
- /**********************************/
- G" O, K' Y7 x! h, T, K( \ - static void StepMotor_1_SetSpeed_Handler(uint32_t speed). P6 ]+ S( x: M
- {) `% E/ I3 t3 J$ Y# |7 e
- speed = speed * StepMotor[0].triggerNum;8 e; D- n* _3 n
- 6 A' F. e; f# R2 T& ?$ A
- //frequency < 1k, psc = 7200# S% o5 w/ K/ {( H0 `; X% `/ \
- if (speed < 1000)
J) K9 @ c7 m. b - {
; U& L9 T/ l) ?! \5 Z5 O1 W8 n1 [ - TIM5->PSC = 7200 - 1;
' ]1 ~9 E T6 J2 ` - }& c L+ T+ U( k1 C" e1 t7 m9 U3 a
- //frequency < 10k, psc = 7204 [+ S3 Y. U Q1 z
- else if (speed < 10000)' n% a7 g- w5 p: ^! I( T) Z/ L
- {
( {. Y, P# J7 j9 _% y/ j* Z9 w' j - TIM5->PSC = 720 - 1; ^( j- F: t7 b P! E5 g' O: K+ Q/ X! ?
- }
3 R6 m! u% ] u! C3 u/ f+ [ - //frequency < 100k, psc = 72( F% b0 m& J* m5 b/ d; W" E
- else if (speed < 100000)
2 ~' f4 ^9 Z* S) g! W5 {1 ` - {
+ z- u3 ]$ T) K( V `* p5 K% S* V - TIM5->PSC = 72 - 1;- m1 t) r1 B0 G2 K3 ^! i
- }* L3 P8 Z- D0 J$ q; y M
- else$ l* B7 X2 \3 |
- {/ C. P2 H; M8 c# z
- return;
) ?7 c$ P* f) k' T7 b- c - }- O" b, }: V/ n# i/ _; E) Y
- " [/ X; R: I; A( B3 e9 a' ?
- //Set the update frequency of the timer.
. P U1 b- B6 T9 s1 c( q - TIM5->ARR = 72000000.0 / (TIM5->PSC + 1) / speed - 1; [! l4 } O& s6 a+ f, K* b
* B. V+ [7 z7 @8 Q W- return;
" F! p6 F" K9 @8 @ - }
, w! t( y& P1 J9 ~2 u - 4 P9 n# j+ D" Q; g& W* y: j
- static void StepMotor_2_SetSpeed_Handler(uint32_t speed)+ s( f3 g1 `$ H! m& F7 U+ r2 P8 l, j
- {+ j. {+ K X5 n. \4 Y1 C/ I
- speed = speed * StepMotor[1].triggerNum;
0 A. V4 x, p; n7 X( `/ b - + n3 S! q* k1 w4 ^
- //frequency < 1k, psc = 7200
8 X8 \6 q" {" Z; x# {1 c4 F* Z( \ - if (speed < 1000)
+ T; K; \+ a. r) k' u1 d - {( f8 J# ^; D, b8 {( Y
- TIM6->PSC = 7200 - 1;: m" w$ |3 Z2 I$ M2 V8 a$ u6 o. Z
- }
0 b. m) u K) x+ ^3 x4 J' k1 B$ x - //frequency < 10k, psc = 720! V0 H, i8 I! v0 U/ x( [0 u+ w9 U6 K
- else if (speed < 10000)1 N' T) X Q2 }, a3 S8 o2 N
- {1 y! ]2 \. n! U2 G/ R
- TIM6->PSC = 720 - 1;
3 L4 E+ M7 J8 @8 H; b2 L, L - }
) o( W% f0 F# q0 l: v - //frequency < 100k, psc = 72
: a0 K# ^" W7 x# I' t4 J) } - else if (speed < 100000)
; c. ~2 V; a5 K. \ - {
* o" c1 S+ b' Y$ \ - TIM6->PSC = 72 - 1; L) f( ^3 y; j9 {' g8 x
- }
8 }, q7 I; ~/ X" q0 p - else
- u" r* z. @8 d4 a/ t - {2 j" R; Y- S$ P+ f! k, u
- return;- z/ P/ }4 P# D% g
- }+ W% M9 h/ a% T) L! ^! u
* v3 m8 d$ r0 j- //Set the update frequency of the timer.
, x: y& }3 u& z - TIM6->ARR = 72000000.0 / (TIM6->PSC + 1) / speed - 1;
3 i6 H1 d" G/ y0 ^# W/ O, }0 i
* }9 N+ p; q8 B: i- return;
! u' W) ]( H+ {2 A9 k - }
6 A" J; `/ ]$ g2 A, E0 N - : X' X) b$ r8 J6 z2 Z) h/ K
- static void StepMotor_3_SetSpeed_Handler(uint32_t speed)5 r8 ?: S- a5 O/ M* ?
- {
/ Y+ t7 n5 G% x - speed = speed * StepMotor[2].triggerNum;& q# ~1 O3 e2 P1 b* h2 I
- : P$ K5 U' F5 S
- //frequency < 1k, psc = 7200. I2 j8 L7 g$ [
- if (speed < 1000)
, {% y D/ J3 i, T5 p" W$ ~ - {
- C0 L! R) B4 b7 e - TIM7->PSC = 7200 - 1;
0 Y, ]6 W4 Y# _1 C3 |% V - }3 P, P ]3 J0 y) g: k
- //frequency < 10k, psc = 7203 M( T) R- N9 V/ E
- else if (speed < 10000)
: _& R5 y8 ~2 G6 r: H, h9 z - {! T9 z& d2 _+ t3 }; @. ], B
- TIM7->PSC = 720 - 1;0 v( V+ _0 a2 ?4 O* @6 L2 D" _ N
- }
0 o9 P+ h7 |" V; C) t - //frequency < 100k, psc = 724 ~5 {) u! |7 p+ c/ u
- else if (speed < 100000)) b; n/ B# D+ k$ O
- {
- j" W% z$ U& x - TIM7->PSC = 72 - 1;
/ f! H5 c7 D! a. y- M8 k0 T - }& i( B8 `* H: ?) t: j, }
- else8 R# ~& x7 O0 _
- {
+ j7 T' v8 [- P( \; s - return;7 [( S8 e7 L- S) E7 R6 p
- }
. w: S$ n4 r, U$ U) ~
# F, m6 R. L2 r [+ W- //Set the update frequency of the timer.) @6 e ^2 a; E0 H. F7 j6 L6 u3 S
- TIM7->ARR = 72000000.0 / (TIM7->PSC + 1) / speed - 1;
) A3 B' E9 M9 s9 a$ G9 d, V: j9 M
0 Y5 K$ c+ }8 z4 ]7 Y" R4 z- return;2 n. Z% U/ b$ F! x& H9 a6 X, {
- }
2 D" Q7 v x% M0 u - " a( L$ W& u) y# ?- X
- static void StepMotor_4_SetSpeed_Handler(uint32_t speed)
# P8 ~! h: w* u2 D - {, y, H/ A. z. I& G& |, Q: X1 Z: L7 m, E
- speed = speed * StepMotor[3].triggerNum;
. _2 v4 ~7 b. h3 T* Z u
5 t0 v) k Y9 F- //frequency < 1k, psc = 72006 t" {: v4 L* R/ r7 h
- if (speed < 1000)9 G/ s1 C w! H% ^
- {
! X: Y3 R$ I6 K7 P, T5 w - TIM8->PSC = 7200 - 1;1 W: ~; _: R8 { o1 a! \! [
- }9 h$ ~- @5 F7 k. Y8 u
- //frequency < 10k, psc = 720- m7 n% S' M3 }$ G V0 Z
- else if (speed < 10000)
: b" P" l# \! a" o$ I. @ - {% p( _+ ~8 X1 f/ [: R6 m: ]7 s1 @
- TIM8->PSC = 720 - 1;: A% d4 J( f; C7 ?4 D; Z) {* u8 m
- }1 p. O: C$ @% ?2 H* M+ R3 ~) j
- //frequency < 100k, psc = 72 A- p/ a0 C9 p0 }5 G. q
- else if (speed < 100000)* F1 e6 c1 p4 _0 I
- {5 b! }3 n* @- H
- TIM8->PSC = 72 - 1;
# b3 |7 R/ K& O* G6 `5 F - }; h; b4 h7 C% t8 v; f8 T- P& h
- else9 |9 A9 F; @; g4 Y8 y3 [: A
- {: L; X! ]9 v; X+ i9 F
- return;
* s8 b) a& v9 r) o% F9 I6 N - }
1 ]5 r3 w: _$ O* J# G8 Q! ]) Q - , N6 }" o( w- F/ b! N
- //Set the update frequency of the timer.- U1 X$ i4 s5 Z& z9 G7 g$ J
- TIM8->ARR = 72000000.0 / (TIM8->PSC + 1) / speed - 1;0 l* E2 v) |) \; h
- 4 j; x# J. p" B9 C# q: v, {
- return;
- o" ^( ~; Q- H( i7 ]! B i# y - }* B1 Z; r3 Z2 f$ y- T- o% L
- * h9 ~$ E/ @9 v/ N
- /**********************************/
F: j, z) G: l* G - static void StepMotor_1_SetExec_Handler(uint8_t state), q6 ]- B) I, d4 Z& D' F+ D
- {, L l9 t: W/ _9 O
- TIM_Cmd(TIM5, state);: v% r3 P+ p$ G! G9 g
- }7 H: f0 a4 Z: ~& p- T
- $ |# z% |! G/ _( n
- static void StepMotor_2_SetExec_Handler(uint8_t state)% n% L2 }9 H% D2 ^
- {
, f: O2 t9 a* c$ Z - TIM_Cmd(TIM6, state);8 }3 b2 Q' e' Y% V# y3 a+ g, h
- }- L. W9 z, l5 ]$ q' u, n
# A* e! e* H7 S* H3 c- static void StepMotor_3_SetExec_Handler(uint8_t state) s. C/ N# h$ v2 t+ E) K ?
- {& n3 ?. c8 F: N) X6 A& y' X
- TIM_Cmd(TIM7, state);7 M( w& l, J$ f. k
- }1 N! [5 y$ s. }+ g" G7 U1 _ y1 d' c6 T4 k
- ) e3 H% z8 Q1 k( P
- static void StepMotor_4_SetExec_Handler(uint8_t state)
2 D) f& S2 C# s1 v' U- i' o8 D3 X - {" G1 L% T( c$ b. t+ x8 M- b
- TIM_Cmd(TIM8, state);
7 ^: X+ |$ Y* z& ?+ A - }/ j/ v* Y$ e* F5 n) X
. D! c& I& N7 R; U+ i7 {8 |0 f6 v: V- StepMotor_Struct StepMotor[SETPMOTOR_NUM] =
; g- X- q/ |* k3 `' ~' j - {: B g/ _' x$ s6 u- s7 Y; |: y4 h! a
- //Initialization of step motor 16 S* L. d8 P" d$ \$ Q" {
- {" ?1 {6 i, E" ~8 I2 G; D
- .IsRun = 0,9 u" q, i, W* a1 ^8 L9 D4 F! Z
- .triggerNum = 2,8 I2 P5 N$ O+ w; q$ g
- .StepCount = 0,
! U; I* o: o( m. r! A6 F6 C - .LoopCount = 0,5 v! [, h. b' Z7 U8 a5 n! n
- .StepSV = 100,9 |6 O& p6 o4 P4 ?- u( s
- .WorkMode = 0,
2 P3 y& y, p, y8 r1 `0 R* @& K - 3 j$ \4 @# m2 P8 p
- .SetEn = StepMotor_1_SetEn_Handler,
{3 ^% w4 Z) m I( R - .SetDir = StepMotor_1_SetDir_Handler,5 l0 k& x/ x6 F& A7 G
- .SetPulse = StepMotor_1_SetPulse_Handler,
5 l$ I/ B9 }0 M! m3 Z/ }$ u9 l - .SetSpeed = StepMotor_1_SetSpeed_Handler,
: _8 m" D6 k+ d+ R; ? - .SetExec = StepMotor_1_SetExec_Handler, y0 s7 N" @# v/ D7 n/ \2 q( |
- },+ h6 U. S- q6 e" D' R. v
- 4 B6 h) L% h( r& `2 |" u$ E
- //Initialization of step motor 2
$ T9 T4 t9 e# ^; M2 T - {
9 c9 r5 F& A. U! x - .IsRun = 0,/ t+ x5 c9 Y; q% S: }- P' ?
- .triggerNum = 2,4 V/ @" X* o; k( U
- .StepCount = 0,
1 k. O- ^& j/ D5 a' L( A - .LoopCount = 0,* R3 `7 M2 k! ^4 n( t7 A2 V
- .StepSV = 100,, P% W# Y6 e. j8 t" O
- .WorkMode = 0,' i4 \! O0 D$ B' j8 ^
- 5 P2 {, T; [, h3 u Z
- .SetEn = StepMotor_2_SetEn_Handler, @! t7 M/ S; p7 X
- .SetDir = StepMotor_2_SetDir_Handler,
2 T1 d4 q$ v* t! Z( v/ M - .SetPulse = StepMotor_2_SetPulse_Handler,
" V+ E/ k5 g) B - .SetSpeed = StepMotor_2_SetSpeed_Handler,
6 C3 j3 A4 ]; J( }( L6 f1 \) Q0 Q - .SetExec = StepMotor_2_SetExec_Handler,
9 ^) @% \ l) \& g - },) r7 d, U- y# S) P. B% m6 z: V0 V
2 [+ W. [0 r7 S& P& ]& B1 i/ V- //Initialization of step motor 3
2 M2 E l& K0 x9 y - {
& ?5 E u, N# a2 C% E - .IsRun = 0,
) w. O! }+ }: o4 n - .triggerNum = 2,5 `$ v7 K: H8 O3 f0 d
- .StepCount = 0,
! X/ J" t" R" D$ o! n3 Q - .LoopCount = 0,* w% ^; e/ [3 q$ G
- .StepSV = 100,
9 [+ P& G% R w - .WorkMode = 0,
6 j$ C7 Q* {8 C' I
9 |3 R- U& I7 _( s- .SetEn = StepMotor_3_SetEn_Handler,$ m+ t9 H: \9 e
- .SetDir = StepMotor_3_SetDir_Handler,9 C |( B7 s1 }7 X0 L
- .SetPulse = StepMotor_3_SetPulse_Handler,- D5 p8 J& b# q: z2 ~
- .SetSpeed = StepMotor_3_SetSpeed_Handler,
. S/ c8 H. j% p - .SetExec = StepMotor_3_SetExec_Handler,( D* P; ]2 m. p" C: i0 j
- },
) O' B$ y, A4 z. S1 `) n4 u6 f" ^
g' s5 r3 o6 B5 P2 T- //Initialization of step motor 49 V3 n% g1 e1 h7 D8 e4 q; C
- {
" I( ~2 j, v2 G: U! I - .IsRun = 0,
6 W/ m' g7 x+ ~* i2 [ - .triggerNum = 2,) ^8 d' S S. f h @ h5 Z
- .StepCount = 0,% A: x% F. K- f# T
- .LoopCount = 0,
9 D$ q5 R* A d0 q - .StepSV = 100,
3 Y z+ z- ?% x! J - .WorkMode = 0,
) n$ C: o; ]$ y
6 p7 H* Q/ i9 {: ~- .SetEn = StepMotor_4_SetEn_Handler,
9 D# _8 D7 j# U \8 w2 O - .SetDir = StepMotor_4_SetDir_Handler,
. \7 T0 \9 I; \0 Q& ? - .SetPulse = StepMotor_4_SetPulse_Handler,
" w+ q" \/ S7 @3 ^" J7 }5 H6 u - .SetSpeed = StepMotor_4_SetSpeed_Handler,
( y0 h) u4 G V% D% b ^ - .SetExec = StepMotor_4_SetExec_Handler,
# e$ q) }& y0 p: Z6 i - },8 Z* P- z+ I, H( N2 h
- };+ N6 }* f5 H4 }/ u0 g, Q
4 `+ B3 N) i2 Q( r; i* W& H5 x
- V/ L8 I( Q7 }( ?- /**8 F" k0 |# R' ~ r! _+ _0 H
- * @brief 完成步进电机的初始化0 L! X8 Y0 W3 W
- */8 B- i7 Q4 K/ A7 k
- void StepMotor_Init(), O2 ?" o$ _9 C* Z# d$ e* @
- {
% W c: d/ d& t w. L7 }7 w& s z5 ~
7 M4 d4 b& q% h: k- }, F' u" e6 c1 ]' Z9 J' P
- B K; N3 p$ j8 p- Q
1 y& @8 ]+ w, R+ i* s- /** y; }+ s) @8 B, o
- * @brief 设置步进电机的运动步数2 L* U3 S: d9 J ^3 W% S
- * 运动步数与内部的StepCount, StepSV的关系主要取决于triggerNum,即触发多少次电机实际运行1步。
* R- ~( y7 R% b. P6 K - * 在驱动信号的形式上即是单边沿触发(triggerNum = 1, 跳变信号)还是双边沿触发(triggerNum = 2, 脉冲信号)4 K5 B) m5 U4 S
- * @param StepMotor_P: 步进电机结构体指针
' W5 e( ^* i. @6 B# p! T( [. e - * @param step: 电机运动步数" U9 T7 Q6 ]3 C1 W+ o& R% x
- */" ^* f8 t z) Q2 V, G& M9 y
- void StepMotor_SetStep(StepMotor_Struct* StepMotor_P, uint32_t step)
0 A; e N0 h/ W - {- n# ]6 G9 k. x1 O }
- StepMotor_P->StepSV = step * StepMotor_P->triggerNum;
, V/ i; d- P P, {4 }+ ~, p - StepMotor_P->StepCount = StepMotor_P->StepSV;
! G: g" i0 X4 Y: K& o - }
* e3 o( B( C( q0 G& p - c; T" k" O9 m3 q1 k u) U+ u
6 p* c- A: `2 M# ^- /**
$ P c) |, b$ |, I - * @brief 设置步进电机的工作模式+ m( \, G; n7 P6 y: ?0 E* h* r9 d
- * @param StepMotor_P: 步进电机结构体指针
4 ]( k7 t+ N1 @- L& I1 x! g6 A - * @param mode: 电机工作模式. @& s' U! d3 x. L [+ v: _% U9 m
- * @arg 0: 点动+ u' b; i$ ?0 _. D! k; c% L5 }4 A
- * @arg 1: 往复6 b8 L" _; S* b' k- ?
- * @arg 2: 长动; i) a0 S; P/ `; r2 h% h
- */1 C/ o, l7 r4 |8 U1 z2 D
- void StepMotor_SetWorkMode(StepMotor_Struct* StepMotor_P, uint8_t mode)
. |9 ?7 R3 E8 _" s# }) {+ v( K( S6 u - {( Q6 r3 q) G7 Z
- StepMotor_P->WorkMode = mode;- V% f6 r0 X% o( j( W( t' o
- }3 z! T4 a1 _& L4 c! g
- ! F' G$ G& t; w# r7 ?2 S
- /**
- H% P- e; ^1 D$ J8 E( h6 J+ X - * @brief 设置步进电机的循环次数6 G2 T: l8 f/ j% N2 P; v
- * @param StepMotor_P: 步进电机结构体指针+ P- q n/ R( {! [" K
- * @param loopCount: 电机循环次数(仅在工作模式为往复时生效)
8 E- R- K5 L, ^ - */+ i. X: }: r- |
- void StepMotor_SetLoopCount(StepMotor_Struct* StepMotor_P, uint32_t loopCount)
: [2 t: ^/ Y! t - {+ I: s A" o: m9 O
- StepMotor_P->LoopCount = loopCount;( i3 i' K7 f, o0 b l
- }9 Z9 A2 T. O: e% D$ q
- " h% ]" V$ `' A& @2 k" y/ U
- /**% g. s$ [2 r# G! K/ Q5 x: M; A% B$ Z
- * @brief 在定时器的更新中断服务函数中周期性调用此函数 W; z0 n; @7 A G
- * @param StepMotor_P: 步进电机结构体指针
# P- w# Z) y7 s7 f* d. n; @0 W - * 需要注意的是,传入的步进电机结构体的SetSpeed与SetExec指针所依赖的驱动定时器
7 p; V9 T" C0 H! _/ k' P! Y - * 应当与调用本函数的驱动定时器一致,否则会出现错误1 h! v! z4 k6 u+ _. z2 N0 e( T
- */
; J3 X6 |% Z2 L - void StepMotor_TryMove(StepMotor_Struct* StepMotor_P)2 V& q' b4 c+ t: N' e b
- {
: s# n' a0 ?3 @' r2 ^& b - //正常工作模式 v. }; R5 b8 K# P; g" \: g5 \: ]" V5 ~
- if (StepMotor_P->WorkMode == 0)
9 k; {/ k$ i1 ^2 C - {( x v% l" P& S( B' q) k& K5 f
- if (StepMotor_P->StepCount != 0)1 z4 B+ ^& j0 V- f' X. i! ]* y
- {
3 X& E3 y8 N5 G" A" a: v - //翻转一次脉冲端口电平
; k* `% ]5 _+ h- `/ s0 p& Q- w- p4 Z - StepMotor_P->SetPulse(2);# y: P; e+ u- ~3 d3 }1 P
- StepMotor_P->StepCount--;, F) s& H8 b7 O4 j- I+ a
- }
+ [2 P ]1 E& S$ h+ H - else' }7 ?& R( @) s
- {; l0 X, i3 m' A$ Q& B, J# J" i( t$ V
- //停止步进电机
* g9 P4 x. Q J6 o+ n/ ?+ ]( @ - StepMotor_P->SetExec(0);# I* x" t2 d/ Q: o2 L9 C9 q
- StepMotor_P->IsRun = 0;
0 z6 I( N. u3 h5 f - }
* m" Y8 \" g/ h6 j - }
5 k" k# o' g/ B& [, X8 N - //往复工作模式! v! b: B" w9 f: ^0 J2 y2 G
- else if (StepMotor_P->WorkMode == 1)
( }$ ~$ a+ K+ x, Z1 R) ^ - {
- N0 J8 k' I+ W H8 U$ R7 V0 j4 ]8 d - if (StepMotor_P->LoopCount != 0). U$ ~7 z; L" ^/ q/ _+ E
- {
$ W; |: T7 y. p1 z* S8 I! J. { - if (StepMotor_P->StepCount != 0); U6 g( q! |: U! M5 z7 O; K. _
- {8 R& t1 s8 I3 u( Y& I' ]
- StepMotor_P->SetPulse(2);
2 Z) e' H7 y, I7 N$ ~# c4 g - StepMotor_P->StepCount--;
8 @. |/ R# T6 o+ ~6 H5 m - }$ q' {6 W' @0 s8 K# q3 ~
- else
5 }/ W1 w3 X* [$ [* Y1 e' ~) j - {3 Y/ [0 H) V0 n! E6 K! q
- StepMotor_P->LoopCount--;
& a6 B% H. p' B4 h1 L& u } - StepMotor_P->StepCount = StepMotor_P->StepSV;
: N5 Z" ~: d# `; W4 |# K - //翻转方向: t( t- U8 b/ ~! u9 Q- f* f
- StepMotor_P->SetDir(2);( S& I- ?3 T, U% F# h: W
- }
. ]! b w J4 a. J+ k6 t - }
0 } f5 h g6 L3 f7 ` - else
6 K$ ]* r+ S; u: `: I, j - {
! V. q! S. k5 S+ W/ y - StepMotor_P->SetExec(0);; g( W& U. v6 F3 O: Z/ f
- StepMotor_P->IsRun = 0;. ?/ K1 o0 T7 i D2 z! e
- }
, V$ n7 _0 [/ z2 m - }
) y/ [6 N% x1 Y4 ^0 E- Y( U - //连续工作模式
* ?1 p+ r& B9 @6 ] - else if (StepMotor_P->WorkMode == 2)2 a( f. b1 e7 Y: l
- {7 _1 [! e% P% g, Q5 w+ k
- //翻转一次脉冲端口电平% F0 O# ~2 ?: j+ x+ M: u5 m/ v
- StepMotor_P->SetPulse(2);3 }' ` h! P) d2 J& y
- }+ y. U" X* |5 ?! b
- else1 d+ o# A% y2 b; S5 W" x- S
- {
8 I) P& e* C6 b K) p
. L( U2 g# U( D; j& a9 h8 O; Q- }
0 k- d# a9 l+ J% w2 [ - }
! ^& y$ {5 W7 v3 h7 Q4 D$ a
9 l3 s% {" V, y8 r8 o9 a. v- . g$ `& M# P J1 N7 G. @- b
- #endif7 l2 x* Y7 N2 v1 v& y I
复制代码 3 r8 [" ?. u+ [& }& o2 z
应用指南
& }" m; b& R5 E8 t: a( ]% {3 P* `( Q根据硬件平台实现结构体成员中的各函数指针(SetState_FP SetEn,SetState_FP SetDir,SetState_FP SetPulse,SetParam_FP SetSpeed,SetState_FP SetExec,)的回调函数。这些回调函数或是操作GPIO的电平输出,或者控制着硬件定时器的频率与启停。以STM32F103平台为例:
3 |; D' \. j& ~7 h [9 O$ q% a( O- //SetEn的回调函数/ _2 K9 i5 V5 E
- static void StepMotor_1_SetEn_Handler(uint8_t state)6 B! ?1 W+ f- l8 H
- {
, m4 E. L! r" ~' w& | - STEPMOTOR_1_EN = state;8 J( O0 I; t E/ `: W
- }
0 g) S5 Y2 q+ \8 t( r$ [( t& w - 8 j- h4 O. ^+ Y& i0 \/ [
- //SetDir的回调函数/ Y, H( }$ Z( l( x6 m
- static void StepMotor_1_SetDir_Handler(uint8_t state)
/ ^2 T( T& F6 \! I, }2 c+ i - {
; Q; Z7 m, ?* W4 T# u - if(state < 2)
$ W9 ?0 |7 n: j- E - {( d& F$ r8 q/ N- y K2 f
- STEPMOTOR_1_DIR = state;
" C3 l( p, j( I( C; k/ n - }
+ D$ g: E4 ^& J4 A1 x, s, [# B - else
5 k) R; E; [6 j7 }- { - {
& d( f5 c* D7 b8 I5 p( [ - STEPMOTOR_1_DIR = ~STEPMOTOR_1_DIR;
# w! w0 e* F9 m1 Y, f - }+ a' u& P/ a0 y0 R
- }: i3 e$ P4 H7 a% L# i9 o) m
0 e2 E% {" j4 ?% f& k/ Y, l: U- //SetPulse的回调函数
3 a( `2 Y6 C* A. L/ _% R7 r8 r - static void StepMotor_1_SetPulse_Handler(uint8_t state)7 L6 T1 m- l( K8 A5 n( Q5 A
- {
4 Z F9 o2 f3 w) S4 w1 \9 V7 a - if(state < 2)
a. k3 |; M. @1 t" p1 D+ a - {
) P2 }5 B4 e: T7 A - STEPMOTOR_1_PUL = state;9 J- N% w' [7 u% M& ?% N
- }
' l" r3 U X) U* O4 M B g& O - else
/ F/ s6 o6 N1 G4 e% a - {/ W5 Y4 `/ J- C
- STEPMOTOR_1_PUL = ~STEPMOTOR_1_PUL;' {7 w2 t/ \0 k+ l: \" U6 v
- } ?8 x2 z/ Y- X2 C( [! v
- }. T* A9 Q6 r+ m- }9 D
# E$ j9 D9 h# F5 y+ M- //SetSpeed的回调函数% f9 M6 c1 F* d' c. P
- static void StepMotor_1_SetSpeed_Handler(uint32_t speed)
N4 o5 @6 p8 j6 p! h4 R6 T+ F - {" b3 \/ V, R7 p+ a
- speed = speed * StepMotor[0].triggerNum;
8 T3 Z$ U, ^/ P5 o7 U. F - : t( d+ }( }6 ^; _
- //frequency < 1k, psc = 7200& g0 H, ]0 R4 ?0 i9 @* ~
- if (speed < 1000)$ ^/ X5 M) p& r3 p! {. l0 D2 {; A
- {
( f" b6 ^4 W4 X - TIM5->PSC = 7200 - 1;
$ \4 {: F$ A2 _: c( i0 ^6 o9 F8 r - }
9 w! A( f0 U6 S - //frequency < 10k, psc = 7206 a3 z9 {' [9 y, j4 x* P8 _0 N
- else if (speed < 10000)
: n4 v( x* g; G4 V6 p - {
7 a9 N8 X5 g6 L# V& Z5 n - TIM5->PSC = 720 - 1;
' M+ C$ v. K+ L' E( {! @ - }; i5 j% Y& N4 T5 {
- else" M# R& K2 ^' ~
- {. a4 n2 t) x, a3 t
- return;
8 I8 s3 a0 M$ t4 Z - }
- F; Z( C3 O% O# H - 8 s( P, k; k5 Z& b. }; |" H) b
- //Set the update frequency of the timer./ H* z& {# B1 J( p" E6 Q6 K/ ?7 ]
- TIM5->ARR = 72000000.0 / (TIM5->PSC + 1) / speed - 1;- i6 `$ L+ J- S2 D
% S- `! Q6 f/ i! e- return;
7 V3 i0 O4 E# S0 K- z g# R - }0 F0 ?2 ^9 c7 E
- " d9 ?1 Q7 V2 ^7 {, R$ n* B. m
- //SetExec的回调函数
7 ]8 p" e' u; x, a' K( v' o+ \. ` - static void StepMotor_1_SetExec_Handler(uint8_t state)1 U+ D" Y E8 D5 K5 y$ _
- {
3 c* a3 R* y% ?, a, \% h - TIM_Cmd(TIM5, state);- B4 u% s2 A! l3 v1 l# ^+ T ~" z
- }) H* l+ K: ~8 E: M& Z
复制代码
9 i: f8 c7 d. g P2 m+ ? |初始化步进电机结构体变量(数组)。可以在声明时直接将结构体变量(数组)进行初始化,也可以利用void StepMotor_Init()函数实现结构体变量(数组)的初始化。& e0 \7 z! N6 x. M7 W2 g# }! L: x
- StepMotor_Struct StepMotor[SETPMOTOR_NUM] =
Q4 L2 }! o6 |9 Q, `& O. g - {
, K3 {" g9 [3 ]! U& A3 ` - //Initialization of step motor 1* L: @) q9 m( E6 N- y9 _5 K
- {
4 Q! B! N e/ C& i( c3 ] - .IsRun = 0,
. g" I0 L5 E0 j. n( h+ {; h - .triggerNum = 2,; Y" ^4 L) N! C5 S
- .StepCount = 0,
( Y: P% j7 i8 h, @8 f8 r - .LoopCount = 0,- Q5 [* }, {- r" ]+ O1 t9 z( K
- .StepSV = 100,
( i F$ w& f3 W" b - .WorkMode = 0,
) a6 p/ @$ v6 y0 D6 A( [5 A
% F: r' E4 W: L2 l! {9 E# Z. d& z- .SetEn = StepMotor_1_SetEn_Handler,
! f( p/ _) X- K. U% h; D3 a - .SetDir = StepMotor_1_SetDir_Handler,
% J* R0 t x5 i( k! n - .SetPulse = StepMotor_1_SetPulse_Handler,5 i& z2 O' _5 Z0 q0 }$ W$ \# H
- .SetSpeed = StepMotor_1_SetSpeed_Handler,) E9 z% f* a1 Y3 P( F
- .SetExec = StepMotor_1_SetExec_Handler," W: a E7 c' B6 f3 o- N
- },
6 ~6 B# b" n0 F- h6 T
2 u/ d: p1 Z7 L1 c5 A) i# C- //Initialization of step motor 2
$ J% |, O1 ?; T2 f& U8 Q - {
4 J/ \' @% E- a3 a# Y. m) ?$ k - .IsRun = 0,
4 }# a6 a8 q- ]( b! n - .triggerNum = 2,
+ m1 A1 M# V5 T7 R! H- ^2 f6 ` - .StepCount = 0,
" b/ E8 c3 T; E1 v* U - .LoopCount = 0,
$ @, u$ P9 ~, a: }$ f - .StepSV = 100,+ h @9 c, `3 C' N2 g. T4 h6 u
- .WorkMode = 0,
5 w, ^3 t! V; R/ ~" P - / x' k* I/ ~: T1 t5 V9 T/ w9 k
- .SetEn = StepMotor_2_SetEn_Handler,! E/ B! I. p, Z& [ F
- .SetDir = StepMotor_2_SetDir_Handler,
/ l; ]/ T* [* s9 x) F) h/ Y - .SetPulse = StepMotor_2_SetPulse_Handler,
~2 a" p0 I5 t, G. _7 Q* H - .SetSpeed = StepMotor_2_SetSpeed_Handler,: W$ x: t7 Q: q3 N: z v( p
- .SetExec = StepMotor_2_SetExec_Handler,/ p- E* v. I( Y, [
- },
4 M% t0 o$ n# J& O. E - ! n( Z3 }6 Y% P: A
- //Initialization of step motor 3, t: I/ P3 K3 P7 h* m- s4 K9 f
- {% `+ W. ?8 w( H- V2 E( g
- .IsRun = 0,
$ T' i' L+ G9 D. w0 \4 l - .triggerNum = 2,
3 h$ Q0 x) ]% K9 c7 v2 E/ A" C - .StepCount = 0,* | ?8 _9 q1 N
- .LoopCount = 0,9 g2 O) ~* I' a; H
- .StepSV = 100,
9 \& r" }0 a) v - .WorkMode = 0,
. L+ C8 l; O1 f. D% S; {: W - $ d, z. i: P) ~$ q) X6 i0 `
- .SetEn = StepMotor_3_SetEn_Handler,
- r! ?1 R; g0 L# e - .SetDir = StepMotor_3_SetDir_Handler,
" f; r; j+ S. d; R" j - .SetPulse = StepMotor_3_SetPulse_Handler,
% M6 F/ g1 L6 T* g - .SetSpeed = StepMotor_3_SetSpeed_Handler,
/ o# ^ Z- `6 F1 `8 P/ [ - .SetExec = StepMotor_3_SetExec_Handler,$ ?) M, w1 S5 |# }4 D
- },
h0 A# n) \7 E @8 R. e7 m1 o - ) h$ B$ p7 A9 B( C( }4 t Q
- //Initialization of step motor 45 Y: J& b. L6 q5 A- |; U1 z+ I
- {
* \% h9 R5 F, S- K- k6 g5 @) x - .IsRun = 0,
# M, u: m& A/ J - .triggerNum = 2,2 D# h* u: N2 ]
- .StepCount = 0,8 @6 d" L/ n! }+ F
- .LoopCount = 0,$ ?6 _! o: N% v. z0 `
- .StepSV = 100,, E# z# k, T4 ~' l% X' F) R
- .WorkMode = 0,
* P6 i6 i: z! S* J8 X, I
8 t2 O: ?" J( K: l/ [- .SetEn = StepMotor_4_SetEn_Handler,
- x, }" S% E1 f% L, \# @ - .SetDir = StepMotor_4_SetDir_Handler,
% m8 d W' E5 I% l) [) b. n - .SetPulse = StepMotor_4_SetPulse_Handler,
! C8 O) L# Q5 O) l: q1 } - .SetSpeed = StepMotor_4_SetSpeed_Handler,
. `7 }$ x9 }! v& p2 H- M: o# l& F - .SetExec = StepMotor_4_SetExec_Handler,
+ y7 u3 T+ k( Q) j. K* F1 y - },. N9 q; x! o) C/ d: r* t
- };
复制代码 ' l4 Y. a) U+ {6 }
在项目工程的合适位置配置控制功能的回调函数中所依赖的定时器,并配置定时器的更新中断, z: O, t: W" H# Z' g) M
- void Timer_Config()* q; A" N2 ]7 U- F9 M
- {
9 a* U( _# q8 U5 Y1 X6 Y - TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
N2 F: C% S; a, \& A$ w
& [" r- B+ g2 n {* e- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
) y/ d" O: W# n; Z N: B
m+ {' d7 l' d- _ v9 N- TIM_DeInit(TIM5);% Z+ p/ L4 x9 t. c! K2 u- f
- TIM_TimeBaseInitStructure.TIM_Prescaler = TIM5_Prescaler - 1;# t) |" j+ M9 Z9 u
- TIM_TimeBaseInitStructure.TIM_Period = TIM5_Period - 1;
1 L s' q, b7 {% v9 k( V0 g - TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
$ t4 {( S6 _* f5 C7 w* c - TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
5 h: J5 P7 ] k% ^9 M4 q - TIM_TimeBaseInit(TIM5, &TIM_TimeBaseInitStructure);
. Z' U6 r5 b6 `7 L - 6 Y1 A4 y0 { g! E$ _
- TIM_ClearFlag(TIM5, TIM_FLAG_Update);, _( y! T6 e9 R4 ^
- TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
1 c& j) O2 F& q+ b$ N+ S7 C6 @ - // TIM_ARRPreloadConfig(TIM5, ENABLE);
$ `1 x4 ~% \$ [4 n, o - }
0 C: R& }" x9 O - 6 h& ]1 C) b2 g! i# K# \: |
- - y# R& V% I8 Z
- void NVIC_Config()4 P* t' L6 V7 Y# T+ _0 D
- {
6 t! v/ q! o, A# h. ^8 k; _7 z - NVIC_InitTypeDef NVIC_InitStructure;
# I& l- `! ?& a! K' Y8 O - . ]" Q' | E/ e! e# x. `' G
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);7 R0 E8 O& l* ]1 R' s
- ; V# G T" {$ K2 G3 d
- //Timer5 is used for drive step motor 1.
) e; n2 t4 K/ ^& J - NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;: @' ~( U: y- c6 ~
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;; S. P2 L! H5 f" M
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
; v( _# z9 ]" }8 z0 C - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
, c5 U- u* Y5 S7 }; J& y6 c - NVIC_Init(&NVIC_InitStructure);) C. Z6 {+ o( |* ]; _) U& [
- }
复制代码
1 b1 C) K' B& H6 d( X在定时器的更新中断服务函数中调用void StepMotor_TryMove(StepMotor_Struct* StepMotor_P)。
. T7 d. f3 A. j6 n; h5 K" k- void TIM5_IRQHandler(void)
3 A* G1 m( s6 L2 ?; ]" x: {+ Y. ] - {3 o- b9 G' l1 a# @; R
- if(TIM_GetITStatus(TIM5,TIM_IT_Update)!=RESET)+ Q) E2 }- j) \( m
- { x7 l* R1 q" L+ Q& B4 j4 t7 d
- StepMotor_TryMove(&StepMotor[0]);7 e, S$ J, T. |: Y
-
/ X+ \' t0 \9 G. o2 z; w8 ^ - TIM_ClearFlag(TIM5,TIM_FLAG_Update);+ s" ?5 q# Q) u+ q
- TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
: C- A$ @( A3 w8 [* B - }
p3 k# a: l: x3 S2 r- J - }
复制代码
! V% P4 y5 ?0 L: Y+ B& H设置电机的启动参数并启动电机。) F+ E! W4 j( g3 v8 `$ W) A
- void StepMotor_RunM1(void)4 \' S) h. ]/ O$ s+ _. {5 v
- {. c6 A' Z6 {* h! s; s5 r' W1 E
- //设置运动参数
% e' s7 J) _1 Q" z& H, f4 q - StepMotor_SetStep(&StepMotor[0], 1200);
+ F+ e7 Z" G0 ~- f6 |) s - StepMotor_SetWorkMode(&StepMotor[0], 1);
1 l G" v" q! U4 j - StepMotor_SetLoopCount(&StepMotor[0], 50);% K$ H$ x- a7 J+ f- V; C1 Q
- StepMotor[0].SetSpeed(3000);
( F( t' i# s" z3 E' l/ z
, S2 r' Q; @5 E- Q% l- //启动电机并设置标志位( o; u6 l: J* A- ]; o
- StepMotor[0].SetExec(1);
0 c, X0 ]- N- b& }1 ~% y - StepMotor[0].IsRun = 1;
c5 X2 O* @. B0 D' b; i - }
复制代码 ( W$ `1 u' c2 k% e+ ?
停止电机,一般在光电对管触发的外部中断服务函数中调用。
$ ]% z; D3 c, H0 O/ ]6 e- void StepMotor_StopM1(void)$ N* F* q5 I; w
- {
& y, D# K# Y8 q' x7 l9 i, S - //停止电机并设置标志位1 [2 f5 S8 @8 y, n
- StepMotor[0].SetExec(0);' F! h/ j+ U5 F/ o9 t2 x
- StepMotor[0].IsRun = 0;4 M2 s5 {( [$ F9 ~- _
- StepMotor[0].StepCount = 0;
0 f* l% U. ]$ X$ X- f - }
复制代码 5 K$ F4 I; F/ b F
特别说明1:在光电对管触发的外部中断服务函数中调用停止电机函数时,应当注意软件滤波消除抖动,也可以适当延时后再关闭电机以摆脱光电对管触发的临界点位置。% B4 Q, Y* k7 n0 `* G. U; K+ S
- void EXTI0_IRQHandler(void)+ c' p3 b5 E* L: C( a
- {5 W3 H" c" K0 Y/ }9 c3 O2 M
- if (EXTI_GetITStatus(EXTI_Line0) != RESET)2 {) k' [5 a1 O' n+ B6 y
- {0 }& F) I% A* ?" z! j6 v* g$ y
- //延时以消除抖动4 n% Q1 S7 l$ ?- _- j( S' C
- delay_ms(10);
0 m1 {" ~* \0 R7 ]/ K9 Z+ j' v& U -
/ _ T1 j4 `% F: X* X$ J - if(MOTOR_LIMIT_SWITCH_1_1 == 0)5 s+ {/ h* i' G3 I
- {5 u) L4 r4 {9 J0 O( e; }9 U( d
- //Stop motor 1
7 v, c) R b( d. d' I& y - StepMotor[0].SetExec(0);
* e& _" A0 ^( ~8 `6 V - StepMotor[0].IsRun = 0;' o. B1 R' L* p5 R7 g2 E4 O' u
- StepMotor[0].StepCount = 0;
/ y2 z j/ {6 X! |, r3 y, Y/ ]0 S - }& ^/ P$ a& _9 B* h$ F3 t
- ; Z0 G c9 g7 I8 d: G
- EXTI_ClearITPendingBit(EXTI_Line0);
) w7 o. l6 @; A3 q8 \, r& E) f, f - }
, B$ \6 M/ l4 k1 k - }
复制代码 ( c1 Y' p: A8 n- z6 v
特别说明2:触发光电对管后,电机再启动需要有方向保护,不能继续向原方向运动,否则会由于光电对管无法再次被触发而导致电机失控。可以在控制电机启停的业务代码中增加对光电对管状态的判断:
$ ?1 B% G0 {+ [: h7 v$ c- X. }4 b- void StepMotor_RunM1(void)
5 L: t' I, Q7 R9 `: |$ A& Z - {6 C; P8 V" x. B' _, z6 b: y- j, r4 w
- //设置运动参数
2 B& ^$ C6 m1 k - StepMotor_SetStep(&StepMotor[0], 1200);
7 y$ W$ d3 L: n' n' b$ [ - StepMotor_SetWorkMode(&StepMotor[0], 1);
; c+ J( z6 \5 w* Z: ]" W8 k! z7 i2 ] - StepMotor_SetLoopCount(&StepMotor[0], 50);
7 @: F* [8 @7 K# n3 w. t7 T8 X! d - StepMotor[0].SetSpeed(3000);
* C7 K, q$ G- E5 k7 q L5 u, F
/ f V0 `+ v0 h6 E- if((MOTOR_LIMIT_SWITCH_1_1 ==0) &&(STEPMOTOR_1_DIR == 0))
6 q$ A. W9 w( ~ - {
9 V$ q1 [8 J/ {5 _" G, n" K - return;
7 x ~) b3 S; Q( D+ c* [ - }
, }% t8 D$ i$ E4 P2 [& z - else if((MOTOR_LIMIT_SWITCH_1_2 ==0) &&(STEPMOTOR_1_DIR == 1))) D7 c! F, w# Y2 Q4 [
- {
0 `' O3 H9 K4 N( s - return;
, R6 |! N U# y `9 b5 [5 C - }
: a; W' A+ h/ n" w- D/ w7 d5 g9 w - else
5 n# K# n/ j a* Z. r4 C - {
7 |; r" Y2 K- Z: X( o# z2 ?% H - //启动电机并设置标志位
7 W1 V* C2 \% X - StepMotor[0].SetExec(1);
0 F2 d) D }" Q5 o0 d. w+ w2 j4 h7 ~ - StepMotor[0].IsRun = 1;
$ O) s" W2 }' p' I E! u& v - }: u5 V& a* e: e4 f$ Z. w: Z! O
- }
复制代码
: T& B/ ^: V% _2 r
3 P' X+ b6 p. M! }0 ]5 b- ]$ j2 ~
. h m- e) v; X/ Q9 C
& I! n0 r& \/ m; Z |