From 1cb597f4ad10dae753d43a4c717716891154870c Mon Sep 17 00:00:00 2001 From: "lxbpxylps@126.com" Date: Sun, 17 Oct 2021 15:32:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=8E=A5=E6=94=B6=E7=BD=91?= =?UTF-8?q?=E5=85=B3=E6=8C=87=E4=BB=A4=E5=B9=B6=E5=90=91=E7=BD=91=E5=85=B3?= =?UTF-8?q?=E5=8F=91=E9=80=81=E6=95=B0=E6=8D=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Libraries/SX1278 | 2 +- System/uart/uart.c | 1 - System/uart/uart.h | 8 +-- User/Main/main.c | 123 ++++++++++++++++++++++++++++++++++++--------- 4 files changed, 104 insertions(+), 30 deletions(-) diff --git a/Libraries/SX1278 b/Libraries/SX1278 index a7a4236..d57a2a8 160000 --- a/Libraries/SX1278 +++ b/Libraries/SX1278 @@ -1 +1 @@ -Subproject commit a7a42360525ea26f45b459f37c06580706f030b6 +Subproject commit d57a2a85dfd2d34f91483ad0ad6ede816c7c2e84 diff --git a/System/uart/uart.c b/System/uart/uart.c index d385a42..8b42a96 100644 --- a/System/uart/uart.c +++ b/System/uart/uart.c @@ -10,7 +10,6 @@ #include "stdio.h" -#include "sys.h" #include "systick.h" #include "uart.h" diff --git a/System/uart/uart.h b/System/uart/uart.h index b01aa7e..83bc26d 100644 --- a/System/uart/uart.h +++ b/System/uart/uart.h @@ -15,9 +15,11 @@ #ifndef __UART_H #define __UART_H +#include "sys.h" + //注释此处语句可禁用特定串口 #define UART1_FIFO_EN 1 -#define UART2_FIFO_EN 1 +#define UART2_FIFO_EN 0 //定义端口号 typedef enum @@ -35,8 +37,8 @@ typedef enum #if UART2_FIFO_EN == 1 #define UART2_BAUD 115200 -#define UART2_TX_BUF_SIZE 1 * 64 -#define UART2_RX_BUF_SIZE 1 * 64 +#define UART2_TX_BUF_SIZE 1 * 512 +#define UART2_RX_BUF_SIZE 1 * 512 #endif //回调函数定义 diff --git a/User/Main/main.c b/User/Main/main.c index 2a8f7c8..04d8cbc 100644 --- a/User/Main/main.c +++ b/User/Main/main.c @@ -1,13 +1,14 @@ /** * @file main.c * @author Myth - * @version 0.8 - * @date 2021.10.15 + * @version 1.0 + * @date 2021.10.17 * @brief 工程主函数文件 * @details 初始化及主循环 * @note 此版本实现功能: - * 串口回显,回显时 PC13 上的 LED 闪烁 - * 接收来自主节点的字符串数据 + * + * 网关通过 LoRa 模块轮流向节点请求数据,节点(本机)将数据返回给主节点 + * * JTAG 已禁用,请使用 SWD 调试 */ @@ -17,8 +18,19 @@ #include "led.h" #include "lora.h" +#include "aht20.h" +#include "bh1750.h" -void Echo(uint8_t byte); +#define NODE_SEQ_NUM '2' //节点标号:2 ~ 5 + +typedef struct +{ + uint8_t seq; + float humi; + float temp; + float light; + uint8_t end; +} DataPack; //数据包定义,由于平台相同,网关和节点通讯无需考虑对齐问题 int main(void) { @@ -31,35 +43,96 @@ int main(void) SysTick_Init(); //初始化 SysTick 和软件定时器 UART_Init(); //初始化串口 - LED_Init(); //初始化 LED - LoRa_Init(); //初始化 LoRa 模块 + LED_Init(); //初始化 LED + LoRa_Init(); //初始化 LoRa 模块 + AHT20_Init(); //初始化 AHT20 + BH1750_Init(); //初始化 BH1750 - UART_BindReceiveHandle(COM1, Echo); //绑定 COM1 串口接收中断至 Echo 函数 + DataPack pack; //发送的数据包 + pack.seq = NODE_SEQ_NUM; //数据包序号,标明发送节点 + pack.end = '@'; //数据包结束字节,用于网关校验 - uint8_t buffer[255]; - uint8_t size; + uint8_t buffer[3]; //接收缓冲区 + uint8_t size; //接收到的包大小 + int32_t time; //当前时间,控制时序 + + //首先获取一次数据,防止发送空数据 + AHT20_Start(); // AHT20 开始测量 + BH1750_Start(); // BH1750 开始测量 + + Delay_ms(80); + AHT20_Read(&(pack.humi), &(pack.temp)); + Delay_ms(100); + pack.light = BH1750_Read(); while (1) { //程序主循环 - size = LoRa_Receive(buffer); //接收 - if (size == 0) - continue; + AHT20_Start(); // AHT20 开始测量 + BH1750_Start(); // BH1750 开始测量 - printf("Receive %d bytes: %s\n", size, buffer); + //在等待测量的过程中检测是否有命令包索取数据 + //说明: + //下面一段代码显然应该封装为函数。但在封装为函数后出现恶性 Bug,猜测为溢出导致,正在排除 + //即使不封装为函数,将 buffer size 声明为全局变量也会导致此 Bug,表现为接收后死机 + time = SysTick_GetRunTime(); + while (SysTick_CheckRunTime(time) < 80) //等待 80 ms + { + size = LoRa_Receive(buffer); //接收 + if (size < 1) + { + continue; + } + else if (size != 3 || buffer[0] != '#' || buffer[1] != NODE_SEQ_NUM || buffer[2] != '@') //判断命令包是否正确,是否向自己查询 + { + printf("Get Wrong Command[%d]: %c%c%c\n", size, buffer[0], buffer[1], buffer[2]); + continue; + } - LED1_Toggle; + //命令包正确 + LED1_Toggle; // LED 翻转 + printf("Get Right Command[%d]: %c%c%c\n", size, buffer[0], buffer[1], buffer[2]); + + //返回最近一个获得的数据。由于无 DIO0 的 LoRa 通讯不可靠,发送 3 次 + for (uint8_t i = 0; i < 3; i++) + { + printf("Send!\n"); + LoRa_Send(&pack, sizeof(DataPack)); + Delay_ms(300); + } + } + + AHT20_Read(&(pack.humi), &(pack.temp)); // 80ms 后可读取 AHT20 + + time = SysTick_GetRunTime(); + while (SysTick_CheckRunTime(time) < 100) //等待 100 ms + { + size = LoRa_Receive(buffer); //接收 + if (size < 1) + { + continue; + } + else if (size != 3 || buffer[0] != '#' || buffer[1] != NODE_SEQ_NUM || buffer[2] != '@') //判断命令包是否正确,是否向自己查询 + { + printf("Get Wrong Command[%d]: %c%c%c\n", size, buffer[0], buffer[1], buffer[2]); + continue; + } + + //命令包正确 + LED1_Toggle; // LED 翻转 + printf("Get Right Command[%d]: %c%c%c\n", size, buffer[0], buffer[1], buffer[2]); + + //返回最近一个获得的数据。由于无 DIO0 的 LoRa 通讯不可靠,发送 3 次 + for (uint8_t i = 0; i < 3; i++) + { + printf("Send!\n"); + LoRa_Send(&pack, sizeof(DataPack)); + Delay_ms(300); + } + } + + pack.light = BH1750_Read(); // 180ms 后才能读取 BH1750 } return 1; } - -/** - * @brief 串口回显函数 - * @param byte: 本次中断接收到的字节 - */ -void Echo(uint8_t byte) -{ - LED1_Slow_Toggle; - UART_SendChar(COM1, byte); -}