浏览代码

实现开关键和SOS键按下时间计算RTC

xuxinyi 2 月之前
父节点
当前提交
62583e3491
共有 1 个文件被更改,包括 23 次插入17 次删除
  1. 23 17
      Core/Src/stm32f1xx_it.c

+ 23 - 17
Core/Src/stm32f1xx_it.c

@@ -415,24 +415,28 @@ void RTC_Alarm_IRQHandler(void)
 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
   RTC_TimeTypeDef currentTime = {0};
   RTC_TimeTypeDef currentTime = {0};
   HAL_RTC_GetTime(&hrtc, &currentTime, RTC_FORMAT_BIN);
   HAL_RTC_GetTime(&hrtc, &currentTime, RTC_FORMAT_BIN);
+
   // ===== SOS_KEY =====
   // ===== 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");
       printf("SOS_KEY 松开\r\n");
 
 
-      printf("Current time: %02d:%02d:%02d\n",
-             currentTime.Hours,
-             currentTime.Minutes,
-             currentTime.Seconds);
+      // 计算按下时间
+      int pressedSeconds = (currentTime.Hours * 3600 + currentTime.Minutes * 60 + currentTime.Seconds)
+                         - (SOS_key_struct.Hours * 3600 + SOS_key_struct.Minutes * 60 + SOS_key_struct.Seconds);
+
+      // 防止跨天导致负值
+      if (pressedSeconds < 0) {
+        pressedSeconds += 24 * 3600;
+      }
+
+      printf("SOS_KEY 按下持续时间: %d 秒\r\n", pressedSeconds);
+
     } else {
     } else {
       // 按下(下降沿)
       // 按下(下降沿)
       printf("SOS_KEY 按下\r\n");
       printf("SOS_KEY 按下\r\n");
-      // SOS_key_struct = currentTime;
-      printf("Current time: %02d:%02d:%02d\n",
-             currentTime.Hours,
-             currentTime.Minutes,
-             currentTime.Seconds);
+      SOS_key_struct = currentTime;
     }
     }
   }
   }
   // ===== JUGE_KEY =====
   // ===== JUGE_KEY =====
@@ -440,17 +444,19 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_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");
       printf("JUGE_KEY 松开\r\n");
-      printf("Current time: %02d:%02d:%02d\n",
-             currentTime.Hours,
-             currentTime.Minutes,
-             currentTime.Seconds);
+
+      int pressedSeconds = (currentTime.Hours * 3600 + currentTime.Minutes * 60 + currentTime.Seconds)
+                         - (JUGE_key_struct.Hours * 3600 + JUGE_key_struct.Minutes * 60 + JUGE_key_struct.Seconds);
+      if (pressedSeconds < 0) {
+        pressedSeconds += 24 * 3600;
+      }
+
+      printf("JUGE_KEY 按下持续时间: %d 秒\r\n", pressedSeconds);
+
     } else {
     } else {
       // 按下(下降沿)
       // 按下(下降沿)
       printf("JUGE_KEY 按下\r\n");
       printf("JUGE_KEY 按下\r\n");
-      printf("Current time: %02d:%02d:%02d\n",
-             currentTime.Hours,
-             currentTime.Minutes,
-             currentTime.Seconds);
+      JUGE_key_struct = currentTime;
     }
     }
   }
   }
 }
 }