175
Chapter 16 Stepping Motor
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//continuous rotation function, the parameter steps specifies the rotation cycles, every
four steps is a cycle
void
moveSteps
(
int
dir
,
int
ms
,
int
steps
){
int
i
;
for
(
i
=
0
;
i
<
steps
;
i
++){
moveOnePeriod
(
dir
,
ms
);
}
}
void
motorStop
(){
//function used to stop rotating
int
i
;
for
(
i
=
0
;
i
<
4
;
i
++){
digitalWrite
(
motorPins
[
i
],
LOW
);
}
}
int
main
(
void
){
int
i
;
if
(
wiringPiSetup
()
==
-
1
){
//when initialize wiring failed,print message to screen
printf
(
"setup wiringPi failed !"
);
return
1
;
}
for
(
i
=
0
;
i
<
4
;
i
++){
pinMode
(
motorPins
[
i
],
OUTPUT
);
}
while
(
1
){
moveSteps
(
1
,
3
,
512
);
//
rotating 360°
clockwise, a total of 2048 steps in a
circle, namely, 512 cycles.
delay
(
500
);
moveSteps
(
0
,
3
,
512
);
//rotating
360°
anticlockwise
delay
(
500
);
}
return
0
;
}
In the code, define four pins of stepping motor and coil power supply order of four steps rotation mode.
const
int
motorPins
[]={
1
,
4
,
5
,
6
};
//define pins connected to four phase ABCD of stepper
motor
const
int
CCWStep
[]={
0x01
,
0x02
,
0x04
,
0x08
};
//define power supply order for coil for
rotating anticlockwise
const
int
CWStep
[]={
0x08
,
0x04
,
0x02
,
0x01
};
//define power supply order for coil for
rotating clockwise
Subfunction
moveOnePeriod
((int dir,int ms) will drive the stepping motor rotating four step clockwise or
anticlockwise, four step as a cycle. Where parameter "dir" indicates the rotation direction, if "dir" is 1, the servo
will rotate forward, otherwise it rotates to reverse direction. Parameter "ms" indicates the time between each
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...