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
/
AVR_PWM_StepperControl.ino
155 lines (132 loc) · 5.33 KB
/
AVR_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/****************************************************************************************************************************
AVR_PWM_StepperControl.ino
For AVR-based boards (UNO, Nano, Mega, 32U4, 16U4, etc. )
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ContinuousStepper_Generic
Licensed under MIT license
This is pure hardware-based PWM
*****************************************************************************************************************************/
/******************************************************************************************************************************
// For UNO / Nano
Timer0 ( 8-bit) used by delay(), millis() and micros(), and PWM generation on pins 5 (6 not usable)
Timer1 (16-bit) used by the Servo.h library and PWM generation on pins 9 and 10
Timer2 ( 8-bit) used by Tone() and PWM generation on pins 3 and 11
// For Mega
Timer0 ( 8-bit) used by delay(), millis() and micros(), and PWM generation on pins 4 (13 not usable)
Timer1 (16-bit) used by the Servo.h library and PWM generation on pins 11, 12
Timer2 ( 8-bit) used by Tone() and PWM generation on pins 9 and 10
Timer3 (16-bit) used by PWM generation on pins 2, 3 and 5
Timer4 (16-bit) used by PWM generation on pins 6, 7 and 8
Timer5 (16-bit) used by PWM generation on pins 44, 45 and 46
////////////////////////////////////////////
// For Mega (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46)
Pin 2 => TIMER3B // PE 4 ** 2 ** PWM2
Pin 3 => TIMER3C // PE 5 ** 3 ** PWM3
Pin 4 => TIMER0B // PG 5 ** 4 ** PWM4
Pin 5 => TIMER3A // PE 3 ** 5 ** PWM5
Pin 6 => TIMER4A // PH 3 ** 6 ** PWM6
Pin 7 => TIMER4B // PH 4 ** 7 ** PWM7
Pin 8 => TIMER4C // PH 5 ** 8 ** PWM8
Pin 9 => TIMER2B // PH 6 ** 9 ** PWM9
Pin 10 => TIMER2A // PB 4 ** 10 ** PWM10
Pin 11 => TIMER1A // PB 5 ** 11 ** PWM11
Pin 12 => TIMER1B // PB 6 ** 12 ** PWM12
Pin 13 => TIMER0A // PB 7 ** 13 ** PWM13
Pin 44 => TIMER5C // PL 5 ** 44 ** D44
Pin 45 => TIMER5B // PL 4 ** 45 ** D45
Pin 46 => TIMER5A // PL 3 ** 46 ** D46
////////////////////////////////////////////
// For 32u4 (3, 5, 6, 9, 10, 11, 13)
Pin 3 => TIMER0B
Pin 5 => TIMER3A
Pin 6 => TIMER4D
Pin 9 => TIMER1A
Pin 10 => TIMER1B
Pin 11 => TIMER0A
Pin 13 => TIMER4A
////////////////////////////////////////////
// For UNO, Nano (3, 5, 6, 9, 10, 11)
Pin 3 => TIMER2B,
Pin 5 => TIMER0B
Pin 6 => TIMER0A
Pin 9 => TIMER1A
Pin 10 => TIMER1B
Pin 11 => TIMER2(A)
******************************************************************************************************************************/
// Use with Stepper-Motor driver, such as TMC2209
#define _PWM_LOGLEVEL_ 4
#include "ContinuousStepper_Generic.h"
#if ( PWM_USING_ATMEGA2560 )
// Pins tested OK in Mega
//#define STEP_PIN 12 // Timer1B on Mega
//#define STEP_PIN 11 // Timer1A on Mega
//#define STEP_PIN 9 // Timer2B on Mega
//#define STEP_PIN 2 // Timer3B on Mega
//#define STEP_PIN 3 // Timer3C on Mega
//#define STEP_PIN 5 // Timer3A on Mega
//#define STEP_PIN 6 // Timer4A on Mega
//#define STEP_PIN 7 // Timer4B on Mega
#define STEP_PIN 8 // Timer4C on Mega
//#define STEP_PIN 46 // Timer5A on Mega
//#define STEP_PIN 45 // Timer5B on Mega
//#define STEP_PIN 44 // Timer5C on Mega
#elif ( PWM_USING_ATMEGA_32U4 )
// Pins tested OK on 32u4
//#define STEP_PIN 5 // Timer3A on 32u4
#define STEP_PIN 9 // Timer1A on 32u4
//#define STEP_PIN 10 // Timer1B on 32u4
#else
// Pins tested OK on Nano / UNO
//#define STEP_PIN 9 // Timer1A on UNO, Nano, etc
#define STEP_PIN 10 // Timer1B on UNO, Nano, etc
//#define STEP_PIN 5 // Timer0B on UNO, Nano, e
//#define STEP_PIN 3 // Timer2B on UNO, Nano, etc
#endif
#define DIR_PIN 12
AVR_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 AVR_PWM_StepperControl on "));
Serial.println(BOARD_NAME);
Serial.println(AVR_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 AVR_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);
}