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

how to count pulse with nrf51822 : revisited

I am trying to implement the pulse counting mentioned in a previous post of the same name, but the code is eluding me. I am using the heart rate service example with new code shown below. Tomorrow I will instrument the code more, but I could use some guidance as to if I am on the right track.

/** @brief Function for initializing the Timer 2 peripheral as a Counter. */
static void timer2_init(void)	 
{
    NRF_TIMER2->MODE  = TIMER_MODE_MODE_Counter;
    NRF_TIMER2->TASKS_CLEAR = 1;		// Sets Count to 0.
    NRF_TIMER2->BITMODE   = TIMER_BITMODE_BITMODE_16Bit << TIMER_BITMODE_BITMODE_Pos;
    NRF_TIMER2->TASKS_START = 1;		// Start Counting        	  
}

/** @brief Function for initializing the GPIO Tasks/Events peripheral. */
static void gpiote_init(void)
{   
    APP_GPIOTE_INIT(APP_GPIOTE_MAX_USERS);	 
    nrf_gpio_cfg_input(PWM_INPUT_PIN_NUMBER, NRF_GPIO_PIN_PULLUP);	// Configure PWM_INPUT_PIN_NUMBER as an input.
    nrf_gpio_cfg_sense_input(PWM_INPUT_PIN_NUMBER, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);	 
}

/** @brief Function for initializing the Programmable Peripheral Interconnect peripheral. */
static void ppi_init(void)
{
    // Configure PPI channel 0 to increment Counter on every time PWM input pin changes
    NRF_PPI->CH[1].EEP = (uint32_t)&NRF_GPIOTE->EVENTS_PORT; 
    NRF_PPI->CH[1].TEP = (uint32_t)&NRF_TIMER2->TASKS_COUNT;

    // Enable PPI channels 0-2.
    NRF_PPI->CHEN = (PPI_CHEN_CH0_Enabled << PPI_CHEN_CH0_Pos)
                  | (PPI_CHEN_CH1_Enabled << PPI_CHEN_CH1_Pos)
                  | (PPI_CHEN_CH2_Enabled << PPI_CHEN_CH2_Pos);
}

/*****************************************************************************

  • Main Function *****************************************************************************/

    /**@brief Function for the application main entry. */ int main(void) { uint32_t err_code;

      timers_init();
      gpiote_init();
      buttons_init();
      timer2_init();
      ppi_init();	
      ble_stack_init();
      device_manager_init();
    
      // Initialize Bluetooth Stack parameters.
      gap_params_init();
      advertising_init();
      services_init();
      conn_params_init();
    
      // Start advertising.
      advertising_start();
    
      // Enter main loop.
      for (;;)
      {
          // Switch to a low power state until an event is available for the application
          err_code = sd_app_evt_wait();
          APP_ERROR_CHECK(err_code);
          // Read Counter value
          NRF_TIMER2->TASKS_CAPTURE[0];
          m_cur_heart_rate = NRF_TIMER2->CC[0];	
      }
    

    }

Related