6 years, 11 months ago.

Interfacing with SPI Encoder (AMT 203V)

Hi all,

Got a query about using an AMT 203V encoder as I cannot seem to get my code to work as follows;

  1. include "mbed.h"

Initialise Encoder Ports Serial pc(USBTX, USBRX); SPI EncoderTest(PC_12, PC_11, PC_10); MOSI MISO CLK DigitalOut CS(PD_2); DigitalIn button(USER_BUTTON); int main(){ CS =1; int HighByte; int LowByte; int Value; EncoderTest.format(8,1); 8bit, mode 1 EncoderTest.frequency(800000); 800kHz while(true){ if(button ==1){ wait(0.3f); master will send the rd_pos command (0x10), it will then send no operation commands (nop_ a5, 0x00) until it receives the original rd_pos command back. Once it receives the rd_pos response from the encoder, it knows that the next two bytes of data will be the MSB and the LSB respectively. To receive those two bytes the master will send two no operation commands. CS = 0; put CSB on low Value = EncoderTest.write(0x10); Read position command printf("Value %d \n\r", Value); while (EncoderTest.write(0x00) != 0x10){}; wait until encoder ready HighByte = EncoderTest.write(0x00); LowByte = EncoderTest.write(0x00); printf("High: %d, Low: %d \n\r",HighByte, LowByte);

} CS=1; put CSB back on high } } end int main

The commented section is taken directly from the data sheet. There does not seem to be any initialisation sequence that is required either. I seem to get inconsistent results; Either the code gets stuck in the while loop, or it exits it and spits out both the high and low bytes as being equal to 0.

Any help would be greatly appreciated, cheer.

Be the first to answer this question.