From 1eb61c88fd8c83f272cb70c5d4e778275df0571c Mon Sep 17 00:00:00 2001
From: Pascal Koch <pascal.koch@stud.h-da.de>
Date: Mon, 2 Dec 2024 13:24:36 +0100
Subject: [PATCH] prak3 aufgabe2

---
 src/main.c | 60 +++++++++++++++++++++++++++++++-----------------------
 1 file changed, 34 insertions(+), 26 deletions(-)

diff --git a/src/main.c b/src/main.c
index 046ef8b..a806612 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
-- 
GitLab