Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewriting writeZeroPositon and writeDirection #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/AS5X47.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
AS5X47::AS5X47(uint8_t chipSelectPin) : spi(chipSelectPin) {
}

void AS5X47::initSPI(){
spi.init();
}

ReadDataFrame AS5X47::readRegister(uint16_t registerAddress) {
CommandFrame command;
command.values.rw = READ;
Expand Down Expand Up @@ -77,11 +81,31 @@ void AS5X47::writeSettings1(Settings1 values) {
void AS5X47::writeSettings2(Settings2 values){
writeRegister(SETTINGS2_REG, values.raw);
}
void AS5X47::writeZeroPosition(Zposm zposm, Zposl zposl){

void AS5X47::writeZeroPosition(){
ReadDataFrame readDataFrame;
readDataFrame = readRegister(ANGLE_REG);
Angle angle;
angle.raw = readDataFrame.values.data;

Zposm zposm;
Zposl zposl;

zposm.values.zposm = ((angle.raw >> 6) & 0x00ff);
zposl.values.zposl = angle.raw & 0x003f;
writeRegister(ZPOSM_REG, zposm.raw);
writeRegister(ZPOSL_REG, zposl.raw);
}

void AS5X47::writeDirection(uint8_t dir_){
ReadDataFrame readDataFrame;
readDataFrame = readRegister(SETTINGS1_REG);
Settings1 settings1DIR;
settings1DIR.raw = readDataFrame.values.data;
settings1DIR.values.dir = dir_;
writeSettings1(settings1DIR);
}

void AS5X47::printDebugString() {
ReadDataFrame readDataFrame;
readDataFrame = readRegister(ERRFL_REG);
Expand Down
6 changes: 4 additions & 2 deletions src/AS5X47.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,16 @@ typedef union {
class AS5X47 {
public:
AS5X47(uint8_t chipSelectPin);
void initSPI();
ReadDataFrame readRegister(uint16_t registerAddress);
void writeRegister(uint16_t registerAddress, uint16_t registerValue);
float readAngle();
void writeSettings1(Settings1 values);
void writeSettings2(Settings2 values);
void writeZeroPosition(Zposm zposm, Zposl zposl);
void writeZeroPosition();
void printDebugString();

void writeDirection(uint8_t dir_);

private:
bool isEven(uint16_t data);
AS5X47Spi spi;
Expand Down
12 changes: 7 additions & 5 deletions src/AS5X47Spi/AS5X47Spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,27 @@ AS5X47Spi::AS5X47Spi(uint8_t _chipSelectPin) {
chipSelectPin = _chipSelectPin;
pinMode(chipSelectPin, OUTPUT);
digitalWrite(chipSelectPin, HIGH);
SPI.begin();
}


void AS5X47Spi::init() {
SPI.begin();
}
void AS5X47Spi::writeData(uint16_t command, uint16_t value) {
// @todo Expose the SPI Maximum Frequency in library interface.
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE1));
// Send command
digitalWrite(chipSelectPin, LOW);
SPI.transfer16(command);
digitalWrite(chipSelectPin, HIGH);
delayMicroseconds(1);
// delayMicroseconds(1);
delay(10);
// Read data
digitalWrite(chipSelectPin, LOW);
SPI.transfer16(value);
digitalWrite(chipSelectPin, HIGH);
SPI.endTransaction();
delayMicroseconds(1);

// delayMicroseconds(1);
delay(10);
}

uint16_t AS5X47Spi::readData(uint16_t command, uint16_t nopCommand) {
Expand Down
1 change: 1 addition & 0 deletions src/AS5X47Spi/AS5X47Spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class AS5X47Spi {
public:

AS5X47Spi(uint8_t chipSelectPin);
void init();
void writeData(uint16_t command, uint16_t value);
uint16_t readData(uint16_t command, uint16_t nopCommand);

Expand Down