Jump to content

Stargazers Lounge Uses Cookies

Like most websites, SGL uses cookies in order to deliver a secure, personalised service, to provide social media functions and to analyse our traffic. Continued use of SGL indicates your acceptance of our cookie policy.

  • Announcements

    sgl_imaging_challenge_banner_widefield.jpg.36065d79cb2625eb299137a5b4432c96.jpg

Gina

Arduino Based Weather Station

Recommended Posts

Gina    8,763

Thought the damping magnets could be causing the problem so removed them - now about 4m from the sensor chip.  Tried the axle magnet on top of the chip again but still no joy.  Wonder if it does need the FET and resistor from MISO.  I would have thought that if this was the problem the check would fail.

Share this post


Link to post
Share on other sites
Gina    8,763

I think the answer to the -1 display might be in here :-

/*
  MLX90316.cpp -  Library to use with Melexis MLX90316 rotary encoder chip.
  Created by Martin Nawrath KHM 2010.
  http://interface.khm.de
  Released into the public domain.
*/

#include "Arduino.h"
#include "MLX90316.h"


// constructor
MLX90316::MLX90316(){


};
// attach
void MLX90316::attach(int pinSS, int pinSCK, int pinMOSI )
{
	_pinSS = pinSS;
	_pinSCK = pinSCK;
	_pinMOSI = pinMOSI;

	pinMode(_pinSS , OUTPUT);
	pinMode(_pinSCK , OUTPUT);
	pinMode(_pinMOSI , OUTPUT);
}

//******************************************************
int MLX90316::readAngle() {
  byte bb;
  int ret=-1;
  unsigned int rr;
  long int lo;

  digitalWrite(_pinSS,0);


  bb=_spiByte(0x55);  // send sync byte ( AA reversed order = 55?)
  bb=_spiByte(0xFF);  // send sync byte FF)
 
   bb=_spiByte(0xFF); // receive 1. byte of mlx msb data
   rr= (bb << 8);   
   bb=_spiByte(0xFF); // receive 2. byte of lsb mlx data
   rr=rr+bb;
   
  if ((rr & 3) == 2) {
   
    if ( rr & (1 << 4)) ret=-2;  // signal to strong
    if ( rr & (1 << 5)) ret=-3;  // signal to weak
}
   
  if ((rr & 3) == 1) { // valid angle data ?
     rr = (rr >> 2);
     lo= rr ;
     lo=lo *  3600 / 16384;	// scale output to 360 deg, untit in tens of deg.	
    ret= lo;
  }

  digitalWrite(_pinSS,1);
  return(ret);


}

//*************************************************************
// send and receive SPI byte  to/from MLX90316 Chip
uint8_t  MLX90316::_spiByte(uint8_t  tx) {
  byte rxb=0;

  for (int ix = 0; ix < 8; ix++){  // receive/transmit 8 SPI bits

    digitalWrite(_pinSCK,1);    // clocksignal positive slope
    rxb = ( rxb << 1);           // shift received byte left

    pinMode(_pinMOSI,INPUT);    // switch MOSI pin to input
    digitalWrite(_pinMOSI,1);   // turn port internal pullup resistor on

    if (digitalRead(_pinMOSI) ==1) rxb = rxb | 1; // read respose bit from sensor
    digitalWrite(_pinMOSI,0);   // turn port internal pullup resistor off
    pinMode(_pinMOSI,OUTPUT);   // switch MOSI pin to output

    // write SPI transmit bit to sensor
    if ((tx & 1) != 0) digitalWrite(_pinMOSI,1);
    else digitalWrite(_pinMOSI,0);
    tx= ( tx >> 1);

    digitalWrite(_pinSCK,0);   // clocksignal negative slope
    digitalWrite(_pinMOSI,0);  // set MOSI databit 0
  }
 
  return(rxb);
 

}

 

Share this post


Link to post
Share on other sites
Gina    8,763

YEP!!  Returns angle if angle data is valid else -1.  If the magnetic field is too strong or too weak it returns -2 or -3 from these lines of code :-

    if ( rr & (1 << 4)) ret=-2;  // signal to strong
    if ( rr & (1 << 5)) ret=-3;  // signal to weak

SO a return of -1 indicates invalid angle data (from whatever cause other than magnetic field strength).

Edited by Gina

Share this post


Link to post
Share on other sites
Gina    8,763

The MLX90316.cpp reads the data from the device and parses the data to determine if the angle is valid or if there's an error in conjunction with information from the datasheet.  If the LSB 2 bits are 0 1 the data represents a valid angle otherwise there is an error condition as shown by the table in the second screenshot.

5a36b66cec3c0_SensorData04.thumb.png.46e889d69b9ca454d84fd17a34985beb.png5a36b66be60d4_SensorData05.thumb.png.4c1e9d4264c3e03b6ddca34ea7b752e2.png

Share this post


Link to post
Share on other sites
Gina    8,763

I could edit the code in MLX90316.cpp to take the errors and convert to a negative number to show what's wrong.  I don't think it's anything to do with the MISO and FET etc. though it wouldn't take much to add the components birds-nest fashion :D  I guess that would eliminate that possibility.

Edited by Gina

Share this post


Link to post
Share on other sites
JamesF    7,842

I think that would make a lot of sense.  It seems obtuse to only return a meaningful error value for two of the possible error conditions.

James

  • Like 1

Share this post


Link to post
Share on other sites
Gina    8,763

Yes and that's probably the easiest thing to do.  I only have to take this code section and produce a general negative number from the error code.  I can interpret it later.

  if ((rr & 3) == 2) {
   
    if ( rr & (1 << 4)) ret=-2;  // signal to strong
    if ( rr & (1 << 5)) ret=-3;  // signal to weak
}

I propose this code :-

  if ((rr & 3) == 2) { 
    ret=-rr;  // error code as negative number
}

 

Share this post


Link to post
Share on other sites
MarkyD    196

-1 means there is no SPI signal.  -2 is the signal is to strong.  -3 is the signal is to weak

Share this post


Link to post
Share on other sites
Gina    8,763

That still produced -1 so the data is neither a valid angle nor a valid error code.  Changing the code to return -rr if not a valid angle produced a code of 1 meaning rr was -1 ie. all 1s - a data transfer error.  The Arduino is not reading the MLX90316 output register or the register is stuck on all ones.

Next stage then is to add the FET and resistor to see if that cures it but if not I shall have to assume the chip is damaged.  I did buy a spare.

Edited by Gina

Share this post


Link to post
Share on other sites
Gina    8,763

I think I have MOSI & MISO mixed up!  I'll re-read the datasheet.

Share this post


Link to post
Share on other sites
Stub Mandrel    5,850
4 hours ago, Gina said:

Read carefully through the MLX90316 datasheet and can see no mention of error output.  But generally where the device gives a positive number to show a measurement an output of -1 indicates an error and therefore I assume that I am getting an error from the sensor chip.  I have Googled till I'm blue in the face for any information about MXL90316 errors.  The datasheet says an error will occur if the magnetic field is to weak or too strong.  So this is one possibility.

Unless it's sending and endless stream of 0xFF because something is stuck high?

 

Share this post


Link to post
Share on other sites
Stub Mandrel    5,850

Just seen the next page of comment heading towards a similar conclusion...

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×