You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Here you go:

    var ll = require("NRF52LL");
    // source of events - compare D31 against vref/2
    var comp = ll.lpcomp({pin:D31,vref:8});
    // A place to recieve events - a counter
    var ctr = ll.timer(3,{type:"counter"});
    // Set up and enable PPI
    ll.ppiEnable(0, comp.eCross, ctr.tCount);
    /* This function triggers a Task by hand to 'capture' the counter's value. It can then clear it and read back the relevant `cc` register */
    function getCtr() {
      poke32(ctr.tCapture[0],1);
      poke32(ctr.tClear,1); // reset it
      return peek32(ctr.cc[0]);
    }
    
    // Every 10 seconds, wake and print out the number of crosses
    setInterval(function() {
      print(getCtr());
    }, 10000);
    

    It configures hardware to count crosses using the comparator:

    • You have to use pins with analog inputs - check the pinout page for the board you're using for info
    • The voltage reference is in 16ths - so vref:8 is a half of VCC. You can also use another analog pin as a reference - proper docs in http://www.espruino.com/NRF52LL
    • They are 32 bit counters, so you've got no issue with the counter overflowing
    • It seems pretty low power - drawing around 100uA

    Hope that helps!

About

Avatar for Gordon @Gordon started