I realized that my previous post was quite extensive, therefore I reduced it to a minimum working example with the following Arduino code:
#include <SPI.h>
#define CS 10 //Chip or Slave select
uint8_t temp[1];
void setup() {
pinMode(CS,OUTPUT);//Slave Select
digitalWrite(CS,HIGH);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV32);
Serial.begin(115200);
Serial.println("starting");
Serial.flush();
delay(2000);
SPI.end();
}
uint8_t SPI_T (uint8_t msg) //Repetive SPI transmit sequence
{
uint8_t msg_temp = 0; //variable to hold recieved data
digitalWrite(CS,LOW); //select spi device
msg_temp = SPI.transfer(msg); //send and recieve
digitalWrite(CS,HIGH); //deselect spi device
return(msg_temp); //return recieved byte
}
void loop() {
uint8_t recieved = 0xA5;
SPI.begin(); //start transmition
SPI_T(0x10); //issue read command
while (recieved != 0x10) //loop while encoder is not ready to send
{
recieved = SPI_T(0x00); //cleck again if encoder is still working
delay(2); //wait a bit
Serial.print("Acknowledgement = ");
Serial.print(recieved,DEC);
Serial.print('\n');
}
temp[0] = SPI_T(0x00); //Recieve MSB
temp[1] = SPI_T(0x00); // recieve LSB
Serial.print("First message = ");
Serial.print(temp[0],DEC);
Serial.print('\n');
Serial.print("Second message = ");
Serial.print(temp[1],DEC);
Serial.print('\n');
SPI.end(); //end transmition
delay(1000);
This gives me the following output at the serial monitor:
starting
Acknowledgement = 165
Acknowledgement = 165
Acknowledgement = 16 %<-- Here the encoder acknowledges that it's ready
First message = 12 % This is the first message containing the upper four bits
Second message = 115 % These are the lower eight bits
Acknowledgement = 165
Acknowledgement = 165
Acknowledgement = 16 %<-- Here the encoder acknowledges that it's ready
First message = 12
Second message = 115
Acknowledgement = 165
Acknowledgement = 165
I tried to make this in MATLAB by:
clc
clear all
close all
%% Message definitions
rd_pos = hex2dec('10');
nop_a5 = hex2dec('00');
%% Arduino properties
Arduino = arduino('COM5', 'UNO', 'Libraries', 'SPI');
%% SPI bus properties
SPI_CSB_pin = 'D10'; % Valid for Uno
SPI_BUS_mode = 0;
SPI_BUS_BitOrder = 'msbfirst'; % From manual of amt20 sensor
SPI_BUS_Bitrate = 1e9; % From manual of amt20 sensor
SPI_BUS = spidev( Arduino, SPI_CSB_pin,...
'Mode', SPI_BUS_mode,...
'BitOrder',SPI_BUS_BitOrder, ...
'Bitrate',SPI_BUS_Bitrate);
%% Check if devide is ready to provide a position
resp = 0;
MaxAttemps = 10;
SPI_BUS.writeRead(rd_pos);
nAttemps = 0;
while ~(resp==rd_pos)
if nAttemps>MaxAttemps
error(['Tried to read encoder ' num2str(nAttemps) ' times, no valid response'])
end
resp = writeRead(SPI_BUS,nop_a5);
nAttemps = nAttemps + 1;
disp(['Acknowledgement = ' num2str(resp) ])
end
%% Read the upper 4 bits
FirstResp = writeRead(obj.SPI_BUS,obj.nop_a5,'uint8');
disp(['First message = ' num2str(FirstResp) ])
UpperFour = bitand(hex2dec('0F'),FirstResp,'uint8'); % Mask the first four bits
%% Read the lower 8 bits
LowerEight = writeRead(obj.SPI_BUS,obj.nop_a5);
disp(['Second message = ' num2str(LowerEight) ])
I never get out of the while loop and receive the following response:
Acknowledgement = 247
Acknowledgement = 24
Acknowledgement = 14
Acknowledgement = 255
Acknowledgement = 247
Acknowledgement = 247
Acknowledgement = 247
Acknowledgement = 247
Acknowledgement = 247
Acknowledgement = 247
Acknowledgement = 247
Error using Rotary_encoder_test (line 30)
Tried to read encoder 11 times, no valid response
What I see is that when I use Arduino IDE I receive 165 and then 16 when it's ready (as expected). With MATLAB I receive 247 and then 24 (when I think it is ready).
I read the help from the readwrite command in MATLAB where the following example is given:
writeCmd = bin2dec('0000 0010');
address = [255 0];
data = [250 155];
dataIn = [writeCmd address data];
out = writeRead(dev,dataIn)
What surprises me is that and address and data is is defined. When I use the same address and data as in the example, I receive a vector back everytime. This vector also does not contain the expected output.
I was thinking that it maybe does not give back uint8 but some other format but in which format is 0x10 respresented by 24?

