Drive a stepper motor using A4988 with asynchronous. No wait() function.
Diff: AsyncStepper.cpp
- Revision:
- 1:0f8209567b7d
- Parent:
- 0:26fa30c35f74
--- a/AsyncStepper.cpp Thu Jul 12 01:27:04 2018 +0000 +++ b/AsyncStepper.cpp Sat Jul 28 18:22:05 2018 +0000 @@ -46,6 +46,8 @@ void AsyncStepper::ISR_PULSEOUT( void ) { + DISABLE_INTERRUPTS; + if ( !stopPulseOut ) { stepOut->write( !stepOut->read() ); @@ -58,16 +60,20 @@ } } + RESTORE_INTERRUPTS; return; } void AsyncStepper::SetRPM( uint32_t rpm ) { + DISABLE_INTERRUPTS; + ticker->detach(); oneRotationSteps = oneRotationFullSteps * ( stepMode + 1 ); pulseWidth_us = (uint64_t)( 30000000 / ( rpm * oneRotationSteps ) ); ticker->attach_us( callback(this, &AsyncStepper::ISR_PULSEOUT), pulseWidth_us ); + RESTORE_INTERRUPTS; return; } @@ -101,11 +107,14 @@ void AsyncStepper::Rotate( direction_e direction, uint32_t steps ) { + DISABLE_INTERRUPTS; + dirOut->write( direction ); AsyncStepper::Enable(); currentMaxStepCount = steps; stopPulseOut = false; + RESTORE_INTERRUPTS; return; }