This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnRF52_PWM_StepperControl.ino
92 lines (70 loc) · 2.81 KB
/
nRF52_PWM_StepperControl.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/****************************************************************************************************************************
nRF52_PWM_StepperControl.ino
For nRF52-based boards usinghardware-based PWM with Adafruit_nRF52_Arduino core
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ContinuousStepper_Generic
Licensed under MIT license
Credits of Paul van Dinther (https://github.com/dinther). Check https://github.com/khoih-prog/RP2040_PWM/issues/16
*****************************************************************************************************************************/
// Use with Stepper-Motor driver, such as TMC2209
#if !(defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \
defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || \
defined(NRF52840_CLUE) || defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || \
defined(MDBT50Q_RX) || defined(NINA_B302_ublox) || defined(NINA_B112_ublox) )
#error This code is designed to run on nRF52-based platform! Please check your Tools->Board setting.
#endif
#define _PWM_LOGLEVEL_ 4
// Select false to use PWM
#define USING_TIMER false //true
#include "ContinuousStepper_Generic.h"
// OK for Feather_nRF52840_Express (5, 6, 9-13, 14-21/A0-A7, etc.)
// OK for ItsyBitsy_nRF52840_Express (5, 7, 9-13, 14-20/A0-A6, etc.)
#define STEP_PIN 5
#define DIR_PIN 9
nRF52_PWM* stepper;
// The Stepper RPM will be ( speed * 60 ) / steps-per-rev
// For example, 28BYJ-48 Stepper Motor (https://lastminuteengineers.com/28byj48-stepper-motor-arduino-tutorial/) has 32 Steps/Rev
// Speed = 640 Hz => Stepper RPM = (640 * 60 / 32) = 1200 RPM
void setSpeed(int speed)
{
if (speed == 0)
{
// Use DC = 0 to stop stepper
stepper->setPWM(STEP_PIN, 500, 0);
}
else
{
// Set the frequency of the PWM output and a duty cycle of 50%
digitalWrite(DIR_PIN, (speed < 0));
stepper->setPWM(STEP_PIN, abs(speed), 50);
}
}
void setup()
{
pinMode(DIR_PIN, OUTPUT);
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(100);
Serial.print(F("\nStarting nRF52_PWM_StepperControl on "));
Serial.println(BOARD_NAME);
Serial.println(NRF52_PWM_VERSION);
Serial.println(CONTINUOUS_STEPPER_GENERIC_VERSION);
// Create PWM object and passed just a random frequency of 500
// The duty cycle is how you turn the motor on and off
stepper = new nRF52_PWM(STEP_PIN, 500, 0);
}
void loop()
{
// The Stepper RPM will be ( speed * 60 ) / steps-per-rev
setSpeed(1000);
delay(3000);
// Stop before reversing
setSpeed(0);
delay(3000);
// Reversing
setSpeed(-500);
delay(3000);
// Stop before reversing
setSpeed(0);
delay(3000);
}