From b8fac32aed712a3e0e87cea99b9c6d5ef5e12d2b Mon Sep 17 00:00:00 2001
From: zhangbo <zhangbo@qq.com>
Date: 星期五, 07 三月 2025 18:02:15 +0800
Subject: [PATCH] 现在单个测距完成,然后3s测一次距功能实现

---
 keil/include/drivers/mk_misc.h |   37 +++++++++++++++++++++++++++----------
 1 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/keil/include/drivers/mk_misc.h b/keil/include/drivers/mk_misc.h
index 7dae24d..dff302b 100644
--- a/keil/include/drivers/mk_misc.h
+++ b/keil/include/drivers/mk_misc.h
@@ -40,24 +40,38 @@
 
 #ifndef MK_MISC_H_
 #define MK_MISC_H_
-#include "mk_common.h"
+#include "mk_clock.h"
 
 /**
  * @addtogroup MK8000_MISC
  * @{
  */
 
+// DIV = 1    (max: ~68.8s,     resolution: ~16ns)
+// DIV = 16   (max: ~1101.2s,   resolution: ~256ns)
+// DIV = 256  (max: ~17620.3s   resolution: ~65us)
+#define SYS_TIMER_DIV 1
+
+/** System timer frequency */
+#if SYS_TIMER_DIV == 1
+#define SYS_TIMER_FREQ (62400000 >> (AHB_DIV + APB_DIV))
+#elif SYS_TIMER_DIV == 16
+#define SYS_TIMER_FREQ (3900000 >> (AHB_DIV + APB_DIV))
+#else
+#define SYS_TIMER_FREQ (243750 >> (AHB_DIV + APB_DIV))
+#endif
+
 /** us to system tick converter, us < 68,829us */
-#define __US_TO_TICKS(us) (((us) * ((sys_timer_freq) / 1000)) / 1000)
+#define __US_TO_TICKS(us) (((us) * ((SYS_TIMER_FREQ) / 1000)) / 1000)
 /** system tick to us converter */
-#define __TICKS_TO_US(tick) ((tick) / (((sys_timer_freq) / 1000) / 1000))
+#define __TICKS_TO_US(tick) ((tick) / (((SYS_TIMER_FREQ) / 1000) / 1000))
 /** system tick to us converter, allow long long type multiply operation to promote precision */
-#define __TICKS_TO_US_LL(tick) (uint32_t)(((uint64_t)(tick)*1000) / (sys timer freg / 1000))
+#define __TICKS_TO_US_LL(tick) (uint32_t)(((uint64_t)(tick)*1000) / (SYS_TIMER_FREQ / 1000))
 
 /** ms to system tick converter, ms < 68,829ms */
-#define __MS_TO_TICKS(ms) ((ms) * ((sys_timer_freq) / 1000))
+#define __MS_TO_TICKS(ms) ((ms) * ((SYS_TIMER_FREQ) / 1000))
 /** system tick to ms converter */
-#define __TICKS_TO_MS(tick) ((tick) / ((sys_timer_freq) / 1000))
+#define __TICKS_TO_MS(tick) ((tick) / ((SYS_TIMER_FREQ) / 1000))
 
 /** ms to sleep timer count converter */
 #define __MS_TO_32K_CNT(ms) ((uint32_t)((float)(ms)*32768.0f / 1000.0f))
@@ -157,8 +171,6 @@
 void bor_close(void);
 
 /* SYS Timer */
-/** System timer frequency */
-extern uint32_t sys_timer_freq;
 
 /**
  * @brief Open the system timer, system timer is a free running counter, using the Dual timer 0.
@@ -174,7 +186,11 @@
  * @brief Get the system timer count.
  * @return the system timer count
  */
+#if defined(__ICCARM__)
 uint32_t sys_timer_get(void);
+#else
+uint32_t RAM_FUNC sys_timer_get(void);
+#endif
 
 /**
  * @brief Delay us.
@@ -217,7 +233,7 @@
     uint32_t load;
     uint32_t val;
     uint32_t elapse;
-    void (*callback)(void);
+    void (*callback)(uint32_t elapsed_ticks);
 };
 
 /**
@@ -249,12 +265,13 @@
  * @return elapsed time in ms
  */
 uint32_t sys_tick_elapse_ms(void);
+uint32_t sys_tick_elapse_ticks(void);
 
 /**
  * @brief Register callback function for systick timer.
  * @return callback systick timer callback function
  */
-void sys_tick_callback_set(void (*callback)(void));
+void sys_tick_callback_set(void (*callback)(uint32_t elapsed_ticks));
 
 /**
  * @brief Pause the systick timer.

--
Gitblit v1.9.3