SPI question

Posted on
Page
of 2
Prev
/ 2
  • I tried out the code and it sort of works!

    The only problem with including trtn.temp and trtn.fault in the same variable is the way the chip works. It supplies a 14 bit word that has temperature and fault information. Therefore when the fault flag is raised it still gives a temperature, just the wrong temperature. I included the if else statement in the earlier code to account for this. Maybe someone can suggest a way to accomplish with the trtn.temp and trtn.falut included in the same variable.

    {"temp":2047.75,"fault":1,
    "faultstring":"No probe detected"
    }

    see the example above where no probe is detected yet the temperature is reported back as 2047.75C.

    Ideally I think what is needed for simple example temperature measurement is the correct temperature or the fault condition.

  • Updated on my site. Code is:

    exports.connect = function(spi,cs) {
      return new MAX31855(spi,cs);
    };
    
    function MAX31855(spi,cs) {
      this.spi=spi;
      this.cs=cs;
    }
    
    MAX31855.prototype.getTemp = function () {
      var d = SPI1.send("\0\0\0\0",this.cs); 
      if (d.charCodeAt(1) & 1)  {
        var trtn = {fault: (d.charCodeAt(3) & 7)};
        switch (trtn.fault) {
          case 1:
            trtn.faultstring="No probe detected";
            break;
          case 3:
            trtn.faultstring="Probe shorted to Ground";
            break;
          case 4:
            trtn.faultstring="Probe shorted to VCC";
            break;
        }
        return trtn;
      } else {
        return {temp: (d.charCodeAt(0)<<6 | d.charCodeAt(1)>>2)*0.25, fault: 0};
      }
    };
    
  • SPI1 should be this.spi :-)

  • I tested and this code works. Just need to change one last item that Possmann stated above and its ready to be added to the modules. I spent some time working with I2C over the weekend and can provide a couple of other temperature modules to post soon.

  • Great! It's already done! https://github.com/espruino/EspruinoDocs/blob/master/devices/MAX31855.js

    It'll go live next time I update the website...

  • It is really a good idea, to return an error message string instead of an error code? I guess thois could safe some memory and performance. What do you think?

  • I'm not sure it's such a big worry here. One nice improvement would be to define the error strings in an array: trtn.faultstring=[,"No probe detected",,"Probe shorted to Ground","Probe shorted to VCC"][trtn.fault]

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

SPI question

Posted by Avatar for user7143 @user7143

Actions