Преглед на файлове

提交部分RTC代码。

xuxinyi преди 2 месеца
родител
ревизия
7975d89e93
променени са 3 файла, в които са добавени 70 реда и са изтрити 59 реда
  1. 4 3
      Core/Inc/RTC_SLEEP.h
  2. 56 55
      Core/Src/stm32f1xx_it.c
  3. 10 1
      问题记录/问题笔记.md

+ 4 - 3
Core/Inc/RTC_SLEEP.h

@@ -24,9 +24,10 @@ typedef struct {
 } RTC_DateTimeTypeDef;
 
 typedef struct {
-    uint8_t key_Duration;
-    uint8_t key_Count;
-    uint8_t key_CheckActive;// 标志位,表示正在检测中
+    RTC_DateTimeTypeDef startTime;  // 按下时的完整RTC时间
+    uint32_t key_Duration;          // 按压持续时长 (秒),改成32位
+    uint8_t  key_Count;             // 按键次数(可选)
+    uint8_t  key_CheckActive;       // 是否正在检测中
 } RTC_key_struct;
 
 extern RTC_DateTimeTypeDef firstPressTime;

+ 56 - 55
Core/Src/stm32f1xx_it.c

@@ -413,90 +413,91 @@ void RTC_Alarm_IRQHandler(void)
 }
 
 /* USER CODE BEGIN 1 */
+void Print_RTC_DateTime(RTC_DateTimeTypeDef *t)
+{
+  printf("%02u-%02u-%02u %02u:%02u:%02u\r\n",
+         t->year + 2000,  // 如果 year 是两位数
+         t->month,
+         t->date,
+         t->hours,
+         t->minutes,
+         t->seconds);
+}
+/* 把 RTC_DateTimeTypeDef 转成当天秒数 */
+uint32_t RTC_ToSeconds(RTC_DateTimeTypeDef *t)
+{
+  return t->hours * 3600 + t->minutes * 60 + t->seconds;
+}
+
+/* 计算两个时间点的秒差,支持跨天(不跨月/跨年) */
+uint32_t RTC_CalcDuration(RTC_DateTimeTypeDef *start, RTC_DateTimeTypeDef *end)
+{
+  uint32_t startSec = RTC_ToSeconds(start);
+  uint32_t endSec   = RTC_ToSeconds(end);
+
+  if (start->year == end->year && start->month == end->month && start->date == end->date) {
+    // 同一天
+    return (endSec >= startSec) ? (endSec - startSec) : 0;
+  } else {
+    // 跨天
+    uint32_t daysDiff = end->date - start->date;
+    uint32_t duration = (24*3600 - startSec) + endSec;
+    if (daysDiff > 1) {
+      duration += (daysDiff - 1) * 86400;
+    }
+    return duration;
+  }
+}
+
+
 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
 {
   RTC_DateTimeTypeDef now;
-  RTC_GetDateTime(&hrtc, &now);  // 获取当前时间
+  RTC_GetDateTime(&hrtc, &now);  // 获取当前RTC时间到你自定义结构体
 
   // ===== SOS_KEY =====
-  if(GPIO_Pin == SOS_KEY_Pin)
+  if (GPIO_Pin == SOS_KEY_Pin)
   {
-    if(HAL_GPIO_ReadPin(SOS_KEY_GPIO_Port, SOS_KEY_Pin) == GPIO_PIN_SET)
+    if (HAL_GPIO_ReadPin(SOS_KEY_GPIO_Port, SOS_KEY_Pin) == GPIO_PIN_SET)
     {
-      // 松开(上升沿)
+      // 松开
       printf("SOS_KEY 松开\r\n");
-
-      uint16_t key_Duration = 0;
-      if(SOS_key_struct.key_Duration > now.seconds) {
-        key_Duration = now.seconds + 60 - SOS_key_struct.key_Duration;
-      } else {
-        key_Duration = now.seconds - SOS_key_struct.key_Duration;
-      }
-
-      printf("SOS_KEY 按压了:%d 秒\r\n", key_Duration);
-      SOS_key_struct.key_Duration = 0;
+      SOS_key_struct.key_Duration = RTC_CalcDuration(&SOS_key_struct.startTime, &now);
+      printf("SOS_KEY 按压了:%lu 秒\r\n", SOS_key_struct.key_Duration);
+      Print_RTC_DateTime(&now);
       SOS_key_struct.key_CheckActive = 0;
       SOS_key_struct.key_Count = 0;
     }
     else
     {
-      // 按下(下降沿)
+      // 按下
       printf("SOS_KEY 按下\r\n");
-      SOS_key_struct.key_Duration = now.seconds;
+      SOS_key_struct.startTime = now;
+      Print_RTC_DateTime(&now);
+      SOS_key_struct.key_CheckActive = 1;
     }
   }
   // ===== JUGE_KEY =====
-  else if(GPIO_Pin == JUGE_PIN_Pin)
+  else if (GPIO_Pin == JUGE_PIN_Pin)
   {
-    if(HAL_GPIO_ReadPin(JUGE_PIN_GPIO_Port, JUGE_PIN_Pin) == GPIO_PIN_SET)
+    if (HAL_GPIO_ReadPin(JUGE_PIN_GPIO_Port, JUGE_PIN_Pin) == GPIO_PIN_SET)
     {
-      // 松开(上升沿)
+      // 松开
       printf("JUGE_KEY 松开\r\n");
-      // HAL_GPIO_WritePin(POWER_ON_GPIO_Port, POWER_ON_Pin, GPIO_PIN_RESET);
+      JUGE_key_struct.key_Duration = RTC_CalcDuration(&JUGE_key_struct.startTime, &now);
+      printf("JUGE_KEY 按压了:%lu 秒\r\n", JUGE_key_struct.key_Duration);
 
-      uint16_t key_Duration = 0;
-      if(JUGE_key_struct.key_Duration > now.seconds) {
-        key_Duration = now.seconds + 60 - JUGE_key_struct.key_Duration;
-      } else {
-        key_Duration = now.seconds - JUGE_key_struct.key_Duration;
-      }
-
-      printf("SOS_KEY 按压了:%d 秒\r\n", key_Duration);
-      JUGE_key_struct.key_Duration = 0;
       JUGE_key_struct.key_CheckActive = 0;
       JUGE_key_struct.key_Count = 0;
-
     }
     else
     {
-      // 按下(下降沿)
+      // 按下
       printf("JUGE_KEY 按下\r\n");
-      JUGE_key_struct.key_Duration = now.seconds;
+      JUGE_key_struct.startTime = now;
+      JUGE_key_struct.key_CheckActive = 1;
     }
   }
-
-  // if (GPIO_Pin == SOS_KEY_Pin)
-  // {
-  //   if (HAL_GPIO_ReadPin(SOS_KEY_GPIO_Port, SOS_KEY_Pin) == GPIO_PIN_SET)
-  //   {
-  //     printf("SOS_KEY 上升沿\r\n");  // 现在是高电平,说明刚从低到高
-  //   }
-  //   else
-  //   {
-  //     printf("SOS_KEY 下降沿\r\n");  // 现在是低电平,说明刚从高到低
-  //   }
-  // }
-  // else if (GPIO_Pin == JUGE_PIN_Pin)
-  // {
-  //   if (HAL_GPIO_ReadPin(JUGE_PIN_GPIO_Port, JUGE_PIN_Pin) == GPIO_PIN_SET)
-  //   {
-  //     printf("JUGE_KEY 上升沿\r\n");
-  //   }
-  //   else
-  //   {
-  //     printf("JUGE_KEY 下降沿\r\n");
-  //   }
-  // }
 }
 
 /* USER CODE END 1 */

+ 10 - 1
问题记录/问题笔记.md

@@ -233,4 +233,13 @@
    00 00 00 00   00 00 00 00   00 00 00 00   00 00 00 00   │ ················ │
    00 00 00 00   00 00 00 00   00 00 00 00   00 00 00 00   │ ················ │
    00 00 00 00   00 00 00 00   00 00 00 00   00 00 00 00   │ ················ │
-   00 00 00 00   00 00 00 00   00 00 00 00   00 00 00 00
+   00 00 00 00   00 00 00 00   00 00 00 00   00 00 00 00
+   
+
+RTC 计算秒数不准
+
+SOS_KEY 按下
+2037-16-23 18:48:83
+SOS_KEY 松开
+SOS_KEY 按压了:0 秒
+2037-16-23 18:49:02