Procházet zdrojové kódy

实现SOS按键计算秒数

xuxinyi před 2 měsíci
rodič
revize
6bd366d06e
5 změnil soubory, kde provedl 83 přidání a 22 odebrání
  1. 6 6
      Core/Inc/RTC_SLEEP.h
  2. 10 5
      Core/Src/RTC_SLEEP.c
  3. 10 4
      Core/Src/main.c
  4. 56 6
      Core/Src/stm32f1xx_it.c
  5. 1 1
      lora_bl.ioc

+ 6 - 6
Core/Inc/RTC_SLEEP.h

@@ -24,14 +24,14 @@ typedef struct {
 } RTC_DateTimeTypeDef;
 
 typedef struct {
-    uint8_t SOS_key_Duration;
-    uint8_t SOS_key_Count;
-    uint8_t sosCheckActive;// 标志位,表示正在检测中
-} RTC_SOS_key_struct;
+    uint8_t key_Duration;
+    uint8_t key_Count;
+    uint8_t key_CheckActive;// 标志位,表示正在检测中
+} RTC_key_struct;
 
 extern RTC_DateTimeTypeDef firstPressTime;
-extern RTC_SOS_key_struct SOS_key_struct;
-extern uint8_t RTC_Sleep_Flag;
+extern RTC_key_struct SOS_key_struct;
+extern RTC_key_struct JUGE_key_struct;
 
 void RTC_GetDateTime(RTC_HandleTypeDef *hrtc, RTC_DateTimeTypeDef *datetime);
 

+ 10 - 5
Core/Src/RTC_SLEEP.c

@@ -4,13 +4,18 @@
 
 #include "../Inc/RTC_SLEEP.h"
 
-RTC_SOS_key_struct SOS_key_struct = {
-    .SOS_key_Count = 0,
-    .SOS_key_Duration = 0,
-    .sosCheckActive = FALSE
+RTC_key_struct SOS_key_struct = {
+    .key_Count = 0,
+    .key_Duration = 0,
+    .key_CheckActive = FALSE
+};
+
+RTC_key_struct JUGE_key_struct = {
+    .key_Count = 0,
+    .key_Duration = 0,
+    .key_CheckActive = FALSE
 };
 
-RTC_DateTimeTypeDef firstPressTime;
 
 // 单独的RTC时间和日期设置函数
 void RTC_SetDateTime(RTC_HandleTypeDef *hrtc, uint8_t hours, uint8_t minutes, uint8_t seconds,

+ 10 - 4
Core/Src/main.c

@@ -349,7 +349,7 @@ int main(void)
     /* USER CODE END WHILE */
 
     /* USER CODE BEGIN 3 */
-    printf("deviceInfo.commandFromCloud:%02x\r\n",deviceInfo.commandFromCloud);
+    // printf("deviceInfo.commandFromCloud:%02x\r\n",deviceInfo.commandFromCloud);
     // GPIO_PinState sosState = HAL_GPIO_ReadPin(SOS_KEY_GPIO_Port, SOS_KEY_Pin);
     // GPIO_PinState powerState = HAL_GPIO_ReadPin(JUGE_PIN_GPIO_Port, JUGE_PIN_Pin);
     //
@@ -835,11 +835,17 @@ static void MX_GPIO_Init(void)
   /*Configure GPIO pin Output Level */
   HAL_GPIO_WritePin(PowerLED_GPIO_Port, PowerLED_Pin, GPIO_PIN_RESET);
 
-  /*Configure GPIO pins : SOS_KEY_Pin JUGE_PIN_Pin */
-  GPIO_InitStruct.Pin = SOS_KEY_Pin|JUGE_PIN_Pin;
+  /*Configure GPIO pin : SOS_KEY_Pin */
+  GPIO_InitStruct.Pin = SOS_KEY_Pin;
+  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
+  GPIO_InitStruct.Pull = GPIO_PULLUP;
+  HAL_GPIO_Init(SOS_KEY_GPIO_Port, &GPIO_InitStruct);
+
+  /*Configure GPIO pin : JUGE_PIN_Pin */
+  GPIO_InitStruct.Pin = JUGE_PIN_Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
   GPIO_InitStruct.Pull = GPIO_PULLUP;
-  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+  HAL_GPIO_Init(JUGE_PIN_GPIO_Port, &GPIO_InitStruct);
 
   /*Configure GPIO pin : POWER_ON_Pin */
   GPIO_InitStruct.Pin = POWER_ON_Pin;

+ 56 - 6
Core/Src/stm32f1xx_it.c

@@ -416,20 +416,70 @@ void RTC_Alarm_IRQHandler(void)
 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
 {
   RTC_DateTimeTypeDef now;
-  RTC_GetDateTime(&hrtc, &now);
+  RTC_GetDateTime(&hrtc, &now);  // 获取当前时间
 
+  // ===== SOS_KEY =====
   if(GPIO_Pin == SOS_KEY_Pin)
   {
-    printf("SOS_KEY 按下\r\n");
+    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;
+      }
 
-    // 处理 SOS
+      printf("SOS_KEY 按压了:%d 秒\r\n", key_Duration);
+    }
+    else
+    {
+      // 按下(下降沿)
+      printf("SOS_KEY 按下\r\n");
+      SOS_key_struct.key_Duration = now.seconds;
+    }
   }
+  // ===== JUGE_KEY =====
   else if(GPIO_Pin == JUGE_PIN_Pin)
   {
-    printf("JUGE_KEY 按下\r\n");
-    // 处理 JUGE
-    HAL_GPIO_WritePin(POWER_ON_GPIO_Port, POWER_ON_Pin, GPIO_PIN_RESET);
+    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");
+      HAL_GPIO_WritePin(POWER_ON_GPIO_Port, POWER_ON_Pin, GPIO_PIN_RESET);
+    }
   }
+
+  // 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 */

+ 1 - 1
lora_bl.ioc

@@ -117,7 +117,7 @@ PB11.Mode=Asynchronous
 PB11.Signal=USART3_RX
 PB14.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultEXTI
 PB14.GPIO_Label=SOS_KEY
-PB14.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING
+PB14.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_RISING_FALLING
 PB14.GPIO_PuPd=GPIO_PULLUP
 PB14.Locked=true
 PB14.Signal=GPXTI14