sensor_sr04: fix shadow counter value issue
This commit is contained in:
parent
25fcf8fe0b
commit
b82520fa9b
@ -95,6 +95,7 @@ void sensor_sr04_trigger(void)
|
||||
timer_clear_flag(TIM(SENSOR_SR04_TIMER), TIM_SR_UIF); // clear update flag
|
||||
timer_clear_flag(TIM(SENSOR_SR04_TIMER), TIM_SR_CCIF(SENSOR_SR04_CHANNEL)); // clear input compare flag
|
||||
timer_set_counter(TIM(SENSOR_SR04_TIMER), 0); // reset counter
|
||||
timer_generate_event(TIM(SENSOR_SR04_TIMER), TIM_EGR_UG); // clear shadow register
|
||||
timer_ic_set_polarity(TIM(SENSOR_SR04_TIMER), TIM_IC(SENSOR_SR04_CHANNEL), TIM_IC_RISING); // capture on incoming echo (rising edge)
|
||||
sensor_sr04_echo = false; // echo has not been received yet
|
||||
|
||||
@ -105,30 +106,6 @@ void sensor_sr04_trigger(void)
|
||||
|
||||
// start timer to measure the echo (the rest is done in the ISR)
|
||||
timer_enable_counter(TIM(SENSOR_SR04_TIMER)); // start timer
|
||||
|
||||
/*
|
||||
while (!gpio_get(GPIO_PORT(SENSOR_SR04_ECHO), GPIO_PIN(SENSOR_SR04_ECHO)) && !timer_get_flag(TIM(SENSOR_SR04_TIMER), TIM_SR_UIF)); // wait for echo
|
||||
if (timer_get_flag(TIM(SENSOR_SR04_TIMER), TIM_SR_UIF)) { // timeout reached
|
||||
// timer is already disabled
|
||||
return 0; // did not get any echo
|
||||
}
|
||||
|
||||
// the echo should have been received, measure it
|
||||
timer_set_counter(TIM(SENSOR_SR04_TIMER), 0); // reset counter to measure echo
|
||||
while (gpio_get(GPIO_PORT(SENSOR_SR04_ECHO), GPIO_PIN(SENSOR_SR04_ECHO)) && !timer_get_flag(TIM(SENSOR_SR04_TIMER), TIM_SR_UIF)); // wait for end of echo
|
||||
if (timer_get_flag(TIM(SENSOR_SR04_TIMER), TIM_SR_UIF)) { // timeout reached
|
||||
// timer is already disabled
|
||||
return UINT16_MAX; // object out of range
|
||||
}
|
||||
|
||||
// convert to distance
|
||||
const uint16_t count = timer_get_counter(TIM(SENSOR_SR04_TIMER)); // get measured time
|
||||
timer_disable_counter(TIM(SENSOR_SR04_TIMER)); // stop timer (just to be clean)
|
||||
|
||||
// calculate distance in mm
|
||||
return (count * (TIM_PSC(TIM(SENSOR_SR04_TIMER)) + 1) * (343 / 2)) / (rcc_ahb_frequency / 1000);
|
||||
// TODO use input capture, and interrupts
|
||||
*/
|
||||
}
|
||||
|
||||
/** interrupt service routine called for timer */
|
||||
@ -144,8 +121,11 @@ void TIM_ISR(SENSOR_SR04_TIMER)(void)
|
||||
} else if (timer_get_flag(TIM(SENSOR_SR04_TIMER), TIM_SR_CCIF(SENSOR_SR04_CHANNEL))) { // edge detected on input capture
|
||||
timer_clear_flag(TIM(SENSOR_SR04_TIMER), TIM_SR_CCIF(SENSOR_SR04_CHANNEL)); // clear input compare flag
|
||||
if (!sensor_sr04_echo) { // echo is incoming
|
||||
timer_disable_counter(TIM(SENSOR_SR04_TIMER)); // disable while reconfiguring
|
||||
timer_set_counter(TIM(SENSOR_SR04_TIMER), 0); // reset timer counter
|
||||
timer_generate_event(TIM(SENSOR_SR04_TIMER), TIM_EGR_UG); // clear shadow register
|
||||
timer_ic_set_polarity(TIM(SENSOR_SR04_TIMER), TIM_IC(SENSOR_SR04_CHANNEL), TIM_IC_FALLING); // capture on end of echo (falling edge)
|
||||
timer_enable_counter(TIM(SENSOR_SR04_TIMER)); // re-enable
|
||||
sensor_sr04_echo = true; // remember echo is incoming
|
||||
} else { // end of echo
|
||||
timer_disable_counter(TIM(SENSOR_SR04_TIMER)); // disable counter now that measurement is complete
|
||||
|
Loading…
Reference in New Issue
Block a user