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 0
/
RP2040_PWM_StepperControl.ino
92 lines (71 loc) · 2.65 KB
/
RP2040_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
/****************************************************************************************************************************
RP2040_PWM_StepperControl.ino
For RP2040 boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ContinuousStepper_Generic
Licensed under MIT license
The RP2040 PWM block has 8 identical slices. Each slice can drive two PWM output signals, or measure the frequency
or duty cycle of an input signal. This gives a total of up to 16 controllable PWM outputs. All 30 GPIO pins can be driven
by the PWM block
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
#define _PWM_LOGLEVEL_ 1
#if ( defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || \
defined(ARDUINO_GENERIC_RP2040) ) && !defined(ARDUINO_ARCH_MBED)
#if(_PWM_LOGLEVEL_>3)
#warning USING_RP2040_PWM
#endif
#else
#error This code is intended to run on the RP2040 arduino-pico platform! Please check your Tools->Board setting.
#endif
#include "ContinuousStepper_Generic.h"
RP2040_PWM* stepper;
#define STEP_PIN 8
#define DIR_PIN 9
// 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 RP2040_PWM_StepperControl on "));
Serial.println(BOARD_NAME);
Serial.println(RP2040_PWM_VERSION);
Serial.println(CONTINUOUS_STEPPER_GENERIC_VERSION);
// Create PWM object and passed just a random frequency of 500 in it
// The duty cycle is how you turn the motor on and off
stepper = new RP2040_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);
}