This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Setup Timer 2 interrupt

I'm trying to setup a 20Hz interrupt for Timer 2:

  #define number_of_ms 50  
  void timer_config(void) {
      NRF_TIMER2->TASKS_STOP = 1;  // Stop timer
      NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;  // taken from Nordic dev zone
      NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
      NRF_TIMER2->PRESCALER = 9;  // 32us resolution
      NRF_TIMER2->TASKS_CLEAR = 1;  // Clear timer
      // With 32 us ticks, we need to multiply by 31.25 to get milliseconds
      NRF_TIMER2->CC[0] = number_of_ms * 31;
      NRF_TIMER2->CC[0] += number_of_ms / 4;
      
      NRF_TIMER2->INTENSET = TIMER_INTENSET_COMPARE0_Enabled
                              << TIMER_INTENSET_COMPARE0_Pos;
      NRF_TIMER2->SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled
                            << TIMER_SHORTS_COMPARE0_CLEAR_Pos) &
                            TIMER_SHORTS_COMPARE0_CLEAR_Msk;
     
      attachInterrupt(TIMER2_IRQn, TIMER2_Interrupt);
      NRF_TIMER2->TASKS_START = 1;  // Start TIMER
    }

However no matter how I changed number_of_ms, the sampling frequency does not seem to change and seem to be 8Hz. I even changed the prescaler to 10!!

 124767540, 2621,  103
 124767540, 2621,  102
 124767540, 2621,  100
 124767540, 2621,  102
 124767540, 2623,  100
 124767540, 2618,  102
 124767540, 2621,  103
 124767541, 2622,   98
 124767541, 2628,   99
 124767541, 2619,   99
 124767541, 2620,  101
 124767541, 2622,  103
 124767541, 2619,  104
 124767541, 2621,   99
 124767541, 2623,   97
 124767542, 2622,   98
 124767542, 2625,   98
 124767542, 2620,  100
 124767542, 2620,   99
 124767542, 2621,  101
 124767542, 2619,  102
 124767542, 2620,  100

If I used millisecond resolution time, then the interrupt handler is fired up every 114ms, which is 8.77Hz.

163063, 2860,   82
163177, 2863,   82
163291, 2863,   84
163459, 2865,   83
163573, 2862,   81
163687, 2858,   82
163854, 2860,   82
163968, 2857,   82
164082, 2859,   81
164250, 2860,   81
164364, 2864,   82
164478, 2861,   82
164646, 2862,   82
164763, 2863,   82
164877, 2860,   82
165042, 2864,   82
165156, 2858,   83
165270, 2861,   82
165438, 2862,   81
165552, 2860,   82
165666, 2862,   83

How could this problem be solved?

UPDATE:

This is the code that write to SD card:

void loop() {
  // Reading data from sensors to a buffer
  if (readSensorFlag) {
    readSensorFlag = false;
    heartBeatReadFlag = true;
    
    if (bufferCounter < BUFLEN - SAMPLESIZEINCHAR + 1) {
      epoch = now();
      proximity = vcnl.readProximity();
      ambientLight = vcnl.readAmbient();

      // format to be fixed length as opposed to variable length
      // sacrifice overhead in written data with simpler and more robust code
      // since we know exactly how much to increment after each write
      int written = snprintf(&buffer[inBuf][bufferCounter], SAMPLESIZEINCHAR,
            "%10lu,%5d,%5d\n",
            epoch, proximity, ambientLight);
  
      if (written > 0) {
        bufferCounter += written;
      }
    } else {
      // only switch buffer if out buffer is written to SD card
      if (!outBufHasData) {
        inBuf = inBuf? 0:1;    
        bufferCounter = 0;
        outBufHasData = true;
      }
    }
  }
Parents Reply Children
No Data
Related