|
| 1 | +/**************************************************************************************************************************** |
| 2 | + PWM_StepperControl.ino |
| 3 | + For Arduino megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. ) |
| 4 | + Written by Khoi Hoang |
| 5 | +
|
| 6 | + Built by Khoi Hoang https://github.com/khoih-prog/megaAVR_PWM |
| 7 | + Licensed under MIT license |
| 8 | +
|
| 9 | + This is pure hardware-based PWM |
| 10 | +*****************************************************************************************************************************/ |
| 11 | +/****************************************************************************************************************************** |
| 12 | + Pins can be used for hardware-PWM |
| 13 | + // For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.) |
| 14 | + TCA0 (16-bit) used by PWM generation on pins 5, 9 and 10 |
| 15 | + TCB0 (16-bit) used by PWM generation on pin 6 |
| 16 | + TCB1 (16-bit) used by PWM generation on pin 3 |
| 17 | + TCB2 (16-bit) |
| 18 | + TCB3 (16-bit) |
| 19 | + //////////////////////////////////////////// |
| 20 | + // For ATmega4809 (Nano Every, Uno WiFi Rev2, etc.) |
| 21 | + Pin 3 => TIMERB1, // 3 PF5, 8-bit PWM, 16-bit counter |
| 22 | + Pin 5 => TIMERA0, // 5 PB2, 16-bit PWM, 16-bit counter |
| 23 | + Pin 6 => TIMERB0, // 6 PF4, 8-bit PWM, 16-bit counter |
| 24 | + Pin 9 => TIMERA0, // 9 PB0, 16-bit PWM, 16-bit counter |
| 25 | + Pin 10 => TIMERA0, // 10 PB1, 16-bit PWM, 16-bit counter |
| 26 | + //////////////////////////////////////////// |
| 27 | +******************************************************************************************************************************/ |
| 28 | + |
| 29 | +// Use with Stepper-Motor driver, such as TMC2209 |
| 30 | + |
| 31 | +#define _PWM_LOGLEVEL_ 4 |
| 32 | + |
| 33 | +#include "megaAVR_PWM.h" |
| 34 | + |
| 35 | +#define USING_TIMERB true |
| 36 | + |
| 37 | +#if USING_TIMERB |
| 38 | + // Pins tested OK in Nano Every ATmega4809 |
| 39 | + #define STEP_PIN 3 // TimerB1, for higher frequencies, up to 100KHz |
| 40 | + //#define STEP_PIN 6 // TimerB0, for higher frequencies, up to 100KHz |
| 41 | +#elif USING_ARDUINO_MEGA_AVR_CORE |
| 42 | + // Pins tested OK in Nano Every ATmega4809 using Arduino megaAVR core |
| 43 | + // TimerA0 somehow can't be used with MegaCoreX |
| 44 | + #define STEP_PIN 5 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock |
| 45 | + //#define STEP_PIN 9 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock |
| 46 | + //#define STEP_PIN 10 // TimerA0, only accurate @ low frequencies (< 1KHz) because of low 250KHz clock |
| 47 | +#else |
| 48 | + #error TimerA0 to be used with Arduino megaAVR Core |
| 49 | +#endif |
| 50 | + |
| 51 | +#define DIR_PIN 4 |
| 52 | + |
| 53 | +//////////////////////////////////////////// |
| 54 | + |
| 55 | +megaAVR_PWM* stepper; |
| 56 | + |
| 57 | +void setSpeed(int speed) |
| 58 | +{ |
| 59 | + if (speed == 0) |
| 60 | + { |
| 61 | + // Use DC = 0 to stop stepper |
| 62 | + stepper->setPWM(STEP_PIN, 500, 0); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + // Set the frequency of the PWM output and a duty cycle of 50% |
| 67 | + digitalWrite(DIR_PIN, (speed < 0)); |
| 68 | + stepper->setPWM(STEP_PIN, abs(speed), 50); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void setup() |
| 73 | +{ |
| 74 | + pinMode(DIR_PIN, OUTPUT); |
| 75 | + |
| 76 | + Serial.begin(115200); |
| 77 | + |
| 78 | + while (!Serial && millis() < 5000); |
| 79 | + |
| 80 | + delay(100); |
| 81 | + |
| 82 | + Serial.print(F("\nStarting PWM_StepperControl on ")); |
| 83 | + Serial.println(BOARD_NAME); |
| 84 | + Serial.println(MEGA_AVR_PWM_VERSION); |
| 85 | + |
| 86 | + // Create PWM object and passed just a random frequency of 500 |
| 87 | + // The duty cycle is how you turn the motor on and off |
| 88 | + stepper = new megaAVR_PWM(STEP_PIN, 500, 0); |
| 89 | +} |
| 90 | + |
| 91 | +void loop() |
| 92 | +{ |
| 93 | + setSpeed(1000); |
| 94 | + delay(3000); |
| 95 | + |
| 96 | + // Stop before reversing |
| 97 | + setSpeed(0); |
| 98 | + delay(3000); |
| 99 | + |
| 100 | + // Reversing |
| 101 | + setSpeed(-500); |
| 102 | + delay(3000); |
| 103 | + |
| 104 | + // Stop before reversing |
| 105 | + setSpeed(0); |
| 106 | + delay(3000); |
| 107 | +} |
0 commit comments