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

213 lines
3.1 KiB
C++
Raw Normal View History

2022-07-08 10:11:40 -04:00
//
// FILE: DRV8825.cpp
// AUTHOR: Rob Tillaart
2022-11-02 06:52:32 -04:00
// VERSION: 0.1.3
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
2022-11-02 06:52:32 -04:00
//
// HISTORY: see changelog.md
2022-07-08 10:11:40 -04:00
#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);
digitalWrite(_enablePin, LOW); // page 3
}
if (RST != 255)
{
_resetPin = RST;
pinMode(_resetPin, OUTPUT);
digitalWrite(_resetPin, LOW); // page 3
}
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);
2022-07-09 03:39:01 -04:00
if (_us > 0) delayMicroseconds(_us);
2022-07-08 10:11:40 -04:00
digitalWrite(_stepPin, LOW);
2022-07-09 03:39:01 -04:00
if (_us > 0) delayMicroseconds(_us);
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)
{
_pos++;
if (_pos >= _stepsPerRotation) _pos = 0;
}
else
{
if (_pos == 0) _pos = _stepsPerRotation;
_pos--;
}
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
2022-07-08 10:11:40 -04:00
void DRV8825::setStepPulseLength(uint16_t us)
{
_us = us;
}
uint16_t DRV8825::getStepPulseLength()
{
return _us;
}
2022-07-09 03:39:01 -04:00
bool DRV8825::setPosition(uint16_t pos)
{
if (pos >= _stepsPerRotation) return false;
_pos = pos;
return true;
}
uint16_t DRV8825::getPosition()
{
return _pos;
}
2022-07-15 04:22:42 -04:00
// Table page 3
void DRV8825::enable()
{
if (_enablePin != 255)
{
digitalWrite(_enablePin, LOW);
}
}
void DRV8825::disable()
{
if (_enablePin != 255)
{
digitalWrite(_enablePin, HIGH);
}
}
bool DRV8825::isEnabled()
{
if (_enablePin != 255)
{
return (digitalRead(_enablePin) == LOW);
}
return true;
}
void DRV8825::reset()
{
if (_resetPin != 255)
{
digitalWrite(_resetPin, HIGH);
delay(1);
digitalWrite(_resetPin, LOW);
}
}
void DRV8825::sleep()
{
if (_sleepPin != 255)
{
digitalWrite(_sleepPin, LOW);
}
}
void DRV8825::wakeup()
{
if (_sleepPin != 255)
{
digitalWrite(_sleepPin, HIGH);
}
}
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 --