diff --git a/projects/01bsp_us_ranging/01bsp_us_ranging.c b/projects/01bsp_us_ranging/01bsp_us_ranging.c index 52248db13..d327f1d41 100644 --- a/projects/01bsp_us_ranging/01bsp_us_ranging.c +++ b/projects/01bsp_us_ranging/01bsp_us_ranging.c @@ -4,6 +4,7 @@ * * @brief This is a short example of how to do Ultrasound ranging with HC-SR04 sensor on the DotBot board. * This code currently works on nRF52840. To use the code on DotBot you need to define different GPIO for US sensor ON and READ pin (trigger and echo) in hc_sr04.c + * The result of the US ranging is passed in the us_callback as the us_reading parameter * * @copyright Inria, 2022 * @@ -16,7 +17,11 @@ #include "hc_sr04.h" -//=========================== defines ========================================= +//=========================== defines =========================================== + +// Defines for setting up the trigger pulse width and frequency +#define PULSE_DURATION_MS 0.01 +#define PULSE_OFFSET_MS 200 //=========================== prototypes ========================================= @@ -25,10 +30,12 @@ void timer_callback(void); // callback for timer0 after pulse_of //=========================== variables ========================================= +typedef struct { + NRF_TIMER_Type *timer0; // Pointer to the TIMER structure used for triggering US sensor + NRF_TIMER_Type *timer1; // Pointer to the TIMER structure used for reading the range on the US sensor +} app_vars_t; -NRF_TIMER_Type *timer0; // variable for passing the Timer for sensor trigger -NRF_TIMER_Type *timer1; // variable for passing the Timer for sensor echo measurement - +static app_vars_t app_vars; //=========================== main ========================================= @@ -36,30 +43,29 @@ NRF_TIMER_Type *timer1; // variable for passing the Timer for * @brief The program starts executing here. */ int main(void) { - - timer0 = NRF_TIMER0; - timer1 = NRF_TIMER1; + app_vars.timer0 = NRF_TIMER0; + app_vars.timer1 = NRF_TIMER1; // uncomment if using this code on DotBot Turn ON the DotBot board regulator - //db_board_init(); + db_board_init(); // initilize the US sensor, set callback and chose the timers - hc_sr04_init(&us_callback, &timer_callback, timer0, timer1); - - // start ranging - hc_sr04_start(); + hc_sr04_init(&us_callback, &timer_callback, app_vars.timer0, app_vars.timer1); + + // initial timer settings for the trigger pulse - we can call this function if we want to change the parameters of the pulse + hc_sr04_on_set_trigger(PULSE_DURATION_MS, PULSE_OFFSET_MS); // start high frequency clock needed for PPI hfclk_init(); + + // start ranging + hc_sr04_start(); - while (1) { - + while (1) { __WFE(); __SEV(); __WFE(); - } - // one last instruction, doesn't do anything, it's just to have a place to put a breakpoint. __NOP(); } @@ -69,9 +75,7 @@ int main(void) { * The code ends up here every time a new measurement is taken. The new measurement is stored in us_reading argument */ void us_callback(uint32_t us_reading) { - __NOP(); - } /** @@ -80,9 +84,7 @@ void us_callback(uint32_t us_reading) { * We could change the pulse_offset_ms here by writting a new value to timer0->CC[0] and timer0->CC[1] */ void timer_callback(void) { - __NOP(); - }