diff --git a/src/main.c b/src/main.c index cb69edfb2fa8b135ef191e4a361c96f96db81a9e..6f332b2602afb9e6df210a74b36b944f9f9241b7 100644 --- a/src/main.c +++ b/src/main.c @@ -120,21 +120,54 @@ void set_progress(double percentage) void callback(uint gpio, uint32_t events) { - int b = 4; - int c = 9; - volatile uint another_slice_num = pwm_gpio_to_slice_num(27); - if (events & GPIO_IRQ_EDGE_RISE) // reset pwm counter + uint32_t event3 = events; + /* volatile uint another_slice_num = pwm_gpio_to_slice_num(27); + + if (events & GPIO_IRQ_LEVEL_HIGH) { - pwm_set_counter(another_slice_num, 0); + volatile uint16_t test = pwm_get_counter(another_slice_num); } - else if (events & GPIO_IRQ_EDGE_FALL) + + else if (events & GPIO_IRQ_LEVEL_LOW) { - volatile uint16_t test = pwm_get_counter(another_slice_num); - set_progress(100); + pwm_set_counter(another_slice_num, 0); } + */ + // volatile uint16_t test = pwm_get_counter(another_slice_num); + // volatile float distance = (test * 314) / (20000); + // if (distance > 20) + // { + // distance = 20; + // } + + // distance = 20 - distance; + + // volatile int percentage = (distance / 10) * 100; + + // set_progress(percentage); + // pwm_set_counter(another_slice_num, 0); } +void init_pwm() +{ + gpio_set_function(28, GPIO_FUNC_PWM); + + volatile uint slice_num = pwm_gpio_to_slice_num(28); + pwm_set_clkdiv(slice_num, 133); + pwm_set_wrap(slice_num, 0xFFFF); + pwm_set_chan_level(slice_num, PWM_CHAN_A, 10); + pwm_set_enabled(slice_num, true); +} +void pwm_echo_init() +{ + volatile uint slice_num = pwm_gpio_to_slice_num(27); + + pwm_set_clkdiv(slice_num, 1); + pwm_set_wrap(slice_num, 0xFFFF); + pwm_set_clkdiv_mode(slice_num, PWM_DIV_FREE_RUNNING); + pwm_set_enabled(slice_num, true); +} int a = 0; int main() @@ -163,23 +196,13 @@ int main() i2c_write_blocking(i2c_default, 0x70, empty, 17, false); - gpio_set_function(28, GPIO_FUNC_PWM); - - volatile uint slice_num = pwm_gpio_to_slice_num(28); - pwm_set_clkdiv(slice_num, 133); - pwm_set_wrap(slice_num, 0xFFFF); - pwm_set_chan_level(slice_num, PWM_CHAN_A, 10); - pwm_set_enabled(slice_num, true); - - volatile uint another_slice_num = pwm_gpio_to_slice_num(27); + init_pwm(); + pwm_echo_init(); - pwm_set_clkdiv(another_slice_num, 1); - pwm_set_wrap(another_slice_num, 0xFFFF); - pwm_set_clkdiv_mode(another_slice_num, PWM_DIV_FREE_RUNNING); - pwm_set_enabled(another_slice_num, true); - set_progress(0); + gpio_set_irq_enabled_with_callback(27, GPIO_IRQ_LEVEL_LOW, true, &callback); - gpio_set_irq_enabled_with_callback(27, GPIO_IRQ_LEVEL_LOW | GPIO_IRQ_LEVEL_HIGH | GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, &callback); + while (1) + ; /* timer_hw->dbgpause = 0; gpio_init(27);