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