diff --git a/Arduino_AMT22_lib/AMT22_lib.cpp b/Arduino_AMT22_lib/AMT22_lib.cpp index f22ec40..243166b 100644 --- a/Arduino_AMT22_lib/AMT22_lib.cpp +++ b/Arduino_AMT22_lib/AMT22_lib.cpp @@ -6,10 +6,11 @@ #include "AMT22_lib.h" -AMT22::AMT22(uint8_t cs, uint8_t resolution) { +AMT22::AMT22(uint8_t cs, uint8_t resolution, SPISettings settings) { digitalWrite(cs, HIGH); //Get the CS line high which is the default inactive state _cs = cs; _resolution = resolution; + _settings = settings; } /* @@ -62,9 +63,18 @@ uint8_t AMT22::spiWriteRead(uint8_t sendByte, uint8_t releaseLine){ //There is a minimum time requirement after CS goes low before data can be clocked out of the encoder. delayMicroseconds(3); + if(!releaseLine){ + SPI.beginTransaction(_settings); + } + //send the command data = SPI.transfer(sendByte); delayMicroseconds(3); //There is also a minimum time after clocking that CS should remain asserted before we release it + + if(releaseLine){ + SPI.endTransaction(); + } + setCSLine(releaseLine); //if releaseLine is high set it high else it stays low return data; @@ -111,6 +121,10 @@ void AMT22::setResolution(uint8_t resolution) { _resolution = resolution; } +void AMT22::setSettings(SPISettings settings) { + _settings = settings; +} + /* * This function is not related to the AMT22 class. It allows to set up communication via SPI. * It must be performed in the setup section of the Arduino main. diff --git a/Arduino_AMT22_lib/AMT22_lib.h b/Arduino_AMT22_lib/AMT22_lib.h index 7716168..dff3d9c 100644 --- a/Arduino_AMT22_lib/AMT22_lib.h +++ b/Arduino_AMT22_lib/AMT22_lib.h @@ -19,15 +19,17 @@ class AMT22 { public: - AMT22(uint8_t cs, uint8_t resolution); + AMT22(uint8_t cs, uint8_t resolution, SPISettings settings); uint16_t getPositionSPI(); void setZeroSPI(); void resetAMT22(); void setResolution(uint8_t resolution); + void setSettings(SPISettings settings); private: uint8_t _cs, _resolution; + SPISettings _settings; uint8_t spiWriteRead(uint8_t sendByte, uint8_t releaseLine); void setCSLine (uint8_t csLine); diff --git a/Arduino_AMT22_lib/examples/AMT22_Sample_Code.ino b/Arduino_AMT22_lib/examples/AMT22_Sample_Code.ino index e0612cf..7fd5996 100644 --- a/Arduino_AMT22_lib/examples/AMT22_Sample_Code.ino +++ b/Arduino_AMT22_lib/examples/AMT22_Sample_Code.ino @@ -50,6 +50,8 @@ /*Object creation*/ AMT22* Encoder; +SPISettings settings = SPISettings(2000000, MSBFIRST, SPI_MODE0); + void setup(){ //Initialize the UART serial connection for debugging @@ -57,7 +59,7 @@ void setup(){ //Initialize the SPI communication setUpSPI(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CLOCK_DIV32); //This operation only needs to be done once, not once for each Encoder object. //Initialize the encoder object with necessary parameters - Encoder = new AMT22(ENC,RES14); + Encoder = new AMT22(ENC,RES14, settings); } void loop() diff --git a/README.md b/README.md index 6f46334..4f36716 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ The public functions present are: * setZeroSPI() * resetAMT22() * setResolution(uint8 t resolution) +* setSettings(SPISettings settings) The first function gets the absolute position from the AMT22 encoder using the SPI bus. The AMT22 position includes 2 checkbits to use for position verification. Both 12-bit and 14-bit possible encoders transfer position via two bytes, giving 16-bits regardless of resolution. For 12-bit encoders the position is left-shifted two bits, leaving the right two bits as zeros. This gives the impression that the encoder is actually sending 14-bits, when it is actually sending 12-bit values, where every number is multiplied by 4.