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

Timer 0 interface program

Hi,

 i am trying to interface ultrasonic sensor with nrf9160, but i dont get any examples regarding ultrasonic. so i tried the interrupt method to measure the time of gpio high state, i used

k_cycle_get_32(); function to get starting timing and also i used the same function while the high is getting low. then i send the count to SYS_CLOCK_HW_CYCLES_TO_NS() function, but it doesnt give the exact value. kindly help me to sort this out. here i attached the interrupt callback function.

static void UltraSonicData(struct device *port, struct gpio_callback *cb, u32_t pins)
{
  static uint8_t DataFlag;
  float Distance;
  if(DataFlag == 0)
  {
      Time =  k_cycle_get_32();
      printk("UltraSonic high Started\n starting time: %d \n ",Time);
     int err;
   gpio_pin_configure(gpio_dev, dpin1,
			   (GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | 
			    GPIO_INT_ACTIVE_LOW));
                            gpio_init_callback(&ultrasonic_cb, UltraSonicData, BIT(dpin1));
                            err = gpio_add_callback(gpio_dev, &ultrasonic_cb);
                            
        if (err) 
        {
          printk("UltraSonicConfig Cannot add callbacks");
	}
        err = gpio_pin_enable_callback(gpio_dev, dpin1);
        if (err) 
        {
          printk("UltraSonicConfig Cannot enable callbacks");
	}
    DataFlag = 1;
  }
  else
  {
       Time =  k_cycle_get_32() - Time;
       printf("Time:%d\n ",Time);
       Distance = (((SYS_CLOCK_HW_CYCLES_TO_NS(Time)/10.0)/5882.0));//*0.0343)/2.0); 
       printf("Distance:%f\n ",Distance);
   int err;
   gpio_pin_configure(gpio_dev, dpin1,
			   (GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | GPIO_PUD_PULL_DOWN |
			    GPIO_INT_ACTIVE_HIGH));
                            gpio_init_callback(&ultrasonic_cb, UltraSonicData, BIT(dpin1));
                            err = gpio_add_callback(gpio_dev, &ultrasonic_cb);
                            
        if (err) 
        {
          printk("UltraSonicConfig Cannot add callbacks");
	}

     DataFlag = 0;
  }
}

thanks in advance

hmdra 

Parents
  • Hi,

     

    What is the required accuracy for the signal? Right now you're using the RTC (the kernel timer), which is 32.768 kHz base frequency, which might not be accurate enough for your application.

    In order to do this as accurate as possible, it would require:

    * NRF_TIMER

    * 2 x GPIOTE IN (One on rising, one on falling edge)

    * Two PPI channels (one to start, one to stop TIMER)

     

    Connect the signal you want to time to two GPIOs (configured as IN), as "GPIOTE IN Toggle" will not be able to differentiate between the start and stop of the signal here.

    This method will require either bare-metal or the usage of nrfx drivers directly.

     

    Kind regards,

    Håkon

Reply
  • Hi,

     

    What is the required accuracy for the signal? Right now you're using the RTC (the kernel timer), which is 32.768 kHz base frequency, which might not be accurate enough for your application.

    In order to do this as accurate as possible, it would require:

    * NRF_TIMER

    * 2 x GPIOTE IN (One on rising, one on falling edge)

    * Two PPI channels (one to start, one to stop TIMER)

     

    Connect the signal you want to time to two GPIOs (configured as IN), as "GPIOTE IN Toggle" will not be able to differentiate between the start and stop of the signal here.

    This method will require either bare-metal or the usage of nrfx drivers directly.

     

    Kind regards,

    Håkon

Children
Related