Hello,
So I am trying to Interface AMT203-V Encoder with STM32f103.
I have connected the Controller to Encoder via 3.3V-5V Bi-directional logic level converter.
I wrote the code based on the logic that I had used last time while interfacing the Encoder to Arduino 2560 Mega.
But the data on the MISO line is wrong because encoder is not behaving as expected.
Following statement is from the datasheet.
"For example, to read the position the master will send the rd_pos command (0x10), it (encoder) will then send no operation commands (nop_ a5, 0x00) until it (master) 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. It is recommended that the master leave a 20 μs delay between reads to avoid extending the read time by forcing wait sequences. Each byte transmitted must be followed by a release of the Chip Select line (CSB)."
None of this can be seen on MISO line.
I believe I've followed everything stated in the datasheet but still having problems communicating with the Encoder module. I have attached the Screenshots of the data from DSO. Also I am new to the STM32 environment.
Datasheet:
Screenshots:
CH1: Clock; CH2: MISO; CH3: MOSI; CH4: CS.
only CS pin (CH4) is connected to the Logic converter hence rest of the pins are at 3.3V.
I have deliberately lowered the frequency. The results were same on higher frequency as well.
Connections:
CODE:
/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart2_rx;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SPI1_Init(void);
/* USER CODE BEGIN PFP */
const uint8_t nop_a5 = 0x00;
const uint8_t rd_pos = 0x10;
const uint8_t set_zero_point = 0x70;
int main(void)
{
uint8_t Return_Value;
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_USART2_UART_Init();
MX_SPI1_Init();
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0,GPIO_PIN_SET); //Chip Select pin.
/* Infinite loop */
while (1)
{
//Sending/Receiving first Byte.
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)&rd_pos, (uint8_t *)&Return_Value, sizeof(uint8_t), 300);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY){};
//Waiting for the encoder to send rd_pos(0x10).
while(Return_Value != 0x10){
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)&nop_a5 , (uint8_t *)&Return_Value, sizeof(uint8_t), 300);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY){};
}
//Sending two nop_a5(0x00) commands after receiving rd_pos(0x10).
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)&nop_a5 , (uint8_t *)&Return_Value, sizeof(uint8_t), 300);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY){};
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)&nop_a5 , (uint8_t *)&Return_Value, sizeof(uint8_t), 300);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY){};
}
}
Any kind of help will be appreciated.
Thank You


