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

Using timer as counter

Hi,

I am using sdk12.2 and customised nrf52 board.

I want to interface ultrasonic sensor with nrf52. In this I am enabling trigger pin as high and after sometime my echo pin goes low. I want to start timer when I am enabling trigger pin and stop timer when echo pin goes low. I want to measure the time in Seconds between high to low transition.

I am using timer library.

My code chunk is as below. How to use timer as counter to implement this?

I am aware about using timer that terminates after few seconds.

 nrf_gpio_pin_clear(pinTrig);
  nrf_delay_us(20);
  nrf_gpio_pin_set(pinTrig);
  nrf_delay_us(12);
  nrf_gpio_pin_clear(pinTrig);
  nrf_delay_us(20);

  // listen for echo and time it
  //       ____________
  // _____|            |___
  
  // wait till Echo pin goes high
  while(!nrf_gpio_pin_read(pinEcho));
  // reset counter
  tCount = 0;
  // wait till Echo pin goes low
  while(nrf_gpio_pin_read(pinEcho));
 }

Parents Reply Children
  • See the PPI Example

    I'd start by playing with the TIMER and GPIOTE via HAL. Set up some GPIOTE tasks like pin toggles that are triggered by a TIMER's compare tasks. Use a digital analyzer to see how the GPIOs behave. This will teach you the basics of the PPI system (EVENT --> TASK) and how to set up the TIMER and GPIOTE. 

    The PPI system uses the register address of an EVENT and couples it to the register address of a TASK. All drivers or HALs should have a function for getting the address of an EVENT or TASK. Those addresses are also given in the Registers chapter of a peripheral's technical specification. 

Related