Advertisement
Guest User

AMT203-V SPI Encoder

a guest
Jul 3rd, 2020
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * AMT20_SPI_Sample_Code.ino
  3.  * Company: CUI Inc.
  4.  * Author: Jason Kelly
  5.  * Version: 1.0.0.0
  6.  * Date: August 8, 2016
  7.  *
  8.  * This sample code can be used with the Arduino Uno to control the AMT20 encoder.
  9.  * It uses SPI to control the encoder and the the Arduino UART to report back to the PC
  10.  * via the Arduino Serial Monitor. Code can be modified for any Arduino with an SPI bus.
  11.  * For more information or assistance contact CUI Inc for support.
  12.  *
  13.  * After uploading code to Arduino UNO open the open the Serial Monitor under the Tools
  14.  * menu and set the baud rate to 115200 to view the serial stream the position from the AMT20.
  15.  *
  16.  * Arduino Pin Connections
  17.  * SPI Clock (SCK): Pin 13
  18.  * SPI MOSI:        Pin 11
  19.  * SPI MISO:        Pin 12
  20.  * SPI Chip Select: Pin 10
  21.  *
  22.  * AMT20 Pin Connections
  23.  * SPI Clock (SCK):       Pin 5
  24.  * SPI MOSI:              Pin 7
  25.  * SPI MISO:              PIN 3
  26.  * SPI Chip Select (CSB): Pin 2
  27.  * Vdd (5V):              Pin 6
  28.  * GND:                   Pin 4
  29.  *
  30.  *
  31.  * This is free and unencumbered software released into the public domain.
  32.  * Anyone is free to copy, modify, publish, use, compile, sell, or
  33.  * distribute this software, either in source code form or as a compiled
  34.  * binary, for any purpose, commercial or non-commercial, and by any
  35.  * means.
  36.  *
  37.  * In jurisdictions that recognize copyright laws, the author or authors
  38.  * of this software dedicate any and all copyright interest in the
  39.  * software to the public domain. We make this dedication for the benefit
  40.  * of the public at large and to the detriment of our heirs and
  41.  * successors. We intend this dedication to be an overt act of
  42.  * relinquishment in perpetuity of all present and future rights to this
  43.  * software under copyright law.
  44.  *
  45.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  46.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  47.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  48.  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  49.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  50.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  51.  * OTHER DEALINGS IN THE SOFTWARE.
  52.  */
  53.  
  54. //include SPI library
  55. #include <SPI.h>
  56.  
  57. //this is the serial baud rate for talking to the Arduino
  58. #define baudRate 115200
  59.  
  60. //this will be our SPI timout limit
  61. #define timoutLimit 100
  62.  
  63. //SPI commands used by the AMT20
  64. #define nop 0x00            //no operation
  65. #define rd_pos 0x10         //read position
  66. #define set_zero_point 0x70 //set zero point
  67.  
  68. //set the chip select pin for the AMT20
  69. const int CS = 10;
  70.  
  71. //Arduino uses a setup function for all program initializations
  72. void setup()
  73. {
  74.   //Initialize the UART serial connection
  75.   Serial.begin(baudRate);
  76.  
  77.   //Set I/O mode of all SPI pins.
  78.   pinMode(SCK, OUTPUT);
  79.   pinMode(MOSI, OUTPUT);
  80.   pinMode(MISO, INPUT);
  81.   pinMode(CS, OUTPUT);
  82.  
  83.   //Initialize SPI using the SPISettings(speedMaxium, dataOrder, dataAMode) function
  84.   //For our settings we will use a clock rate of 500kHz, and the standard SPI settings
  85.   //of MSB First and SPI Mode 0
  86.   SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
  87.  
  88.   //Using SPI.beginTransaction seems to require explicitly setting the beginning state
  89.   //of the CS pin as opposed to the SPI.begin() function that does this for us.
  90.   digitalWrite(CS, HIGH);
  91.  
  92. }
  93.  
  94. //After the setup() method this loop gets entered and is the main() function for our program
  95. void loop()
  96. {
  97.   uint8_t data;               //this will hold our returned data from the AMT20
  98.   uint8_t timeoutCounter;     //our timeout incrementer
  99.   uint16_t currentPosition;   //this 16 bit variable will hold our 12-bit position
  100.  
  101.   while(true)
  102.   {
  103.     //reset the timoutCounter;
  104.     timeoutCounter = 0;
  105.    
  106.     //send the rd_pos command to have the AMT20 begin obtaining the current position
  107.     data = SPIWrite(rd_pos);
  108.  
  109.     //we need to send nop commands while the encoder processes the current position. We
  110.     //will keep sending them until the AMT20 echos the rd_pos command, or our timeout is reached.
  111.     while (data != rd_pos && timeoutCounter++ < timoutLimit)
  112.     {
  113.       data = SPIWrite(nop);
  114.     }
  115.  
  116.  
  117.     if (timeoutCounter < timoutLimit) //rd_pos echo received
  118.     {
  119.       //We received the rd_pos echo which means the next two bytes are the current encoder position.
  120.       //Since the AMT20 is a 12 bit encoder we will throw away the upper 4 bits by masking.
  121.  
  122.       //Obtain the upper position byte. Mask it since we only need it's lower 4 bits, and then
  123.       //shift it left 8 bits to make room for the lower byte.
  124.       currentPosition = (SPIWrite(nop)& 0x0F) << 8;
  125.  
  126.       //OR the next byte with the current position
  127.       currentPosition |= SPIWrite(nop);
  128.     }
  129.     else //timeout reached
  130.     {
  131.       //This means we had a problem with the encoder, most likely a lost connection. For our
  132.       //purposes we will alert the user via the serial connection, and then stay here forever.
  133.  
  134.       Serial.write("Error obtaining position.\n");
  135.       Serial.write("Reset Arduino to restart program.\n");
  136.      
  137.       while(true);
  138.     }
  139.  
  140.     Serial.write("Current position: ");
  141.     Serial.print(currentPosition, DEC); //current position in decimal
  142.     Serial.write("\t 0x");
  143.     Serial.print(currentPosition, HEX); //current position in hexidecimal
  144.     Serial.write("\t 0b");
  145.     Serial.print(currentPosition, BIN); //current position in binary
  146.     Serial.write("\n");
  147.    
  148.    
  149.     //Since we are displaying our position over the serial monitor we don't need updates that fast
  150.     delay(250);
  151.   }
  152. }
  153.  
  154. //We will use this function to handle transmitting SPI commands in order to keep our code clear and concise.
  155. //It will return the byte received from SPI.transfer()
  156. uint8_t SPIWrite(uint8_t sendByte)
  157. {
  158.   //holder for the received over SPI
  159.   uint8_t data;
  160.  
  161.   //the AMT20 requires the release of the CS line after each byte
  162.   digitalWrite(CS, LOW);
  163.   data = SPI.transfer(sendByte);
  164.   digitalWrite(CS, HIGH);
  165.  
  166.   //we will delay here to prevent the AMT20 from having to prioritize SPI over obtaining our position
  167.   delayMicroseconds(10);
  168.  
  169.   return data;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement