GY-63_MS5611/libraries/DRV8825/DRV8825.cpp

205 lines
3.3 KiB
C++
Raw Normal View History

2022-07-08 10:11:40 -04:00
//
// FILE: DRV8825.cpp
// AUTHOR: Rob Tillaart
2023-09-17 04:17:04 -04:00
// VERSION: 0.2.0
2022-07-08 10:11:40 -04:00
// PURPOSE: Arduino library for DRV8825 stepper motor driver
// DATE: 2022-07-07
// URL: https://github.com/RobTillaart/DRV8825
#include "DRV8825.h"
DRV8825::DRV8825()
{
}
2022-07-15 04:22:42 -04:00
bool DRV8825::begin(uint8_t DIR, uint8_t STEP, uint8_t EN, uint8_t RST, uint8_t SLP)
2022-07-08 10:11:40 -04:00
{
2022-07-15 04:22:42 -04:00
_directionPin = DIR;
_stepPin = STEP;
2022-07-08 10:11:40 -04:00
pinMode(_directionPin, OUTPUT);
pinMode(_stepPin, OUTPUT);
2022-07-15 04:22:42 -04:00
digitalWrite(_directionPin, LOW);
digitalWrite(_stepPin, LOW);
2022-07-09 03:39:01 -04:00
2022-07-15 04:22:42 -04:00
// handle conditional parameters
if (EN != 255)
{
_enablePin = EN;
pinMode(_enablePin, OUTPUT);
2023-02-19 08:16:16 -05:00
digitalWrite(_enablePin, LOW); // page 3
2022-07-15 04:22:42 -04:00
}
if (RST != 255)
{
_resetPin = RST;
pinMode(_resetPin, OUTPUT);
2023-09-17 04:17:04 -04:00
digitalWrite(_resetPin, HIGH); // page 3
2022-07-15 04:22:42 -04:00
}
if (SLP != 255)
{
_sleepPin = SLP;
pinMode(_sleepPin, OUTPUT);
digitalWrite(_sleepPin, HIGH); // page 3
}
2022-07-08 10:11:40 -04:00
return true;
}
void DRV8825::setStepsPerRotation(uint16_t stepsPerRotation)
{
_stepsPerRotation = stepsPerRotation;
}
uint16_t DRV8825::getStepsPerRotation()
{
return _stepsPerRotation;
}
bool DRV8825::setDirection(uint8_t direction)
{
if (direction > 1) return false;
_direction = direction;
2022-07-09 03:39:01 -04:00
// timing from datasheet 650 ns figure 1
delayMicroseconds(1);
2022-07-08 10:11:40 -04:00
digitalWrite(_directionPin, _direction);
2022-07-09 03:39:01 -04:00
delayMicroseconds(1);
2022-07-08 10:11:40 -04:00
return true;
}
uint8_t DRV8825::getDirection()
{
return digitalRead(_directionPin);
}
void DRV8825::step()
{
digitalWrite(_stepPin, HIGH);
2023-09-17 04:17:04 -04:00
if (_stepPulseLength > 0) delayMicroseconds(_stepPulseLength);
2022-07-08 10:11:40 -04:00
digitalWrite(_stepPin, LOW);
2023-09-17 04:17:04 -04:00
if (_stepPulseLength > 0) delayMicroseconds(_stepPulseLength);
2022-07-08 10:11:40 -04:00
2022-07-09 03:39:01 -04:00
_steps++;
2022-07-08 10:11:40 -04:00
if (_stepsPerRotation > 0)
{
2022-07-09 03:39:01 -04:00
if (_direction == DRV8825_CLOCK_WISE)
{
2023-09-17 04:17:04 -04:00
_position++;
if (_position >= _stepsPerRotation) _position = 0;
2022-07-09 03:39:01 -04:00
}
else
{
2023-09-17 04:17:04 -04:00
if (_position == 0) _position = _stepsPerRotation;
_position--;
2022-07-09 03:39:01 -04:00
}
2022-07-08 10:11:40 -04:00
}
}
2022-07-09 03:39:01 -04:00
uint32_t DRV8825::resetSteps(uint32_t s)
2022-07-08 10:11:40 -04:00
{
2022-07-09 03:39:01 -04:00
uint32_t t = _steps;
2022-07-08 10:11:40 -04:00
_steps = s;
return t;
}
2022-07-09 03:39:01 -04:00
uint32_t DRV8825::getSteps()
2022-07-08 10:11:40 -04:00
{
return _steps;
}
2022-07-09 03:39:01 -04:00
2023-09-17 04:17:04 -04:00
void DRV8825::setStepPulseLength(uint16_t stepPulseLength)
2022-07-08 10:11:40 -04:00
{
2023-09-17 04:17:04 -04:00
_stepPulseLength = stepPulseLength;
2022-07-08 10:11:40 -04:00
}
uint16_t DRV8825::getStepPulseLength()
{
2023-09-17 04:17:04 -04:00
return _stepPulseLength;
2022-07-08 10:11:40 -04:00
}
2023-09-17 04:17:04 -04:00
bool DRV8825::setPosition(uint16_t position)
2022-07-09 03:39:01 -04:00
{
2023-09-17 04:17:04 -04:00
if (position >= _stepsPerRotation) return false;
_position = position;
2022-07-09 03:39:01 -04:00
return true;
}
uint16_t DRV8825::getPosition()
{
2023-09-17 04:17:04 -04:00
return _position;
2022-07-09 03:39:01 -04:00
}
2022-07-15 04:22:42 -04:00
// Table page 3
2023-02-19 08:16:16 -05:00
bool DRV8825::enable()
2022-07-15 04:22:42 -04:00
{
2023-02-19 08:16:16 -05:00
if (_enablePin == 255) return false;
digitalWrite(_enablePin, LOW);
return true;
2022-07-15 04:22:42 -04:00
}
2023-02-19 08:16:16 -05:00
bool DRV8825::disable()
2022-07-15 04:22:42 -04:00
{
2023-02-19 08:16:16 -05:00
if (_enablePin == 255) return false;
digitalWrite(_enablePin, HIGH);
return true;
2022-07-15 04:22:42 -04:00
}
bool DRV8825::isEnabled()
{
if (_enablePin != 255)
{
return (digitalRead(_enablePin) == LOW);
}
return true;
}
2023-02-19 08:16:16 -05:00
bool DRV8825::reset()
2022-07-15 04:22:42 -04:00
{
2023-02-19 08:16:16 -05:00
if (_resetPin == 255) return false;
digitalWrite(_resetPin, LOW);
2023-09-17 04:17:04 -04:00
delay(1);
digitalWrite(_resetPin, HIGH);
2023-02-19 08:16:16 -05:00
return true;
2022-07-15 04:22:42 -04:00
}
2023-02-19 08:16:16 -05:00
bool DRV8825::sleep()
2022-07-15 04:22:42 -04:00
{
2023-02-19 08:16:16 -05:00
if (_sleepPin == 255) return false;
digitalWrite(_sleepPin, LOW);
return true;
2022-07-15 04:22:42 -04:00
}
2023-02-19 08:16:16 -05:00
bool DRV8825::wakeup()
2022-07-15 04:22:42 -04:00
{
2023-02-19 08:16:16 -05:00
if (_sleepPin == 255) return false;
digitalWrite(_sleepPin, HIGH);
return true;
2022-07-15 04:22:42 -04:00
}
bool DRV8825::isSleeping()
{
if (_sleepPin != 255)
{
return (digitalRead(_sleepPin) == LOW);
}
return false;
}
2022-07-09 03:39:01 -04:00
2022-07-08 10:11:40 -04:00
// -- END OF FILE --