![]() ![]() Initialize the UART serial connection for debugging Once all that is configured it is ok to begin the SPI bus. Please reference Arduino documentation for more information. The UNO has a clock rate of 16 MHz, so by using the SPI_CLOCK_DIV32 value we get a clock rate of 500 kHz. We will do this by using the SPI clock divider. For prototyping we will use 500 kHz, but the encoder works up to 2 MHz. And lastly, to use the SPI bus with the AMT22 we need to select a clock rate. We will do that by feeding the Serial.begin() function to our defined BAUDRATE.īefore initializing SPI, we should also make sure the chip select line is in the proper state. We also need to initialize the serial port so we can get data back to the host computer. However, it does not hurt to do so, and can prevent issues in the event that the code is transferred to another device with different libraries or processors. It is not necessary to set the direction of the main SPI pins (except chip select) since the SPI interface will do that for us. Using the Arduino function pinMode() we will set the 4 SPI pins to the proper direction. In the setup() function we will initialize all of our SPI pins and set up our serial interfaces. The first one is called setup() where you can make all of your initializations, and a second one called loop() which once entered, repeats continuously. ![]() ![]() Normally in C the executable code all starts in the main() function but for Arduino it is separated into two separate functions. Now that we have finished the compiler defines we can move on to executable code. * We will use these define macros so we can write code once compatible with 12 or 14 bit encoders */ * Include the SPI library for the arduino boards */ It is not necessary to define the main pins for SPI communication, but it is helpful to do as a reference. Moving all the numbers to the top also makes the code easier to read. This way if the code needs to be modified for a different board, or we want to change which pins we use, it can easily be done here in one place. We also want to use defines at the top for the SPI pins. So, we will create two defines for the resolution of the encoder that we can use with our function calls. The AMT22 encoder is available as a 12-bit or 14-bit encoder, but we only want to write code once. Let us also define a couple of characters we can send over serial for formatting our data. ![]() Because the encoder does not care about the content of the first byte, we will define it as a NOP (no-operation). We should also define the commands used by the AMT22. We will set the baud rate to 115200 in a define macro. We want to get the position from the Arduino to the computer, so we will use the built-in serial connection to the Arduino IDE. The connections to various Arduino boards will be different than this example, so consult Arduino documentation for additional information.įirst, since we are using the Arduino's SPI bus we need to include the SPI library. The following connections are for the Arduino Uno board, however, most all Arduino boards will be compatible with this code. Now mount the AMT22 encoder to your motor or assembly using the AMT mounting instructions to ensure proper installation. You can use the programming module for the AMT22 and our AMT Viewpoint software to verify the encoder firmware is up-to-date.Īfter confirming the encoder has the correct firmware, put the encoder into RUN mode by moving the switch on the back of the encoder to the proper position. With the ability to zero the encoder's position, you can define where the encoder should have its origin.īefore beginning make sure that the AMT22 encoder has the latest firmware. Knowing the absolute position means that you will always know the exact angle of the device. No matter how many times the device is rotated the encoder will always know its absolute position. For a 12-bit encoder there are 4096 distinct positions, and for a 14-bit encoder there are 16384 positions. This means that over a single revolution the encoder will have that many distinct positions. The AMT22 is available as a 12-bit or 14-bit absolute encoder.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |