Skip to content

Adding SPISettings #3

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

Open
wants to merge 2 commits into
base: main
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
16 changes: 15 additions & 1 deletion Arduino_AMT22_lib/AMT22_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion Arduino_AMT22_lib/AMT22_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion Arduino_AMT22_lib/examples/AMT22_Sample_Code.ino
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@

/*Object creation*/
AMT22* Encoder;
SPISettings settings = SPISettings(2000000, MSBFIRST, SPI_MODE0);


void setup(){
//Initialize the UART serial connection for debugging
Serial.begin(BAUDRATE);
//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()
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down