diff --git a/src/main.c b/src/main.c index 046ef8babe7c794e15154896d851538016bb9d49..a8066127d4d74d3dd29276461e7669168afb81cb 100644 --- a/src/main.c +++ b/src/main.c @@ -2,7 +2,8 @@ #include <hardware/i2c.h> #include <stdio.h> #include <unistd.h> -#include "hardware/pwm.h" +#include <hardware/pwm.h> + const uint8_t init[4] = {0x21, 0xa0, 0xe1, 0x81}; const uint8_t zeile[8] = {0x00, 0x02, 0x04, 0x06, 0x08, 0x10, 0x12, 0x14}; const uint8_t fill[17] = {0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; @@ -105,26 +106,24 @@ const uint8_t better_matrix[65][17] = { {0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0b11111111, 0x00}, }; -void set_progress(int percentage) +void set_progress(double percentage) { int numerator = (percentage * 32) / 100; for (int i = 0; i <= numerator; i++) { i2c_write_blocking(i2c_default, 0x70, better_matrix[i], 17, false); - // sleep - // for (int j = 0; j < 100000; j++) - // { - // volatile int i = 0; - // } } } -int a = 0; void callback() { - a = 1; + int b = 4; + int c = 9; } + +int a = 0; + int main() { // clock enable: 100000 baud (bit/s) @@ -151,33 +150,42 @@ int main() i2c_write_blocking(i2c_default, 0x70, empty, 17, false); - set_progress(200); - set_progress(30); - gpio_set_function(28, GPIO_FUNC_PWM); uint slice_num = pwm_gpio_to_slice_num(28); - - // 133 Mhz / 133 = 1 Mhz => 1 cycle = 1 us - // 10 mikrosekunden pwm_set_clkdiv(slice_num, 133); - pwm_set_wrap(slice_num, 65000); + pwm_set_wrap(slice_num, 0xFFFF); pwm_set_chan_level(slice_num, PWM_CHAN_A, 10); pwm_set_enabled(slice_num, true); - uint test = 500; - set_progress(0); - uint another_slice_num = pwm_gpio_to_slice_num(27); - // 65000 * 133 * 10^6 10 meters - // 65000 max cycles + pwm_set_clkdiv(another_slice_num, 133); - pwm_set_wrap(another_slice_num, 65000); - pwm_set_clkdiv_mode(another_slice_num, PWM_DIV_B_HIGH); + pwm_set_wrap(another_slice_num, 0xFFFF); + pwm_set_clkdiv_mode(another_slice_num, PWM_DIV_FREE_RUNNING); pwm_set_enabled(slice_num, true); - int b = 7; - gpio_set_irq_enabled_with_callback(27, GPIO_IRQ_EDGE_FALL, true, callback); - set_progress(100); + timer_hw->dbgpause = 0; + gpio_init(27); + gpio_init(28); + + gpio_set_dir(28, GPIO_OUT); + while (1) + { + gpio_put(28, true); + sleep_us(10); + gpio_put(28, false); + while (!gpio_get(27)) + ; + uint32_t start = time_us_32(); + while (gpio_get(27)) + ; + uint32_t end = time_us_32(); + + uint32_t duration = end - start; + volatile float distance = duration * 314 / 2000; + } + + a = 3; return 0; } \ No newline at end of file