I used the Adafruit Bluefruit NRF52840 LE and added some simple code to this existing timer/counter for pulses: https://devzone.nordicsemi.com/f/nordic-q-a/62633/nrf52-timer-as-counter-adafruit-feather It counts the number of LOW pulses within a certain time period. I changed it to count HIGH pulses. it works in Arduino.
But I would like to measure the time between two pulses (the period between the RISING or HIGH peaks of two consecutive pulses). There are only two pulses coming about every 2-3 seconds. The expected duration of the two pulses is between 100-2000 us (micro-seconds).
I suspect that I need to read the respective "time-stamp" of the Timer at the HIGH (RISING) of each of the two pulses and subtract the 1st reading from the 2nd reading. Right now I am using the "micros()" but that only gives me 1us resolution and defeats the purpose of the Timer' nano-second resolution.
Is there a sample code someone can point me to add that "time-stamp" feature in there?
#define FREQ_MEASURE_PIN 6u // was 7u PIN 11 on express 52840
#define PPI_CHANNEL 1u
void setup() {
Serial.begin(115200);
pinMode(LED_RED, OUTPUT);
initCounter();
}
void loop()
{
readCounter();
// digitalToggle(LED_RED);
// delay(1);
}
void readCounter()
{
static int counts;
NRF_TIMER2->TASKS_CAPTURE[0] = 1;
counts = NRF_TIMER2->CC[0];
unsigned long counts1 = micros();
unsigned long counts2 = (counts - counts1);
if (counts >0) {
//Serial.print("pulses: ");
//Serial.println(counts);
Serial.println(counts1);
Serial.println(counts2);
counts1 =0;
counts2 = 0;
counts = 0;
}
NRF_TIMER2->TASKS_CLEAR = 1;
NRF_TIMER2->TASKS_START = 1;
}
void initCounter()
{
NRF_P0->PIN_CNF[FREQ_MEASURE_PIN] = GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos |
GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos |
GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_Pos |
GPIO_PIN_CNF_SENSE_High << GPIO_PIN_CNF_SENSE_Pos;
NRF_PPI->CHEN |= 1 << PPI_CHANNEL;
NRF_PPI->CH[PPI_CHANNEL].EEP = (uint32_t)&NRF_GPIOTE->EVENTS_IN[PPI_CHANNEL];
NRF_PPI->CH[PPI_CHANNEL].TEP = (uint32_t)&NRF_TIMER2->TASKS_COUNT;
NRF_GPIOTE->CONFIG[PPI_CHANNEL] = GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos |
FREQ_MEASURE_PIN << GPIOTE_CONFIG_PSEL_Pos |
GPIOTE_CONFIG_POLARITY_LoToHi << GPIOTE_CONFIG_POLARITY_Pos;
NRF_TIMER2->TASKS_STOP = 1;
NRF_TIMER2->TASKS_CLEAR = 1;
NRF_TIMER2->MODE = TIMER_MODE_MODE_LowPowerCounter << TIMER_MODE_MODE_Pos;
NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_32Bit << TIMER_BITMODE_BITMODE_Pos;
NRF_TIMER2->TASKS_START = 1;
}