ADXL362 with Puck.js

Posted on
  • I just started working on puck.js and I'm trying to get data from my ADXL362 connected to it, but my code always returns me zeros or 255, here it is:

    var spi = new SPI();
    
    spi.setup({mosi:D29, miso:D30, sck:D28});
    
    var x;
    var y;
    var z;
    
    function SPIreadOneRegister(regAddress, spi){
        var d = spi.send([0x0B,regAddress,0x00], D31);
        return d[1];
    }
    
    function SPIwriteOneRegister(regAddress, regValue, spi){
    	spi.write([0x0A, regAddress, regValue]);
    }
    
    function beginMeasure(spi) {
    	var temp = SPIreadOneRegister(0x2D, spi);
    
    	console.log(  "Reg 2D before = " + temp);
    
    	var tempWrite = temp | 0x02;
    	SPIwriteOneRegister(0x2D, tempWrite, spi);
    	setTimeout(function() {
          temp = SPIreadOneRegister(0x2D, spi);
          console.log(  "Reg 2D after = " + temp);
        }, 10);
    }
    
    function readXData(spi){
        var XDATA = SPIreadOneRegister(0x08, spi);
        console.log("XDATA = " + XDATA);
        return XDATA;
    }
    
    function readYData(spi){
        var YDATA = SPIreadOneRegister(0x09, spi);
        console.log("\tYDATA = " + YDATA);
        return YDATA;
    }
    
    function readZData(spi){
        var ZDATA = SPIreadOneRegister(0x0A, spi);
        console.log("\tZDATA = " + ZDATA);
        return ZDATA;
    }
    
    SPIwriteOneRegister(0x1F, 0x52, spi);
    beginMeasure(spi);
    
    
    x = readXData(spi);
    y = readYData(spi);
    z = readZData(spi);
    console.log("*");
    

    If you have any solution please tell me.

  • You could try:

    function SPIreadOneRegister(regAddress, spi){
        var d = spi.send([0x0B,regAddress,0x00], D31);
        return d[2];
    }
    

    You're sending 3 bytes so you'll get 3 bytes back - but it's the last one (index 2) that'll contain the data you need.

  • It still doesn't work

  • Try changing:

    function SPIwriteOneRegister(regAddress, regValue, spi){
        spi.write([0x0A, regAddress, regValue]);
    }
    

    to

    function SPIwriteOneRegister(regAddress, regValue, spi){
        spi.write([0x0A, regAddress, regValue], D31);
    }
    

    ... you weren't specifying a 'CS' pin, so the writes likely wouldn't have been working.

    Also, if you change:

    function SPIreadOneRegister(regAddress, spi){
        var d = spi.send([0x0B,regAddress,0x00], D31);
        return d[2];
    }
    

    to

    function SPIreadOneRegister(regAddress, spi){
        var d = spi.send([0x0B,regAddress,0x00], D31);
        print(d);
        return d[2];
    }
    

    Then what sort of things are printed to the console?

  • It worked writing my cs pin in write(), thank you very much!!

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

ADXL362 with Puck.js

Posted by Avatar for user89762 @user89762

Actions