diff --git a/.vscode/settings.json b/.vscode/settings.json index 4bac2e4..798b888 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,8 @@ "tim.h": "c", "compare": "c", "type_traits": "c", - "gpio.h": "c" + "gpio.h": "c", + "stdint.h": "c", + "time.h": "c" } } \ No newline at end of file diff --git a/Core/App/app.c b/Core/App/app.c index 49fdc9e..fa3b4bd 100644 --- a/Core/App/app.c +++ b/Core/App/app.c @@ -3,6 +3,7 @@ #include "ui.h" #include "keypad.h" #include "global.h" +#include "time.h" void app_init() { // 程序开始时执行一次 diff --git a/Core/Inc/time.h b/Core/Inc/time.h new file mode 100644 index 0000000..4833b6f --- /dev/null +++ b/Core/Inc/time.h @@ -0,0 +1,6 @@ +#pragma once +#include + +int32_t ticks_ms(); +int32_t ticks_add(int32_t t1, int32_t delta); +int32_t ticks_diff(int32_t t1, int32_t t2); diff --git a/Core/Src/time.c b/Core/Src/time.c new file mode 100644 index 0000000..acf1a44 --- /dev/null +++ b/Core/Src/time.c @@ -0,0 +1,16 @@ +#include +#include "time.h" +#include "main.h" + +int32_t ticks_ms() { + return HAL_GetTick() & INT32_MAX; +} + +int32_t ticks_add(int32_t t1, int32_t delta) { + return (t1 + delta) & INT32_MAX; +} + +int32_t ticks_diff(int32_t t1, int32_t t2) { + int32_t half = (INT32_MAX / 2) + 1; + return ((t1 - t2 + half) & INT32_MAX) - half; +} diff --git a/Makefile b/Makefile index 8b63699..ee8735f 100644 --- a/Makefile +++ b/Makefile @@ -68,6 +68,7 @@ Core/Src/gpio.c \ Core/Src/usart.c \ Core/Src/spi.c \ Core/Src/system_stm32g0xx.c \ +Core/Src/time.c \ Core/App/app.c \ Core/App/global.c \ Core/Hardware/RotaryCoder/RotaryCoder.c \