Quellcode durchsuchen

随机数1-3分钟发一次。添加E104_BT5005A .c .h文件

xuxinyi vor 2 Monaten
Ursprung
Commit
347feab7b6
4 geänderte Dateien mit 142 neuen und 40 gelöschten Zeilen
  1. 3 1
      CMakeLists.txt
  2. 34 0
      Core/Inc/E104-BT5005A.h
  3. 69 0
      Core/Src/E104-BT5005A.c
  4. 36 39
      Core/Src/main.c

+ 3 - 1
CMakeLists.txt

@@ -32,7 +32,9 @@ message("Build type: " ${CMAKE_BUILD_TYPE})
 enable_language(C ASM)
 
 # Create an executable object type
-add_executable(${CMAKE_PROJECT_NAME})
+add_executable(${CMAKE_PROJECT_NAME}
+        Core/Src/E104-BT5005A.c
+        Core/Inc/E104-BT5005A.h)
 
 # Add STM32CubeMX generated sources
 add_subdirectory(cmake/stm32cubemx)

+ 34 - 0
Core/Inc/E104-BT5005A.h

@@ -0,0 +1,34 @@
+//
+// Created by EDZ on 25-8-28.
+//
+
+#ifndef E104_BT5005A_H
+#define E104_BT5005A_H
+
+#include "main.h"
+
+#include <time.h>
+#include <stdlib.h>
+
+#include "string.h"
+#include "E104-BT5005A.h"
+
+
+#define E104_BT5005A_ROLE "AT+ROLE=2" //设置为观察值模式
+
+#define E104_BT5005A_SCANINTV "AT+SCANINTV=500" //设置扫描间隔
+
+#define E104_BT5005A_RESET "AT+RESET" //不知道有没有用
+
+#define E104_BT5005A_SLEEP "AT+SLEEP" //睡眠模式
+
+#define E104_BT5005A_WAKE_UP "AT" //睡眠模式下,随便发个串口数据,就可以唤醒
+
+
+// void E104_BT5005A_SLEEP_Fun(void);
+void E104_BT5005A_ROLE_Fun(void);
+void E104_BT5005A_SCANINTV_Fun(void);
+void E104_BT5005A_RESET_Fun(void);
+void E104_BT5005A_SLEEP_Fun(void);
+void E104_BT5005A_WAKE_UP_Fun(void);
+#endif //E104_BT5005A_H

+ 69 - 0
Core/Src/E104-BT5005A.c

@@ -0,0 +1,69 @@
+//
+// Created by EDZ on 25-8-28.
+//
+
+#include "E104-BT5005A.h"
+#include "stdio.h"
+#include "main.h"
+
+#include <time.h>
+#include <stdlib.h>
+
+#include "string.h"
+#include "E104-BT5005A.h"
+
+void E104_BT5005A_ROLE_Fun(void) {
+    HAL_UART_Transmit(&huart3, (const uint8_t *)E104_BT5005A_ROLE, strlen(E104_BT5005A_ROLE), HAL_MAX_DELAY);
+
+}
+
+void E104_BT5005A_SCANINTV_Fun(void) {
+    HAL_UART_Transmit(&huart3, (const uint8_t *)E104_BT5005A_SCANINTV, strlen(E104_BT5005A_SCANINTV), HAL_MAX_DELAY);
+}
+
+void E104_BT5005A_RESET_Fun(void) {
+    HAL_UART_Transmit(&huart3, (const uint8_t *)E104_BT5005A_RESET, strlen(E104_BT5005A_RESET), HAL_MAX_DELAY);
+}
+
+void E104_BT5005A_SLEEP_Fun(void) {
+    HAL_UART_Transmit(&huart3, (const uint8_t *)E104_BT5005A_SLEEP, strlen(E104_BT5005A_SLEEP), HAL_MAX_DELAY);
+
+}
+
+void E104_BT5005A_WAKE_UP_Fun(void) {
+    HAL_UART_Transmit(&huart3, (const uint8_t *)E104_BT5005A_WAKE_UP, strlen(E104_BT5005A_WAKE_UP), HAL_MAX_DELAY);
+
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 36 - 39
Core/Src/main.c

@@ -23,6 +23,7 @@
 #include <stdlib.h>
 
 #include "string.h"
+#include "E104-BT5005A.h"
 /* Private includes ----------------------------------------------------------*/
 /* USER CODE BEGIN Includes */
 
@@ -96,6 +97,10 @@ void sentLoraData(uint8_t  sendDataNum) {
   HAL_UART_Transmit(&huart2, totalData[sendDataNum], 8, HAL_MAX_DELAY);
   memset(totalData[sendDataNum], 0, sizeof(totalData[sendDataNum]));
 }
+
+uint8_t randomFun() {
+  return rand() % 120 + 60; //随机时间1分钟到3分钟之间随机数
+}
 /* USER CODE END 0 */
 
 /**
@@ -138,23 +143,16 @@ int main(void)
   // 使能 USART2 空闲中断
   __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
 
-  printf("USART3 DMA + IDLE 中断接收已启动\r\n");
-  // 发送一个字符串到UART2
-  const char *msg = "AT+ROLE=2";
-  HAL_UART_Transmit(&huart3, (uint8_t *)msg, strlen(msg), HAL_MAX_DELAY);
-
-
-  const char *msg1 = "AT+SCANINTV=3000";
-  HAL_UART_Transmit(&huart3, (uint8_t *)msg1, strlen(msg1), HAL_MAX_DELAY);
-
-  const char *msg2 = "AT+RESET";
-  HAL_UART_Transmit(&huart3, (uint8_t *)msg2, strlen(msg2), HAL_MAX_DELAY);
+  E104_BT5005A_ROLE_Fun();
+  E104_BT5005A_SCANINTV_Fun();
+  E104_BT5005A_RESET_Fun();
 
   const char *lora_msg = "AT+OPTION=3,0";
   HAL_UART_Transmit(&huart2, (uint8_t *)lora_msg, strlen(lora_msg), HAL_MAX_DELAY);
 
   int i = 0;
   uint32_t timer = 0;
+  srand(HAL_GetTick());
   // uint8_t i = 0xaa;
   /* USER CODE END 2 */
 
@@ -164,37 +162,36 @@ int main(void)
   while (1)
   {
     /* USER CODE END WHILE */
-    // if (workMode == 0) {  // 接收蓝牙数据
-    //   printf("接收中\r\n");
-    // }else if (workMode == 1) { // 发送数据给Lora
-    //   printf("发送中\r\n");
-    //   if (i < 10)
-    //   {
-    //     // 启动 USART2 DMA 接收
-    //     sentLoraData(i);
-    //     i++;
-    //   }else {
-    //     workMode = 2;
-    //     i = 0;
-    //   }
-    // }else if (workMode == 2) {
-    //   // 等待间隔时间
-    //   printf("等待中\r\n");
-    //   if (timer > 10) {
-    //     HAL_UART_Receive_DMA(&huart3, rx_buf, sizeof(rx_buf));
-    //     __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
-    //     workMode = 0;
-    //     timer = 0;
-    //   }else {
-    //     timer++;
-    //   }
-    // }
+    if (workMode == 0) {  // 接收蓝牙数据
+      printf("接收中\r\n");
+    }else if (workMode == 1) { // 发送数据给Lora
+      printf("发送中\r\n");
+      if (i < 10)
+      {
+        // 启动 USART2 DMA 接收
+        sentLoraData(i);
+        i++;
+      }else {
+        workMode = 2;
+        i = 0;
+      }
+    }else if (workMode == 2) {
+      // 等待间隔时间
+      printf("等待中\r\n");
+      if (timer > randomFun()) {
+        HAL_UART_Receive_DMA(&huart3, rx_buf, sizeof(rx_buf));
+        __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
+        workMode = 0;
+        timer = 0;
+      }else {
+        timer++;
+      }
+    }
 
 
     // 使用系统滴答计数作为种子 (不太随机,但简单)
-    srand(HAL_GetTick());
-    int random_num = rand() % 100;  // 0~99 之间的随机数
-    printf("随机数:%d\r\n", random_num);
+    // E104_BT5005A_SLEEP_Fun();
+    // printf("%d\r\n", randomFun());
     HAL_Delay(1000);
     /* USER CODE BEGIN 3 */
   }