Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2 from Adrianotiger/master
Browse files Browse the repository at this point in the history
using SPISettings
  • Loading branch information
benhowes committed Apr 12, 2016
2 parents dd200f5 + 70a5401 commit 84a4ee1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
55 changes: 34 additions & 21 deletions lib/AS5048A/AS5048A.cpp
@@ -1,53 +1,54 @@
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Arduino.h"

#include <AS5048A.h>
#include <SPI.h>

//#define AS5048A_DEBUG

const int AS5048A_CLEAR_ERROR_FLAG = 0x0001;
const int AS5048A_PROGRAMMING_CONTROL = 0x0003;
const int AS5048A_OTP_REGISTER_ZERO_POS_HIGH = 0x0016;
const int AS5048A_OTP_REGISTER_ZERO_POS_LOW = 0x0017;
const int AS5048A_DIAG_AGC = 0x3FFD;
const int AS5048A_MAGNITUDE = 0x3FFE;
const int AS5048A_ANGLE = 0x3FFF;

/**
* Constructor
*/
AS5048A::AS5048A(byte arg_cs){
_cs = arg_cs;
errorFlag = false;
position = 0;

//setup pins
pinMode(_cs,OUTPUT);

}


/**
* Initialiser
* Sets up the SPI interface
*/
void AS5048A::init()
{
SPI.setDataMode(SPI_MODE1);
SPI.setClockDivider(SPI_CLOCK_DIV64);
SPI.setBitOrder(MSBFIRST);
void AS5048A::init(){
// 1MHz clock (AMS should be able to accept up to 10MHz)
settings = SPISettings(1000000, MSBFIRST, SPI_MODE1);

//setup pins
pinMode(_cs, OUTPUT);

//SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
SPI.begin();
}

/**
* Closes the SPI connection
* SPI has an internal SPI-device counter, for each init()-call the close() function must be called exactly 1 time
*/
void AS5048A::close()
{
void AS5048A::close(){
SPI.end();
}

/**
* Utility function used to calculate even parity of word
*/
byte AS5048A::spiCalcEvenParity(word value)
{
byte AS5048A::spiCalcEvenParity(word value){
byte cnt = 0;
byte i;

Expand Down Expand Up @@ -169,6 +170,9 @@ word AS5048A::read(word registerAddress){
Serial.println(command, BIN);
#endif

//SPI - begin transaction
SPI.beginTransaction(settings);

//Send the command
digitalWrite(_cs, LOW);
SPI.transfer(left_byte);
Expand All @@ -181,6 +185,9 @@ word AS5048A::read(word registerAddress){
right_byte = SPI.transfer(0x00);
digitalWrite(_cs, HIGH);

//SPI - end transaction
SPI.endTransaction();

#ifdef AS5048A_DEBUG
Serial.print("Read returned: ");
Serial.print(left_byte, BIN);
Expand Down Expand Up @@ -228,12 +235,15 @@ word AS5048A::write(word registerAddress, word data) {
Serial.println(command, BIN);
#endif

//SPI - begin transaction
SPI.beginTransaction(settings);

//Start the write command with the target address
digitalWrite(_cs, LOW);
SPI.transfer(left_byte);
SPI.transfer(right_byte);
digitalWrite(_cs,HIGH);

word dataToSend = 0b0000000000000000;
dataToSend |= data;

Expand All @@ -252,13 +262,16 @@ word AS5048A::write(word registerAddress, word data) {
SPI.transfer(left_byte);
SPI.transfer(right_byte);
digitalWrite(_cs,HIGH);

//Send a NOP to get the new data in the register
digitalWrite(_cs, LOW);
left_byte =-SPI.transfer(0x00);
right_byte = SPI.transfer(0x00);
digitalWrite(_cs, HIGH);

//SPI - end transaction
SPI.endTransaction();

//Return the data, stripping the parity and error bits
return (( ( left_byte & 0xFF ) << 8 ) | ( right_byte & 0xFF )) & ~0xC000;
}
15 changes: 5 additions & 10 deletions lib/AS5048A/AS5048A.h
@@ -1,15 +1,8 @@
#ifndef as5048_h
#define as5048_h
#define LIBRARY_VERSION 1.0.0
#define LIBRARY_VERSION 1.0.1


#define AS5048A_CLEAR_ERROR_FLAG 0x0001
#define AS5048A_PROGRAMMING_CONTROL 0x0003
#define AS5048A_OTP_REGISTER_ZERO_POS_HIGH 0x0016
#define AS5048A_OTP_REGISTER_ZERO_POS_LOW 0x0017
#define AS5048A_DIAG_AGC 0x3FFD
#define AS5048A_MAGNITUDE 0x3FFE
#define AS5048A_ANGLE 0x3FFF
#include <SPI.h>

class AS5048A{

Expand All @@ -21,7 +14,9 @@ class AS5048A{
byte clk;
word position;
word transaction(word data);


SPISettings settings;

public:

/**
Expand Down

0 comments on commit 84a4ee1

Please sign in to comment.