Go Down

Topic: having issues in interfacing SPI 14 bit rotary encoder  (Read 61 times) previous topic - next topic

rushikesh142

Aug 10, 2018, 10:58 am Last Edit: Aug 10, 2018, 11:02 am by rushikesh142 Reason: attaching image in place of excel sheet
I am interfacing a 14 bit hall effect encoder with arduino. The chip in the encoder is MLX90363.The datasheet is in the attachments.The code provided in the datasheet is for mbed board . I converted it to its arduino equivalent,attached below.I am getting the values but the values are not correct, i measured the angle (real angle) and the values i got from the encoder ,the values are all given in the image which is attached.


Tell me about what is missing or wrong with the code or any hardware etc.
I am a beginner in arduino.

Thanks.




Code: [Select]
#include<SPI.h>
#include<stdlib.h>

const int chipSelectPin = 10;
unsigned int val[8],val1[8];
char val2[2];
long int error;
unsigned int anglefinal,anglefinal1,ang,crc,virtual_gain,roll_cnt;
float angle,ang_r;
float f32_lsb_to_dec_degrees = 0.02197;
void setup() {
  // put your setup code here, to run once:
  pinMode(chipSelectPin, INPUT_PULLUP);
Serial.begin(9600);
 SPI.begin();
 SPI.setClockDivider(SPI_CLOCK_DIV128);
 SPI.setBitOrder(MSBFIRST);
 SPI.setDataMode(SPI_MODE1);
 digitalWrite(chipSelectPin, HIGH);
 delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(chipSelectPin, LOW);
// GET1 message
val1[0]=SPI.transfer(0x00);
val1[1]=SPI.transfer(0x00);
val1[2]=SPI.transfer(0xFF);
val1[3]=SPI.transfer(0xFF);
val1[4]=SPI.transfer(0x00);
val1[5]=SPI.transfer(0x00);
val1[6]=SPI.transfer(0x13);
val1[7]=SPI.transfer(0xEA);
digitalWrite(chipSelectPin, HIGH);
delay(1);
digitalWrite(chipSelectPin, LOW);
//delay(1);
// NOP
val[0]=SPI.transfer(0x00);
val[1]=SPI.transfer(0x00);
val[2]=SPI.transfer(0xAA);
val[3]=SPI.transfer(0xAA);
val[4]=SPI.transfer(0x00);
val[5]=SPI.transfer(0x00);
val[6]=SPI.transfer(0xD0);
val[7]=SPI.transfer(0xAB);

digitalWrite(chipSelectPin, HIGH);
//digitalWrite(chipSelectPin, HIGH);
//delay(500);
anglefinal1=(val[1] & 0x3F)<<8;
Serial.println(" ");
Serial.print("val1  ");
Serial.print(val[1]);
/*char k1;
val[1]=k1;
int intval1 = atol(k1);
Serial.println(" ");
Serial.print("intval1  ");
Serial.print(intval1);*/


Serial.println(" ");
Serial.print("val0  ");
Serial.print(val[0]);
/*char k0;
val[0]=k0;
int intval0 = atol(k0);
Serial.println(" ");
Serial.print("intval0  ");
Serial.print(intval0);*/



Serial.println(" ");
Serial.print("anglefinal1  ");
Serial.print(anglefinal1);
Serial.println(" ");
anglefinal1=anglefinal1 + val[0];
Serial.println(" ");
Serial.print("anglefinal1  ");
Serial.print(anglefinal1);
Serial.println(" ");
/*anglefinal = anglefinal1;
Serial.println(" ");
Serial.print("anglefinal1  ");
Serial.print(anglefinal1);
Serial.println(" ");*/
error=(val[1])>>6;
//Serial.println(anglefinal);
delay(10);
val2[1]= (val[1] & 0x3F)<<8;
val2[0]= val[0];
ang = atoi(val2);
ang_r = ang*f32_lsb_to_dec_degrees;
Serial.print("My_angle=  ");
Serial.println(ang_r,5);
Serial.print("\n");
angle=anglefinal1*f32_lsb_to_dec_degrees;
Serial.print("angle ==== ");
Serial.print(angle,5);
Serial.println(" ");
Serial.print("error=  ");
Serial.print(error);
Serial.println(" ");

crc=val[7];
Serial.print("CRC =  ");
Serial.print(crc);
Serial.println(" ");

virtual_gain = val[4];
Serial.print(" virtual gain =  ");
Serial.print(virtual_gain);
Serial.println(" ");
roll_cnt = val[6] & 0x3F ;
Serial.print(" roll cnt  ");
Serial.print(roll_cnt);
Serial.println(" ");
Serial.println("*****************************************");
delay(5000);
}

pylon

Code: [Select]
val2[1]= (val[1] & 0x3F)<<8;

I don't know what you think this code should do but val2[1] will always be 0 as left shifting a byte by 8 bits never results in anything else than 0.

Code: [Select]
val2[1]= (val[1] & 0x3F)<<8;
val2[0]= val[0];
ang = atoi(val2);
ang_r = ang*f32_lsb_to_dec_degrees;


The output of this code part corresponds to which column in your table?

Go Up