class="hljs-ln-code"> class="hljs-ln-line">void UART1_Init(uint32_t ulBaudrate); // 初始化串口1; GPIO引脚PA9+PA10、中断优先级、通信参数:波特率可设、8位数据、无校验、1个停止位
  • class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="3"> class="hljs-ln-code"> class="hljs-ln-line">// 发送
  • class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="4"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
  • class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="5"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART1_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
  • class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="6"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
  • class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="7"> class="hljs-ln-code"> class="hljs-ln-line">// 接收
  • class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="8"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART1_GetRxNum(void); // 获取接收到的最新一帧字节数
  • class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="9"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t* UART1_GetRxData(void); // 获取接收到的数据 (缓存的地址)
  • class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="10"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_ClearRx(void); // 清理接收到的数据 (清理最后一帧字节数,因为它是判断接收的标志)
  • class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}" onclick="hljs.signin(event)">


    二、文件移植

    1、新建 bsp_UART.c 和 bsp_UART.h这两个文件

    2、工程添加 bsp_UART.c 文件

     3、添加头文件路径;  

    刚才添加了 c文件到工程,还需要添加头文件的路径,编译器才能知道 h 文件的存放位置  。

    操作如下:

           

    4、添加文件引用

    在需要UART功能的文件中,添加:#include "bsp_UART.h"

    如,main.c 中想使用UART,就在它的头部位置添加。

    操作如下:

    好了,移植就是这么简单,至此,你的工程中已经可以正常使用UART功能了。


    三、初始化、引脚使用

    以使用UART1为例,其它几个UART的初始化方法相同。

    调用:UART1_Init (115200) ;    //  参数:波特率     

    操作如下:     

                     

    此函数将会执行:引脚PA9+PA10的初始化、UART1初始化、中断初始化、进入接收状态。

    初始化仅需调用这个函数即可。不用修改代码,不用管其它的。

    各个UART在初始化时,将默认使用以下引脚:

    这些都是STM32不同系列通用的 UART引脚,不管F1还是F4,都可以使用以下引脚,无需修改。

    class="table-box">
    串口端口发送引脚 (TX)接收引脚 (RX)
    UART1PA9PA10
    UART2PA2PA3
    UART3PB10PB11
    UART4PC10PC11
    UART5PC12PD2
    UART6PC6PC7

          


    四、发送

    各个UART的发送,都提供了 3个函数。

    灵活选择,能实现不同场景数据的发送,  如:float、int、数组、字符串、AT指令、结构体,等等。

    发送机制:发送中断+环形队列。当然,这些底层不用管、不用修改,只管调用函数。

    1. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    2. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="2"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
    3. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="3"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART1_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}" onclick="hljs.signin(event)">

    函数解释:

      发送字符串,用:UART1_SendString (char *string,...) 

    使用方法如同 printf,相当灵活,具体可以百度一下printf的格式化如何使用,操作是一样的。

    使用示范:

    1. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1"> class="hljs-ln-code"> class="hljs-ln-line">UART1_SendString("\r 你好吗? \r");
    2. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="2"> class="hljs-ln-code"> class="hljs-ln-line">UART1_SendString("字节数:%d 字节\r", rxNum);
    3. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="3"> class="hljs-ln-code"> class="hljs-ln-line">UART1_SendString("数据 (ASCII): %s\r", (char *)rxData);
    class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}" onclick="hljs.signin(event)">

    ②  发送指定长度的数据,用:UART1_SendData (*data, num)   

    参数:缓存地址、字节数

    可用于发送数组、结构体、字符串 等任意数据。     

    使用示范:                

    1. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1"> class="hljs-ln-code"> class="hljs-ln-line">// 发送字符串
    2. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="2"> class="hljs-ln-code"> class="hljs-ln-line">UART1_SendData((uint8_t*)"你好吗", 6);
    3. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="3"> class="hljs-ln-code"> class="hljs-ln-line">// 发送数组、16进制数制
    4. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="4"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t myArray[50] = {0xAA, 0xBB, 0xCC, 0xDD};
    5. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="5"> class="hljs-ln-code"> class="hljs-ln-line">UART1_SendData(myArray, 8);
    6. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="6"> class="hljs-ln-code"> class="hljs-ln-line">// 发送浮点类型数据
    7. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="7"> class="hljs-ln-code"> class="hljs-ln-line">float myFloat = 520.1314;
    8. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="8"> class="hljs-ln-code"> class="hljs-ln-line">UART1_SendData(&myFloat, sizeof(float));
    class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}" onclick="hljs.signin(event)">

    ③  发送AT指令:UART1_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs);  

    参数:AT指令字符串、期待返回信息字符串、等待超时ms;

    返回:0-执行失败、1-执行成功 

    本函数能很方便地与ESP8266、蓝牙模块等进行通过。

    用于发送AT指令后,等待执行反馈; 

    使用示范:

    1. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1"> class="hljs-ln-code"> class="hljs-ln-line">UART1_SendAT("AT+RESTORE\r\n", "ready", 3000) ? printf("恢复出厂设置: 成功\r\n") : printf("恢复出厂设置: 失败\r\n"); // 恢复模块的出厂设置
    2. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="2"> class="hljs-ln-code"> class="hljs-ln-line">UART1_SendAT("AT+CWMODE=1\r\n", "OK", 3000) ? printf("配置 STA模式: 成功\r\n") : printf("配置 STA模式: 失败\r\n"); // 工作模式:1_STA, 2_AP, 3_STA+AP
    3. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="3"> class="hljs-ln-code"> class="hljs-ln-line">UART1_SendAT("AT+RST\r\n", "ready", 3000) ? printf("重启 ESP8266: 成功\r\n") : printf("重启 ESP8266: 失败\r\n"); // 重启模块: 设置工作模式后,需重启才生效
    4. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="4"> class="hljs-ln-code"> class="hljs-ln-line">
    5. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="5"> class="hljs-ln-code"> class="hljs-ln-line">char strTemp[100] = {0};
    6. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="6"> class="hljs-ln-code"> class="hljs-ln-line">sprintf(strTemp, "AT+CWJAP=\"%s\",\"%s\"\r\n", "wifiName", "wifiPassWord");
    7. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="7"> class="hljs-ln-code"> class="hljs-ln-line">if(UART1_SendAT(strTemp, "OK\r\n", 6000) )
    8. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="8"> class="hljs-ln-code"> class="hljs-ln-line"> printf("连接WiFi网络 成功\r\n");
    9. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="9"> class="hljs-ln-code"> class="hljs-ln-line">else
    10. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="10"> class="hljs-ln-code"> class="hljs-ln-line"> printf("连接WiFi网络 失败\r\n");
    class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}" onclick="hljs.signin(event)">

    printf

            很多人习惯在调试时使用printf。

            在bsp_UART.c里,也做好了它的重定向输出。

            printf已重定向输出到UART1,在初始化 UART1后,直接可用printf。


    五、接收

    接收处理,共3个函数:        

    1. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART1_GetRxNum(void); // 获取接收到的最新一帧字节数
    2. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="2"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t* UART1_GetRxdData(void); // 获取接收到的数据(缓存的地址)
    3. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="3"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_ClearRx(void); // 清理接收到的数据(清理最后一帧字节数,因为它是判断接收的标志)
    class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}" onclick="hljs.signin(event)">

    接收机制上,使用:接收中断 + 空闲中断 + 双缓冲区。这些底层工作都已封装处理好,不用管。使用上,只管调用上面这仨函数。

          

      ①   获取字节数:UART1_GetRxNum ( )    

    作用:获取最后一帧接收到的 字节数;

    ②  获取数据:UART1_GetRxData ( )

    作用:获取最后一帧接收到的 数据地址;

    ③  清0本次接收:UART1_ClearRx ( ) ;      

    作用:在处理完最后一帧的数据后,清0本帧数据的缓存

    下面代码,示范如何接收、处理数据(以串口1为例,其它串口同理):


    六、常见冲突、错误的解决

    本文所提供的 bsp_UART.c 和 bsp_UART.h ,支持标准库、HAL库,复制过去即可。

    1、添加到原子哥的标准库工程中可能的冲突

    原子哥的示例,很多人喜欢使用。

    这些示例中,一般已带有uart.c和printf的重定向处理。

    如果想添加到这些示例中,建议先备份工程!删除工程中UART文件和代码,再移植此文件。

    2、添加到CubeMX、CubeIDE工程中可能的冲突

    明确地,无需在CubeMX、CubeIDE中做任何UART的设置。

    如果工程中已配置UART,建议先备份工程!关闭CubeMX、CubeIDE中对UART使能,再移植。

    如果,使用CubeMX、CubeIDEE配置工程时,想使用此代码,又想标记哪些串口和相关的UART引脚已使用,可以的,在CubeMX、CubeIDE上使能相关的UART即可,这样就能方便地标记哪些引脚被使用了。

    但是,记得使用上述表格中的引脚,和,不能打勾串口配置选项的中断、DMA,否则冲突。

    使用CubeMX时重点注意的冲突:

           

    当在CubeMX、CubeIDE中,如上使能串口后,也能使用HAL库的发送函数、接收函数,不冲突:

    只是,不建议使用了。因为bsp_USART.c的函数,使用起来更方便。

            

    3、不同串口,使用不同的方式

    如果,某个串口,想使用CubeMX进行中断、DMA配置; 而某些串口,想使用文件中的函数:

    可以的.

    在bsp_UART.h文件中,修改下面的宏定义,默认是1,即开启,修改为0值关闭即可。

    宏定义 值解释:

    4、发现通信中漏数据

    这种情况,是缓存过小。

    在bsp_UART.h里,把发送缓存或接收缓存改大一些即可

    5、接收时,发现漏帧

    不是漏了一帧的某些内容,是整帧漏了。

    这种情况,一般是接收处理的不及时。

    如对方每100ms发送一次,但我们的代码while里,每间隔500ms才执行一次接收处理。

    把接收处理的间隔,调小一些即可。或者,while里的接收处理,完全不用时间间隔。


    七、STM32F103 源代码

    可复制以下代码到文件,也可以直接下载:bsp_UART.c + bsp_UART.h

    1、bsp_UART.h

    1. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1"> class="hljs-ln-code"> class="hljs-ln-line">#ifndef __BSP__UART_H
    2. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="2"> class="hljs-ln-code"> class="hljs-ln-line">#define __BSP__UART_H
    3. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="3"> class="hljs-ln-code"> class="hljs-ln-line">/***********************************************************************************************************************************
    4. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="4"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【代码编写】 魔女开发板团队
    5. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="5"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【淘 宝】 魔女开发板 https://demoboard.taobao.com
    6. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="6"> class="hljs-ln-code"> class="hljs-ln-line"> ***********************************************************************************************************************************
    7. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="7"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【文件名称】 bsp_UART.h
    8. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="8"> class="hljs-ln-code"> class="hljs-ln-line"> **
    9. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="9"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【 功 能 】 串口通信底层驱动(UART1、UART2、UART3、UART4、UART5、UART6)
    10. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="10"> class="hljs-ln-code"> class="hljs-ln-line"> ** 波特率-None-8-1
    11. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="11"> class="hljs-ln-code"> class="hljs-ln-line"> ** 调用全局声明中的串口函数,即可完成初始化、发送、接收.
    12. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="12"> class="hljs-ln-code"> class="hljs-ln-line"> **
    13. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="13"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【 约 定 】 本文件所用串口通信,均为异步通信。
    14. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="14"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024年起更新的示例,串口通信函数命名时,统一使用"UART",而不使用旧文件中的"USART".
    15. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="15"> class="hljs-ln-code"> class="hljs-ln-line"> **
    16. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="16"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【适用平台】 STM32F103 + keil5 + HAL库/标准库
    17. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="17"> class="hljs-ln-code"> class="hljs-ln-line"> **
    18. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="18"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【串口引脚】 各个串口的初始化函数UARTx_Init(),将按以下默认引脚进行初始化:
    19. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="19"> class="hljs-ln-code"> class="hljs-ln-line"> ** 1- UART1 TX_PA9 RX_PA10 特别说明:魔女开发板系列,均板载USB转TTL,PCB已布线连接好UART1, 使用和烧录用的同一USB接口,即可通过UART1和电脑进行通信。具体查看资料文件夹中的说明文件。
    20. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="20"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2- UART2 TX_PA2 RX_PA3
    21. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="21"> class="hljs-ln-code"> class="hljs-ln-line"> ** 3- UART3 TX_PB10 RX_PB11
    22. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="22"> class="hljs-ln-code"> class="hljs-ln-line"> ** 4- UART4 TX_PC10 RX_PC11
    23. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="23"> class="hljs-ln-code"> class="hljs-ln-line"> ** 5- UART5 TX_PC12 RX_PD2
    24. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="24"> class="hljs-ln-code"> class="hljs-ln-line"> **
    25. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="25"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【移植说明】 1- 如果使用CubeMX配置的工程,无须对UART进行任何配置;
    26. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="26"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2- 适用于HAL库、标准库工程,移植后,均可使用;
    27. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="27"> class="hljs-ln-code"> class="hljs-ln-line"> ** 3- 复制本工程的UART文件夹,到目标工程文件夹中;
    28. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="28"> class="hljs-ln-code"> class="hljs-ln-line"> ** 4- 添加头文件路径: Keil > Option > C/C++ > Include Paths;
    29. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="29"> class="hljs-ln-code"> class="hljs-ln-line"> ** 5- 添加C文件到工程: Keil > 左侧工程管理器中双击目标文件夹 > 选择 bsp_UART.c;
    30. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="30"> class="hljs-ln-code"> class="hljs-ln-line"> ** 6- 添加文件引用: #include "bsp_uart.h",即哪个文件要用串口功能,就在其代码文件顶部添加引用;
    31. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="31"> class="hljs-ln-code"> class="hljs-ln-line"> **
    32. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="32"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【代码使用】 每组串口,已封装好7个函数 (初始化1个、发送3个、接收3个)。下面以UART1作示范说明:
    33. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="33"> class="hljs-ln-code"> class="hljs-ln-line"> ** 1、初始化: void UART1_Init(uint32_t ulBaudrate); // 初始化串口; 配置GPIO引脚PA9+PA10、配置通信参数:波特率-None-8-1、配置中断
    34. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="34"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2、发送指定长度的数据 void UART1_SendData(uint8_t *puData, uint16_t usNum); // 发送指定长度的数据; 参数:数据地址、字节数
    35. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="35"> class="hljs-ln-code"> class="hljs-ln-line"> ** 3、发送字符串 void UART1_SendString(const char *pcString,...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    36. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="36"> class="hljs-ln-code"> class="hljs-ln-line"> ** 4、发送AT指令 uint8_t UART1_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    37. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="37"> class="hljs-ln-code"> class="hljs-ln-line"> ** 5、获取接收的字节数: uint16_t UART1_GetRxNum(void); // 获取接收到的字节数; 如果返回值大于0,即为接收到新一帧数据
    38. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="38"> class="hljs-ln-code"> class="hljs-ln-line"> ** 6、获取接收的数据: uint8_t* UART1_GetRxData(void); // 获取接收到的数据(缓存的地址)
    39. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="39"> class="hljs-ln-code"> class="hljs-ln-line"> ** 7、清零接收标志: void UART1_ClearRx(void); // 清0接收标志,即清0接收到的字节数; 每次处理完成数据,就要调用这个函数清0,方可进入下轮接收判断
    40. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="40"> class="hljs-ln-code"> class="hljs-ln-line"> **
    41. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="41"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【更新记录】 2024-10-22 中断服务函数:使用宏定义代替位操作,以提高可读情; 优化注释;
    42. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="42"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-09-26 增加MD、HD的判断
    43. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="43"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-07-02 增加Modbus_CRC16校验的追加、判断函数
    44. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="44"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-06-30 优化UART初始化:增加奇偶校验3个寄存器位的代码配置行
    45. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="45"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-06-20 优化AT指令函数, 完善反馈等待机制; 取消:UARTx_WaitACK();
    46. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="46"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-06-07 增加AT指令等待反馈处理函数:UARTx_WaitACK();
    47. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="47"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-06-07 优化函数:UARTx_SendString();
    48. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="48"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-06-06 优化代码适配,使文件能适用于标准库、HAL库工程
    49. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="49"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-04-04 UARTx_SendString(),取消临时缓存的static,以减少RAM占用,适配LVGL移植;
    50. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="50"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-03-27 修改xUSATR_TypeDef结构体,优化缓冲的存储方式
    51. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="51"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-02-04 简化接收函数命名,函数名中使用Rx字符,代替旧函数名称中的Reveived字符;
    52. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="52"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-01-09 取消部分静态变量,并入结构体中,使用调用更直观
    53. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="53"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-01-03 完善函数、注释
    54. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="54"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2023-12-25 增加接收函数的封装,取消接收变量(全局)的使用
    55. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="55"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2023-12-23 完成对HAL库的支持
    56. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="56"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2023-01-27 增加宏定义、完善注释
    57. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="57"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-12-16 完善接收机制:取消接收标志,判断接收字节数>0即为接收到新数据
    58. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="58"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-11-03 完善接收函数返回值处理
    59. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="59"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-08-14 增加宏定义:接收缓存区大小设定值,使空间占用更可控;
    60. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="60"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-08-14 修改发送函数名称:UARTx_Printf(),修改为:UARTx_SendString();
    61. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="61"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2020-09-02 文件建立、UART1接收中断、空闲中断、发送中断、DMA收发
    62. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="62"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-06-04 UART1、2、3及UART4、5 的收发完善代码
    63. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="63"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-06-09 完善文件格式、注释
    64. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="64"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-06-22 完善注释说明
    65. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="65"> class="hljs-ln-code"> class="hljs-ln-line"> **
    66. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="66"> class="hljs-ln-code"> class="hljs-ln-line">************************************************************************************************************************************/
    67. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="67"> class="hljs-ln-code"> class="hljs-ln-line">#include "stdio.h" // 引用C语言标准库,它定义了标准输入输出函数; 如:printf、scanf、sprintf、fopen 等;
    68. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="68"> class="hljs-ln-code"> class="hljs-ln-line">#include "stdarg.h" // 引用C语言标准库,它定义了处理可变参数的宏; 如:va_start、va_arg、va_end、va_list 等;
    69. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="69"> class="hljs-ln-code"> class="hljs-ln-line">#include "string.h" // 引用C语言标准库,它定义了操作字符串的函数; 如:strcpy、strcmp、strlen、memset、memcpy 等;
    70. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="70"> class="hljs-ln-code"> class="hljs-ln-line">
    71. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="71"> class="hljs-ln-code"> class="hljs-ln-line">
    72. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="72"> class="hljs-ln-code"> class="hljs-ln-line">
    73. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="73"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    74. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="74"> class="hljs-ln-code"> class="hljs-ln-line"> #include "stm32f10x.h" // 引用 标准库的底层支持文件
    75. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="75"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    76. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="76"> class="hljs-ln-code"> class="hljs-ln-line">
    77. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="77"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER
    78. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="78"> class="hljs-ln-code"> class="hljs-ln-line"> #include "stm32f1xx_hal.h" // 引用 HAL库的底层支持文件
    79. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="79"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    80. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="80"> class="hljs-ln-code"> class="hljs-ln-line">
    81. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="81"> class="hljs-ln-code"> class="hljs-ln-line">
    82. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="82"> class="hljs-ln-code"> class="hljs-ln-line">/*****************************************************************************
    83. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="83"> class="hljs-ln-code"> class="hljs-ln-line"> ** 移植配置修改区
    84. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="84"> class="hljs-ln-code"> class="hljs-ln-line"> ** 备注:除非有特殊要求,否则,下面参数已通用如蓝牙通信、ESP8266通信、串口屏等
    85. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="85"> class="hljs-ln-code"> class="hljs-ln-line">****************************************************************************/
    86. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="86"> class="hljs-ln-code"> class="hljs-ln-line">// 串口开关
    87. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="87"> class="hljs-ln-code"> class="hljs-ln-line">#define UART1_EN 1 // 串口1,0=关、1=启用; 倘若没用到USART1, 置0,就不会开辟USART1发送缓存、接收缓存,省一点资源;
    88. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="88"> class="hljs-ln-code"> class="hljs-ln-line">#define UART2_EN 1 // 串口2,0=关、1=启用; 同上;
    89. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="89"> class="hljs-ln-code"> class="hljs-ln-line">#define UART3_EN 1 // 串口3,0=关、1=启用; 同上;
    90. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="90"> class="hljs-ln-code"> class="hljs-ln-line">#define UART4_EN 1 // 串口4,0=关、1=启用; 同上;
    91. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="91"> class="hljs-ln-code"> class="hljs-ln-line">#define UART5_EN 1 // 串口5,0=关、1=启用; 同上;
    92. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="92"> class="hljs-ln-code"> class="hljs-ln-line">// 发送缓冲区大小
    93. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="93"> class="hljs-ln-code"> class="hljs-ln-line">#define UART1_TX_BUF_SIZE 2048 // 配置每组USART发送环形缓冲区数组的大小,单位:字节数;
    94. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="94"> class="hljs-ln-code"> class="hljs-ln-line">#define UART2_TX_BUF_SIZE 512 // -- 只有在前面串口开关在打开状态,才会定义具体的缓冲区数组
    95. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="95"> class="hljs-ln-code"> class="hljs-ln-line">#define UART3_TX_BUF_SIZE 512 // -- 默认值:512,此值已能适配大部场景的通信; 如果与ESP8266之类的设备通信,可适当增大此值。
    96. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="96"> class="hljs-ln-code"> class="hljs-ln-line">#define UART4_TX_BUF_SIZE 512 // -- 值范围:1~65535; 注意初始化后,不要超过芯片最大RAM值。
    97. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="97"> class="hljs-ln-code"> class="hljs-ln-line">#define UART5_TX_BUF_SIZE 512 // -- 注意此值是一个环形缓冲区大小,决定每一帧或多帧数据进入发送前的总缓存字节数,先进先出。
    98. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="98"> class="hljs-ln-code"> class="hljs-ln-line">// 接收缓冲区大小
    99. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="99"> class="hljs-ln-code"> class="hljs-ln-line">#define UART1_RX_BUF_SIZE 2048 // 配置每组USART接收缓冲区的大小,单位:字节; 此值影响范围:中断里的接收缓存大小,接收后数据缓存的大小
    100. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="100"> class="hljs-ln-code"> class="hljs-ln-line">#define UART2_RX_BUF_SIZE 512 // --- 当接收到的一帧数据字节数,小于此值时,数据正常;
    101. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="101"> class="hljs-ln-code"> class="hljs-ln-line">#define UART3_RX_BUF_SIZE 512 // --- 当接收到的一帧数据字节数,超过此值时,超出部分,将在中断中直接弃舍,直到此帧接收结束(发生空闲中断);
    102. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="102"> class="hljs-ln-code"> class="hljs-ln-line">#define UART4_RX_BUF_SIZE 512 //
    103. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="103"> class="hljs-ln-code"> class="hljs-ln-line">#define UART5_RX_BUF_SIZE 512 //
    104. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="104"> class="hljs-ln-code"> class="hljs-ln-line">// 结束-配置修改
    105. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="105"> class="hljs-ln-code"> class="hljs-ln-line">
    106. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="106"> class="hljs-ln-code"> class="hljs-ln-line">
    107. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="107"> class="hljs-ln-code"> class="hljs-ln-line">
    108. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="108"> class="hljs-ln-code"> class="hljs-ln-line">/*****************************************************************************
    109. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="109"> class="hljs-ln-code"> class="hljs-ln-line"> ** 声明全局函数
    110. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="110"> class="hljs-ln-code"> class="hljs-ln-line"> **
    111. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="111"> class="hljs-ln-code"> class="hljs-ln-line"> ** 每个串口的函数:
    112. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="112"> class="hljs-ln-code"> class="hljs-ln-line"> ** 初始化 1个 波特率-None-8-1
    113. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="113"> class="hljs-ln-code"> class="hljs-ln-line"> ** 发送 3个 发送指定长度数据、字符串、AT指令
    114. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="114"> class="hljs-ln-code"> class="hljs-ln-line"> ** 接收 3个 获取字节数、获取数据、清0
    115. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="115"> class="hljs-ln-code"> class="hljs-ln-line">****************************************************************************/
    116. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="116"> class="hljs-ln-code"> class="hljs-ln-line">// UART1
    117. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="117"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_Init(uint32_t ulBaudrate); // 初始化串口1; GPIO引脚PA9+PA10、中断优先级、通信参数:波特率可设、8位数据、无校验、1个停止位
    118. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="118"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
    119. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="119"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    120. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="120"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART1_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    121. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="121"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART1_GetRxNum(void); // 获取接收到的最新一帧字节数
    122. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="122"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t * UART1_GetRxData(void); // 获取接收到的数据 (缓存的地址)
    123. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="123"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_ClearRx(void); // 清理接收到的数据 (清理最后一帧字节数,因为它是判断接收的标志)
    124. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="124"> class="hljs-ln-code"> class="hljs-ln-line">// UART2
    125. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="125"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_Init(uint32_t ulBaudrate); // 初始化串口2; GPIO引脚PA2+PA3、中断优先级、通信参数:波特率可设、8位数据、无校验、1个停止位
    126. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="126"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
    127. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="127"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    128. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="128"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART2_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    129. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="129"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART2_GetRxNum(void); // 获取接收到的最新一帧字节数
    130. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="130"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t * UART2_GetRxData(void); // 获取接收到的数据 (缓存的地址)
    131. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="131"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_ClearRx(void); // 清理接收到的数据 (清理最后一帧字节数,因为它是判断接收的标志)
    132. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="132"> class="hljs-ln-code"> class="hljs-ln-line">// UART3
    133. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="133"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_Init(uint32_t ulBaudrate); // 初始化串口3; GPIO引脚PB10+PB11、中断优先级、通信参数:波特率可设、8位数据、无校验、1个停止位
    134. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="134"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
    135. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="135"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    136. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="136"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART3_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    137. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="137"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART3_GetRxNum(void); // 获取接收到的最新一帧字节数
    138. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="138"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t * UART3_GetRxData(void); // 获取接收到的数据 (缓存的地址)
    139. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="139"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_ClearRx(void); // 清理接收到的数据 (清理最后一帧字节数,因为它是判断接收的标志)
    140. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="140"> class="hljs-ln-code"> class="hljs-ln-line">
    141. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="141"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef STM32F10X_HD
    142. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="142"> class="hljs-ln-code"> class="hljs-ln-line">// UART4
    143. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="143"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_Init(uint32_t ulBaudrate); // 初始化串口4; GPIO引脚PC10+PC11、中断优先级、通信参数:波特率可设、8位数据、无校验、1个停止位
    144. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="144"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
    145. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="145"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    146. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="146"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART4_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    147. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="147"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART4_GetRxNum(void); // 获取接收到的最新一帧字节数
    148. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="148"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t * UART4_GetRxData(void); // 获取接收到的数据 (缓存的地址)
    149. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="149"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_ClearRx(void); // 清理接收到的数据 (清理最后一帧字节数,因为它是判断接收的标志)
    150. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="150"> class="hljs-ln-code"> class="hljs-ln-line">// UART5
    151. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="151"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_Init(uint32_t ulBaudrate); // 初始化串口5; GPIO引脚PC12+PD2、中断优先级、通信参数:波特率可设、8位数据、无校验、1个停止位
    152. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="152"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
    153. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="153"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    154. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="154"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART5_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    155. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="155"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART5_GetRxNum(void); // 获取接收到的最新一帧字节数
    156. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="156"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t * UART5_GetRxData(void); // 获取接收到的数据 (缓存的地址)
    157. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="157"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_ClearRx(void); // 清理接收到的数据 (清理最后一帧字节数,因为它是判断接收的标志)
    158. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="158"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    159. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="159"> class="hljs-ln-code"> class="hljs-ln-line">
    160. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="160"> class="hljs-ln-code"> class="hljs-ln-line">// 辅助函数:Modbus_CRC16校验
    161. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="161"> class="hljs-ln-code"> class="hljs-ln-line">void Modbus_AddCRC16(uint8_t *_pcData, uint16_t _usLen); // 对数据追加2字节的ModbusCRC16校验值到末尾; 参数:原始数据、原始数据字节数; 注意:调用函数后,原始数据会增加2字节
    162. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="162"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t Modbus_CheckCRC16(uint8_t *_pcData, uint16_t _usLen); // 对带ModbusCRC16校验的数据段进行校验; 返回:0-错误、1-匹配正确;
    163. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="163"> class="hljs-ln-code"> class="hljs-ln-line">
    164. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="164"> class="hljs-ln-code"> class="hljs-ln-line">
    165. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="165"> class="hljs-ln-code"> class="hljs-ln-line">
    166. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="166"> class="hljs-ln-code"> class="hljs-ln-line">
    167. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="167"> class="hljs-ln-code"> class="hljs-ln-line">
    168. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="168"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    169. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="169"> class="hljs-ln-code"> class="hljs-ln-line">
    170. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="170"> class="hljs-ln-code"> class="hljs-ln-line">// 文件结尾,需要保留至少1空行
    class="hide-preCode-box"> class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}" onclick="hljs.signin(event)">

    2、bsp_UART.c

    1. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1"> class="hljs-ln-code"> class="hljs-ln-line">/***********************************************************************************************************************************
    2. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="2"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【代码编写】 魔女开发板团队
    3. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="3"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【代码更新】 2024-10-23-1
    4. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="4"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【淘 宝】 魔女开发板 https://demoboard.taobao.com
    5. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="5"> class="hljs-ln-code"> class="hljs-ln-line"> ***********************************************************************************************************************************
    6. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="6"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【文件名称】 bsp_UART.c
    7. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="7"> class="hljs-ln-code"> class="hljs-ln-line"> **
    8. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="8"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【文件功能】 各USART的GPIO配置、通信协议配置、中断配置,及功能函数实现
    9. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="9"> class="hljs-ln-code"> class="hljs-ln-line"> **
    10. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="10"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【适用平台】 STM32F103 + keil5 + HAL库/标准库
    11. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="11"> class="hljs-ln-code"> class="hljs-ln-line"> **
    12. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="12"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【特别说明】 1、为什么这个文件中,同时有标准库、HAL库的代码?
    13. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="13"> class="hljs-ln-code"> class="hljs-ln-line"> ** 是为了方便移植!已做好预编译处理,可适用于标准库、HAL库的工程移植。
    14. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="14"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2、为什么UART初始化、中断,用寄存器操作,而不是用更有可读性的HAL库?
    15. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="15"> class="hljs-ln-code"> class="hljs-ln-line"> ** 因为CubeMX配置工程时,如果不对UART进行配置,且打勾只生成需要的文件,工程中将没有UART的HAL支持文件的。
    16. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="16"> class="hljs-ln-code"> class="hljs-ln-line"> ** 另外, 中断里用寄存器操作,与HAL库的重重封装相比,明显地更高效!
    17. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="17"> class="hljs-ln-code"> class="hljs-ln-line">************************************************************************************************************************************/
    18. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="18"> class="hljs-ln-code"> class="hljs-ln-line">#include "bsp_UART.h" // 引用头文件
    19. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="19"> class="hljs-ln-code"> class="hljs-ln-line">
    20. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="20"> class="hljs-ln-code"> class="hljs-ln-line">
    21. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="21"> class="hljs-ln-code"> class="hljs-ln-line">
    22. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="22"> class="hljs-ln-code"> class="hljs-ln-line">
    23. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="23"> class="hljs-ln-code"> class="hljs-ln-line">
    24. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="24"> class="hljs-ln-code"> class="hljs-ln-line">
    25. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="25"> class="hljs-ln-code"> class="hljs-ln-line">/*****************************************************************************
    26. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="26"> class="hljs-ln-code"> class="hljs-ln-line"> ** 声明本地变量
    27. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="27"> class="hljs-ln-code"> class="hljs-ln-line">****************************************************************************/
    28. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="28"> class="hljs-ln-code"> class="hljs-ln-line">typedef struct
    29. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="29"> class="hljs-ln-code"> class="hljs-ln-line">{
    30. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="30"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t usRxNum; // 新一帧数据,接收到多少个字节数据
    31. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="31"> class="hljs-ln-code"> class="hljs-ln-line"> uint8_t *puRxData; // 新一帧数据,数据缓存; 存放的是空闲中断后,从临时接收缓存复制过来的完整数据,并非接收过程中的不完整数据;
    32. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="32"> class="hljs-ln-code"> class="hljs-ln-line">
    33. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="33"> class="hljs-ln-code"> class="hljs-ln-line"> uint8_t *puTxFiFoData; // 发送缓冲区,环形队列; 为了方便理解阅读,没有封装成队列函数
    34. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="34"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t usTxFiFoData ; // 环形缓冲区的队头
    35. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="35"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t usTxFiFoTail ; // 环形缓冲区的队尾
    36. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="36"> class="hljs-ln-code"> class="hljs-ln-line">} xUSATR_TypeDef;
    37. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="37"> class="hljs-ln-code"> class="hljs-ln-line">
    38. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="38"> class="hljs-ln-code"> class="hljs-ln-line">
    39. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="39"> class="hljs-ln-code"> class="hljs-ln-line">
    40. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="40"> class="hljs-ln-code"> class="hljs-ln-line">#if 0
    41. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="41"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    42. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="42"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: delay_us
    43. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="43"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: ms 延时函数
    44. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="44"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 1、系统时钟72MHz
    45. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="45"> class="hljs-ln-code"> class="hljs-ln-line"> * 2、打勾:Options/ c++ / One ELF Section per Function
    46. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="46"> class="hljs-ln-code"> class="hljs-ln-line"> 3、编译优化级别:Level 3(-O3)
    47. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="47"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t us 微秒值
    48. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="48"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    49. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="49"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    50. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="50"> class="hljs-ln-code"> class="hljs-ln-line">static volatile uint32_t ulTimesUS; // 使用volatile声明,防止变量被编译器优化
    51. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="51"> class="hljs-ln-code"> class="hljs-ln-line">static void delay_us(uint16_t us)
    52. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="52"> class="hljs-ln-code"> class="hljs-ln-line">{
    53. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="53"> class="hljs-ln-code"> class="hljs-ln-line"> ulTimesUS = us ;
    54. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="54"> class="hljs-ln-code"> class="hljs-ln-line"> while (ulTimesUS)
    55. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="55"> class="hljs-ln-code"> class="hljs-ln-line"> ulTimesUS--; // 操作外部变量,防止空循环被编译器优化掉
    56. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="56"> class="hljs-ln-code"> class="hljs-ln-line">}
    57. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="57"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    58. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="58"> class="hljs-ln-code"> class="hljs-ln-line">
    59. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="59"> class="hljs-ln-code"> class="hljs-ln-line">
    60. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="60"> class="hljs-ln-code"> class="hljs-ln-line">
    61. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="61"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    62. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="62"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: delay_ms
    63. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="63"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: ms 延时函数
    64. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="64"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 1、系统时钟72MHz
    65. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="65"> class="hljs-ln-code"> class="hljs-ln-line"> * 2、打勾:Options/ c++ / One ELF Section per Function
    66. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="66"> class="hljs-ln-code"> class="hljs-ln-line"> 3、编译优化级别:Level 3(-O3)
    67. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="67"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ms 毫秒值
    68. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="68"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    69. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="69"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    70. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="70"> class="hljs-ln-code"> class="hljs-ln-line">static volatile uint32_t ulTimesMS; // 使用volatile声明,防止变量被编译器优化
    71. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="71"> class="hljs-ln-code"> class="hljs-ln-line">static void delay_ms(uint16_t ms)
    72. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="72"> class="hljs-ln-code"> class="hljs-ln-line">{
    73. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="73"> class="hljs-ln-code"> class="hljs-ln-line"> ulTimesMS = ms * 5500;
    74. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="74"> class="hljs-ln-code"> class="hljs-ln-line"> while (ulTimesMS)
    75. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="75"> class="hljs-ln-code"> class="hljs-ln-line"> ulTimesMS--; // 操作外部变量,防止空循环被编译器优化掉
    76. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="76"> class="hljs-ln-code"> class="hljs-ln-line">}
    77. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="77"> class="hljs-ln-code"> class="hljs-ln-line">
    78. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="78"> class="hljs-ln-code"> class="hljs-ln-line">
    79. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="79"> class="hljs-ln-code"> class="hljs-ln-line">
    80. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="80"> class="hljs-ln-code"> class="hljs-ln-line">// USART-1 //
    81. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="81"> class="hljs-ln-code"> class="hljs-ln-line">/
    82. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="82"> class="hljs-ln-code"> class="hljs-ln-line">#if UART1_EN
    83. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="83"> class="hljs-ln-code"> class="hljs-ln-line">
    84. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="84"> class="hljs-ln-code"> class="hljs-ln-line">static xUSATR_TypeDef xUART1 = { 0 }; // 定义 UART1 的收发结构体
    85. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="85"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUART1RxData[UART1_RX_BUF_SIZE]; // 定义 UART1 的接收缓存
    86. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="86"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUART1TxFiFoData[UART1_TX_BUF_SIZE]; // 定义 UART1 的发送缓存
    87. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="87"> class="hljs-ln-code"> class="hljs-ln-line">
    88. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="88"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    89. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="89"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_Init
    90. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="90"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 初始化USART1的通信引脚、协议参数、中断优先级
    91. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="91"> class="hljs-ln-code"> class="hljs-ln-line"> * 引脚:TX-PA10、RX-PA11
    92. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="92"> class="hljs-ln-code"> class="hljs-ln-line"> * 协议:波特率-None-8-1
    93. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="93"> class="hljs-ln-code"> class="hljs-ln-line"> * 发送:发送中断
    94. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="94"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收:接收+空闲中断
    95. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="95"> class="hljs-ln-code"> class="hljs-ln-line"> *
    96. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="96"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ulBaudrate 通信波特率
    97. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="97"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    98. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="98"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    99. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="99"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_Init(uint32_t ulBaudrate)
    100. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="100"> class="hljs-ln-code"> class="hljs-ln-line">{
    101. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="101"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    102. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="102"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    103. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="103"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能GPIOA
    104. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="104"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 使能USART1
    105. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="105"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 UART
    106. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="106"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); // 使能重置
    107. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="107"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); // 取消重置
    108. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="108"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    109. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="109"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStructure = {0}; // GPIO 初始化结构体
    110. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="110"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // TX 引脚
    111. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="111"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 引脚功能
    112. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="112"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 输出速度
    113. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="113"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化引脚
    114. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="114"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    115. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="115"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // RX 引脚
    116. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="116"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // RX引脚工作模式:上拉输入; 如果使用浮空输入,引脚空置时可能产生误输入; 当电路上为一主多从电路时,可以使用复用开漏模式
    117. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="117"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化引脚
    118. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="118"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 USART
    119. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="119"> class="hljs-ln-code"> class="hljs-ln-line"> USART_DeInit(USART1); // 复位串口
    120. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="120"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitTypeDef USART_InitStructure = {0}; // UART配置结构体
    121. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="121"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_BaudRate = ulBaudrate; // 波特率
    122. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="122"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 位长
    123. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="123"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位
    124. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="124"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Parity = USART_Parity_No; // 奇偶校验
    125. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="125"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 硬件流控制
    126. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="126"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 使能工作模式
    127. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="127"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Init(USART1, &USART_InitStructure); // 初始化串口
    128. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="128"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    129. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="129"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART1, USART_IT_TXE, DISABLE); // 发送中断 使能
    130. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="130"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 接收中断 使能
    131. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="131"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); // 空闲中断 使能
    132. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="132"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    133. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="133"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitTypeDef NVIC_InitStructure = {0}; // 优先级配置结构体
    134. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="134"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannel = USART1_IRQn; // 中断编号
    135. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="135"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 0 ; // 抢占优先级
    136. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="136"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelSubPriority = 0; // 子优先级
    137. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="137"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
    138. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="138"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_Init(&NVIC_InitStructure); // 初始化中断优先级
    139. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="139"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,打开串口
    140. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="140"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Cmd(USART1, ENABLE); // 配置完成,打开串口, 开始工作
    141. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="141"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    142. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="142"> class="hljs-ln-code"> class="hljs-ln-line">
    143. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="143"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER
    144. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="144"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    145. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="145"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOA_CLK_ENABLE(); // 使能GPIOA
    146. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="146"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART1_CLK_ENABLE(); // 使能USART1
    147. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="147"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 UART
    148. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="148"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART1_FORCE_RESET(); // 使能重置
    149. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="149"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART1_RELEASE_RESET(); // 取消重置
    150. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="150"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    151. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="151"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStruct = {0}; // 声明初始化要用到的结构体
    152. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="152"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_9 ; // 引脚 TX-PA9
    153. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="153"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // 工作模式
    154. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="154"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_NOPULL; // 上下拉
    155. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="155"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // 引脚速率
    156. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="156"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // 初始化引脚工作模式
    157. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="157"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    158. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="158"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_10; // 引脚 RX-PA10
    159. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="159"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // 工作模式
    160. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="160"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_PULLUP; // 上下拉
    161. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="161"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // 引脚速率
    162. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="162"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // 初始化引脚工作模式
    163. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="163"> class="hljs-ln-code"> class="hljs-ln-line"> // 计算波特率参数
    164. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="164"> class="hljs-ln-code"> class="hljs-ln-line"> float temp;
    165. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="165"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t mantissa, fraction;
    166. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="166"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新系统运行频率全局值; 函数SystemCoreClock( ),在标准库、HAL库通用
    167. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="167"> class="hljs-ln-code"> class="hljs-ln-line"> temp = (float)SystemCoreClock / (ulBaudrate * 16); // 波特率公式计算; USART1挂载在APB2, 时钟为系统时钟的1分频; 全局变量SystemCoreClock,在标准库、HAL库通用;
    168. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="168"> class="hljs-ln-code"> class="hljs-ln-line"> mantissa = temp; // 整数部分
    169. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="169"> class="hljs-ln-code"> class="hljs-ln-line"> fraction = (temp - mantissa) * 16; // 小数部分
    170. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="170"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> BRR = mantissa << 4 | fraction; // 设置波特率
    171. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="171"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    172. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="172"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 = 0; // 清0
    173. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="173"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x01 << 2; // 接收使能[02]: 0=失能、1=使能
    174. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="174"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x01 << 3; // 发送使能[03]:0=失能、1=使能
    175. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="175"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x00 << 9; // 奇偶校验[09]:0=偶Even、1=奇Odd; 注意:使用奇偶校验,下面两项要置1
    176. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="176"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x00 << 10; // 校验位 [10]:0=禁用、1=使能; 注意,使用奇偶校验,此位要置1 (否则无效、数据错乱)
    177. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="177"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x00 << 12; // 数据位 [12]:0=8位、 1=9位; 注意:使用奇偶校验,此位要置1 (否则数据会发生错乱)
    178. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="178"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR2 = 0; // 数据清0
    179. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="179"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR2 &= ~(0x03 << 12); // 停止位[13:12]:00b=1个停止位、01b=0.5个停止位、10b=2个停止位、11b=1.5个停止位
    180. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="180"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR3 = 0; // 数据清0
    181. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="181"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR3 &= ~(0x01 << 6); // DMA接收[6]: 0=禁止、1=使能
    182. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="182"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR3 &= ~(0x01 << 7); // DMA发送[7]: 0=禁止、1=使能
    183. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="183"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    184. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="184"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 &= ~(0x01 << 7); // 关闭发送中断
    185. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="185"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x01 << 5; // 使能接收中断: 接收缓冲区非空
    186. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="186"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x01 << 4; // 使能空闲中断:超过1字节时间没收到新数据
    187. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="187"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> SR = ~(0x00F0); // 清理中断标志
    188. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="188"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    189. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="189"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); // 设置指定中断的优先级; 参数:中断请求编号、抢占级、子优先级
    190. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="190"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_EnableIRQ(USART1_IRQn); // 使能、启用指定的中断
    191. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="191"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,打开串口
    192. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="192"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x01 << 13; // 使能UART开始工作
    193. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="193"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    194. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="194"> class="hljs-ln-code"> class="hljs-ln-line">
    195. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="195"> class="hljs-ln-code"> class="hljs-ln-line"> // 关联缓冲区
    196. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="196"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.puRxData = uaUART1RxData; // 关联接收缓冲区的地址
    197. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="197"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.puTxFiFoData = uaUART1TxFiFoData; // 关联发送缓冲区的地址
    198. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="198"> class="hljs-ln-code"> class="hljs-ln-line">
    199. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="199"> class="hljs-ln-code"> class="hljs-ln-line"> // 输出提示
    200. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="200"> class="hljs-ln-code"> class="hljs-ln-line"> printf("\r\r\r=========== STM32F103 外设 初始化报告 ===========\r"); // 输出到串口助手
    201. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="201"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新一下系统运行频率变量
    202. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="202"> class="hljs-ln-code"> class="hljs-ln-line"> printf("系统时钟频率 %d MHz\r", SystemCoreClock / 1000000); // 输出到串口助手
    203. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="203"> class="hljs-ln-code"> class="hljs-ln-line"> printf("UART1 初始化配置 %d-None-8-1; 已完成初始化配置、收发配置\r", ulBaudrate); // 输出到串口助手
    204. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="204"> class="hljs-ln-code"> class="hljs-ln-line">}
    205. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="205"> class="hljs-ln-code"> class="hljs-ln-line">
    206. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="206"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    207. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="207"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: USART1_IRQHandler
    208. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="208"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: USART1的接收中断、空闲中断、发送中断
    209. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="209"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    210. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="210"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    211. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="211"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数,当产生中断事件时,由硬件调用。
    212. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="212"> class="hljs-ln-code"> class="hljs-ln-line"> * 如果使用本文件代码,在工程文件的其它地方,要注释同名函数,否则冲突。
    213. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="213"> class="hljs-ln-code"> class="hljs-ln-line">******************************************************************************/
    214. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="214"> class="hljs-ln-code"> class="hljs-ln-line">void USART1_IRQHandler(void)
    215. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="215"> class="hljs-ln-code"> class="hljs-ln-line">{
    216. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="216"> class="hljs-ln-code"> class="hljs-ln-line"> static uint16_t cnt = 0; // 接收字节数累计:每一帧数据已接收到的字节数
    217. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="217"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t rxTemp[UART1_RX_BUF_SIZE]; // 接收数据缓存数组:每新接收1个字节,先顺序存放到这里,当一帧接收完(发生空闲中断), 再转存到外部缓存:xUARTx.puRxData[ ]
    218. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="218"> class="hljs-ln-code"> class="hljs-ln-line">
    219. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="219"> class="hljs-ln-code"> class="hljs-ln-line"> // 发送中断:用于把环形缓冲的数据,逐字节发出
    220. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="220"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART1->SR & USART_SR_TXE) && (USART1->CR1 & USART_CR1_TXEIE)) // 检查发送寄存器空中断使能,且发送寄存器为空; TXE(发送数据寄存器空)、TXEIE(发送缓冲区空中断使能)
    221. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="221"> class="hljs-ln-code"> class="hljs-ln-line"> {
    222. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="222"> class="hljs-ln-code"> class="hljs-ln-line"> USART1->DR = xUART1.puTxFiFoData[xUART1.usTxFiFoTail++]; // 从FIFO队列中取出一个数据,放入USART的发送寄存器(硬件会自动发出),然后将FIFO的尾指针递增,指向下一个要发送的数据
    223. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="223"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART1.usTxFiFoTail == UART1_TX_BUF_SIZE) // 检查FIFO尾指针是否到达了FIFO队列的末尾
    224. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="224"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.usTxFiFoTail = 0; // 将尾指针重置为0,实现环形队列的功能
    225. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="225"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART1.usTxFiFoTail == xUART1.usTxFiFoData) // 检查FIFO尾指针是否追上了头指针,即所有数据是否都已发送完毕
    226. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="226"> class="hljs-ln-code"> class="hljs-ln-line"> USART1->CR1 &= ~USART_CR1_TXEIE; // 关闭发送寄存器空中断,防止中断服务程序被不必要地调用
    227. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="227"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    228. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="228"> class="hljs-ln-code"> class="hljs-ln-line"> }
    229. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="229"> class="hljs-ln-code"> class="hljs-ln-line">
    230. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="230"> class="hljs-ln-code"> class="hljs-ln-line"> // 接收中断:用于逐个字节接收,存放到临时缓存
    231. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="231"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART1->SR & USART_SR_RXNE) // 检查RXNE(读数据寄存器非空标志位); RXNE中断清理方法:读DR时自动清理;
    232. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="232"> class="hljs-ln-code"> class="hljs-ln-line"> {
    233. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="233"> class="hljs-ln-code"> class="hljs-ln-line"> if (cnt == UART1_RX_BUF_SIZE) // 当前帧已接收的字节量,已满缓存区的大小; 为避免溢出,本包后面接收到的数据直接舍弃;
    234. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="234"> class="hljs-ln-code"> class="hljs-ln-line"> {
    235. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="235"> class="hljs-ln-code"> class="hljs-ln-line"> printf("警告:UART1单帧接收量,已超出接收缓存大小!\r修改文件bsp_UART.h的UART1_RX_BUF_SIZE值,可解决此问题!\r");
    236. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="236"> class="hljs-ln-code"> class="hljs-ln-line"> USART1->DR; // 读取数据寄存器的数据,但不保存.主要作用:读DR时自动清理接收中断标志;
    237. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="237"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    238. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="238"> class="hljs-ln-code"> class="hljs-ln-line"> }
    239. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="239"> class="hljs-ln-code"> class="hljs-ln-line"> rxTemp[cnt++] = USART1->DR ; // 把新收到的字节数据,顺序存放到RXTemp数组中;注意:读取DR时自动清零中断位;
    240. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="240"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    241. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="241"> class="hljs-ln-code"> class="hljs-ln-line"> }
    242. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="242"> class="hljs-ln-code"> class="hljs-ln-line">
    243. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="243"> class="hljs-ln-code"> class="hljs-ln-line"> // 空闲中断:用于判断一帧数据结束,整理数据到外部备读
    244. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="244"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART1->SR & USART_SR_IDLE) // 检查IDLE(空闲中断标志位); IDLE中断标志清理方法:序列清零,USART1 ->SR; USART1 ->DR;
    245. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="245"> class="hljs-ln-code"> class="hljs-ln-line"> {
    246. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="246"> class="hljs-ln-code"> class="hljs-ln-line"> memcpy(xUART1.puRxData, rxTemp, UART1_RX_BUF_SIZE); // 把本帧接收到的数据,存入到结构体的数组成员xUARTx.puRxData中, 等待处理; 注意:复制的是整个数组,包括0值,以方便字符串输出时尾部以0作字符串结束符
    247. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="247"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.usRxNum = cnt; // 把接收到的字节数,存入到结构体变量xUARTx.usRxNum中;
    248. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="248"> class="hljs-ln-code"> class="hljs-ln-line"> cnt = 0; // 接收字节数累计器,清零; 准备下一次的接收
    249. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="249"> class="hljs-ln-code"> class="hljs-ln-line"> memset(rxTemp, 0, UART1_RX_BUF_SIZE); // 接收数据缓存数组,清零; 准备下一次的接收
    250. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="250"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 ->SR;
    251. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="251"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 ->DR; // 清零IDLE中断标志位!! 序列清零,顺序不能错!!
    252. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="252"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    253. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="253"> class="hljs-ln-code"> class="hljs-ln-line"> }
    254. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="254"> class="hljs-ln-code"> class="hljs-ln-line">
    255. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="255"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    256. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="256"> class="hljs-ln-code"> class="hljs-ln-line">}
    257. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="257"> class="hljs-ln-code"> class="hljs-ln-line">
    258. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="258"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    259. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="259"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_SendData
    260. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="260"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过中断发送数据
    261. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="261"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】本函数可发送各种数据,而不限于字符串,如int,char
    262. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="262"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】注意h文件中所定义的发缓冲区大小、注意数据压入缓冲区的速度与串口发送速度的冲突
    263. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="263"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* puData 需发送数据的地址
    264. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="264"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usNum 发送的字节数 ,数量受限于h文件中设置的发送缓存区大小宏定义
    265. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="265"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    266. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="266"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    267. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="267"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendData(uint8_t *puData, uint16_t usNum)
    268. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="268"> class="hljs-ln-code"> class="hljs-ln-line">{
    269. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="269"> class="hljs-ln-code"> class="hljs-ln-line"> for (uint16_t i = 0; i < usNum; i++) // 把数据放入环形缓冲区
    270. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="270"> class="hljs-ln-code"> class="hljs-ln-line"> {
    271. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="271"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.puTxFiFoData[xUART1.usTxFiFoData++] = puData[i]; // 把字节放到缓冲区最后的位置,然后指针后移
    272. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="272"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART1.usTxFiFoData == UART1_TX_BUF_SIZE) // 如果指针位置到达缓冲区的最大值,则归0
    273. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="273"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.usTxFiFoData = 0;
    274. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="274"> class="hljs-ln-code"> class="hljs-ln-line"> } // 为了方便阅读理解,这里没有把此部分封装成队列函数,可以自行封装
    275. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="275"> class="hljs-ln-code"> class="hljs-ln-line">
    276. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="276"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART1->CR1 & USART_CR1_TXEIE) == 0) // 检查USART寄存器的发送缓冲区空置中断(TXEIE)是否已打开
    277. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="277"> class="hljs-ln-code"> class="hljs-ln-line"> USART1->CR1 |= USART_CR1_TXEIE; // 打开TXEIE中断
    278. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="278"> class="hljs-ln-code"> class="hljs-ln-line">}
    279. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="279"> class="hljs-ln-code"> class="hljs-ln-line">
    280. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="280"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    281. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="281"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_SendString
    282. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="282"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送字符串
    283. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="283"> class="hljs-ln-code"> class="hljs-ln-line"> * 用法请参考printf,及示例中的展示
    284. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="284"> class="hljs-ln-code"> class="hljs-ln-line"> * 注意,最大发送字节数为512-1个字符,可在函数中修改上限
    285. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="285"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: const char *pcString, ... (如同printf的用法)
    286. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="286"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    287. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="287"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    288. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="288"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendString(const char *pcString, ...)
    289. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="289"> class="hljs-ln-code"> class="hljs-ln-line">{
    290. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="290"> class="hljs-ln-code"> class="hljs-ln-line"> char mBuffer[512] = {0};; // 开辟一个缓存, 并把里面的数据置0
    291. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="291"> class="hljs-ln-code"> class="hljs-ln-line"> va_list ap; // 新建一个可变参数列表
    292. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="292"> class="hljs-ln-code"> class="hljs-ln-line"> va_start(ap, pcString); // 列表指向第一个可变参数
    293. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="293"> class="hljs-ln-code"> class="hljs-ln-line"> vsnprintf(mBuffer, 512, pcString, ap); // 把所有参数,按格式,输出到缓存; 参数2用于限制发送的最大字节数,如果达到上限,则只发送上限值-1; 最后1字节自动置'\0'
    294. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="294"> class="hljs-ln-code"> class="hljs-ln-line"> va_end(ap); // 清空可变参数列表
    295. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="295"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_SendData((uint8_t *)mBuffer, strlen(mBuffer)); // 把字节存放环形缓冲,排队准备发送
    296. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="296"> class="hljs-ln-code"> class="hljs-ln-line">}
    297. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="297"> class="hljs-ln-code"> class="hljs-ln-line">
    298. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="298"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    299. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="299"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_SendAT
    300. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="300"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送AT命令, 并等待返回信息
    301. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="301"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char *pcString AT指令字符串
    302. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="302"> class="hljs-ln-code"> class="hljs-ln-line"> * char *pcAckString 期待的指令返回信息字符串
    303. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="303"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usTimeOut 发送命令后等待的时间,毫秒
    304. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="304"> class="hljs-ln-code"> class="hljs-ln-line"> *
    305. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="305"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回 值: 0-执行失败、1-执行正常
    306. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="306"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    307. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="307"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART1_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs)
    308. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="308"> class="hljs-ln-code"> class="hljs-ln-line">{
    309. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="309"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_ClearRx(); // 清0
    310. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="310"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_SendString(pcAT); // 发送AT指令字符串
    311. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="311"> class="hljs-ln-code"> class="hljs-ln-line">
    312. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="312"> class="hljs-ln-code"> class="hljs-ln-line"> while (usTimeOutMs--) // 判断是否起时(这里只作简单的循环判断次数处理)
    313. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="313"> class="hljs-ln-code"> class="hljs-ln-line"> {
    314. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="314"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART1_GetRxNum()) // 判断是否接收到数据
    315. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="315"> class="hljs-ln-code"> class="hljs-ln-line"> {
    316. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="316"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_ClearRx(); // 清0接收字节数; 注意:接收到的数据内容 ,是没有被清0的
    317. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="317"> class="hljs-ln-code"> class="hljs-ln-line"> if (strstr((char *)UART1_GetRxData(), pcAckString)) // 判断返回数据中是否有期待的字符
    318. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="318"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 返回:0-超时没有返回、1-正常返回期待值
    319. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="319"> class="hljs-ln-code"> class="hljs-ln-line"> }
    320. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="320"> class="hljs-ln-code"> class="hljs-ln-line"> delay_ms(1); // 延时; 用于超时退出处理,避免死等
    321. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="321"> class="hljs-ln-code"> class="hljs-ln-line"> }
    322. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="322"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 返回:0-超时、返回异常,1-正常返回期待值
    323. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="323"> class="hljs-ln-code"> class="hljs-ln-line">}
    324. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="324"> class="hljs-ln-code"> class="hljs-ln-line">
    325. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="325"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    326. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="326"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_SendStringForDMA
    327. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="327"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过DMA发送数据,省了占用中断的时间
    328. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="328"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】字符串,字节数非常多,
    329. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="329"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】1:只适合发送字符串,不适合发送可能含0的数值类数据; 2-时间间隔要足够
    330. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="330"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char strintTemp 要发送的字符串首地址
    331. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="331"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    332. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="332"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数为保留函数,留作用户参考。为了方便移植,本文件对外不再使用本函数。
    333. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="333"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    334. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="334"> class="hljs-ln-code"> class="hljs-ln-line">#if 0
    335. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="335"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendStringForDMA(char *stringTemp)
    336. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="336"> class="hljs-ln-code"> class="hljs-ln-line">{
    337. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="337"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t Flag_DmaTxInit = 0; // 用于标记是否已配置DMA发送
    338. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="338"> class="hljs-ln-code"> class="hljs-ln-line"> uint32_t num = 0; // 发送的数量,注意发送的单位不是必须8位的
    339. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="339"> class="hljs-ln-code"> class="hljs-ln-line"> char *t = stringTemp ; // 用于配合计算发送的数量
    340. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="340"> class="hljs-ln-code"> class="hljs-ln-line">
    341. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="341"> class="hljs-ln-code"> class="hljs-ln-line"> while (*t++ != 0) num++; // 计算要发送的数目,这步比较耗时,测试发现每多6个字节,增加1us,单位:8位
    342. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="342"> class="hljs-ln-code"> class="hljs-ln-line">
    343. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="343"> class="hljs-ln-code"> class="hljs-ln-line"> while (DMA1_Channel4->CNDTR > 0); // 重要:如果DMA还在进行上次发送,就等待; 得进完成中断清标志,F4不用这么麻烦,发送完后EN自动清零
    344. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="344"> class="hljs-ln-code"> class="hljs-ln-line"> if (Flag_DmaTxInit == 0) // 是否已进行过USAART_TX的DMA传输配置
    345. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="345"> class="hljs-ln-code"> class="hljs-ln-line"> {
    346. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="346"> class="hljs-ln-code"> class="hljs-ln-line"> Flag_DmaTxInit = 1; // 设置标记,下次调用本函数就不再进行配置了
    347. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="347"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 ->CR3 |= 1 << 7; // 使能DMA发送
    348. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="348"> class="hljs-ln-code"> class="hljs-ln-line"> RCC->AHBENR |= 1 << 0; // 开启DMA1时钟 [0]DMA1 [1]DMA2
    349. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="349"> class="hljs-ln-code"> class="hljs-ln-line">
    350. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="350"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR = 0; // 失能, 清0整个寄存器, DMA必须失能才能配置
    351. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="351"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CNDTR = num; // 传输数据量
    352. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="352"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CMAR = (uint32_t)stringTemp; // 存储器地址
    353. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="353"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CPAR = (uint32_t)&USART1->DR; // 外设地址
    354. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="354"> class="hljs-ln-code"> class="hljs-ln-line">
    355. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="355"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 1 << 4; // 数据传输方向 0:从外设读 1:从存储器读
    356. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="356"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 0 << 5; // 循环模式 0:不循环 1:循环
    357. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="357"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 0 << 6; // 外设地址非增量模式
    358. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="358"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 1 << 7; // 存储器增量模式
    359. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="359"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 0 << 8; // 外设数据宽度为8位
    360. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="360"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 0 << 10; // 存储器数据宽度8位
    361. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="361"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 0 << 12; // 中等优先级
    362. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="362"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 0 << 14; // 非存储器到存储器模式
    363. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="363"> class="hljs-ln-code"> class="hljs-ln-line"> }
    364. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="364"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR &= ~((uint32_t)(1 << 0)); // 失能,DMA必须失能才能配置
    365. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="365"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CNDTR = num; // 传输数据量
    366. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="366"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CMAR = (uint32_t)stringTemp; // 存储器地址
    367. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="367"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 1 << 0; // 开启DMA传输
    368. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="368"> class="hljs-ln-code"> class="hljs-ln-line">}
    369. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="369"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    370. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="370"> class="hljs-ln-code"> class="hljs-ln-line">
    371. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="371"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    372. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="372"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_GetRxNum
    373. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="373"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据的字节数
    374. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="374"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    375. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="375"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 0=没有接收到数据,非0=新一帧数据的字节数
    376. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="376"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    377. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="377"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART1_GetRxNum(void)
    378. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="378"> class="hljs-ln-code"> class="hljs-ln-line">{
    379. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="379"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART1.usRxNum ;
    380. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="380"> class="hljs-ln-code"> class="hljs-ln-line">}
    381. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="381"> class="hljs-ln-code"> class="hljs-ln-line">
    382. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="382"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    383. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="383"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_GetRxData
    384. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="384"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据 (数据的地址)
    385. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="385"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    386. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="386"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 缓存地址(uint8_t*)
    387. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="387"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    388. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="388"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t *UART1_GetRxData(void)
    389. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="389"> class="hljs-ln-code"> class="hljs-ln-line">{
    390. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="390"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART1.puRxData ;
    391. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="391"> class="hljs-ln-code"> class="hljs-ln-line">}
    392. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="392"> class="hljs-ln-code"> class="hljs-ln-line">
    393. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="393"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    394. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="394"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_ClearRx
    395. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="395"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 清理最后一帧数据的缓存
    396. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="396"> class="hljs-ln-code"> class="hljs-ln-line"> * 主要是清0字节数,因为它是用来判断接收的标准
    397. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="397"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    398. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="398"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    399. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="399"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    400. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="400"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_ClearRx(void)
    401. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="401"> class="hljs-ln-code"> class="hljs-ln-line">{
    402. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="402"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.usRxNum = 0 ;
    403. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="403"> class="hljs-ln-code"> class="hljs-ln-line">}
    404. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="404"> class="hljs-ln-code"> class="hljs-ln-line">#endif // endif UART1_EN
    405. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="405"> class="hljs-ln-code"> class="hljs-ln-line">
    406. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="406"> class="hljs-ln-code"> class="hljs-ln-line">
    407. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="407"> class="hljs-ln-code"> class="hljs-ln-line">
    408. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="408"> class="hljs-ln-code"> class="hljs-ln-line">
    409. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="409"> class="hljs-ln-code"> class="hljs-ln-line">
    410. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="410"> class="hljs-ln-code"> class="hljs-ln-line">// USART-2 //
    411. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="411"> class="hljs-ln-code"> class="hljs-ln-line">/
    412. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="412"> class="hljs-ln-code"> class="hljs-ln-line">#if UART2_EN
    413. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="413"> class="hljs-ln-code"> class="hljs-ln-line">
    414. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="414"> class="hljs-ln-code"> class="hljs-ln-line">static xUSATR_TypeDef xUART2 = { 0 }; // 定义 UART2 的收发结构体
    415. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="415"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUART2RxData[UART2_RX_BUF_SIZE]; // 定义 UART2 的接收缓存
    416. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="416"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUART2TxFiFoData[UART2_TX_BUF_SIZE]; // 定义 UART2 的发送缓存
    417. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="417"> class="hljs-ln-code"> class="hljs-ln-line">
    418. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="418"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    419. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="419"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_Init
    420. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="420"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 初始化USART2的通信引脚、协议参数、中断优先级
    421. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="421"> class="hljs-ln-code"> class="hljs-ln-line"> * 引脚:TX-PA2、RX-PA3
    422. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="422"> class="hljs-ln-code"> class="hljs-ln-line"> * 协议:波特率-None-8-1
    423. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="423"> class="hljs-ln-code"> class="hljs-ln-line"> * 发送:发送中断
    424. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="424"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收:接收+空闲中断
    425. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="425"> class="hljs-ln-code"> class="hljs-ln-line"> *
    426. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="426"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ulBaudrate 通信波特率
    427. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="427"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    428. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="428"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    429. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="429"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_Init(uint32_t ulBaudrate)
    430. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="430"> class="hljs-ln-code"> class="hljs-ln-line">{
    431. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="431"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    432. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="432"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    433. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="433"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能GPIOA
    434. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="434"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // 使能USART2
    435. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="435"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 UART
    436. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="436"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE); // 使能重置
    437. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="437"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE); // 取消重置
    438. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="438"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    439. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="439"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStructure = {0}; // GPIO 初始化结构体
    440. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="440"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // TX 引脚
    441. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="441"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 引脚功能
    442. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="442"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 输出速度
    443. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="443"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化引脚
    444. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="444"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    445. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="445"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // RX 引脚
    446. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="446"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // RX引脚工作模式:上拉输入; 如果使用浮空输入,引脚空置时可能产生误输入; 当电路上为一主多从电路时,可以使用复用开漏模式
    447. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="447"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化引脚
    448. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="448"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 USART
    449. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="449"> class="hljs-ln-code"> class="hljs-ln-line"> USART_DeInit(USART2); // 复位串口
    450. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="450"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitTypeDef USART_InitStructure = {0}; // UART配置结构体
    451. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="451"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_BaudRate = ulBaudrate; // 波特率
    452. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="452"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 位长
    453. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="453"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位
    454. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="454"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Parity = USART_Parity_No; // 奇偶校验
    455. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="455"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 硬件流控制
    456. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="456"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 使能工作模式
    457. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="457"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Init(USART2, &USART_InitStructure); // 初始化串口
    458. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="458"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    459. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="459"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART2, USART_IT_TXE, DISABLE); // 发送中断 使能
    460. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="460"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // 接收中断 使能
    461. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="461"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); // 空闲中断 使能
    462. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="462"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    463. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="463"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitTypeDef NVIC_InitStructure = {0}; // 优先级配置结构体
    464. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="464"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannel = USART2_IRQn; // 中断编号
    465. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="465"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 0 ; // 抢占优先级
    466. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="466"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelSubPriority = 0; // 子优先级
    467. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="467"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
    468. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="468"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_Init(&NVIC_InitStructure); // 初始化中断优先级
    469. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="469"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,打开串口
    470. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="470"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Cmd(USART2, ENABLE); // 配置完成,打开串口, 开始工作
    471. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="471"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    472. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="472"> class="hljs-ln-code"> class="hljs-ln-line">
    473. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="473"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER // HAL库 配置
    474. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="474"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    475. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="475"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOA_CLK_ENABLE(); // 使能GPIOA
    476. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="476"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART2_CLK_ENABLE(); // 使能USART2
    477. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="477"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 UART
    478. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="478"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART2_FORCE_RESET(); // 使能重置
    479. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="479"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART2_RELEASE_RESET(); // 取消重置
    480. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="480"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    481. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="481"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStruct = {0}; // 声明初始化要用到的结构体
    482. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="482"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_2 ; // 引脚 TX-PA2
    483. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="483"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // 工作模式
    484. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="484"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_NOPULL; // 上下拉
    485. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="485"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // 引脚速率
    486. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="486"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // 初始化引脚工作模式
    487. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="487"> class="hljs-ln-code"> class="hljs-ln-line"> // RX引脚配置
    488. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="488"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_3; // 引脚 RX-PA3
    489. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="489"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // 工作模式
    490. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="490"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_PULLUP; // 上下拉
    491. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="491"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // 引脚速率
    492. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="492"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // 初始化引脚工作模式
    493. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="493"> class="hljs-ln-code"> class="hljs-ln-line"> // 计算波特率参数
    494. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="494"> class="hljs-ln-code"> class="hljs-ln-line"> float temp;
    495. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="495"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t mantissa, fraction;
    496. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="496"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新系统运行频率全局值; 函数SystemCoreClock( ),在标准库、HAL库通用
    497. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="497"> class="hljs-ln-code"> class="hljs-ln-line"> temp = (float)(SystemCoreClock / 2) / (ulBaudrate * 16); // 波特率公式计算; USART2挂载在APB1, 时钟为系统时钟的2分频; 全局变量SystemCoreClock,在标准库、HAL库通用;
    498. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="498"> class="hljs-ln-code"> class="hljs-ln-line"> mantissa = temp; // 整数部分
    499. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="499"> class="hljs-ln-code"> class="hljs-ln-line"> fraction = (temp - mantissa) * 16; // 小数部分
    500. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="500"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> BRR = mantissa << 4 | fraction; // 设置波特率
    501. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="501"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    502. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="502"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 = 0; // 清0
    503. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="503"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x01 << 2; // 接收使能[02]: 0=失能、1=使能
    504. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="504"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x01 << 3; // 发送使能[03]:0=失能、1=使能
    505. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="505"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x00 << 9; // 奇偶校验[09]:0=偶Even、1=奇Odd; 注意:使用奇偶校验,下面两项要置1
    506. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="506"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x00 << 10; // 校验位 [10]:0=禁用、1=使能; 注意,使用奇偶校验,此位要置1 (否则无效、数据错乱)
    507. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="507"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x00 << 12; // 数据位 [12]:0=8位、 1=9位; 注意:使用奇偶校验,此位要置1 (否则数据会发生错乱)
    508. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="508"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR2 = 0; // 数据清0
    509. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="509"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR2 &= ~(0x03 << 12); // 停止位[13:12]:00b=1个停止位、01b=0.5个停止位、10b=2个停止位、11b=1.5个停止位
    510. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="510"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR3 = 0; // 数据清0
    511. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="511"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR3 &= ~(0x01 << 6); // DMA接收[6]: 0=禁止、1=使能
    512. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="512"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR3 &= ~(0x01 << 7); // DMA发送[7]: 0=禁止、1=使能
    513. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="513"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    514. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="514"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 &= ~(0x01 << 7); // 关闭发送中断
    515. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="515"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x01 << 5; // 使能接收中断: 接收缓冲区非空
    516. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="516"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x01 << 4; // 使能空闲中断:超过1字节时间没收到新数据
    517. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="517"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> SR = ~(0x00F0); // 清理中断标志
    518. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="518"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    519. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="519"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); // 设置指定中断的优先级; 参数:中断请求编号、抢占级、子优先级
    520. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="520"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_EnableIRQ(USART2_IRQn); // 使能、启用指定的中断
    521. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="521"> class="hljs-ln-code"> class="hljs-ln-line"> // 开启USART2
    522. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="522"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x01 << 13; // 使能UART开始工作
    523. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="523"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    524. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="524"> class="hljs-ln-code"> class="hljs-ln-line">
    525. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="525"> class="hljs-ln-code"> class="hljs-ln-line"> // 关联缓冲区
    526. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="526"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.puRxData = uaUART2RxData; // 获取接收缓冲区的地址
    527. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="527"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.puTxFiFoData = uaUART2TxFiFoData; // 获取发送缓冲区的地址
    528. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="528"> class="hljs-ln-code"> class="hljs-ln-line"> // 输出提示
    529. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="529"> class="hljs-ln-code"> class="hljs-ln-line"> printf("UART2 初始化配置 %d-None-8-1; 已完成初始化配置、收发配置\r", ulBaudrate);
    530. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="530"> class="hljs-ln-code"> class="hljs-ln-line">}
    531. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="531"> class="hljs-ln-code"> class="hljs-ln-line">
    532. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="532"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    533. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="533"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: USART2_IRQHandler
    534. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="534"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: USART2的接收中断、空闲中断、发送中断
    535. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="535"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    536. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="536"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    537. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="537"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数,当产生中断事件时,由硬件调用。
    538. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="538"> class="hljs-ln-code"> class="hljs-ln-line"> * 如果使用本文件代码,在工程文件的其它地方,要注释同名函数,否则冲突。
    539. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="539"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    540. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="540"> class="hljs-ln-code"> class="hljs-ln-line">void USART2_IRQHandler(void)
    541. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="541"> class="hljs-ln-code"> class="hljs-ln-line">{
    542. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="542"> class="hljs-ln-code"> class="hljs-ln-line"> static uint16_t cnt = 0; // 接收字节数累计:每一帧数据已接收到的字节数
    543. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="543"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t rxTemp[UART2_RX_BUF_SIZE]; // 接收数据缓存数组:每新接收1个字节,先顺序存放到这里,当一帧接收完(发生空闲中断), 再转存到外部缓存:xUARTx.puRxData[ ]
    544. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="544"> class="hljs-ln-code"> class="hljs-ln-line">
    545. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="545"> class="hljs-ln-code"> class="hljs-ln-line"> // 发送中断:用于把环形缓冲的数据,逐字节发出
    546. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="546"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART2->SR & USART_SR_TXE) && (USART2->CR1 & USART_CR1_TXEIE)) // 检查发送寄存器空中断使能,且发送寄存器为空; TXE(发送数据寄存器空)、TXEIE(发送缓冲区空中断使能)
    547. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="547"> class="hljs-ln-code"> class="hljs-ln-line"> {
    548. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="548"> class="hljs-ln-code"> class="hljs-ln-line"> USART2->DR = xUART2.puTxFiFoData[xUART2.usTxFiFoTail++]; // 从FIFO队列中取出一个数据,放入USART的发送寄存器(硬件会自动发出),然后将FIFO的尾指针递增,指向下一个要发送的数据
    549. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="549"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART2.usTxFiFoTail == UART2_TX_BUF_SIZE) // 检查FIFO尾指针是否到达了FIFO队列的末尾
    550. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="550"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.usTxFiFoTail = 0; // 将尾指针重置为0,实现环形队列的功能
    551. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="551"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART2.usTxFiFoTail == xUART2.usTxFiFoData) // 检查FIFO尾指针是否追上了头指针,即所有数据是否都已发送完毕
    552. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="552"> class="hljs-ln-code"> class="hljs-ln-line"> USART2->CR1 &= ~USART_CR1_TXEIE; // 关闭发送寄存器空中断,防止中断服务程序被不必要地调用
    553. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="553"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    554. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="554"> class="hljs-ln-code"> class="hljs-ln-line"> }
    555. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="555"> class="hljs-ln-code"> class="hljs-ln-line">
    556. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="556"> class="hljs-ln-code"> class="hljs-ln-line"> // 接收中断:用于逐个字节接收,存放到临时缓存
    557. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="557"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART2->SR & USART_SR_RXNE) // 检查RXNE(读数据寄存器非空标志位); RXNE中断清理方法:读DR时自动清理;
    558. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="558"> class="hljs-ln-code"> class="hljs-ln-line"> {
    559. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="559"> class="hljs-ln-code"> class="hljs-ln-line"> if (cnt == UART2_RX_BUF_SIZE) // 当前帧已接收的字节量,已满缓存区的大小; 为避免溢出,本包后面接收到的数据直接舍弃;
    560. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="560"> class="hljs-ln-code"> class="hljs-ln-line"> {
    561. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="561"> class="hljs-ln-code"> class="hljs-ln-line"> printf("警告:UART2单帧接收量,已超出接收缓存大小!\r修改文件bsp_UART.h的UART2_RX_BUF_SIZE值,可解决此问题!\r");
    562. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="562"> class="hljs-ln-code"> class="hljs-ln-line"> USART2->DR; // 读取数据寄存器的数据,但不保存.主要作用:读DR时自动清理接收中断标志;
    563. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="563"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    564. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="564"> class="hljs-ln-code"> class="hljs-ln-line"> }
    565. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="565"> class="hljs-ln-code"> class="hljs-ln-line"> rxTemp[cnt++] = USART2->DR ; // 把新收到的字节数据,顺序存放到RXTemp数组中;注意:读取DR时自动清零中断位;
    566. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="566"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    567. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="567"> class="hljs-ln-code"> class="hljs-ln-line"> }
    568. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="568"> class="hljs-ln-code"> class="hljs-ln-line">
    569. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="569"> class="hljs-ln-code"> class="hljs-ln-line"> // 空闲中断:用于判断一帧数据结束,整理数据到外部备读
    570. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="570"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART2->SR & USART_SR_IDLE) // 检查IDLE(空闲中断标志位); IDLE中断标志清理方法:序列清零,USART1 ->SR; USART1 ->DR;
    571. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="571"> class="hljs-ln-code"> class="hljs-ln-line"> {
    572. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="572"> class="hljs-ln-code"> class="hljs-ln-line"> memcpy(xUART2.puRxData, rxTemp, UART2_RX_BUF_SIZE); // 把本帧接收到的数据,存入到结构体的数组成员xUARTx.puRxData中, 等待处理; 注意:复制的是整个数组,包括0值,以方便字符串输出时尾部以0作字符串结束符
    573. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="573"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.usRxNum = cnt; // 把接收到的字节数,存入到结构体变量xUARTx.usRxNum中;
    574. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="574"> class="hljs-ln-code"> class="hljs-ln-line"> cnt = 0; // 接收字节数累计器,清零; 准备下一次的接收
    575. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="575"> class="hljs-ln-code"> class="hljs-ln-line"> memset(rxTemp, 0, UART2_RX_BUF_SIZE); // 接收数据缓存数组,清零; 准备下一次的接收
    576. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="576"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 ->SR;
    577. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="577"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 ->DR; // 清零IDLE中断标志位!! 序列清零,顺序不能错!!
    578. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="578"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    579. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="579"> class="hljs-ln-code"> class="hljs-ln-line"> }
    580. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="580"> class="hljs-ln-code"> class="hljs-ln-line">
    581. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="581"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    582. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="582"> class="hljs-ln-code"> class="hljs-ln-line">}
    583. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="583"> class="hljs-ln-code"> class="hljs-ln-line">
    584. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="584"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    585. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="585"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_SendData
    586. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="586"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过中断发送数据
    587. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="587"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】本函数可发送各种数据,而不限于字符串,如int,char
    588. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="588"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】注意h文件中所定义的发缓冲区大小、注意数据压入缓冲区的速度与串口发送速度的冲突
    589. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="589"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* puData 需发送数据的地址
    590. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="590"> class="hljs-ln-code"> class="hljs-ln-line"> * uint8_t usNum 发送的字节数 ,数量受限于h文件中设置的发送缓存区大小宏定义
    591. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="591"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    592. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="592"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    593. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="593"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_SendData(uint8_t *puData, uint16_t usNum)
    594. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="594"> class="hljs-ln-code"> class="hljs-ln-line">{
    595. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="595"> class="hljs-ln-code"> class="hljs-ln-line"> for (uint16_t i = 0; i < usNum; i++) // 把数据放入环形缓冲区
    596. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="596"> class="hljs-ln-code"> class="hljs-ln-line"> {
    597. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="597"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.puTxFiFoData[xUART2.usTxFiFoData++] = puData[i]; // 把字节放到缓冲区最后的位置,然后指针后移
    598. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="598"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART2.usTxFiFoData == UART2_TX_BUF_SIZE) // 如果指针位置到达缓冲区的最大值,则归0
    599. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="599"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.usTxFiFoData = 0;
    600. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="600"> class="hljs-ln-code"> class="hljs-ln-line"> }
    601. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="601"> class="hljs-ln-code"> class="hljs-ln-line">
    602. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="602"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART2->CR1 & USART_CR1_TXEIE) == 0) // 检查USART寄存器的发送缓冲区空置中断(TXEIE)是否已打开
    603. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="603"> class="hljs-ln-code"> class="hljs-ln-line"> USART2->CR1 |= USART_CR1_TXEIE; // 打开TXEIE中断
    604. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="604"> class="hljs-ln-code"> class="hljs-ln-line">}
    605. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="605"> class="hljs-ln-code"> class="hljs-ln-line">
    606. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="606"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    607. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="607"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_SendString
    608. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="608"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送字符串
    609. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="609"> class="hljs-ln-code"> class="hljs-ln-line"> * 用法请参考printf,及示例中的展示
    610. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="610"> class="hljs-ln-code"> class="hljs-ln-line"> * 注意,最大发送字节数为512-1个字符,可在函数中修改上限
    611. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="611"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: const char *pcString, ... (如同printf的用法)
    612. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="612"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    613. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="613"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    614. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="614"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_SendString(const char *pcString, ...)
    615. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="615"> class="hljs-ln-code"> class="hljs-ln-line">{
    616. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="616"> class="hljs-ln-code"> class="hljs-ln-line"> char mBuffer[512] = {0};; // 开辟一个缓存, 并把里面的数据置0
    617. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="617"> class="hljs-ln-code"> class="hljs-ln-line"> va_list ap; // 新建一个可变参数列表
    618. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="618"> class="hljs-ln-code"> class="hljs-ln-line"> va_start(ap, pcString); // 列表指向第一个可变参数
    619. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="619"> class="hljs-ln-code"> class="hljs-ln-line"> vsnprintf(mBuffer, 512, pcString, ap); // 把所有参数,按格式,输出到缓存; 参数2用于限制发送的最大字节数,如果达到上限,则只发送上限值-1; 最后1字节自动置'\0'
    620. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="620"> class="hljs-ln-code"> class="hljs-ln-line"> va_end(ap); // 清空可变参数列表
    621. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="621"> class="hljs-ln-code"> class="hljs-ln-line"> UART2_SendData((uint8_t *)mBuffer, strlen(mBuffer)); // 把字节存放环形缓冲,排队准备发送
    622. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="622"> class="hljs-ln-code"> class="hljs-ln-line">}
    623. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="623"> class="hljs-ln-code"> class="hljs-ln-line">
    624. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="624"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    625. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="625"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_SendAT
    626. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="626"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送AT命令, 并等待返回信息
    627. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="627"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char *pcString AT指令字符串
    628. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="628"> class="hljs-ln-code"> class="hljs-ln-line"> * char *pcAckString 期待的指令返回信息字符串
    629. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="629"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usTimeOut 发送命令后等待的时间,毫秒
    630. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="630"> class="hljs-ln-code"> class="hljs-ln-line"> *
    631. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="631"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回 值: 0-执行失败、1-执行正常
    632. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="632"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    633. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="633"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART2_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs)
    634. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="634"> class="hljs-ln-code"> class="hljs-ln-line">{
    635. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="635"> class="hljs-ln-code"> class="hljs-ln-line"> UART2_ClearRx(); // 清0
    636. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="636"> class="hljs-ln-code"> class="hljs-ln-line"> UART2_SendString(pcAT); // 发送AT指令字符串
    637. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="637"> class="hljs-ln-code"> class="hljs-ln-line">
    638. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="638"> class="hljs-ln-code"> class="hljs-ln-line"> while (usTimeOutMs--) // 判断是否起时(这里只作简单的循环判断次数处理)
    639. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="639"> class="hljs-ln-code"> class="hljs-ln-line"> {
    640. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="640"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART2_GetRxNum()) // 判断是否接收到数据
    641. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="641"> class="hljs-ln-code"> class="hljs-ln-line"> {
    642. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="642"> class="hljs-ln-code"> class="hljs-ln-line"> UART2_ClearRx(); // 清0接收字节数; 注意:接收到的数据内容 ,是没有被清0的
    643. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="643"> class="hljs-ln-code"> class="hljs-ln-line"> if (strstr((char *)UART2_GetRxData(), pcAckString)) // 判断返回数据中是否有期待的字符
    644. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="644"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 返回:0-超时没有返回、1-正常返回期待值
    645. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="645"> class="hljs-ln-code"> class="hljs-ln-line"> }
    646. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="646"> class="hljs-ln-code"> class="hljs-ln-line"> delay_ms(1); // 延时; 用于超时退出处理,避免死等
    647. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="647"> class="hljs-ln-code"> class="hljs-ln-line"> }
    648. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="648"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 返回:0-超时、返回异常,1-正常返回期待值
    649. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="649"> class="hljs-ln-code"> class="hljs-ln-line">}
    650. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="650"> class="hljs-ln-code"> class="hljs-ln-line">
    651. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="651"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    652. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="652"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_GetRxNum
    653. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="653"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据的字节数
    654. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="654"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    655. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="655"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 0=没有接收到数据,非0=新一帧数据的字节数
    656. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="656"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    657. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="657"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART2_GetRxNum(void)
    658. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="658"> class="hljs-ln-code"> class="hljs-ln-line">{
    659. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="659"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART2.usRxNum ;
    660. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="660"> class="hljs-ln-code"> class="hljs-ln-line">}
    661. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="661"> class="hljs-ln-code"> class="hljs-ln-line">
    662. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="662"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    663. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="663"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_GetRxData
    664. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="664"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据 (数据的地址)
    665. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="665"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    666. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="666"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 数据的地址(uint8_t*)
    667. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="667"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    668. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="668"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t *UART2_GetRxData(void)
    669. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="669"> class="hljs-ln-code"> class="hljs-ln-line">{
    670. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="670"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART2.puRxData ;
    671. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="671"> class="hljs-ln-code"> class="hljs-ln-line">}
    672. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="672"> class="hljs-ln-code"> class="hljs-ln-line">
    673. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="673"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    674. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="674"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_ClearRx
    675. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="675"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 清理最后一帧数据的缓存
    676. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="676"> class="hljs-ln-code"> class="hljs-ln-line"> * 主要是清0字节数,因为它是用来判断接收的标准
    677. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="677"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    678. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="678"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    679. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="679"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    680. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="680"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_ClearRx(void)
    681. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="681"> class="hljs-ln-code"> class="hljs-ln-line">{
    682. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="682"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.usRxNum = 0 ;
    683. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="683"> class="hljs-ln-code"> class="hljs-ln-line">}
    684. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="684"> class="hljs-ln-code"> class="hljs-ln-line">#endif // endif UART2_EN
    685. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="685"> class="hljs-ln-code"> class="hljs-ln-line">
    686. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="686"> class="hljs-ln-code"> class="hljs-ln-line">
    687. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="687"> class="hljs-ln-code"> class="hljs-ln-line">
    688. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="688"> class="hljs-ln-code"> class="hljs-ln-line">
    689. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="689"> class="hljs-ln-code"> class="hljs-ln-line">
    690. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="690"> class="hljs-ln-code"> class="hljs-ln-line">// USART-3 //
    691. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="691"> class="hljs-ln-code"> class="hljs-ln-line">/
    692. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="692"> class="hljs-ln-code"> class="hljs-ln-line">#if UART3_EN
    693. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="693"> class="hljs-ln-code"> class="hljs-ln-line">
    694. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="694"> class="hljs-ln-code"> class="hljs-ln-line">static xUSATR_TypeDef xUART3 = { 0 }; // 定义 UART3 的收发结构体
    695. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="695"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart3RxData[UART3_RX_BUF_SIZE]; // 定义 UART3 的接收缓存
    696. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="696"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart3TxFiFoData[UART3_TX_BUF_SIZE]; // 定义 UART3 的发送缓存
    697. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="697"> class="hljs-ln-code"> class="hljs-ln-line">
    698. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="698"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    699. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="699"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_Init
    700. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="700"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 初始化USART3的通信引脚、协议参数、中断优先级
    701. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="701"> class="hljs-ln-code"> class="hljs-ln-line"> * 引脚:TX-PB10、RX-PB11
    702. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="702"> class="hljs-ln-code"> class="hljs-ln-line"> * 协议:波特率-None-8-1
    703. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="703"> class="hljs-ln-code"> class="hljs-ln-line"> * 发送:发送中断
    704. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="704"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收:接收+空闲中断
    705. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="705"> class="hljs-ln-code"> class="hljs-ln-line"> *
    706. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="706"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ulBaudrate 通信波特率
    707. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="707"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    708. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="708"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    709. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="709"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_Init(uint32_t ulBaudrate)
    710. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="710"> class="hljs-ln-code"> class="hljs-ln-line">{
    711. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="711"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    712. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="712"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    713. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="713"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // 使能GPIOB
    714. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="714"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); // 使能UART3
    715. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="715"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 UART
    716. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="716"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE); // 使能重置
    717. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="717"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE); // 取消重置
    718. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="718"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    719. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="719"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStructure = {0}; // GPIO 初始化结构体
    720. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="720"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // TX 引脚
    721. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="721"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 引脚功能
    722. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="722"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 输出速度
    723. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="723"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOB, &GPIO_InitStructure); // 初始化引脚
    724. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="724"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    725. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="725"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // RX 引脚
    726. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="726"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // RX引脚工作模式:上拉输入; 如果使用浮空输入,引脚空置时可能产生误输入; 当电路上为一主多从电路时,可以使用复用开漏模式
    727. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="727"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOB, &GPIO_InitStructure); // 初始化引脚
    728. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="728"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 USART
    729. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="729"> class="hljs-ln-code"> class="hljs-ln-line"> USART_DeInit(USART3); // 复位串口
    730. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="730"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitTypeDef USART_InitStructure = {0}; // UART配置结构体
    731. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="731"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_BaudRate = ulBaudrate; // 波特率
    732. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="732"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 位长
    733. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="733"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位
    734. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="734"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Parity = USART_Parity_No; // 奇偶校验
    735. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="735"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 硬件流控制
    736. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="736"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 使能工作模式
    737. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="737"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Init(USART3, &USART_InitStructure); // 初始化串口
    738. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="738"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    739. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="739"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART3, USART_IT_TXE, DISABLE); // 发送中断 使能
    740. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="740"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // 接收中断 使能
    741. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="741"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART3, USART_IT_IDLE, ENABLE); // 空闲中断 使能
    742. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="742"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    743. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="743"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitTypeDef NVIC_InitStructure = {0}; // 优先级配置结构体
    744. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="744"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannel = USART3_IRQn; // 中断编号
    745. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="745"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 0 ; // 抢占优先级
    746. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="746"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelSubPriority = 0; // 子优先级
    747. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="747"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
    748. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="748"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_Init(&NVIC_InitStructure); // 初始化中断优先级
    749. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="749"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,打开串口
    750. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="750"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Cmd(USART3, ENABLE); // 配置完成,打开串口, 开始工作
    751. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="751"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    752. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="752"> class="hljs-ln-code"> class="hljs-ln-line">
    753. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="753"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER
    754. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="754"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    755. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="755"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOB_CLK_ENABLE(); // 使能GPIOB
    756. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="756"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART3_CLK_ENABLE(); // 使能USART3
    757. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="757"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 UART
    758. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="758"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART3_FORCE_RESET(); // 使能重置
    759. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="759"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART3_RELEASE_RESET(); // 取消重置
    760. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="760"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    761. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="761"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStruct = {0}; // 声明初始化要用到的结构体
    762. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="762"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_10 ; // 引脚 TX-PB10
    763. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="763"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // 工作模式
    764. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="764"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_NOPULL; // 上下拉
    765. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="765"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // 引脚速率
    766. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="766"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); // 初始化引脚工作模式
    767. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="767"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    768. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="768"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_11; // 引脚 RX-PB11
    769. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="769"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // 工作模式
    770. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="770"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_PULLUP; // 上下拉
    771. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="771"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // 引脚速率
    772. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="772"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); // 初始化引脚工作模式
    773. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="773"> class="hljs-ln-code"> class="hljs-ln-line"> // 计算波特率参数
    774. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="774"> class="hljs-ln-code"> class="hljs-ln-line"> float temp;
    775. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="775"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t mantissa, fraction;
    776. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="776"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新系统运行频率全局值; 函数SystemCoreClock( ),在标准库、HAL库通用
    777. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="777"> class="hljs-ln-code"> class="hljs-ln-line"> temp = (float)(SystemCoreClock / 2) / (ulBaudrate * 16); // 波特率公式计算; USART3挂载在APB1, 时钟为系统时钟的2分频; 全局变量SystemCoreClock,在标准库、HAL库通用;
    778. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="778"> class="hljs-ln-code"> class="hljs-ln-line"> mantissa = temp; // 整数部分
    779. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="779"> class="hljs-ln-code"> class="hljs-ln-line"> fraction = (temp - mantissa) * 16; // 小数部分
    780. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="780"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> BRR = mantissa << 4 | fraction; // 设置波特率
    781. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="781"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    782. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="782"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 = 0; // 清0
    783. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="783"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x01 << 2; // 接收使能[02]: 0=失能、1=使能
    784. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="784"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x01 << 3; // 发送使能[03]:0=失能、1=使能
    785. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="785"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x00 << 9; // 奇偶校验[09]:0=偶Even、1=奇Odd; 注意:使用奇偶校验,下面两项要置1
    786. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="786"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x00 << 10; // 校验位 [10]:0=禁用、1=使能; 注意,使用奇偶校验,此位要置1 (否则无效、数据错乱)
    787. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="787"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x00 << 12; // 数据位 [12]:0=8位、 1=9位; 注意:使用奇偶校验,此位要置1 (否则数据会发生错乱)
    788. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="788"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR2 = 0; // 数据清0
    789. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="789"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR2 &= ~(0x03 << 12); // 停止位[13:12]:00b=1个停止位、01b=0.5个停止位、10b=2个停止位、11b=1.5个停止位
    790. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="790"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR3 = 0; // 数据清0
    791. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="791"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR3 &= ~(0x01 << 6); // DMA接收[6]: 0=禁止、1=使能
    792. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="792"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR3 &= ~(0x01 << 7); // DMA发送[7]: 0=禁止、1=使能
    793. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="793"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    794. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="794"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 &= ~(0x01 << 7); // 关闭发送中断
    795. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="795"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x01 << 5; // 使能接收中断: 接收缓冲区非空
    796. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="796"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x01 << 4; // 使能空闲中断:超过1字节时间没收到新数据
    797. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="797"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> SR = ~(0x00F0); // 清理中断标志
    798. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="798"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    799. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="799"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_SetPriority(USART3_IRQn, 0, 0); // 设置指定中断的优先级; 参数:中断请求编号、抢占级、子优先级
    800. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="800"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_EnableIRQ(USART3_IRQn); // 使能、启用指定的中断
    801. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="801"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,打开串口
    802. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="802"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x01 << 13; // 使能UART开始工作
    803. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="803"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    804. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="804"> class="hljs-ln-code"> class="hljs-ln-line">
    805. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="805"> class="hljs-ln-code"> class="hljs-ln-line"> // 关联缓冲区
    806. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="806"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.puRxData = uaUart3RxData; // 获取接收缓冲区的地址
    807. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="807"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.puTxFiFoData = uaUart3TxFiFoData; // 获取发送缓冲区的地址
    808. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="808"> class="hljs-ln-code"> class="hljs-ln-line"> // 输出提示
    809. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="809"> class="hljs-ln-code"> class="hljs-ln-line"> printf("UART3 初始化配置 %d-None-8-1; 已完成初始化配置、收发配置\r", ulBaudrate);
    810. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="810"> class="hljs-ln-code"> class="hljs-ln-line">}
    811. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="811"> class="hljs-ln-code"> class="hljs-ln-line">
    812. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="812"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    813. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="813"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: USART3_IRQHandler
    814. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="814"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: USART3的接收中断、空闲中断、发送中断
    815. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="815"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    816. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="816"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    817. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="817"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数,当产生中断事件时,由硬件调用。
    818. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="818"> class="hljs-ln-code"> class="hljs-ln-line"> * 如果使用本文件代码,在工程文件的其它地方,要注释同名函数,否则冲突。
    819. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="819"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    820. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="820"> class="hljs-ln-code"> class="hljs-ln-line">void USART3_IRQHandler(void)
    821. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="821"> class="hljs-ln-code"> class="hljs-ln-line">{
    822. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="822"> class="hljs-ln-code"> class="hljs-ln-line"> static uint16_t cnt = 0; // 接收字节数累计:每一帧数据已接收到的字节数
    823. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="823"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t rxTemp[UART3_RX_BUF_SIZE]; // 接收数据缓存数组:每新接收1个字节,先顺序存放到这里,当一帧接收完(发生空闲中断), 再转存到外部缓存:xUARTx.puRxData[ ]
    824. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="824"> class="hljs-ln-code"> class="hljs-ln-line">
    825. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="825"> class="hljs-ln-code"> class="hljs-ln-line"> // 发送中断:用于把环形缓冲的数据,逐字节发出
    826. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="826"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART3->SR & USART_SR_TXE) && (USART3->CR1 & USART_CR1_TXEIE)) // 检查发送寄存器空中断使能,且发送寄存器为空; TXE(发送数据寄存器空)、TXEIE(发送缓冲区空中断使能)
    827. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="827"> class="hljs-ln-code"> class="hljs-ln-line"> {
    828. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="828"> class="hljs-ln-code"> class="hljs-ln-line"> USART3->DR = xUART3.puTxFiFoData[xUART3.usTxFiFoTail++]; // 从FIFO队列中取出一个数据,放入USART的发送寄存器(硬件会自动发出),然后将FIFO的尾指针递增,指向下一个要发送的数据
    829. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="829"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART3.usTxFiFoTail == UART3_TX_BUF_SIZE) // 检查FIFO尾指针是否到达了FIFO队列的末尾
    830. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="830"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.usTxFiFoTail = 0; // 将尾指针重置为0,实现环形队列的功能
    831. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="831"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART3.usTxFiFoTail == xUART3.usTxFiFoData) // 检查FIFO尾指针是否追上了头指针,即所有数据是否都已发送完毕
    832. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="832"> class="hljs-ln-code"> class="hljs-ln-line"> USART3->CR1 &= ~USART_CR1_TXEIE; // 关闭发送寄存器空中断,防止中断服务程序被不必要地调用
    833. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="833"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    834. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="834"> class="hljs-ln-code"> class="hljs-ln-line"> }
    835. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="835"> class="hljs-ln-code"> class="hljs-ln-line">
    836. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="836"> class="hljs-ln-code"> class="hljs-ln-line"> // 接收中断:用于逐个字节接收,存放到临时缓存
    837. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="837"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART3->SR & USART_SR_RXNE) // 检查RXNE(读数据寄存器非空标志位); RXNE中断清理方法:读DR时自动清理;
    838. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="838"> class="hljs-ln-code"> class="hljs-ln-line"> {
    839. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="839"> class="hljs-ln-code"> class="hljs-ln-line"> if (cnt == UART3_RX_BUF_SIZE) // 当前帧已接收的字节量,已满缓存区的大小; 为避免溢出,本包后面接收到的数据直接舍弃;
    840. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="840"> class="hljs-ln-code"> class="hljs-ln-line"> {
    841. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="841"> class="hljs-ln-code"> class="hljs-ln-line"> printf("警告:UART3单帧接收量,已超出接收缓存大小!\r修改文件bsp_UART.h的UART3_RX_BUF_SIZE值,可解决此问题!\r");
    842. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="842"> class="hljs-ln-code"> class="hljs-ln-line"> USART3->DR; // 读取数据寄存器的数据,但不保存.主要作用:读DR时自动清理接收中断标志;
    843. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="843"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    844. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="844"> class="hljs-ln-code"> class="hljs-ln-line"> }
    845. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="845"> class="hljs-ln-code"> class="hljs-ln-line"> rxTemp[cnt++] = USART3->DR ; // 把新收到的字节数据,顺序存放到RXTemp数组中;注意:读取DR时自动清零中断位
    846. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="846"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    847. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="847"> class="hljs-ln-code"> class="hljs-ln-line"> }
    848. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="848"> class="hljs-ln-code"> class="hljs-ln-line">
    849. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="849"> class="hljs-ln-code"> class="hljs-ln-line"> // 空闲中断:用于判断一帧数据结束,整理数据到外部备读
    850. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="850"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART3->SR & USART_SR_IDLE) // 检查IDLE(空闲中断标志位); IDLE中断标志清理方法:序列清零,USART1 ->SR; USART1 ->DR;
    851. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="851"> class="hljs-ln-code"> class="hljs-ln-line"> {
    852. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="852"> class="hljs-ln-code"> class="hljs-ln-line"> memcpy(xUART3.puRxData, rxTemp, UART3_RX_BUF_SIZE); // 把本帧接收到的数据,存入到结构体的数组成员xUARTx.puRxData中, 等待处理; 注意:复制的是整个数组,包括0值,以方便字符串输出时尾部以0作字符串结束符
    853. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="853"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.usRxNum = cnt; // 把接收到的字节数,存入到结构体变量xUARTx.usRxNum中;
    854. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="854"> class="hljs-ln-code"> class="hljs-ln-line"> cnt = 0; // 接收字节数累计器,清零; 准备下一次的接收
    855. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="855"> class="hljs-ln-code"> class="hljs-ln-line"> memset(rxTemp, 0, UART3_RX_BUF_SIZE); // 接收数据缓存数组,清零; 准备下一次的接收
    856. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="856"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 ->SR;
    857. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="857"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 ->DR; // 清零IDLE中断标志位!! 序列清零,顺序不能错!!
    858. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="858"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    859. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="859"> class="hljs-ln-code"> class="hljs-ln-line"> }
    860. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="860"> class="hljs-ln-code"> class="hljs-ln-line">
    861. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="861"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    862. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="862"> class="hljs-ln-code"> class="hljs-ln-line">}
    863. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="863"> class="hljs-ln-code"> class="hljs-ln-line">
    864. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="864"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    865. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="865"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_SendData
    866. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="866"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过中断发送数据
    867. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="867"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】本函数可发送各种数据,而不限于字符串,如int,char
    868. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="868"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】注意h文件中所定义的发缓冲区大小、注意数据压入缓冲区的速度与串口发送速度的冲突
    869. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="869"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* puData 需发送数据的地址
    870. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="870"> class="hljs-ln-code"> class="hljs-ln-line"> * uint8_t usNum 发送的字节数 ,数量受限于h文件中设置的发送缓存区大小宏定义
    871. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="871"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    872. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="872"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    873. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="873"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_SendData(uint8_t *puData, uint16_t usNum)
    874. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="874"> class="hljs-ln-code"> class="hljs-ln-line">{
    875. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="875"> class="hljs-ln-code"> class="hljs-ln-line"> for (uint16_t i = 0; i < usNum; i++) // 把数据放入环形缓冲区
    876. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="876"> class="hljs-ln-code"> class="hljs-ln-line"> {
    877. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="877"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.puTxFiFoData[xUART3.usTxFiFoData++] = puData[i]; // 把字节放到缓冲区最后的位置,然后指针后移
    878. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="878"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART3.usTxFiFoData == UART3_TX_BUF_SIZE) // 如果指针位置到达缓冲区的最大值,则归0
    879. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="879"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.usTxFiFoData = 0;
    880. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="880"> class="hljs-ln-code"> class="hljs-ln-line"> }
    881. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="881"> class="hljs-ln-code"> class="hljs-ln-line">
    882. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="882"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART3->CR1 & USART_CR1_TXEIE) == 0) // 检查USART寄存器的发送缓冲区空置中断(TXEIE)是否已打开
    883. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="883"> class="hljs-ln-code"> class="hljs-ln-line"> USART3->CR1 |= USART_CR1_TXEIE; // 打开TXEIE中断
    884. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="884"> class="hljs-ln-code"> class="hljs-ln-line">}
    885. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="885"> class="hljs-ln-code"> class="hljs-ln-line">
    886. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="886"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    887. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="887"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_SendString
    888. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="888"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送字符串
    889. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="889"> class="hljs-ln-code"> class="hljs-ln-line"> * 用法请参考printf,及示例中的展示
    890. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="890"> class="hljs-ln-code"> class="hljs-ln-line"> * 注意,最大发送字节数为512-1个字符,可在函数中修改上限
    891. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="891"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: const char *pcString, ... (如同printf的用法)
    892. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="892"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    893. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="893"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    894. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="894"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_SendString(const char *pcString, ...)
    895. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="895"> class="hljs-ln-code"> class="hljs-ln-line">{
    896. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="896"> class="hljs-ln-code"> class="hljs-ln-line"> char mBuffer[512] = {0};; // 开辟一个缓存, 并把里面的数据置0
    897. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="897"> class="hljs-ln-code"> class="hljs-ln-line"> va_list ap; // 新建一个可变参数列表
    898. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="898"> class="hljs-ln-code"> class="hljs-ln-line"> va_start(ap, pcString); // 列表指向第一个可变参数
    899. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="899"> class="hljs-ln-code"> class="hljs-ln-line"> vsnprintf(mBuffer, 512, pcString, ap); // 把所有参数,按格式,输出到缓存; 参数2用于限制发送的最大字节数,如果达到上限,则只发送上限值-1; 最后1字节自动置'\0'
    900. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="900"> class="hljs-ln-code"> class="hljs-ln-line"> va_end(ap); // 清空可变参数列表
    901. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="901"> class="hljs-ln-code"> class="hljs-ln-line"> UART3_SendData((uint8_t *)mBuffer, strlen(mBuffer)); // 把字节存放环形缓冲,排队准备发送
    902. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="902"> class="hljs-ln-code"> class="hljs-ln-line">}
    903. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="903"> class="hljs-ln-code"> class="hljs-ln-line">
    904. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="904"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    905. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="905"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_SendAT
    906. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="906"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送AT命令, 并等待返回信息
    907. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="907"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char *pcString AT指令字符串
    908. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="908"> class="hljs-ln-code"> class="hljs-ln-line"> * char *pcAckString 期待的指令返回信息字符串
    909. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="909"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usTimeOut 发送命令后等待的时间,毫秒
    910. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="910"> class="hljs-ln-code"> class="hljs-ln-line"> *
    911. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="911"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回 值: 0-执行失败、1-执行正常
    912. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="912"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    913. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="913"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART3_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs)
    914. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="914"> class="hljs-ln-code"> class="hljs-ln-line">{
    915. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="915"> class="hljs-ln-code"> class="hljs-ln-line"> UART3_ClearRx(); // 清0
    916. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="916"> class="hljs-ln-code"> class="hljs-ln-line"> UART3_SendString(pcAT); // 发送AT指令字符串
    917. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="917"> class="hljs-ln-code"> class="hljs-ln-line">
    918. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="918"> class="hljs-ln-code"> class="hljs-ln-line"> while (usTimeOutMs--) // 判断是否起时(这里只作简单的循环判断次数处理)
    919. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="919"> class="hljs-ln-code"> class="hljs-ln-line"> {
    920. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="920"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART3_GetRxNum()) // 判断是否接收到数据
    921. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="921"> class="hljs-ln-code"> class="hljs-ln-line"> {
    922. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="922"> class="hljs-ln-code"> class="hljs-ln-line"> UART3_ClearRx(); // 清0接收字节数; 注意:接收到的数据内容 ,是没有被清0的
    923. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="923"> class="hljs-ln-code"> class="hljs-ln-line"> if (strstr((char *)UART3_GetRxData(), pcAckString)) // 判断返回数据中是否有期待的字符
    924. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="924"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 返回:0-超时没有返回、1-正常返回期待值
    925. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="925"> class="hljs-ln-code"> class="hljs-ln-line"> }
    926. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="926"> class="hljs-ln-code"> class="hljs-ln-line"> delay_ms(1); // 延时; 用于超时退出处理,避免死等
    927. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="927"> class="hljs-ln-code"> class="hljs-ln-line"> }
    928. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="928"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 返回:0-超时、返回异常,1-正常返回期待值
    929. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="929"> class="hljs-ln-code"> class="hljs-ln-line">}
    930. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="930"> class="hljs-ln-code"> class="hljs-ln-line">
    931. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="931"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    932. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="932"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_GetRxNum
    933. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="933"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据的字节数
    934. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="934"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    935. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="935"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 0=没有接收到数据,非0=新一帧数据的字节数
    936. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="936"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    937. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="937"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART3_GetRxNum(void)
    938. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="938"> class="hljs-ln-code"> class="hljs-ln-line">{
    939. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="939"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART3.usRxNum ;
    940. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="940"> class="hljs-ln-code"> class="hljs-ln-line">}
    941. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="941"> class="hljs-ln-code"> class="hljs-ln-line">
    942. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="942"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    943. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="943"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_GetRxData
    944. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="944"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据 (数据的地址)
    945. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="945"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    946. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="946"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 数据的地址(uint8_t*)
    947. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="947"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    948. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="948"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t *UART3_GetRxData(void)
    949. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="949"> class="hljs-ln-code"> class="hljs-ln-line">{
    950. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="950"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART3.puRxData ;
    951. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="951"> class="hljs-ln-code"> class="hljs-ln-line">}
    952. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="952"> class="hljs-ln-code"> class="hljs-ln-line">
    953. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="953"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    954. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="954"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_ClearRx
    955. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="955"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 清理最后一帧数据的缓存
    956. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="956"> class="hljs-ln-code"> class="hljs-ln-line"> * 主要是清0字节数,因为它是用来判断接收的标准
    957. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="957"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    958. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="958"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    959. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="959"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    960. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="960"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_ClearRx(void)
    961. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="961"> class="hljs-ln-code"> class="hljs-ln-line">{
    962. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="962"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.usRxNum = 0 ;
    963. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="963"> class="hljs-ln-code"> class="hljs-ln-line">}
    964. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="964"> class="hljs-ln-code"> class="hljs-ln-line">#endif // endif UART3_EN
    965. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="965"> class="hljs-ln-code"> class="hljs-ln-line">
    966. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="966"> class="hljs-ln-code"> class="hljs-ln-line">
    967. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="967"> class="hljs-ln-code"> class="hljs-ln-line">
    968. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="968"> class="hljs-ln-code"> class="hljs-ln-line">
    969. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="969"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef STM32F10X_HD
    970. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="970"> class="hljs-ln-code"> class="hljs-ln-line">// UART-4 //
    971. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="971"> class="hljs-ln-code"> class="hljs-ln-line">/
    972. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="972"> class="hljs-ln-code"> class="hljs-ln-line">#if UART4_EN
    973. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="973"> class="hljs-ln-code"> class="hljs-ln-line">
    974. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="974"> class="hljs-ln-code"> class="hljs-ln-line">static xUSATR_TypeDef xUART4 = { 0 }; // 定义 UART4 的收发结构体
    975. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="975"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart4RxData[UART4_RX_BUF_SIZE]; // 定义 UART4 的接收缓存
    976. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="976"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart4TxFiFoData[UART4_TX_BUF_SIZE]; // 定义 UART4 的发送缓存
    977. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="977"> class="hljs-ln-code"> class="hljs-ln-line">
    978. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="978"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    979. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="979"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_Init
    980. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="980"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 初始化UART4的通信引脚、协议参数、中断优先级
    981. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="981"> class="hljs-ln-code"> class="hljs-ln-line"> * 引脚:TX-PC10、RX-PC11
    982. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="982"> class="hljs-ln-code"> class="hljs-ln-line"> * 协议:波特率-None-8-1
    983. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="983"> class="hljs-ln-code"> class="hljs-ln-line"> * 发送:发送中断
    984. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="984"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收:接收+空闲中断
    985. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="985"> class="hljs-ln-code"> class="hljs-ln-line"> *
    986. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="986"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ulBaudrate 通信波特率
    987. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="987"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    988. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="988"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    989. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="989"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_Init(uint32_t ulBaudrate)
    990. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="990"> class="hljs-ln-code"> class="hljs-ln-line">{
    991. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="991"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    992. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="992"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    993. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="993"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // 使能GPIOC
    994. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="994"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); // 使能UART4
    995. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="995"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 UART
    996. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="996"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE); // 使能重置
    997. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="997"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE); // 取消重置
    998. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="998"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    999. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="999"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStructure = {0}; // GPIO 初始化结构体
    1000. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1000"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // TX 引脚
    1001. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1001"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 引脚功能
    1002. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1002"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 输出速度
    1003. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1003"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOC, &GPIO_InitStructure); // 初始化引脚
    1004. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1004"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    1005. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1005"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // RX 引脚
    1006. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1006"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // RX引脚工作模式:上拉输入; 如果使用浮空输入,引脚空置时可能产生误输入; 当电路上为一主多从电路时,可以使用复用开漏模式
    1007. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1007"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOC, &GPIO_InitStructure); // 初始化引脚
    1008. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1008"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 USART
    1009. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1009"> class="hljs-ln-code"> class="hljs-ln-line"> USART_DeInit(UART4); // 复位串口
    1010. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1010"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitTypeDef USART_InitStructure = {0}; // UART配置结构体
    1011. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1011"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_BaudRate = ulBaudrate; // 波特率
    1012. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1012"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 位长
    1013. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1013"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位
    1014. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1014"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Parity = USART_Parity_No; // 奇偶校验
    1015. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1015"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 硬件流控制
    1016. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1016"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 使能工作模式
    1017. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1017"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Init(UART4, &USART_InitStructure); // 初始化串口
    1018. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1018"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    1019. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1019"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(UART4, USART_IT_TXE, DISABLE); // 发送中断 使能
    1020. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1020"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); // 接收中断 使能
    1021. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1021"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(UART4, USART_IT_IDLE, ENABLE); // 空闲中断 使能
    1022. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1022"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    1023. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1023"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitTypeDef NVIC_InitStructure = {0}; // 优先级配置结构体
    1024. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1024"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannel = UART4_IRQn; // 中断编号
    1025. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1025"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 0 ; // 抢占优先级
    1026. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1026"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelSubPriority = 0; // 子优先级
    1027. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1027"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
    1028. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1028"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_Init(&NVIC_InitStructure); // 初始化中断优先级
    1029. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1029"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,打开串口
    1030. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1030"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Cmd(UART4, ENABLE); // 配置完成,打开串口, 开始工作
    1031. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1031"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1032. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1032"> class="hljs-ln-code"> class="hljs-ln-line">
    1033. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1033"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER
    1034. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1034"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    1035. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1035"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOC_CLK_ENABLE(); // 使能GPIOC
    1036. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1036"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_UART4_CLK_ENABLE(); // 使能UART4
    1037. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1037"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 UART
    1038. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1038"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_UART4_FORCE_RESET(); // 使能重置
    1039. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1039"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_UART4_RELEASE_RESET(); // 取消重置
    1040. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1040"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    1041. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1041"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStruct = {0}; // 声明初始化要用到的结构体
    1042. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1042"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_10 ; // 引脚 TX-PC10
    1043. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1043"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // 工作模式
    1044. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1044"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_NOPULL; // 上下拉
    1045. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1045"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // 引脚速率
    1046. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1046"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); // 初始化引脚工作模式
    1047. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1047"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    1048. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1048"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_11; // 引脚 RX-PC11
    1049. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1049"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // 工作模式
    1050. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1050"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_PULLUP; // 上下拉
    1051. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1051"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // 引脚速率
    1052. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1052"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); // 初始化引脚工作模式
    1053. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1053"> class="hljs-ln-code"> class="hljs-ln-line"> // 计算波特率参数
    1054. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1054"> class="hljs-ln-code"> class="hljs-ln-line"> float temp;
    1055. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1055"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t mantissa, fraction;
    1056. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1056"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新系统运行频率全局值; 函数SystemCoreClock( ),在标准库、HAL库通用
    1057. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1057"> class="hljs-ln-code"> class="hljs-ln-line"> temp = (float)(SystemCoreClock / 2) / (ulBaudrate * 16); // 波特率公式计算; UART4挂载在APB1, 时钟为系统时钟的2分频; 全局变量SystemCoreClock,在标准库、HAL库通用;
    1058. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1058"> class="hljs-ln-code"> class="hljs-ln-line"> mantissa = temp; // 整数部分
    1059. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1059"> class="hljs-ln-code"> class="hljs-ln-line"> fraction = (temp - mantissa) * 16; // 小数部分
    1060. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1060"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> BRR = mantissa << 4 | fraction; // 设置波特率
    1061. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1061"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    1062. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1062"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 = 0; // 清0
    1063. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1063"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x01 << 2; // 接收使能[02]: 0=失能、1=使能
    1064. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1064"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x01 << 3; // 发送使能[03]:0=失能、1=使能
    1065. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1065"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x00 << 9; // 奇偶校验[09]:0=偶Even、1=奇Odd; 注意:使用奇偶校验,下面两项要置1
    1066. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1066"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x00 << 10; // 校验位 [10]:0=禁用、1=使能; 注意,使用奇偶校验,此位要置1 (否则无效、数据错乱)
    1067. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1067"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x00 << 12; // 数据位 [12]:0=8位、 1=9位; 注意:使用奇偶校验,此位要置1 (否则数据会发生错乱)
    1068. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1068"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR2 = 0; // 数据清0
    1069. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1069"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR2 &= ~(0x03 << 12); // 停止位[13:12]:00b=1个停止位、01b=0.5个停止位、10b=2个停止位、11b=1.5个停止位
    1070. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1070"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR3 = 0; // 数据清0
    1071. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1071"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR3 &= ~(0x01 << 6); // DMA接收[6]: 0=禁止、1=使能
    1072. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1072"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR3 &= ~(0x01 << 7); // DMA发送[7]: 0=禁止、1=使能
    1073. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1073"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    1074. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1074"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 &= ~(0x01 << 7); // 关闭发送中断
    1075. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1075"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x01 << 5; // 使能接收中断: 接收缓冲区非空
    1076. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1076"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x01 << 4; // 使能空闲中断:超过1字节时间没收到新数据
    1077. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1077"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> SR = ~(0x00F0); // 清理中断标志
    1078. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1078"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    1079. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1079"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_SetPriority(UART4_IRQn, 0, 0); // 设置指定中断的优先级; 参数:中断请求编号、抢占级、子优先级
    1080. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1080"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_EnableIRQ(UART4_IRQn); // 使能、启用指定的中断
    1081. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1081"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,打开串口
    1082. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1082"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x01 << 13; // 使能UART开始工作
    1083. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1083"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1084. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1084"> class="hljs-ln-code"> class="hljs-ln-line">
    1085. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1085"> class="hljs-ln-code"> class="hljs-ln-line"> // 关联缓冲区
    1086. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1086"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.puRxData = uaUart4RxData; // 获取接收缓冲区的地址
    1087. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1087"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.puTxFiFoData = uaUart4TxFiFoData; // 获取发送缓冲区的地址
    1088. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1088"> class="hljs-ln-code"> class="hljs-ln-line"> // 输出提示
    1089. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1089"> class="hljs-ln-code"> class="hljs-ln-line"> printf("UART4 初始化配置 %d-None-8-1; 已完成初始化配置、收发配置\r", ulBaudrate);
    1090. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1090"> class="hljs-ln-code"> class="hljs-ln-line">}
    1091. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1091"> class="hljs-ln-code"> class="hljs-ln-line">
    1092. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1092"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1093. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1093"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_IRQHandler
    1094. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1094"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART4的中断处理函数
    1095. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1095"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收中断、空闲中断、发送中断
    1096. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1096"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1097. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1097"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1098. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1098"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数,当产生中断事件时,由硬件调用。
    1099. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1099"> class="hljs-ln-code"> class="hljs-ln-line"> * 如果使用本文件代码,在工程文件的其它地方,要注释同名函数,否则冲突。
    1100. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1100"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1101. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1101"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_IRQHandler(void)
    1102. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1102"> class="hljs-ln-code"> class="hljs-ln-line">{
    1103. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1103"> class="hljs-ln-code"> class="hljs-ln-line"> static uint16_t cnt = 0; // 接收字节数累计:每一帧数据已接收到的字节数
    1104. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1104"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t rxTemp[UART4_RX_BUF_SIZE]; // 接收数据缓存数组:每新接收1个字节,先顺序存放到这里,当一帧接收完(发生空闲中断), 再转存到外部缓存:xUARTx.puRxData[ ]
    1105. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1105"> class="hljs-ln-code"> class="hljs-ln-line">
    1106. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1106"> class="hljs-ln-code"> class="hljs-ln-line"> // 发送中断:用于把环形缓冲的数据,逐字节发出
    1107. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1107"> class="hljs-ln-code"> class="hljs-ln-line"> if ((UART4->SR & USART_SR_TXE) && (UART4->CR1 & USART_CR1_TXEIE)) // 检查发送寄存器空中断使能,且发送寄存器为空; TXE(发送数据寄存器空)、TXEIE(发送缓冲区空中断使能)
    1108. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1108"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1109. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1109"> class="hljs-ln-code"> class="hljs-ln-line"> UART4->DR = xUART4.puTxFiFoData[xUART4.usTxFiFoTail++]; // 从FIFO队列中取出一个数据,放入USART的发送寄存器(硬件会自动发出),然后将FIFO的尾指针递增,指向下一个要发送的数据
    1110. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1110"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART4.usTxFiFoTail == UART4_TX_BUF_SIZE) // 检查FIFO尾指针是否到达了FIFO队列的末尾
    1111. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1111"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.usTxFiFoTail = 0; // 将尾指针重置为0,实现环形队列的功能
    1112. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1112"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART4.usTxFiFoTail == xUART4.usTxFiFoData) // 检查FIFO尾指针是否追上了头指针,即所有数据是否都已发送完毕
    1113. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1113"> class="hljs-ln-code"> class="hljs-ln-line"> UART4->CR1 &= ~USART_CR1_TXEIE; // 关闭发送寄存器空中断,防止中断服务程序被不必要地调用
    1114. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1114"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1115. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1115"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1116. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1116"> class="hljs-ln-code"> class="hljs-ln-line">
    1117. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1117"> class="hljs-ln-code"> class="hljs-ln-line"> // 接收中断:用于逐个字节接收,存放到临时缓存
    1118. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1118"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART4->SR & USART_SR_RXNE) // 检查RXNE(读数据寄存器非空标志位); RXNE中断清理方法:读DR时自动清理;
    1119. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1119"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1120. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1120"> class="hljs-ln-code"> class="hljs-ln-line"> if (cnt == UART4_RX_BUF_SIZE) // 当前帧已接收的字节量,已满缓存区的大小; 为避免溢出,本包后面接收到的数据直接舍弃;
    1121. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1121"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1122. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1122"> class="hljs-ln-code"> class="hljs-ln-line"> printf("警告:UART4单帧接收量,已超出接收缓存大小!\r修改文件bsp_UART.h的UART4_RX_BUF_SIZE值,可解决此问题!\r");
    1123. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1123"> class="hljs-ln-code"> class="hljs-ln-line"> UART4->DR; // 读取数据寄存器的数据,但不保存.主要作用:读DR时自动清理接收中断标志;
    1124. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1124"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1125. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1125"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1126. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1126"> class="hljs-ln-code"> class="hljs-ln-line"> rxTemp[cnt++] = UART4->DR ; // 把新收到的字节数据,顺序存放到RXTemp数组中;注意:读取DR时自动清零中断位
    1127. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1127"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1128. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1128"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1129. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1129"> class="hljs-ln-code"> class="hljs-ln-line">
    1130. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1130"> class="hljs-ln-code"> class="hljs-ln-line"> // 空闲中断:用于判断一帧数据结束,整理数据到外部备读
    1131. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1131"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART4->SR & USART_SR_IDLE) // 检查IDLE(空闲中断标志位); IDLE中断标志清理方法:序列清零,USART1 ->SR; USART1 ->DR;
    1132. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1132"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1133. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1133"> class="hljs-ln-code"> class="hljs-ln-line"> memcpy(xUART4.puRxData, rxTemp, UART4_RX_BUF_SIZE); // 把本帧接收到的数据,存入到结构体的数组成员xUARTx.puRxData中, 等待处理; 注意:复制的是整个数组,包括0值,以方便字符串输出时尾部以0作字符串结束符
    1134. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1134"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.usRxNum = cnt; // 把接收到的字节数,存入到结构体变量xUARTx.usRxNum中;
    1135. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1135"> class="hljs-ln-code"> class="hljs-ln-line"> cnt = 0; // 接收字节数累计器,清零; 准备下一次的接收
    1136. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1136"> class="hljs-ln-code"> class="hljs-ln-line"> memset(rxTemp, 0, UART4_RX_BUF_SIZE); // 接收数据缓存数组,清零; 准备下一次的接收
    1137. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1137"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 ->SR;
    1138. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1138"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 ->DR; // 清零IDLE中断标志位!! 序列清零,顺序不能错!!
    1139. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1139"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1140. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1140"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1141. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1141"> class="hljs-ln-code"> class="hljs-ln-line">
    1142. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1142"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1143. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1143"> class="hljs-ln-code"> class="hljs-ln-line">}
    1144. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1144"> class="hljs-ln-code"> class="hljs-ln-line">
    1145. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1145"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1146. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1146"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_SendData
    1147. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1147"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过中断发送数据
    1148. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1148"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】本函数可发送各种数据,而不限于字符串,如int,char
    1149. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1149"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】注意h文件中所定义的发缓冲区大小、注意数据压入缓冲区的速度与串口发送速度的冲突
    1150. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1150"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* puData 需发送数据的地址
    1151. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1151"> class="hljs-ln-code"> class="hljs-ln-line"> * uint8_t usNum 发送的字节数 ,数量受限于h文件中设置的发送缓存区大小宏定义
    1152. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1152"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1153. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1153"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1154. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1154"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_SendData(uint8_t *puData, uint16_t usNum)
    1155. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1155"> class="hljs-ln-code"> class="hljs-ln-line">{
    1156. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1156"> class="hljs-ln-code"> class="hljs-ln-line"> for (uint16_t i = 0; i < usNum; i++) // 把数据放入环形缓冲区
    1157. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1157"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1158. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1158"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.puTxFiFoData[xUART4.usTxFiFoData++] = puData[i]; // 把字节放到缓冲区最后的位置,然后指针后移
    1159. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1159"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART4.usTxFiFoData == UART4_TX_BUF_SIZE) // 如果指针位置到达缓冲区的最大值,则归0
    1160. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1160"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.usTxFiFoData = 0;
    1161. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1161"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1162. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1162"> class="hljs-ln-code"> class="hljs-ln-line">
    1163. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1163"> class="hljs-ln-code"> class="hljs-ln-line"> if ((UART4->CR1 & USART_CR1_TXEIE) == 0) // 检查USART寄存器的发送缓冲区空置中断(TXEIE)是否已打开
    1164. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1164"> class="hljs-ln-code"> class="hljs-ln-line"> UART4->CR1 |= USART_CR1_TXEIE; // 打开TXEIE中断
    1165. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1165"> class="hljs-ln-code"> class="hljs-ln-line">}
    1166. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1166"> class="hljs-ln-code"> class="hljs-ln-line">
    1167. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1167"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1168. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1168"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_SendString
    1169. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1169"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送字符串
    1170. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1170"> class="hljs-ln-code"> class="hljs-ln-line"> * 用法请参考printf,及示例中的展示
    1171. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1171"> class="hljs-ln-code"> class="hljs-ln-line"> * 注意,最大发送字节数为512-1个字符,可在函数中修改上限
    1172. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1172"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: const char *pcString, ... (如同printf的用法)
    1173. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1173"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1174. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1174"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1175. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1175"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_SendString(const char *pcString, ...)
    1176. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1176"> class="hljs-ln-code"> class="hljs-ln-line">{
    1177. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1177"> class="hljs-ln-code"> class="hljs-ln-line"> char mBuffer[512] = {0};; // 开辟一个缓存, 并把里面的数据置0
    1178. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1178"> class="hljs-ln-code"> class="hljs-ln-line"> va_list ap; // 新建一个可变参数列表
    1179. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1179"> class="hljs-ln-code"> class="hljs-ln-line"> va_start(ap, pcString); // 列表指向第一个可变参数
    1180. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1180"> class="hljs-ln-code"> class="hljs-ln-line"> vsnprintf(mBuffer, 512, pcString, ap); // 把所有参数,按格式,输出到缓存; 参数2用于限制发送的最大字节数,如果达到上限,则只发送上限值-1; 最后1字节自动置'\0'
    1181. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1181"> class="hljs-ln-code"> class="hljs-ln-line"> va_end(ap); // 清空可变参数列表
    1182. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1182"> class="hljs-ln-code"> class="hljs-ln-line"> UART4_SendData((uint8_t *)mBuffer, strlen(mBuffer)); // 把字节存放环形缓冲,排队准备发送
    1183. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1183"> class="hljs-ln-code"> class="hljs-ln-line">}
    1184. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1184"> class="hljs-ln-code"> class="hljs-ln-line">
    1185. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1185"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1186. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1186"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_SendAT
    1187. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1187"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送AT命令, 并等待返回信息
    1188. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1188"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char *pcString AT指令字符串
    1189. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1189"> class="hljs-ln-code"> class="hljs-ln-line"> * char *pcAckString 期待的指令返回信息字符串
    1190. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1190"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usTimeOut 发送命令后等待的时间,毫秒
    1191. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1191"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1192. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1192"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回 值: 0-执行失败、1-执行正常
    1193. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1193"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1194. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1194"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART4_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs)
    1195. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1195"> class="hljs-ln-code"> class="hljs-ln-line">{
    1196. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1196"> class="hljs-ln-code"> class="hljs-ln-line"> UART4_ClearRx(); // 清0
    1197. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1197"> class="hljs-ln-code"> class="hljs-ln-line"> UART4_SendString(pcAT); // 发送AT指令字符串
    1198. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1198"> class="hljs-ln-code"> class="hljs-ln-line">
    1199. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1199"> class="hljs-ln-code"> class="hljs-ln-line"> while (usTimeOutMs--) // 判断是否起时(这里只作简单的循环判断次数处理)
    1200. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1200"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1201. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1201"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART4_GetRxNum()) // 判断是否接收到数据
    1202. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1202"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1203. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1203"> class="hljs-ln-code"> class="hljs-ln-line"> UART4_ClearRx(); // 清0接收字节数; 注意:接收到的数据内容 ,是没有被清0的
    1204. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1204"> class="hljs-ln-code"> class="hljs-ln-line"> if (strstr((char *)UART4_GetRxData(), pcAckString)) // 判断返回数据中是否有期待的字符
    1205. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1205"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 返回:0-超时没有返回、1-正常返回期待值
    1206. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1206"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1207. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1207"> class="hljs-ln-code"> class="hljs-ln-line"> delay_ms(1); // 延时; 用于超时退出处理,避免死等
    1208. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1208"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1209. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1209"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 返回:0-超时、返回异常,1-正常返回期待值
    1210. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1210"> class="hljs-ln-code"> class="hljs-ln-line">}
    1211. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1211"> class="hljs-ln-code"> class="hljs-ln-line">
    1212. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1212"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1213. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1213"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_GetRxNum
    1214. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1214"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据的字节数
    1215. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1215"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1216. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1216"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 0=没有接收到数据,非0=新一帧数据的字节数
    1217. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1217"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1218. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1218"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART4_GetRxNum(void)
    1219. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1219"> class="hljs-ln-code"> class="hljs-ln-line">{
    1220. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1220"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART4.usRxNum ;
    1221. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1221"> class="hljs-ln-code"> class="hljs-ln-line">}
    1222. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1222"> class="hljs-ln-code"> class="hljs-ln-line">
    1223. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1223"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1224. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1224"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_GetRxData
    1225. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1225"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据 (数据的地址)
    1226. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1226"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1227. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1227"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 数据的地址(uint8_t*)
    1228. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1228"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1229. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1229"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t *UART4_GetRxData(void)
    1230. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1230"> class="hljs-ln-code"> class="hljs-ln-line">{
    1231. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1231"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART4.puRxData ;
    1232. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1232"> class="hljs-ln-code"> class="hljs-ln-line">}
    1233. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1233"> class="hljs-ln-code"> class="hljs-ln-line">
    1234. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1234"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1235. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1235"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_ClearRx
    1236. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1236"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 清理最后一帧数据的缓存
    1237. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1237"> class="hljs-ln-code"> class="hljs-ln-line"> * 主要是清0字节数,因为它是用来判断接收的标准
    1238. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1238"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1239. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1239"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1240. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1240"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1241. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1241"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_ClearRx(void)
    1242. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1242"> class="hljs-ln-code"> class="hljs-ln-line">{
    1243. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1243"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.usRxNum = 0 ;
    1244. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1244"> class="hljs-ln-code"> class="hljs-ln-line">}
    1245. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1245"> class="hljs-ln-code"> class="hljs-ln-line">#endif // endif UART4_EN
    1246. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1246"> class="hljs-ln-code"> class="hljs-ln-line">
    1247. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1247"> class="hljs-ln-code"> class="hljs-ln-line">
    1248. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1248"> class="hljs-ln-code"> class="hljs-ln-line">
    1249. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1249"> class="hljs-ln-code"> class="hljs-ln-line">
    1250. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1250"> class="hljs-ln-code"> class="hljs-ln-line">
    1251. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1251"> class="hljs-ln-code"> class="hljs-ln-line">// UART-5 //
    1252. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1252"> class="hljs-ln-code"> class="hljs-ln-line">/
    1253. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1253"> class="hljs-ln-code"> class="hljs-ln-line">#if UART5_EN
    1254. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1254"> class="hljs-ln-code"> class="hljs-ln-line">
    1255. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1255"> class="hljs-ln-code"> class="hljs-ln-line">static xUSATR_TypeDef xUART5 = { 0 }; // 定义 UART5 的收发结构体
    1256. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1256"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart5RxData[UART5_RX_BUF_SIZE]; // 定义 UART5 的接收缓存
    1257. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1257"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart5TxFiFoData[UART5_TX_BUF_SIZE]; // 定义 UART5 的发送缓存
    1258. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1258"> class="hljs-ln-code"> class="hljs-ln-line">
    1259. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1259"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1260. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1260"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_Init
    1261. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1261"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 初始化UART5的通信引脚、协议参数、中断优先级
    1262. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1262"> class="hljs-ln-code"> class="hljs-ln-line"> * 引脚:TX-PC12、RX-PD2
    1263. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1263"> class="hljs-ln-code"> class="hljs-ln-line"> * 协议:波特率-None-8-1
    1264. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1264"> class="hljs-ln-code"> class="hljs-ln-line"> * 发送:发送中断
    1265. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1265"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收:接收+空闲中断
    1266. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1266"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1267. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1267"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ulBaudrate 通信波特率
    1268. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1268"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1269. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1269"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1270. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1270"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_Init(uint32_t ulBaudrate)
    1271. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1271"> class="hljs-ln-code"> class="hljs-ln-line">{
    1272. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1272"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    1273. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1273"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    1274. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1274"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // 使能GPIOC
    1275. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1275"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); // 使能GPIOD
    1276. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1276"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); // 使能UART5
    1277. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1277"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 UART
    1278. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1278"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE); // 使能重置
    1279. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1279"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE); // 取消重置
    1280. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1280"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    1281. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1281"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStructure = {0}; // GPIO 初始化结构体
    1282. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1282"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; // TX 引脚
    1283. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1283"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 引脚功能
    1284. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1284"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 输出速度
    1285. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1285"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOC, &GPIO_InitStructure); // 初始化引脚
    1286. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1286"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    1287. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1287"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // RX 引脚
    1288. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1288"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // RX引脚工作模式:上拉输入; 如果使用浮空输入,引脚空置时可能产生误输入; 当电路上为一主多从电路时,可以使用复用开漏模式
    1289. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1289"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOD, &GPIO_InitStructure); // 初始化引脚
    1290. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1290"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 USART
    1291. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1291"> class="hljs-ln-code"> class="hljs-ln-line"> USART_DeInit(UART5); // 复位串口
    1292. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1292"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitTypeDef USART_InitStructure = {0}; // UART配置结构体
    1293. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1293"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_BaudRate = ulBaudrate; // 波特率
    1294. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1294"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 位长
    1295. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1295"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位
    1296. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1296"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Parity = USART_Parity_No; // 奇偶校验
    1297. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1297"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 硬件流控制
    1298. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1298"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 使能工作模式
    1299. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1299"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Init(UART5, &USART_InitStructure); // 初始化串口
    1300. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1300"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    1301. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1301"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(UART5, USART_IT_TXE, DISABLE); // 发送中断 使能
    1302. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1302"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); // 接收中断 使能
    1303. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1303"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(UART5, USART_IT_IDLE, ENABLE); // 空闲中断 使能
    1304. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1304"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    1305. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1305"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitTypeDef NVIC_InitStructure = {0}; // 优先级配置结构体
    1306. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1306"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannel = UART5_IRQn; // 中断编号
    1307. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1307"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 0 ; // 抢占优先级
    1308. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1308"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelSubPriority = 0; // 子优先级
    1309. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1309"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
    1310. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1310"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_Init(&NVIC_InitStructure); // 初始化中断优先级
    1311. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1311"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,打开串口
    1312. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1312"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Cmd(UART5, ENABLE); // 配置完成,打开串口, 开始工作
    1313. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1313"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1314. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1314"> class="hljs-ln-code"> class="hljs-ln-line">
    1315. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1315"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER
    1316. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1316"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    1317. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1317"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOC_CLK_ENABLE(); // 使能GPIOC
    1318. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1318"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOD_CLK_ENABLE(); // 使能GPIOD
    1319. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1319"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_UART5_CLK_ENABLE(); // 使能UART5
    1320. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1320"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 UART
    1321. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1321"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_UART5_FORCE_RESET(); // 使能重置
    1322. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1322"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_UART5_RELEASE_RESET(); // 取消重置
    1323. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1323"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    1324. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1324"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStruct = {0}; // 声明初始化要用到的结构体
    1325. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1325"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_12 ; // 引脚 TX-PC12
    1326. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1326"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // 工作模式
    1327. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1327"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_NOPULL; // 上下拉
    1328. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1328"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // 引脚速率
    1329. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1329"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); // 初始化引脚工作模式
    1330. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1330"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    1331. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1331"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_2; // 引脚 RX-PD2
    1332. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1332"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // 工作模式
    1333. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1333"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_PULLUP; // 上下拉
    1334. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1334"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // 引脚速率
    1335. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1335"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); // 初始化引脚工作模式
    1336. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1336"> class="hljs-ln-code"> class="hljs-ln-line"> // 计算波特率参数
    1337. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1337"> class="hljs-ln-code"> class="hljs-ln-line"> float temp;
    1338. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1338"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t mantissa, fraction;
    1339. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1339"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新系统运行频率全局值; 函数SystemCoreClock( ),在标准库、HAL库通用
    1340. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1340"> class="hljs-ln-code"> class="hljs-ln-line"> temp = (float)(SystemCoreClock / 2) / (ulBaudrate * 16); // 波特率公式计算; UART5挂载在APB1, 时钟为系统时钟的2分频; 全局变量SystemCoreClock,在标准库、HAL库通用;
    1341. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1341"> class="hljs-ln-code"> class="hljs-ln-line"> mantissa = temp; // 整数部分
    1342. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1342"> class="hljs-ln-code"> class="hljs-ln-line"> fraction = (temp - mantissa) * 16; // 小数部分
    1343. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1343"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> BRR = mantissa << 4 | fraction; // 设置波特率
    1344. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1344"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    1345. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1345"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 = 0; // 清0
    1346. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1346"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x01 << 2; // 接收使能[02]: 0=失能、1=使能
    1347. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1347"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x01 << 3; // 发送使能[03]:0=失能、1=使能
    1348. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1348"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x00 << 9; // 奇偶校验[09]:0=偶Even、1=奇Odd; 注意:使用奇偶校验,下面两项要置1
    1349. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1349"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x00 << 10; // 校验位 [10]:0=禁用、1=使能; 注意,使用奇偶校验,此位要置1 (否则无效、数据错乱)
    1350. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1350"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x00 << 12; // 数据位 [12]:0=8位、 1=9位; 注意:使用奇偶校验,此位要置1 (否则数据会发生错乱)
    1351. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1351"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR2 = 0; // 数据清0
    1352. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1352"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR2 &= ~(0x03 << 12); // 停止位[13:12]:00b=1个停止位、01b=0.5个停止位、10b=2个停止位、11b=1.5个停止位
    1353. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1353"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR3 = 0; // 数据清0
    1354. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1354"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR3 &= ~(0x01 << 6); // DMA接收[6]: 0=禁止、1=使能
    1355. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1355"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR3 &= ~(0x01 << 7); // DMA发送[7]: 0=禁止、1=使能
    1356. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1356"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    1357. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1357"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 &= ~(0x01 << 7); // 关闭发送中断
    1358. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1358"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x01 << 5; // 使能接收中断: 接收缓冲区非空
    1359. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1359"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x01 << 4; // 使能空闲中断:超过1字节时间没收到新数据
    1360. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1360"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> SR = ~(0x00F0); // 清理中断标志
    1361. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1361"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    1362. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1362"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_SetPriority(UART5_IRQn, 0, 0); // 设置指定中断的优先级; 参数:中断请求编号、抢占级、子优先级
    1363. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1363"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_EnableIRQ(UART5_IRQn); // 使能、启用指定的中断
    1364. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1364"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,打开串口
    1365. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1365"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x01 << 13; // 使能UART开始工作
    1366. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1366"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1367. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1367"> class="hljs-ln-code"> class="hljs-ln-line">
    1368. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1368"> class="hljs-ln-code"> class="hljs-ln-line"> // 关联缓冲区
    1369. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1369"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.puRxData = uaUart5RxData; // 获取接收缓冲区的地址
    1370. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1370"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.puTxFiFoData = uaUart5TxFiFoData; // 获取发送缓冲区的地址
    1371. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1371"> class="hljs-ln-code"> class="hljs-ln-line"> // 输出提示
    1372. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1372"> class="hljs-ln-code"> class="hljs-ln-line"> printf("UART5 初始化配置 %d-None-8-1; 已完成初始化配置、收发配置\r", ulBaudrate);
    1373. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1373"> class="hljs-ln-code"> class="hljs-ln-line">}
    1374. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1374"> class="hljs-ln-code"> class="hljs-ln-line">
    1375. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1375"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1376. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1376"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_IRQHandler
    1377. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1377"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART5的接收中断、空闲中断、发送中断
    1378. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1378"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1379. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1379"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1380. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1380"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数,当产生中断事件时,由硬件调用。
    1381. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1381"> class="hljs-ln-code"> class="hljs-ln-line"> * 如果使用本文件代码,在工程文件的其它地方,要注释同名函数,否则冲突。
    1382. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1382"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1383. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1383"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_IRQHandler(void)
    1384. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1384"> class="hljs-ln-code"> class="hljs-ln-line">{
    1385. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1385"> class="hljs-ln-code"> class="hljs-ln-line"> static uint16_t cnt = 0; // 接收字节数累计:每一帧数据已接收到的字节数
    1386. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1386"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t rxTemp[UART5_RX_BUF_SIZE]; // 接收数据缓存数组:每新接收1个字节,先顺序存放到这里,当一帧接收完(发生空闲中断), 再转存到外部缓存:xUARTx.puRxData[ ]
    1387. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1387"> class="hljs-ln-code"> class="hljs-ln-line">
    1388. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1388"> class="hljs-ln-code"> class="hljs-ln-line"> // 发送中断:用于把环形缓冲的数据,逐字节发出
    1389. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1389"> class="hljs-ln-code"> class="hljs-ln-line"> if ((UART5->SR & USART_SR_TXE) && (UART5->CR1 & USART_CR1_TXEIE)) // 检查发送寄存器空中断使能,且发送寄存器为空; TXE(发送数据寄存器空)、TXEIE(发送缓冲区空中断使能)
    1390. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1390"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1391. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1391"> class="hljs-ln-code"> class="hljs-ln-line"> UART5->DR = xUART5.puTxFiFoData[xUART5.usTxFiFoTail++]; // 从FIFO队列中取出一个数据,放入USART的发送寄存器(硬件会自动发出),然后将FIFO的尾指针递增,指向下一个要发送的数据
    1392. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1392"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART5.usTxFiFoTail == UART5_TX_BUF_SIZE) // 检查FIFO尾指针是否到达了FIFO队列的末尾
    1393. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1393"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.usTxFiFoTail = 0; // 将尾指针重置为0,实现环形队列的功能
    1394. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1394"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART5.usTxFiFoTail == xUART5.usTxFiFoData) // 检查FIFO尾指针是否追上了头指针,即所有数据是否都已发送完毕
    1395. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1395"> class="hljs-ln-code"> class="hljs-ln-line"> UART5->CR1 &= ~USART_CR1_TXEIE; // 关闭发送寄存器空中断,防止中断服务程序被不必要地调用
    1396. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1396"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1397. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1397"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1398. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1398"> class="hljs-ln-code"> class="hljs-ln-line">
    1399. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1399"> class="hljs-ln-code"> class="hljs-ln-line"> // 接收中断:用于逐个字节接收,存放到临时缓存
    1400. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1400"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART5->SR & USART_SR_RXNE) // 检查RXNE(读数据寄存器非空标志位); RXNE中断清理方法:读DR时自动清理;
    1401. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1401"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1402. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1402"> class="hljs-ln-code"> class="hljs-ln-line"> if (cnt == UART5_RX_BUF_SIZE) // 当前帧已接收的字节量,已满缓存区的大小; 为避免溢出,本包后面接收到的数据直接舍弃;
    1403. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1403"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1404. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1404"> class="hljs-ln-code"> class="hljs-ln-line"> printf("警告:UART5单帧接收量,已超出接收缓存大小!\r修改文件bsp_UART.h的UART5_RX_BUF_SIZE值,可解决此问题!\r");
    1405. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1405"> class="hljs-ln-code"> class="hljs-ln-line"> UART5->DR; // 读取数据寄存器的数据,但不保存.主要作用:读DR时自动清理接收中断标志;
    1406. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1406"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1407. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1407"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1408. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1408"> class="hljs-ln-code"> class="hljs-ln-line"> rxTemp[cnt++] = UART5->DR ; // 把新收到的字节数据,顺序存放到RXTemp数组中;注意:读取DR时自动清零中断位
    1409. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1409"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1410. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1410"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1411. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1411"> class="hljs-ln-code"> class="hljs-ln-line">
    1412. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1412"> class="hljs-ln-code"> class="hljs-ln-line"> // 空闲中断:用于判断一帧数据结束,整理数据到外部备读
    1413. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1413"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART5->SR & USART_SR_IDLE) // 检查IDLE(空闲中断标志位); IDLE中断标志清理方法:序列清零,USART1 ->SR; USART1 ->DR;
    1414. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1414"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1415. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1415"> class="hljs-ln-code"> class="hljs-ln-line"> memcpy(xUART5.puRxData, rxTemp, UART5_RX_BUF_SIZE); // 把本帧接收到的数据,存入到结构体的数组成员xUARTx.puRxData中, 等待处理; 注意:复制的是整个数组,包括0值,以方便字符串输出时尾部以0作字符串结束符
    1416. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1416"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.usRxNum = cnt; // 把接收到的字节数,存入到结构体变量xUARTx.usRxNum中;
    1417. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1417"> class="hljs-ln-code"> class="hljs-ln-line"> cnt = 0; // 接收字节数累计器,清零; 准备下一次的接收
    1418. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1418"> class="hljs-ln-code"> class="hljs-ln-line"> memset(rxTemp, 0, UART5_RX_BUF_SIZE); // 接收数据缓存数组,清零; 准备下一次的接收
    1419. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1419"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> SR;
    1420. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1420"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> DR; // 清零IDLE中断标志位!! 序列清零,顺序不能错!!
    1421. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1421"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1422. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1422"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1423. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1423"> class="hljs-ln-code"> class="hljs-ln-line">
    1424. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1424"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1425. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1425"> class="hljs-ln-code"> class="hljs-ln-line">}
    1426. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1426"> class="hljs-ln-code"> class="hljs-ln-line">
    1427. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1427"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1428. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1428"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_SendData
    1429. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1429"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过中断发送数据
    1430. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1430"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】本函数可发送各种数据,而不限于字符串,如int,char
    1431. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1431"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】注意h文件中所定义的发缓冲区大小、注意数据压入缓冲区的速度与串口发送速度的冲突
    1432. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1432"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* pudata 需发送数据的地址
    1433. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1433"> class="hljs-ln-code"> class="hljs-ln-line"> * uint8_t usNum 发送的字节数 ,数量受限于h文件中设置的发送缓存区大小宏定义
    1434. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1434"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1435. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1435"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1436. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1436"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_SendData(uint8_t *pudata, uint16_t usNum)
    1437. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1437"> class="hljs-ln-code"> class="hljs-ln-line">{
    1438. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1438"> class="hljs-ln-code"> class="hljs-ln-line"> for (uint16_t i = 0; i < usNum; i++) // 把数据放入环形缓冲区
    1439. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1439"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1440. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1440"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.puTxFiFoData[xUART5.usTxFiFoData++] = pudata[i]; // 把字节放到缓冲区最后的位置,然后指针后移
    1441. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1441"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART5.usTxFiFoData == UART5_TX_BUF_SIZE) // 如果指针位置到达缓冲区的最大值,则归0
    1442. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1442"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.usTxFiFoData = 0;
    1443. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1443"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1444. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1444"> class="hljs-ln-code"> class="hljs-ln-line">
    1445. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1445"> class="hljs-ln-code"> class="hljs-ln-line"> if ((UART5->CR1 & USART_CR1_TXEIE) == 0) // 检查USART寄存器的发送缓冲区空置中断(TXEIE)是否已打开
    1446. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1446"> class="hljs-ln-code"> class="hljs-ln-line"> UART5->CR1 |= USART_CR1_TXEIE; // 打开TXEIE中断
    1447. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1447"> class="hljs-ln-code"> class="hljs-ln-line">}
    1448. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1448"> class="hljs-ln-code"> class="hljs-ln-line">
    1449. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1449"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1450. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1450"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_SendString
    1451. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1451"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送字符串
    1452. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1452"> class="hljs-ln-code"> class="hljs-ln-line"> * 用法请参考printf,及示例中的展示
    1453. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1453"> class="hljs-ln-code"> class="hljs-ln-line"> * 注意,最大发送字节数为512-1个字符,可在函数中修改上限
    1454. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1454"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: const char *pcString, ... (如同printf的用法)
    1455. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1455"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1456. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1456"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1457. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1457"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_SendString(const char *pcString, ...)
    1458. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1458"> class="hljs-ln-code"> class="hljs-ln-line">{
    1459. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1459"> class="hljs-ln-code"> class="hljs-ln-line"> char mBuffer[512] = {0};; // 开辟一个缓存, 并把里面的数据置0
    1460. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1460"> class="hljs-ln-code"> class="hljs-ln-line"> va_list ap; // 新建一个可变参数列表
    1461. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1461"> class="hljs-ln-code"> class="hljs-ln-line"> va_start(ap, pcString); // 列表指向第一个可变参数
    1462. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1462"> class="hljs-ln-code"> class="hljs-ln-line"> vsnprintf(mBuffer, 512, pcString, ap); // 把所有参数,按格式,输出到缓存; 参数2用于限制发送的最大字节数,如果达到上限,则只发送上限值-1; 最后1字节自动置'\0'
    1463. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1463"> class="hljs-ln-code"> class="hljs-ln-line"> va_end(ap); // 清空可变参数列表
    1464. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1464"> class="hljs-ln-code"> class="hljs-ln-line"> UART5_SendData((uint8_t *)mBuffer, strlen(mBuffer)); // 把字节存放环形缓冲,排队准备发送
    1465. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1465"> class="hljs-ln-code"> class="hljs-ln-line">}
    1466. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1466"> class="hljs-ln-code"> class="hljs-ln-line">
    1467. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1467"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1468. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1468"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_SendAT
    1469. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1469"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送AT命令, 并等待返回信息
    1470. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1470"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char *pcString AT指令字符串
    1471. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1471"> class="hljs-ln-code"> class="hljs-ln-line"> * char *pcAckString 期待的指令返回信息字符串
    1472. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1472"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usTimeOut 发送命令后等待的时间,毫秒
    1473. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1473"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1474. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1474"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回 值: 0-执行失败、1-执行正常
    1475. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1475"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1476. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1476"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART5_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs)
    1477. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1477"> class="hljs-ln-code"> class="hljs-ln-line">{
    1478. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1478"> class="hljs-ln-code"> class="hljs-ln-line"> UART5_ClearRx(); // 清0
    1479. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1479"> class="hljs-ln-code"> class="hljs-ln-line"> UART5_SendString(pcAT); // 发送AT指令字符串
    1480. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1480"> class="hljs-ln-code"> class="hljs-ln-line">
    1481. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1481"> class="hljs-ln-code"> class="hljs-ln-line"> while (usTimeOutMs--) // 判断是否起时(这里只作简单的循环判断次数处理)
    1482. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1482"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1483. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1483"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART5_GetRxNum()) // 判断是否接收到数据
    1484. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1484"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1485. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1485"> class="hljs-ln-code"> class="hljs-ln-line"> UART5_ClearRx(); // 清0接收字节数; 注意:接收到的数据内容 ,是没有被清0的
    1486. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1486"> class="hljs-ln-code"> class="hljs-ln-line"> if (strstr((char *)UART5_GetRxData(), pcAckString)) // 判断返回数据中是否有期待的字符
    1487. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1487"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 返回:0-超时没有返回、1-正常返回期待值
    1488. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1488"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1489. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1489"> class="hljs-ln-code"> class="hljs-ln-line"> delay_ms(1); // 延时; 用于超时退出处理,避免死等
    1490. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1490"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1491. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1491"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 返回:0-超时、返回异常,1-正常返回期待值
    1492. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1492"> class="hljs-ln-code"> class="hljs-ln-line">}
    1493. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1493"> class="hljs-ln-code"> class="hljs-ln-line">
    1494. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1494"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1495. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1495"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_GetRxNum
    1496. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1496"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据的字节数
    1497. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1497"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1498. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1498"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 0=没有接收到数据,非0=新一帧数据的字节数
    1499. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1499"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1500. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1500"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART5_GetRxNum(void)
    1501. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1501"> class="hljs-ln-code"> class="hljs-ln-line">{
    1502. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1502"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART5.usRxNum ;
    1503. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1503"> class="hljs-ln-code"> class="hljs-ln-line">}
    1504. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1504"> class="hljs-ln-code"> class="hljs-ln-line">
    1505. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1505"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1506. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1506"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_GetRxData
    1507. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1507"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据 (数据的地址)
    1508. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1508"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1509. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1509"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 数据的地址(uint8_t*)
    1510. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1510"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1511. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1511"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t *UART5_GetRxData(void)
    1512. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1512"> class="hljs-ln-code"> class="hljs-ln-line">{
    1513. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1513"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART5.puRxData ;
    1514. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1514"> class="hljs-ln-code"> class="hljs-ln-line">}
    1515. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1515"> class="hljs-ln-code"> class="hljs-ln-line">
    1516. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1516"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1517. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1517"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_ClearRx
    1518. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1518"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 清理最后一帧数据的缓存
    1519. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1519"> class="hljs-ln-code"> class="hljs-ln-line"> * 主要是清0字节数,因为它是用来判断接收的标准
    1520. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1520"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1521. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1521"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1522. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1522"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1523. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1523"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_ClearRx(void)
    1524. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1524"> class="hljs-ln-code"> class="hljs-ln-line">{
    1525. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1525"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.usRxNum = 0 ;
    1526. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1526"> class="hljs-ln-code"> class="hljs-ln-line">}
    1527. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1527"> class="hljs-ln-code"> class="hljs-ln-line">#endif // endif UART5_EN
    1528. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1528"> class="hljs-ln-code"> class="hljs-ln-line">
    1529. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1529"> class="hljs-ln-code"> class="hljs-ln-line">
    1530. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1530"> class="hljs-ln-code"> class="hljs-ln-line">
    1531. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1531"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1532. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1532"> class="hljs-ln-code"> class="hljs-ln-line">
    1533. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1533"> class="hljs-ln-code"> class="hljs-ln-line">
    1534. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1534"> class="hljs-ln-code"> class="hljs-ln-line">
    1535. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1535"> class="hljs-ln-code"> class="hljs-ln-line">/ ModbusCRC16 校验 /
    1536. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1536"> class="hljs-ln-code"> class="hljs-ln-line">///
    1537. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1537"> class="hljs-ln-code"> class="hljs-ln-line">
    1538. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1538"> class="hljs-ln-code"> class="hljs-ln-line">// CRC16查表法值表数组
    1539. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1539"> class="hljs-ln-code"> class="hljs-ln-line">// 查表法相比直接计算CRC值,大约可减少一半运算时间
    1540. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1540"> class="hljs-ln-code"> class="hljs-ln-line">static const uint8_t aModbusCRC16[] =
    1541. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1541"> class="hljs-ln-code"> class="hljs-ln-line">{
    1542. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1542"> class="hljs-ln-code"> class="hljs-ln-line"> // 高位
    1543. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1543"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1544. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1544"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1545. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1545"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1546. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1546"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1547. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1547"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1548. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1548"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1549. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1549"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1550. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1550"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1551. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1551"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1552. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1552"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1553. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1553"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1554. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1554"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1555. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1555"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1556. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1556"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1557. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1557"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1558. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1558"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1559. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1559"> class="hljs-ln-code"> class="hljs-ln-line"> // 低位
    1560. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1560"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04,
    1561. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1561"> class="hljs-ln-code"> class="hljs-ln-line"> 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8,
    1562. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1562"> class="hljs-ln-code"> class="hljs-ln-line"> 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
    1563. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1563"> class="hljs-ln-code"> class="hljs-ln-line"> 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10,
    1564. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1564"> class="hljs-ln-code"> class="hljs-ln-line"> 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
    1565. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1565"> class="hljs-ln-code"> class="hljs-ln-line"> 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
    1566. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1566"> class="hljs-ln-code"> class="hljs-ln-line"> 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C,
    1567. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1567"> class="hljs-ln-code"> class="hljs-ln-line"> 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0,
    1568. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1568"> class="hljs-ln-code"> class="hljs-ln-line"> 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
    1569. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1569"> class="hljs-ln-code"> class="hljs-ln-line"> 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
    1570. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1570"> class="hljs-ln-code"> class="hljs-ln-line"> 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C,
    1571. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1571"> class="hljs-ln-code"> class="hljs-ln-line"> 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
    1572. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1572"> class="hljs-ln-code"> class="hljs-ln-line"> 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54,
    1573. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1573"> class="hljs-ln-code"> class="hljs-ln-line"> 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98,
    1574. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1574"> class="hljs-ln-code"> class="hljs-ln-line"> 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
    1575. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1575"> class="hljs-ln-code"> class="hljs-ln-line"> 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
    1576. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1576"> class="hljs-ln-code"> class="hljs-ln-line">};
    1577. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1577"> class="hljs-ln-code"> class="hljs-ln-line">
    1578. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1578"> class="hljs-ln-code"> class="hljs-ln-line">
    1579. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1579"> class="hljs-ln-code"> class="hljs-ln-line">/**********************************************************************************************************
    1580. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1580"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: Modbus_AddCRC16
    1581. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1581"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 在数据末尾追加两字节的ModbusCRC16校验值
    1582. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1582"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 1、函数将根据传入的原数据,计算CRC16值,在数据末尾追加两字节的CRC16校验值;
    1583. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1583"> class="hljs-ln-code"> class="hljs-ln-line"> * 2、注意传入的数组地址,要至少预留2字节的空间,用于追加CRC16的校验值;
    1584. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1584"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1585. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1585"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* _pcData:参与计算校验值的数据
    1586. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1586"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t _usLen :数据字节数; 长度为数据的原字节数即可,不用加2;
    1587. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1587"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1588. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1588"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回: 无
    1589. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1589"> class="hljs-ln-code"> class="hljs-ln-line">**********************************************************************************************************/
    1590. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1590"> class="hljs-ln-code"> class="hljs-ln-line">void Modbus_AddCRC16(uint8_t *_pcData, uint16_t _usLen)
    1591. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1591"> class="hljs-ln-code"> class="hljs-ln-line">{
    1592. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1592"> class="hljs-ln-code"> class="hljs-ln-line"> uint8_t ucCRCHi = 0xFF; // CRC值高位
    1593. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1593"> class="hljs-ln-code"> class="hljs-ln-line"> uint8_t ucCRCLo = 0xFF; // CRC值低位
    1594. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1594"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t usIndex; // 索引
    1595. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1595"> class="hljs-ln-code"> class="hljs-ln-line">
    1596. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1596"> class="hljs-ln-code"> class="hljs-ln-line"> while (_usLen--) // 开始逐字节查表
    1597. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1597"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1598. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1598"> class="hljs-ln-code"> class="hljs-ln-line"> usIndex = ucCRCHi ^ *_pcData++; // 注意,这里指针地址加1了
    1599. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1599"> class="hljs-ln-code"> class="hljs-ln-line"> ucCRCHi = ucCRCLo ^ aModbusCRC16[usIndex];
    1600. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1600"> class="hljs-ln-code"> class="hljs-ln-line"> ucCRCLo = aModbusCRC16[usIndex + 256];
    1601. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1601"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1602. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1602"> class="hljs-ln-code"> class="hljs-ln-line"> _pcData[0] = ucCRCHi; // 末尾第1字节,追加CRC16的高位
    1603. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1603"> class="hljs-ln-code"> class="hljs-ln-line"> _pcData[1] = ucCRCLo; // 末尾第2字节,追加CRC16的低位
    1604. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1604"> class="hljs-ln-code"> class="hljs-ln-line">}
    1605. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1605"> class="hljs-ln-code"> class="hljs-ln-line">
    1606. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1606"> class="hljs-ln-code"> class="hljs-ln-line">
    1607. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1607"> class="hljs-ln-code"> class="hljs-ln-line">
    1608. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1608"> class="hljs-ln-code"> class="hljs-ln-line">/**********************************************************************************************************
    1609. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1609"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: Modbus_CheckCRC16
    1610. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1610"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 对带ModbusCRC16校验的数据段进行校验
    1611. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1611"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1612. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1612"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* _pcData:数据段地址
    1613. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1613"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t _usLen :数据段的长度(字节数); 长度是整包数据的字节数,即包含ModbusCRC16值的长度,不用减2;
    1614. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1614"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1615. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1615"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回: 0-错误、1-正确
    1616. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1616"> class="hljs-ln-code"> class="hljs-ln-line">*******************************************************************************************/
    1617. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1617"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t Modbus_CheckCRC16(uint8_t *_pcData, uint16_t _usLen)
    1618. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1618"> class="hljs-ln-code"> class="hljs-ln-line">{
    1619. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1619"> class="hljs-ln-code"> class="hljs-ln-line"> uint8_t ucCRCHi = 0xFF; // CRC值高位
    1620. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1620"> class="hljs-ln-code"> class="hljs-ln-line"> uint8_t ucCRCLo = 0xFF; // CRC值低位
    1621. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1621"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t usIndex; // 数组的索引
    1622. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1622"> class="hljs-ln-code"> class="hljs-ln-line">
    1623. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1623"> class="hljs-ln-code"> class="hljs-ln-line"> _usLen -= 2; // 字节数-2,不计算原数据末尾两字节(ModbusCRC16值)
    1624. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1624"> class="hljs-ln-code"> class="hljs-ln-line">
    1625. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1625"> class="hljs-ln-code"> class="hljs-ln-line"> while (_usLen--) // 开始逐字节查表获得ModbusCRC16值
    1626. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1626"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1627. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1627"> class="hljs-ln-code"> class="hljs-ln-line"> usIndex = ucCRCHi ^ *_pcData++; // 注意,这里指针地址递增加1
    1628. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1628"> class="hljs-ln-code"> class="hljs-ln-line"> ucCRCHi = ucCRCLo ^ aModbusCRC16[usIndex];
    1629. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1629"> class="hljs-ln-code"> class="hljs-ln-line"> ucCRCLo = aModbusCRC16[usIndex + 256];
    1630. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1630"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1631. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1631"> class="hljs-ln-code"> class="hljs-ln-line">
    1632. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1632"> class="hljs-ln-code"> class="hljs-ln-line"> if ((ucCRCHi == *_pcData++) && (ucCRCLo == *_pcData)) // 把数据段的CRC16校验值,与原数据末尾的CRC16值相比较
    1633. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1633"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 成功匹配; 返回: 1
    1634. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1634"> class="hljs-ln-code"> class="hljs-ln-line">
    1635. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1635"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 错误; 返回:0
    1636. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1636"> class="hljs-ln-code"> class="hljs-ln-line">}
    1637. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1637"> class="hljs-ln-code"> class="hljs-ln-line">
    1638. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1638"> class="hljs-ln-code"> class="hljs-ln-line">
    1639. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1639"> class="hljs-ln-code"> class="hljs-ln-line">
    1640. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1640"> class="hljs-ln-code"> class="hljs-ln-line">
    1641. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1641"> class="hljs-ln-code"> class="hljs-ln-line">
    1642. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1642"> class="hljs-ln-code"> class="hljs-ln-line">/ 辅助功能 /
    1643. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1643"> class="hljs-ln-code"> class="hljs-ln-line">///
    1644. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1644"> class="hljs-ln-code"> class="hljs-ln-line">
    1645. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1645"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1646. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1646"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: showData
    1647. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1647"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 把数据,经串口1,发送到串口助手上,方便观察
    1648. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1648"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t *rxData 数据地址
    1649. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1649"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t rxNum 字节数
    1650. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1650"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1651. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1651"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1652. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1652"> class="hljs-ln-code"> class="hljs-ln-line">void showData(uint8_t *rxData, uint16_t rxNum)
    1653. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1653"> class="hljs-ln-code"> class="hljs-ln-line">{
    1654. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1654"> class="hljs-ln-code"> class="hljs-ln-line">#if 0 // 写法1
    1655. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1655"> class="hljs-ln-code"> class="hljs-ln-line"> printf("字节数: %d \r", rxNum); // 显示字节数
    1656. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1656"> class="hljs-ln-code"> class="hljs-ln-line"> printf("ASCII 显示数据: %s\r", (char *)rxData); // 显示数据,以ASCII方式显示,即以字符串的方式显示
    1657. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1657"> class="hljs-ln-code"> class="hljs-ln-line"> printf("16进制显示数据: "); // 显示数据,以16进制方式,显示每一个字节的值
    1658. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1658"> class="hljs-ln-code"> class="hljs-ln-line"> while (rxNum--) // 逐个字节判断,只要不为'\0', 就继续
    1659. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1659"> class="hljs-ln-code"> class="hljs-ln-line"> printf("0x%X ", *rxData++); // 格式化
    1660. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1660"> class="hljs-ln-code"> class="hljs-ln-line"> printf("\r\r"); // 显示换行
    1661. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1661"> class="hljs-ln-code"> class="hljs-ln-line">#else // 写法2
    1662. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1662"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_SendString("字节数:%d \r", rxNum); // 显示字节数
    1663. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1663"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_SendString("ASCII : %s\r", (char *)rxData); // 显示数据,以ASCII方式显示,即以字符串的方式显示
    1664. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1664"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_SendString("16进制: "); // 显示数据,以16进制方式,显示每一个字节的值
    1665. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1665"> class="hljs-ln-code"> class="hljs-ln-line"> while (rxNum--) // 逐个字节判断,只要不为'\0', 就继续
    1666. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1666"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_SendString("0x%X ", *rxData++); // 格式化
    1667. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1667"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_SendString("\r\r"); // 显示换行
    1668. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1668"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1669. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1669"> class="hljs-ln-code"> class="hljs-ln-line">}
    1670. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1670"> class="hljs-ln-code"> class="hljs-ln-line">
    1671. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1671"> class="hljs-ln-code"> class="hljs-ln-line">
    1672. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1672"> class="hljs-ln-code"> class="hljs-ln-line">
    1673. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1673"> class="hljs-ln-code"> class="hljs-ln-line">
    1674. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1674"> class="hljs-ln-code"> class="hljs-ln-line">
    1675. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1675"> class="hljs-ln-code"> class="hljs-ln-line">// printf //
    1676. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1676"> class="hljs-ln-code"> class="hljs-ln-line">///
    1677. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1677"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1678. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1678"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: printf函数支持代码
    1679. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1679"> class="hljs-ln-code"> class="hljs-ln-line"> * 【特别注意】加入以下代码, 使用printf函数时, 不再需要选择use MicroLIB
    1680. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1680"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数:
    1681. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1681"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值:
    1682. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1682"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 最后修改_2020年07月15日
    1683. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1683"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1684. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1684"> class="hljs-ln-code"> class="hljs-ln-line">#pragma import(__use_no_semihosting)
    1685. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1685"> class="hljs-ln-code"> class="hljs-ln-line">
    1686. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1686"> class="hljs-ln-code"> class="hljs-ln-line">struct __FILE
    1687. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1687"> class="hljs-ln-code"> class="hljs-ln-line">{
    1688. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1688"> class="hljs-ln-code"> class="hljs-ln-line"> int handle;
    1689. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1689"> class="hljs-ln-code"> class="hljs-ln-line">}; // 标准库需要的支持函数
    1690. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1690"> class="hljs-ln-code"> class="hljs-ln-line">
    1691. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1691"> class="hljs-ln-code"> class="hljs-ln-line">FILE __stdout; // FILE 在stdio.h文件
    1692. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1692"> class="hljs-ln-code"> class="hljs-ln-line">void _sys_exit(int x)
    1693. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1693"> class="hljs-ln-code"> class="hljs-ln-line">{
    1694. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1694"> class="hljs-ln-code"> class="hljs-ln-line"> x = x; // 定义_sys_exit()以避免使用半主机模式
    1695. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1695"> class="hljs-ln-code"> class="hljs-ln-line">}
    1696. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1696"> class="hljs-ln-code"> class="hljs-ln-line">
    1697. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1697"> class="hljs-ln-code"> class="hljs-ln-line">int fputc(int ch, FILE *f) // 重定向fputc函数,使printf的输出,由fputc输出到UART, 这里使用串口1(USART1)
    1698. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1698"> class="hljs-ln-code"> class="hljs-ln-line">{
    1699. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1699"> class="hljs-ln-code"> class="hljs-ln-line">#if 0 // 方式1-使用常用的方式发送数据,比较容易理解,但等待耗时大
    1700. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1700"> class="hljs-ln-code"> class="hljs-ln-line"> while ((USART1->SR & 0X40) == 0); // 等待上一次串口数据发送完成
    1701. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1701"> class="hljs-ln-code"> class="hljs-ln-line"> USART1->DR = (u8) ch; // 写DR,串口1将发送数据
    1702. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1702"> class="hljs-ln-code"> class="hljs-ln-line">#else
    1703. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1703"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_SendData((uint8_t *)&ch, 1); // 方式2-使用队列+中断方式发送数据; 无需像方式1那样等待耗时,但要借助已写好的函数、环形缓冲
    1704. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1704"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1705. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1705"> class="hljs-ln-code"> class="hljs-ln-line"> return ch;
    1706. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1706"> class="hljs-ln-code"> class="hljs-ln-line">}
    1707. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1707"> class="hljs-ln-code"> class="hljs-ln-line">
    1708. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1708"> class="hljs-ln-code"> class="hljs-ln-line">// 文件结尾,需要保留至少1空行
    class="hide-preCode-box"> class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}" onclick="hljs.signin(event)">


    八、STM32F407 源代码

     可复制以下代码到文件,也可以直接下载:

    bsp_UART.c + bsp_UART.h

    1、bsp_UART.h

    1. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1"> class="hljs-ln-code"> class="hljs-ln-line">#ifndef __BSP__UART_H
    2. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="2"> class="hljs-ln-code"> class="hljs-ln-line">#define __BSP__UART_H
    3. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="3"> class="hljs-ln-code"> class="hljs-ln-line">/***********************************************************************************************************************************
    4. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="4"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【代码编写】 魔女开发板团队
    5. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="5"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【淘 宝】 https://demoboard.taobao.com
    6. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="6"> class="hljs-ln-code"> class="hljs-ln-line"> ***********************************************************************************************************************************
    7. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="7"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【文件名称】 bsp_UART.h
    8. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="8"> class="hljs-ln-code"> class="hljs-ln-line"> **
    9. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="9"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【 功 能 】 串口通信底层驱动(UART1、UART2、UART3、UART4、UART5、UART6)
    10. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="10"> class="hljs-ln-code"> class="hljs-ln-line"> ** 波特率-None-8-1
    11. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="11"> class="hljs-ln-code"> class="hljs-ln-line"> ** 调用全局声明中的串口函数,即可完成初始化、发送、接收.
    12. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="12"> class="hljs-ln-code"> class="hljs-ln-line"> **
    13. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="13"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【 约 定 】 本文件所用串口通信,均为异步通信。
    14. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="14"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024年起更新的示例,串口函数命名时统一使用"UART",而不使用旧文件中的"USART".
    15. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="15"> class="hljs-ln-code"> class="hljs-ln-line"> **
    16. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="16"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【适用平台】 STM32F407 + keil5 + HAL库/标准库
    17. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="17"> class="hljs-ln-code"> class="hljs-ln-line"> **
    18. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="18"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【串口引脚】 各个串口的初始化函数UARTx_Init(),将按以下默认引脚进行初始化:
    19. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="19"> class="hljs-ln-code"> class="hljs-ln-line"> ** 1- UART1 TX_PA9 RX_PA10 特别说明:魔女开发板系列,均板载USB转TTL,PCB已布线连接好UART1, 使用和烧录用的同一USB接口,即可通过UART1和电脑进行通信。具体查看资料文件夹中的说明文件。
    20. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="20"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2- UART2 TX_PA2 RX_PA3
    21. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="21"> class="hljs-ln-code"> class="hljs-ln-line"> ** 3- UART3 TX_PB10 RX_PB11
    22. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="22"> class="hljs-ln-code"> class="hljs-ln-line"> ** 4- UART4 TX_PC10 RX_PC11
    23. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="23"> class="hljs-ln-code"> class="hljs-ln-line"> ** 5- UART5 TX_PC12 RX_PD2
    24. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="24"> class="hljs-ln-code"> class="hljs-ln-line"> ** 6- UART6 TX_PC6 RX_PC7
    25. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="25"> class="hljs-ln-code"> class="hljs-ln-line"> **
    26. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="26"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【移植说明】 1- 如果使用CubeMX配置的工程,无须对UART进行任何配置。
    27. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="27"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2- 适用于HAL库、标准库工程,通过下面操作,均可直接使用。
    28. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="28"> class="hljs-ln-code"> class="hljs-ln-line"> ** 3- 复制本工程的UART文件夹,到目标工程文件夹中;
    29. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="29"> class="hljs-ln-code"> class="hljs-ln-line"> ** 4- 添加头文件路径: Keil > Option > C/C++ > Include Paths;
    30. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="30"> class="hljs-ln-code"> class="hljs-ln-line"> ** 5- 添加C文件到工程: Keil > 左侧工程管理器中双击目标文件夹 > 选择 bsp_UART.c;
    31. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="31"> class="hljs-ln-code"> class="hljs-ln-line"> ** 6- 添加文件引用: #include "bsp_uart.h",即哪个文件要用串口功能,就在其代码文件顶部添加引用;
    32. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="32"> class="hljs-ln-code"> class="hljs-ln-line"> **
    33. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="33"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【代码使用】 每组串口,已封装好7个函数 (初始化1个、发送3个、接收3个)。下面以UART1作示范说明:
    34. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="34"> class="hljs-ln-code"> class="hljs-ln-line"> ** 1、初始化: void UART1_Init(uint32_t ulBaudrate); // 初始化串口; 配置GPIO引脚PA9+PA10、配置通信参数:波特率-None-8-1、配置中断
    35. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="35"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2、发送指定长度的数据 void UART1_SendData(uint8_t *puData, uint16_t usNum); // 发送指定长度的数据; 参数:数据地址、字节数
    36. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="36"> class="hljs-ln-code"> class="hljs-ln-line"> ** 3、发送字符串 void UART1_SendString(const char *pcString,...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    37. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="37"> class="hljs-ln-code"> class="hljs-ln-line"> ** 4、发送AT指令 uint8_t UART1_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    38. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="38"> class="hljs-ln-code"> class="hljs-ln-line"> ** 5、获取接收的字节数: uint16_t UART1_GetRxNum(void); // 获取接收到的字节数; 如果返回值大于0,即为接收到新一帧数据
    39. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="39"> class="hljs-ln-code"> class="hljs-ln-line"> ** 6、获取接收的数据: uint8_t* UART1_GetRxData(void); // 获取接收到的数据(缓存的地址)
    40. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="40"> class="hljs-ln-code"> class="hljs-ln-line"> ** 7、清零接收标志: void UART1_ClearRx(void); // 清0接收标志,即清0接收到的字节数; 每次处理完成数据,就要调用这个函数清0,方可进入下轮接收判断
    41. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="41"> class="hljs-ln-code"> class="hljs-ln-line"> **
    42. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="42"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【更新记录】 2024-10-22 中断服务函数:使用宏定义代替位操作,以提高可读情; 优化注释;
    43. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="43"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-07-02 增加Modbus_CRC16校验的追加、判断函数
    44. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="44"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-06-30 优化UART初始化:增加奇偶校验3个寄存器位的代码配置行
    45. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="45"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-06-20 优化AT指令函数:名称、机制;
    46. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="46"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-06-07 增加AT指令等待反馈处理函数:UARTx_WaitACK();
    47. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="47"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-06-07 优化函数:UARTx_SendString();
    48. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="48"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-06-06 优化代码适配,使文件能适用于标准库、HAL库工程
    49. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="49"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-04-04 UARTx_SendString(),取消临时缓存的static,以减少RAM占用,适配LVGL移植;
    50. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="50"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-03-27 修改xUSATR_TypeDef结构体,优化缓冲的存储方式
    51. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="51"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-02-04 简化接收函数命名,函数名中使用Rx字符,代替旧函数名称中的Reveived字符;
    52. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="52"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-01-09 取消部分静态变量,并入结构体中,使用调用更直观
    53. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="53"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2024-01-03 完善函数、注释
    54. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="54"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2023-12-25 增加接收函数的封装,取消接收变量(全局)的使用
    55. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="55"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2023-12-23 完成对HAL库的支持
    56. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="56"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2023-01-27 增加宏定义、完善注释
    57. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="57"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-12-16 完善接收机制:取消接收标志,判断接收字节数>0即为接收到新数据
    58. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="58"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-11-03 完善接收函数返回值处理
    59. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="59"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-08-14 增加宏定义:接收缓存区大小设定值,使空间占用更可控;
    60. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="60"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-08-14 修改发送函数名称:UARTx_Printf(),修改为:UARTx_SendString();
    61. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="61"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2020-09-02 文件建立、UART1接收中断、空闲中断、发送中断、DMA收发
    62. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="62"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-06-04 UART1、2、3及UART4、5 的收发完善代码
    63. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="63"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-06-09 完善文件格式、注释
    64. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="64"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2021-06-22 完善注释说明
    65. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="65"> class="hljs-ln-code"> class="hljs-ln-line"> **
    66. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="66"> class="hljs-ln-code"> class="hljs-ln-line">************************************************************************************************************************************/
    67. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="67"> class="hljs-ln-code"> class="hljs-ln-line">#include "stdio.h" // 引用C语言标准库,它定义了标准输入输出函数; 如:printf、scanf、sprintf、fopen 等;
    68. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="68"> class="hljs-ln-code"> class="hljs-ln-line">#include "stdarg.h" // 引用C语言标准库,它定义了处理可变参数的宏; 如:va_start、va_arg、va_end、va_list 等;
    69. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="69"> class="hljs-ln-code"> class="hljs-ln-line">#include "string.h" // 引用C语言标准库,它定义了操作字符串的函数; 如:strcpy、strcmp、strlen、memset、memcpy 等;
    70. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="70"> class="hljs-ln-code"> class="hljs-ln-line">
    71. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="71"> class="hljs-ln-code"> class="hljs-ln-line">
    72. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="72"> class="hljs-ln-code"> class="hljs-ln-line">
    73. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="73"> class="hljs-ln-code"> class="hljs-ln-line">
    74. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="74"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    75. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="75"> class="hljs-ln-code"> class="hljs-ln-line"> #include "stm32f4xx.h" // 引用 标准库的底层支持文件
    76. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="76"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    77. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="77"> class="hljs-ln-code"> class="hljs-ln-line">
    78. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="78"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER
    79. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="79"> class="hljs-ln-code"> class="hljs-ln-line"> #include "stm32f4xx_hal.h" // 引用 HAL库的底层支持文件
    80. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="80"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    81. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="81"> class="hljs-ln-code"> class="hljs-ln-line">
    82. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="82"> class="hljs-ln-code"> class="hljs-ln-line">
    83. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="83"> class="hljs-ln-code"> class="hljs-ln-line">
    84. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="84"> class="hljs-ln-code"> class="hljs-ln-line">/*****************************************************************************
    85. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="85"> class="hljs-ln-code"> class="hljs-ln-line"> ** 移植配置修改区
    86. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="86"> class="hljs-ln-code"> class="hljs-ln-line"> ** 备注:除非有特殊要求,否则,下面参数已通用:RS485、蓝牙模块、ESP8266模块、串口屏等
    87. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="87"> class="hljs-ln-code"> class="hljs-ln-line">****************************************************************************/
    88. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="88"> class="hljs-ln-code"> class="hljs-ln-line">// 串口开关
    89. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="89"> class="hljs-ln-code"> class="hljs-ln-line">#define UART1_EN 1 // 串口1,0=关、1=启用; 倘若没用到UART1, 置0,就不会开辟UART1发送缓存、接收缓存,省一点资源;
    90. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="90"> class="hljs-ln-code"> class="hljs-ln-line">#define UART2_EN 1 // 串口2,0=关、1=启用; 同上;
    91. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="91"> class="hljs-ln-code"> class="hljs-ln-line">#define UART3_EN 1 // 串口3,0=关、1=启用; 同上;
    92. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="92"> class="hljs-ln-code"> class="hljs-ln-line">#define UART4_EN 1 // 串口4,0=关、1=启用; 同上;
    93. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="93"> class="hljs-ln-code"> class="hljs-ln-line">#define UART5_EN 1 // 串口5,0=关、1=启用; 同上;
    94. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="94"> class="hljs-ln-code"> class="hljs-ln-line">#define UART6_EN 1 // 串口5,0=关、1=启用; 同上;
    95. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="95"> class="hljs-ln-code"> class="hljs-ln-line">// 发送缓冲区大小
    96. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="96"> class="hljs-ln-code"> class="hljs-ln-line">#define UART1_TX_BUF_SIZE 2048 // 配置每组UART发送环形缓冲区数组的大小,单位:字节数;
    97. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="97"> class="hljs-ln-code"> class="hljs-ln-line">#define UART2_TX_BUF_SIZE 512 // -- 只有在前面串口开关在打开状态,才会定义具体的缓冲区数组
    98. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="98"> class="hljs-ln-code"> class="hljs-ln-line">#define UART3_TX_BUF_SIZE 512 // -- 默认值:512,此值已能适配大部场景的通信; 如果与ESP8266之类的设备通信,可适当增大此值。
    99. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="99"> class="hljs-ln-code"> class="hljs-ln-line">#define UART4_TX_BUF_SIZE 512 // -- 值范围:1~65535; 注意初始化后,不要超过芯片最大RAM值。
    100. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="100"> class="hljs-ln-code"> class="hljs-ln-line">#define UART5_TX_BUF_SIZE 512 // -- 注意此值是一个环形缓冲区大小,决定每一帧或多帧数据进入发送前的总缓存字节数,先进先出。
    101. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="101"> class="hljs-ln-code"> class="hljs-ln-line">#define UART6_TX_BUF_SIZE 512 //
    102. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="102"> class="hljs-ln-code"> class="hljs-ln-line">// 接收缓冲区大小
    103. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="103"> class="hljs-ln-code"> class="hljs-ln-line">#define UART1_RX_BUF_SIZE 2048 // 配置每组UART接收缓冲区的大小,单位:字节; 此值影响范围:中断里的接收缓存大小,接收后数据缓存的大小
    104. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="104"> class="hljs-ln-code"> class="hljs-ln-line">#define UART2_RX_BUF_SIZE 1024 // --- 当接收到的一帧数据字节数,小于此值时,数据正常;
    105. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="105"> class="hljs-ln-code"> class="hljs-ln-line">#define UART3_RX_BUF_SIZE 1024 // --- 当接收到的一帧数据字节数,超过此值时,超出部分,将在中断中直接弃舍,直到此帧接收结束(发生空闲中断);
    106. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="106"> class="hljs-ln-code"> class="hljs-ln-line">#define UART4_RX_BUF_SIZE 1024 //
    107. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="107"> class="hljs-ln-code"> class="hljs-ln-line">#define UART5_RX_BUF_SIZE 1024 //
    108. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="108"> class="hljs-ln-code"> class="hljs-ln-line">#define UART6_RX_BUF_SIZE 1024 //
    109. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="109"> class="hljs-ln-code"> class="hljs-ln-line">// 结束-配置修改
    110. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="110"> class="hljs-ln-code"> class="hljs-ln-line">
    111. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="111"> class="hljs-ln-code"> class="hljs-ln-line">
    112. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="112"> class="hljs-ln-code"> class="hljs-ln-line">
    113. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="113"> class="hljs-ln-code"> class="hljs-ln-line">/*****************************************************************************
    114. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="114"> class="hljs-ln-code"> class="hljs-ln-line"> ** 声明全局函数
    115. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="115"> class="hljs-ln-code"> class="hljs-ln-line"> **
    116. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="116"> class="hljs-ln-code"> class="hljs-ln-line"> ** 每个串口的函数:
    117. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="117"> class="hljs-ln-code"> class="hljs-ln-line"> ** 初始化 1个 波特率-None-8-1
    118. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="118"> class="hljs-ln-code"> class="hljs-ln-line"> ** 发送 3个 发送指定长度数据、字符串、AT指令
    119. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="119"> class="hljs-ln-code"> class="hljs-ln-line"> ** 接收 3个 获取字节数、获取数据、清0
    120. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="120"> class="hljs-ln-code"> class="hljs-ln-line">****************************************************************************/
    121. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="121"> class="hljs-ln-code"> class="hljs-ln-line">// UART1
    122. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="122"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_Init(uint32_t ulBaudrate); // 初始化串口1; GPIO引脚PA9+PA10、中断优先级、通信参数:波特率可设、8位数据、无校验、1个停止位
    123. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="123"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
    124. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="124"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    125. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="125"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART1_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    126. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="126"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART1_GetRxNum(void); // 获取接收到的最新一帧字节数
    127. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="127"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t * UART1_GetRxData(void); // 获取接收到的数据 (缓存的地址)
    128. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="128"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_ClearRx(void); // 清理接收到的数据 (清理最后一帧字节数,因为它是判断接收的标志)
    129. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="129"> class="hljs-ln-code"> class="hljs-ln-line">// UART2
    130. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="130"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_Init(uint32_t ulBaudrate); // 初始化串口2; GPIO引脚PA2+PA3、中断优先级、通信参数:波特率可设、8位数据、无校验、1个停止位
    131. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="131"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
    132. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="132"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    133. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="133"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART2_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    134. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="134"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART2_GetRxNum(void); // 获取接收到的最新一帧字节数
    135. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="135"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t * UART2_GetRxData(void); // 获取接收到的数据 (缓存的地址)
    136. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="136"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_ClearRx(void); // 清理接收到的数据 (清理最后一帧字节数,因为它是判断接收的标志)
    137. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="137"> class="hljs-ln-code"> class="hljs-ln-line">// UART3
    138. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="138"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_Init(uint32_t ulBaudrate); // 初始化串口3; GPIO引脚PB10+PB11、中断优先级、通信参数:波特率可设、8位数据、无校验、1个停止位
    139. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="139"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
    140. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="140"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    141. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="141"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART3_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    142. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="142"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART3_GetRxNum(void); // 获取接收到的最新一帧字节数
    143. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="143"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t * UART3_GetRxData(void); // 获取接收到的数据 (缓存的地址)
    144. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="144"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_ClearRx(void); // 清理接收到的数据 (清理最后一帧字节数,因为它是判断接收的标志)
    145. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="145"> class="hljs-ln-code"> class="hljs-ln-line">// UART4
    146. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="146"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_Init(uint32_t ulBaudrate); // 初始化串口4; GPIO引脚PC10+PC11、中断优先级、通信参数:波特率可设、8位数据、无校验、1个停止位
    147. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="147"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
    148. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="148"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    149. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="149"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART4_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    150. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="150"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART4_GetRxNum(void); // 获取接收到的最新一帧字节数
    151. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="151"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t * UART4_GetRxData(void); // 获取接收到的数据 (缓存的地址)
    152. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="152"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_ClearRx(void); // 清理接收到的数据 (清理最后一帧字节数,因为它是判断接收的标志)
    153. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="153"> class="hljs-ln-code"> class="hljs-ln-line">// UART5
    154. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="154"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_Init(uint32_t ulBaudrate); // 初始化串口5; GPIO引脚PC12+PD2、中断优先级、通信参数:波特率可设、8位数据、无校验、1个停止位
    155. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="155"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
    156. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="156"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    157. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="157"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART5_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    158. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="158"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART5_GetRxNum(void); // 获取接收到的最新一帧字节数
    159. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="159"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t * UART5_GetRxData(void); // 获取接收到的数据 (缓存的地址)
    160. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="160"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_ClearRx(void); // 清理接收到的数据 (清理最后一帧字节数,因为它是判断接收的标志)
    161. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="161"> class="hljs-ln-code"> class="hljs-ln-line">// UART6
    162. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="162"> class="hljs-ln-code"> class="hljs-ln-line">void UART6_Init(uint32_t ulBaudrate); // 初始化串口6; GPIO引脚PC6+PC7、中断优先级、通信参数:波特率可设、8位数据、无校验、1个停止位
    163. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="163"> class="hljs-ln-code"> class="hljs-ln-line">void UART6_SendData(uint8_t *puData, uint16_t usNum); // 发送指定数据; 参数:数据地址、字节数
    164. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="164"> class="hljs-ln-code"> class="hljs-ln-line">void UART6_SendString(const char *pcString, ...); // 发送字符串; 参数:字符串地址; 使用方法如同printf
    165. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="165"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART6_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs); // 本函数,针对ESP8266、蓝牙模块等AT固件,用于等待返回期待的信息; 参数:AT指令字符串、期待返回信息字符串、ms等待超时; 返回:0-执行失败、1-执行成功
    166. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="166"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART6_GetRxNum(void); // 获取接收到的最新一帧字节数
    167. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="167"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t * UART6_GetRxData(void); // 获取接收到的数据 (缓存的地址)
    168. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="168"> class="hljs-ln-code"> class="hljs-ln-line">void UART6_ClearRx(void); // 清理接收到的数据 (清理最后一帧字节数,因为它是判断接收的标志)
    169. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="169"> class="hljs-ln-code"> class="hljs-ln-line">
    170. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="170"> class="hljs-ln-code"> class="hljs-ln-line">// 辅助函数:Modbus_CRC16校验
    171. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="171"> class="hljs-ln-code"> class="hljs-ln-line">void Modbus_AddCRC16(uint8_t *_pcData, uint16_t _usLen); // 对数据追加2字节的ModbusCRC16校验值到末尾; 参数:原始数据、原始数据字节数; 注意:调用函数后,原始数据会增加2字节
    172. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="172"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t Modbus_CheckCRC16(uint8_t *_pcData, uint16_t _usLen); // 对带ModbusCRC16校验的数据段进行校验; 返回:0-错误、1-匹配正确;
    173. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="173"> class="hljs-ln-code"> class="hljs-ln-line">
    174. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="174"> class="hljs-ln-code"> class="hljs-ln-line">
    175. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="175"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    176. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="176"> class="hljs-ln-code"> class="hljs-ln-line">
    class="hide-preCode-box"> class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}" onclick="hljs.signin(event)">

    2、bsp_UART.c

    1. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1"> class="hljs-ln-code"> class="hljs-ln-line">/***********************************************************************************************************************************
    2. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="2"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【代码编写】 魔女开发板团队
    3. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="3"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【最后版本】 2024-07-08-01
    4. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="4"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【淘 宝】 https://demoboard.taobao.com
    5. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="5"> class="hljs-ln-code"> class="hljs-ln-line"> ***********************************************************************************************************************************
    6. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="6"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【文件名称】 bsp_UART.c
    7. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="7"> class="hljs-ln-code"> class="hljs-ln-line"> **
    8. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="8"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【文件功能】 各UART的GPIO配置、通信协议配置、中断配置,及功能函数实现
    9. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="9"> class="hljs-ln-code"> class="hljs-ln-line"> **
    10. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="10"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【适用平台】 STM32F407 + keil5 + HAL库/标准库
    11. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="11"> class="hljs-ln-code"> class="hljs-ln-line"> **
    12. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="12"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【最后更新] 20240708-1
    13. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="13"> class="hljs-ln-code"> class="hljs-ln-line"> **
    14. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="14"> class="hljs-ln-code"> class="hljs-ln-line"> ** 【特别说明】 1、为什么这个文件中,同时有标准库、HAL库的代码?
    15. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="15"> class="hljs-ln-code"> class="hljs-ln-line"> ** 是为了方便移植!已做好预编译处理,可适用于标准库、HAL库的工程移植。
    16. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="16"> class="hljs-ln-code"> class="hljs-ln-line"> ** 2、为什么UART初始化、中断,用寄存器操作,而不是用更有可读性的HAL库?
    17. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="17"> class="hljs-ln-code"> class="hljs-ln-line"> ** 因为CubeMX配置工程时,如果不对UART进行配置,且打勾只生成需要的文件,工程中将没有UART的HAL支持文件的。
    18. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="18"> class="hljs-ln-code"> class="hljs-ln-line"> ** 另外, 中断里用寄存器操作,与HAL库的重重封装相比,明显地更高效!
    19. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="19"> class="hljs-ln-code"> class="hljs-ln-line">************************************************************************************************************************************/
    20. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="20"> class="hljs-ln-code"> class="hljs-ln-line">#include "bsp_UART.h" // 头文件
    21. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="21"> class="hljs-ln-code"> class="hljs-ln-line">
    22. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="22"> class="hljs-ln-code"> class="hljs-ln-line">
    23. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="23"> class="hljs-ln-code"> class="hljs-ln-line">
    24. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="24"> class="hljs-ln-code"> class="hljs-ln-line">
    25. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="25"> class="hljs-ln-code"> class="hljs-ln-line">/*****************************************************************************
    26. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="26"> class="hljs-ln-code"> class="hljs-ln-line"> ** 声明本地变量
    27. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="27"> class="hljs-ln-code"> class="hljs-ln-line">****************************************************************************/
    28. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="28"> class="hljs-ln-code"> class="hljs-ln-line">typedef struct
    29. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="29"> class="hljs-ln-code"> class="hljs-ln-line">{
    30. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="30"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t usRxNum; // 新一帧数据,接收到多少个字节数据
    31. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="31"> class="hljs-ln-code"> class="hljs-ln-line"> uint8_t *puRxData; // 新一帧数据,数据缓存; 存放的是空闲中断后,从临时接收缓存复制过来的完整数据,并非接收过程中的不完整数据;
    32. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="32"> class="hljs-ln-code"> class="hljs-ln-line">
    33. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="33"> class="hljs-ln-code"> class="hljs-ln-line"> uint8_t *puTxFiFoData; // 发送缓冲区,环形队列; 为了方便理解阅读,没有封装成队列函数
    34. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="34"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t usTxFiFoData ; // 环形缓冲区的队头
    35. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="35"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t usTxFiFoTail ; // 环形缓冲区的队尾
    36. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="36"> class="hljs-ln-code"> class="hljs-ln-line">} xUSATR_TypeDef;
    37. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="37"> class="hljs-ln-code"> class="hljs-ln-line">
    38. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="38"> class="hljs-ln-code"> class="hljs-ln-line">
    39. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="39"> class="hljs-ln-code"> class="hljs-ln-line">
    40. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="40"> class="hljs-ln-code"> class="hljs-ln-line">
    41. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="41"> class="hljs-ln-code"> class="hljs-ln-line">
    42. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="42"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    43. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="43"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: delay_ms
    44. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="44"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: ms 延时函数
    45. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="45"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 1、系统时钟168MHz
    46. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="46"> class="hljs-ln-code"> class="hljs-ln-line"> * 2、打勾:Options/ c++ / One ELF Section per Function
    47. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="47"> class="hljs-ln-code"> class="hljs-ln-line"> 3、编译优化级别:Level 3(-O3)
    48. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="48"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ms 毫秒值
    49. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="49"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    50. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="50"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    51. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="51"> class="hljs-ln-code"> class="hljs-ln-line">static volatile uint32_t ulTimesMS; // 使用volatile声明,防止变量被编译器优化
    52. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="52"> class="hljs-ln-code"> class="hljs-ln-line">static void delay_ms(uint16_t ms)
    53. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="53"> class="hljs-ln-code"> class="hljs-ln-line">{
    54. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="54"> class="hljs-ln-code"> class="hljs-ln-line"> ulTimesMS = ms * 16500;
    55. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="55"> class="hljs-ln-code"> class="hljs-ln-line"> while (ulTimesMS)
    56. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="56"> class="hljs-ln-code"> class="hljs-ln-line"> ulTimesMS--; // 操作外部变量,防止空循环被编译器优化掉
    57. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="57"> class="hljs-ln-code"> class="hljs-ln-line">}
    58. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="58"> class="hljs-ln-code"> class="hljs-ln-line">
    59. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="59"> class="hljs-ln-code"> class="hljs-ln-line">
    60. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="60"> class="hljs-ln-code"> class="hljs-ln-line">
    61. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="61"> class="hljs-ln-code"> class="hljs-ln-line">
    62. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="62"> class="hljs-ln-code"> class="hljs-ln-line">
    63. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="63"> class="hljs-ln-code"> class="hljs-ln-line">// UART-1 ///
    64. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="64"> class="hljs-ln-code"> class="hljs-ln-line">/
    65. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="65"> class="hljs-ln-code"> class="hljs-ln-line">#if UART1_EN
    66. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="66"> class="hljs-ln-code"> class="hljs-ln-line">
    67. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="67"> class="hljs-ln-code"> class="hljs-ln-line">static xUSATR_TypeDef xUART1 = { 0 }; // 定义 UART1 的收发结构体
    68. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="68"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUART1RxData[UART1_RX_BUF_SIZE]; // 定义 UART1 的接收缓存
    69. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="69"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUART1TxFiFoData[UART1_TX_BUF_SIZE]; // 定义 UART1 的发送缓存
    70. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="70"> class="hljs-ln-code"> class="hljs-ln-line">
    71. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="71"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    72. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="72"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_Init
    73. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="73"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 初始化USART1的通信引脚、协议参数、中断优先级
    74. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="74"> class="hljs-ln-code"> class="hljs-ln-line"> * 引脚:TX-PA10、RX-PA11
    75. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="75"> class="hljs-ln-code"> class="hljs-ln-line"> * 协议:波特率-None-8-1
    76. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="76"> class="hljs-ln-code"> class="hljs-ln-line"> * 发送:发送中断
    77. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="77"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收:接收+空闲中断
    78. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="78"> class="hljs-ln-code"> class="hljs-ln-line"> *
    79. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="79"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ulBaudrate 通信波特率
    80. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="80"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    81. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="81"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    82. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="82"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_Init(uint32_t ulBaudrate)
    83. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="83"> class="hljs-ln-code"> class="hljs-ln-line">{
    84. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="84"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    85. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="85"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    86. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="86"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // 使能 GPIOA 时钟
    87. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="87"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 使能 USART1 时钟
    88. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="88"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 引脚的复用功能
    89. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="89"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); // 配置PA9复用功能: USART1
    90. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="90"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); // 配置PA10复用功能:USART1
    91. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="91"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 USART
    92. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="92"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); // 使能重置
    93. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="93"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); // 取消重置
    94. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="94"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    95. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="95"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStructure = {0}; // GPIO 初始化结构体
    96. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="96"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // 引脚编号
    97. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="97"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 引脚方向: 复用功能
    98. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="98"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 输出模式:推挽
    99. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="99"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上下拉:上拉
    100. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="100"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 输出速度:50MHz
    101. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="101"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化:把上述参数,更新到芯片寄存器
    102. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="102"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置R X引脚
    103. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="103"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // 引脚编号
    104. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="104"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化:把上述参数,更新到芯片寄存器
    105. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="105"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    106. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="106"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitTypeDef USART_InitStructure; // 声明USART初始化结构体
    107. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="107"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_BaudRate = ulBaudrate; // 设置波特率
    108. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="108"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 设置字长为8位数据格式
    109. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="109"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_StopBits = USART_StopBits_1; // 设置一个停止位
    110. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="110"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Parity = USART_Parity_No; // 无奇偶校验位
    111. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="111"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制
    112. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="112"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式
    113. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="113"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Init(USART1, &USART_InitStructure); // 初始化USART
    114. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="114"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    115. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="115"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 开启 接收中断
    116. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="116"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); // 开启 空闲中断
    117. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="117"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置中断优先级
    118. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="118"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitTypeDef NVIC_InitStructure = {0}; // 中断优先级配置结构体
    119. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="119"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannel = USART1_IRQn; // 指定中断通道
    120. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="120"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 1; // 设置抢占优先级
    121. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="121"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelSubPriority = 1; // 设置响应优先级
    122. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="122"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelCmd = ENABLE; // 使能中断通道
    123. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="123"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_Init(&NVIC_InitStructure); // 初始化NVIC
    124. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="124"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,开启USART
    125. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="125"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Cmd(USART1, ENABLE); // 使能USART1
    126. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="126"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    127. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="127"> class="hljs-ln-code"> class="hljs-ln-line">
    128. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="128"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER // HAL库 配置
    129. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="129"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    130. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="130"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOA_CLK_ENABLE(); // 使能GPIOA
    131. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="131"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART1_CLK_ENABLE(); // 使能USART1
    132. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="132"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 USART
    133. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="133"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART1_FORCE_RESET(); // 使能重置
    134. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="134"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART1_RELEASE_RESET(); // 取消重置
    135. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="135"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 引脚
    136. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="136"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStruct = {0}; // 声明初始化要用到的结构体
    137. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="137"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10; // 引脚 TX-PA9、RX-PA10
    138. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="138"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // 工作模式
    139. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="139"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_PULLUP; // 上下拉
    140. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="140"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; // 引脚速率
    141. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="141"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Alternate = GPIO_AF7_USART1; // 引脚复用功能
    142. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="142"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // 初始化引脚工作模式
    143. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="143"> class="hljs-ln-code"> class="hljs-ln-line"> // 计算波特率参数
    144. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="144"> class="hljs-ln-code"> class="hljs-ln-line"> float temp;
    145. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="145"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t mantissa, fraction;
    146. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="146"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新系统运行频率全局值; 函数SystemCoreClock( ),在标准库、HAL库通用
    147. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="147"> class="hljs-ln-code"> class="hljs-ln-line"> temp = (float)(SystemCoreClock / 2) / (ulBaudrate * 16); // 波特率公式计算; USART1挂载在APB2, 时钟为系统时钟的2分频; 全局变量SystemCoreClock,在标准库、HAL库通用;
    148. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="148"> class="hljs-ln-code"> class="hljs-ln-line"> mantissa = temp; // 整数部分
    149. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="149"> class="hljs-ln-code"> class="hljs-ln-line"> fraction = (temp - mantissa) * 16; // 小数部分
    150. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="150"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> BRR = mantissa << 4 | fraction; // 设置波特率
    151. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="151"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 USART
    152. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="152"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 = 0; // 清0
    153. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="153"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x01 << 2; // 接收使能[02]: 0=失能、1=使能
    154. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="154"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x01 << 3; // 发送使能[03]:0=失能、1=使能
    155. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="155"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x00 << 9; // 奇偶校验[09]:0=偶Even、1=奇Odd; 注意:使用奇偶校验,下面两项要置1
    156. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="156"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x00 << 10; // 校验位 [10]:0=禁用、1=使能; 注意,使用奇偶校验,此位要置1 (否则无效、数据错乱)
    157. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="157"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x00 << 12; // 数据位 [12]:0=8位、 1=9位; 注意:使用奇偶校验,此位要置1 (否则数据会发生错乱)
    158. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="158"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR2 = 0; // 数据清0
    159. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="159"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR2 &= ~(0x03 << 12); // 停止位[13:12]:00b=1个停止位、01b=0.5个停止位、10b=2个停止位、11b=1.5个停止位
    160. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="160"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR3 = 0; // 数据清0
    161. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="161"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR3 &= ~(0x01 << 6); // DMA接收[6]: 0=禁止、1=使能
    162. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="162"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR3 &= ~(0x01 << 7); // DMA发送[7]: 0=禁止、1=使能
    163. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="163"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    164. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="164"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 &= ~(0x01 << 7); // 关闭发送中断
    165. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="165"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x01 << 5; // 使能接收中断: 接收缓冲区非空
    166. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="166"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x01 << 4; // 使能空闲中断:超过1字节时间没收到新数据
    167. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="167"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> SR = ~(0x00F0); // 清理中断
    168. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="168"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    169. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="169"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_SetPriority(USART1_IRQn, 1, 1); // 设置指定中断的响应优先级; 参数:中断请求编号、抢占级、子优先级
    170. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="170"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_EnableIRQ(USART1_IRQn); // 使能、启用指定的中断
    171. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="171"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,开启USART
    172. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="172"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 -> CR1 |= 0x01 << 13; // 使能UART开始工作
    173. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="173"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    174. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="174"> class="hljs-ln-code"> class="hljs-ln-line">
    175. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="175"> class="hljs-ln-code"> class="hljs-ln-line"> // 关联缓冲区
    176. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="176"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.puRxData = uaUART1RxData; // 关联接收缓冲区的地址
    177. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="177"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.puTxFiFoData = uaUART1TxFiFoData; // 关联发送缓冲区的地址
    178. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="178"> class="hljs-ln-code"> class="hljs-ln-line">
    179. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="179"> class="hljs-ln-code"> class="hljs-ln-line"> // 输出提示
    180. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="180"> class="hljs-ln-code"> class="hljs-ln-line"> printf("\r\r\r=========== STM32F407VE 外设 初始化报告 ===========\r"); // 输出到串口助手
    181. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="181"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新一下系统运行频率变量
    182. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="182"> class="hljs-ln-code"> class="hljs-ln-line"> printf("系统时钟频率 %d MHz\r", SystemCoreClock / 1000000); // 输出到串口助手
    183. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="183"> class="hljs-ln-code"> class="hljs-ln-line"> printf("UART1 初始化配置 %d-None-8-1; 已完成初始化配置、收发配置\r", ulBaudrate); // 输出到串口助手
    184. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="184"> class="hljs-ln-code"> class="hljs-ln-line">}
    185. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="185"> class="hljs-ln-code"> class="hljs-ln-line">
    186. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="186"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    187. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="187"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: USART1_IRQHandler
    188. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="188"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: USART1的接收中断、空闲中断、发送中断
    189. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="189"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    190. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="190"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    191. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="191"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数,当产生中断事件时,由硬件调用。
    192. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="192"> class="hljs-ln-code"> class="hljs-ln-line"> * 如果使用本文件代码,在工程文件的其它地方,要注释同名函数,否则冲突。
    193. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="193"> class="hljs-ln-code"> class="hljs-ln-line">******************************************************************************/
    194. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="194"> class="hljs-ln-code"> class="hljs-ln-line">void USART1_IRQHandler(void)
    195. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="195"> class="hljs-ln-code"> class="hljs-ln-line">{
    196. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="196"> class="hljs-ln-code"> class="hljs-ln-line"> static uint16_t cnt = 0; // 接收字节数累计:每一帧数据已接收到的字节数
    197. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="197"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t rxTemp[UART1_RX_BUF_SIZE]; // 接收数据缓存数组:每新接收1个字节,先顺序存放到这里,当一帧接收完(发生空闲中断), 再转存到外部缓存:xUARTx.puRxData[ ]
    198. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="198"> class="hljs-ln-code"> class="hljs-ln-line">
    199. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="199"> class="hljs-ln-code"> class="hljs-ln-line"> // 发送中断:用于把环形缓冲的数据,逐字节发出
    200. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="200"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART1->SR & USART_SR_TXE) && (USART1->CR1 & USART_CR1_TXEIE)) // 检查发送寄存器空中断使能,且发送寄存器为空; TXE(发送数据寄存器空)、TXEIE(发送缓冲区空中断使能)
    201. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="201"> class="hljs-ln-code"> class="hljs-ln-line"> {
    202. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="202"> class="hljs-ln-code"> class="hljs-ln-line"> USART1->DR = xUART1.puTxFiFoData[xUART1.usTxFiFoTail++]; // 从FIFO队列中取出一个数据,放入USART的发送寄存器(硬件会自动发出),然后将FIFO的尾指针递增,指向下一个要发送的数据
    203. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="203"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART1.usTxFiFoTail == UART1_TX_BUF_SIZE) // 检查FIFO尾指针是否到达了FIFO队列的末尾
    204. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="204"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.usTxFiFoTail = 0; // 将尾指针重置为0,实现环形队列的功能
    205. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="205"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART1.usTxFiFoTail == xUART1.usTxFiFoData) // 检查FIFO尾指针是否追上了头指针,即所有数据是否都已发送完毕
    206. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="206"> class="hljs-ln-code"> class="hljs-ln-line"> USART1->CR1 &= ~USART_CR1_TXEIE; // 关闭发送寄存器空中断,防止中断服务程序被不必要地调用
    207. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="207"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    208. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="208"> class="hljs-ln-code"> class="hljs-ln-line"> }
    209. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="209"> class="hljs-ln-code"> class="hljs-ln-line">
    210. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="210"> class="hljs-ln-code"> class="hljs-ln-line"> // 接收中断:用于逐个字节接收,存放到临时缓存
    211. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="211"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART1->SR & USART_SR_RXNE) // 检查RXNE(读数据寄存器非空标志位); RXNE中断清理方法:读DR时自动清理;
    212. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="212"> class="hljs-ln-code"> class="hljs-ln-line"> {
    213. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="213"> class="hljs-ln-code"> class="hljs-ln-line"> if (cnt == UART1_RX_BUF_SIZE) // 当前帧已接收的字节量,已满缓存区的大小; 为避免溢出,本包后面接收到的数据直接舍弃;
    214. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="214"> class="hljs-ln-code"> class="hljs-ln-line"> {
    215. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="215"> class="hljs-ln-code"> class="hljs-ln-line"> printf("警告:UART1单帧接收量,已超出接收缓存大小!\r修改文件bsp_UART.h的UART1_RX_BUF_SIZE值,可解决此问题!\r");
    216. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="216"> class="hljs-ln-code"> class="hljs-ln-line"> USART1->DR; // 读取数据寄存器的数据,但不保存.主要作用:读DR时自动清理接收中断标志;
    217. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="217"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    218. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="218"> class="hljs-ln-code"> class="hljs-ln-line"> }
    219. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="219"> class="hljs-ln-code"> class="hljs-ln-line"> rxTemp[cnt++] = USART1->DR ; // 把新收到的字节数据,顺序存放到RXTemp数组中;注意:读取DR时自动清零中断位;
    220. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="220"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    221. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="221"> class="hljs-ln-code"> class="hljs-ln-line"> }
    222. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="222"> class="hljs-ln-code"> class="hljs-ln-line">
    223. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="223"> class="hljs-ln-code"> class="hljs-ln-line"> // 空闲中断:用于判断一帧数据结束,整理数据到外部备读
    224. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="224"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART1->SR & USART_SR_IDLE) // 检查IDLE(空闲中断标志位); IDLE中断标志清理方法:序列清零,USART1 ->SR; USART1 ->DR;
    225. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="225"> class="hljs-ln-code"> class="hljs-ln-line"> {
    226. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="226"> class="hljs-ln-code"> class="hljs-ln-line"> memcpy(xUART1.puRxData, rxTemp, UART1_RX_BUF_SIZE); // 把本帧接收到的数据,存入到结构体的数组成员xUARTx.puRxData中, 等待处理; 注意:复制的是整个数组,包括0值,以方便字符串输出时尾部以0作字符串结束符
    227. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="227"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.usRxNum = cnt; // 把接收到的字节数,存入到结构体变量xUARTx.usRxNum中;
    228. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="228"> class="hljs-ln-code"> class="hljs-ln-line"> cnt = 0; // 接收字节数累计器,清零; 准备下一次的接收
    229. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="229"> class="hljs-ln-code"> class="hljs-ln-line"> memset(rxTemp, 0, UART1_RX_BUF_SIZE); // 接收数据缓存数组,清零; 准备下一次的接收
    230. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="230"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 ->SR;
    231. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="231"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 ->DR; // 清零IDLE中断标志位!! 序列清零,顺序不能错!!
    232. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="232"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    233. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="233"> class="hljs-ln-code"> class="hljs-ln-line"> }
    234. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="234"> class="hljs-ln-code"> class="hljs-ln-line">
    235. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="235"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    236. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="236"> class="hljs-ln-code"> class="hljs-ln-line">}
    237. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="237"> class="hljs-ln-code"> class="hljs-ln-line">
    238. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="238"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    239. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="239"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_SendData
    240. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="240"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过中断发送数据
    241. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="241"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】本函数可发送各种数据,而不限于字符串,如int,char
    242. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="242"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】注意h文件中所定义的发缓冲区大小、注意数据压入缓冲区的速度与串口发送速度的冲突
    243. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="243"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* puData 需发送数据的地址
    244. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="244"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usNum 发送的字节数 ,数量受限于h文件中设置的发送缓存区大小宏定义
    245. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="245"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    246. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="246"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    247. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="247"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendData(uint8_t *puData, uint16_t usNum)
    248. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="248"> class="hljs-ln-code"> class="hljs-ln-line">{
    249. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="249"> class="hljs-ln-code"> class="hljs-ln-line"> for (uint16_t i = 0; i < usNum; i++) // 把数据放入环形缓冲区
    250. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="250"> class="hljs-ln-code"> class="hljs-ln-line"> {
    251. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="251"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.puTxFiFoData[xUART1.usTxFiFoData++] = puData[i]; // 把字节放到缓冲区最后的位置,然后指针后移
    252. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="252"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART1.usTxFiFoData == UART1_TX_BUF_SIZE) // 如果指针位置到达缓冲区的最大值,则归0
    253. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="253"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.usTxFiFoData = 0;
    254. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="254"> class="hljs-ln-code"> class="hljs-ln-line"> } // 为了方便阅读理解,这里没有把此部分封装成队列函数,可以自行封装
    255. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="255"> class="hljs-ln-code"> class="hljs-ln-line">
    256. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="256"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART1->CR1 & USART_CR1_TXEIE) == 0) // 检查USART寄存器的发送缓冲区空置中断(TXEIE)是否已打开
    257. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="257"> class="hljs-ln-code"> class="hljs-ln-line"> USART1->CR1 |= USART_CR1_TXEIE; // 打开TXEIE中断
    258. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="258"> class="hljs-ln-code"> class="hljs-ln-line">}
    259. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="259"> class="hljs-ln-code"> class="hljs-ln-line">
    260. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="260"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    261. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="261"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_SendString
    262. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="262"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送字符串
    263. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="263"> class="hljs-ln-code"> class="hljs-ln-line"> * 用法请参考printf,及示例中的展示
    264. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="264"> class="hljs-ln-code"> class="hljs-ln-line"> * 注意,最大发送字节数为512-1个字符,可在函数中修改上限
    265. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="265"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: const char *pcString, ... (如同printf的用法)
    266. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="266"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    267. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="267"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    268. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="268"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendString(const char *pcString, ...)
    269. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="269"> class="hljs-ln-code"> class="hljs-ln-line">{
    270. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="270"> class="hljs-ln-code"> class="hljs-ln-line"> char mBuffer[512] = {0};; // 开辟一个缓存, 并把里面的数据置0
    271. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="271"> class="hljs-ln-code"> class="hljs-ln-line"> va_list ap; // 新建一个可变参数列表
    272. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="272"> class="hljs-ln-code"> class="hljs-ln-line"> va_start(ap, pcString); // 列表指向第一个可变参数
    273. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="273"> class="hljs-ln-code"> class="hljs-ln-line"> vsnprintf(mBuffer, 512, pcString, ap); // 把所有参数,按格式,输出到缓存; 参数2用于限制发送的最大字节数,如果达到上限,则只发送上限值-1; 最后1字节自动置'\0'
    274. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="274"> class="hljs-ln-code"> class="hljs-ln-line"> va_end(ap); // 清空可变参数列表
    275. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="275"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_SendData((uint8_t *)mBuffer, strlen(mBuffer)); // 把字节存放环形缓冲,排队准备发送
    276. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="276"> class="hljs-ln-code"> class="hljs-ln-line">}
    277. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="277"> class="hljs-ln-code"> class="hljs-ln-line">
    278. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="278"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    279. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="279"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_SendAT
    280. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="280"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送AT命令, 并等待返回信息
    281. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="281"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char *pcString AT指令字符串
    282. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="282"> class="hljs-ln-code"> class="hljs-ln-line"> * char *pcAckString 期待的指令返回信息字符串
    283. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="283"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usTimeOut 发送命令后等待的时间,毫秒
    284. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="284"> class="hljs-ln-code"> class="hljs-ln-line"> *
    285. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="285"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回 值: 0-执行失败、1-执行正常
    286. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="286"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    287. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="287"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART1_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs)
    288. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="288"> class="hljs-ln-code"> class="hljs-ln-line">{
    289. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="289"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_ClearRx(); // 清0
    290. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="290"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_SendString(pcAT); // 发送AT指令字符串
    291. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="291"> class="hljs-ln-code"> class="hljs-ln-line"> while (usTimeOutMs--) // 判断是否起时(这里只作简单的循环判断次数处理)
    292. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="292"> class="hljs-ln-code"> class="hljs-ln-line"> {
    293. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="293"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART1_GetRxNum()) // 判断是否接收到数据
    294. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="294"> class="hljs-ln-code"> class="hljs-ln-line"> {
    295. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="295"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_ClearRx(); // 清0接收字节数; 注意:接收到的数据内容 ,是没有被清0的
    296. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="296"> class="hljs-ln-code"> class="hljs-ln-line"> if (strstr((char *)UART1_GetRxData(), pcAckString)) // 判断返回数据中是否有期待的字符
    297. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="297"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 返回:0-超时没有返回、1-正常返回期待值
    298. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="298"> class="hljs-ln-code"> class="hljs-ln-line"> }
    299. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="299"> class="hljs-ln-code"> class="hljs-ln-line"> delay_ms(1); // 延时; 用于超时退出处理,避免死等
    300. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="300"> class="hljs-ln-code"> class="hljs-ln-line"> }
    301. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="301"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 返回:0-超时、返回异常,1-正常返回期待值
    302. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="302"> class="hljs-ln-code"> class="hljs-ln-line">}
    303. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="303"> class="hljs-ln-code"> class="hljs-ln-line">
    304. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="304"> class="hljs-ln-code"> class="hljs-ln-line">
    305. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="305"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    306. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="306"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_SendStringForDMA
    307. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="307"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过DMA发送数据,省了占用中断的时间
    308. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="308"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】字符串,字节数非常多,
    309. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="309"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】1:只适合发送字符串,不适合发送可能含0的数值类数据; 2-时间间隔要足够
    310. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="310"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char strintTemp 要发送的字符串首地址
    311. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="311"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    312. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="312"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数为保留函数,留作用户参考。为了方便移植,本文件对外不再使用本函数。
    313. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="313"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    314. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="314"> class="hljs-ln-code"> class="hljs-ln-line">#if 0
    315. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="315"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_SendStringForDMA(char *stringTemp)
    316. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="316"> class="hljs-ln-code"> class="hljs-ln-line">{
    317. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="317"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t Flag_DmaTxInit = 0; // 用于标记是否已配置DMA发送
    318. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="318"> class="hljs-ln-code"> class="hljs-ln-line"> uint32_t num = 0; // 发送的数量,注意发送的单位不是必须8位的
    319. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="319"> class="hljs-ln-code"> class="hljs-ln-line"> char *t = stringTemp ; // 用于配合计算发送的数量
    320. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="320"> class="hljs-ln-code"> class="hljs-ln-line">
    321. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="321"> class="hljs-ln-code"> class="hljs-ln-line"> while (*t++ != 0) num++; // 计算要发送的数目,这步比较耗时,测试发现每多6个字节,增加1us,单位:8位
    322. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="322"> class="hljs-ln-code"> class="hljs-ln-line">
    323. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="323"> class="hljs-ln-code"> class="hljs-ln-line"> while (DMA1_Channel4->CNDTR > 0); // 重要:如果DMA还在进行上次发送,就等待; 得进完成中断清标志,F4不用这么麻烦,发送完后EN自动清零
    324. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="324"> class="hljs-ln-code"> class="hljs-ln-line"> if (Flag_DmaTxInit == 0) // 是否已进行过USAART_TX的DMA传输配置
    325. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="325"> class="hljs-ln-code"> class="hljs-ln-line"> {
    326. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="326"> class="hljs-ln-code"> class="hljs-ln-line"> Flag_DmaTxInit = 1; // 设置标记,下次调用本函数就不再进行配置了
    327. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="327"> class="hljs-ln-code"> class="hljs-ln-line"> USART1 ->CR3 |= 1 << 7; // 使能DMA发送
    328. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="328"> class="hljs-ln-code"> class="hljs-ln-line"> RCC->AHBENR |= 1 << 0; // 开启DMA1时钟 [0]DMA1 [1]DMA2
    329. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="329"> class="hljs-ln-code"> class="hljs-ln-line">
    330. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="330"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR = 0; // 失能, 清0整个寄存器, DMA必须失能才能配置
    331. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="331"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CNDTR = num; // 传输数据量
    332. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="332"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CMAR = (uint32_t)stringTemp; // 存储器地址
    333. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="333"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CPAR = (uint32_t)&USART1->DR; // 外设地址
    334. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="334"> class="hljs-ln-code"> class="hljs-ln-line">
    335. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="335"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 1 << 4; // 数据传输方向 0:从外设读 1:从存储器读
    336. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="336"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 0 << 5; // 循环模式 0:不循环 1:循环
    337. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="337"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 0 << 6; // 外设地址非增量模式
    338. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="338"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 1 << 7; // 存储器增量模式
    339. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="339"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 0 << 8; // 外设数据宽度为8位
    340. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="340"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 0 << 10; // 存储器数据宽度8位
    341. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="341"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 0 << 12; // 中等优先级
    342. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="342"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 0 << 14; // 非存储器到存储器模式
    343. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="343"> class="hljs-ln-code"> class="hljs-ln-line"> }
    344. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="344"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR &= ~((uint32_t)(1 << 0)); // 失能,DMA必须失能才能配置
    345. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="345"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CNDTR = num; // 传输数据量
    346. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="346"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CMAR = (uint32_t)stringTemp; // 存储器地址
    347. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="347"> class="hljs-ln-code"> class="hljs-ln-line"> DMA1_Channel4->CCR |= 1 << 0; // 开启DMA传输
    348. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="348"> class="hljs-ln-code"> class="hljs-ln-line">}
    349. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="349"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    350. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="350"> class="hljs-ln-code"> class="hljs-ln-line">
    351. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="351"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    352. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="352"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_GetRxNum
    353. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="353"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据的字节数
    354. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="354"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    355. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="355"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 0=没有接收到数据,非0=新一帧数据的字节数
    356. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="356"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    357. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="357"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART1_GetRxNum(void)
    358. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="358"> class="hljs-ln-code"> class="hljs-ln-line">{
    359. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="359"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART1.usRxNum ;
    360. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="360"> class="hljs-ln-code"> class="hljs-ln-line">}
    361. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="361"> class="hljs-ln-code"> class="hljs-ln-line">
    362. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="362"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    363. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="363"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_GetRxData
    364. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="364"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据 (数据的地址)
    365. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="365"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    366. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="366"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 缓存地址(uint8_t*)
    367. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="367"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    368. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="368"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t *UART1_GetRxData(void)
    369. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="369"> class="hljs-ln-code"> class="hljs-ln-line">{
    370. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="370"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART1.puRxData ;
    371. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="371"> class="hljs-ln-code"> class="hljs-ln-line">}
    372. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="372"> class="hljs-ln-code"> class="hljs-ln-line">
    373. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="373"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    374. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="374"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART1_ClearRx
    375. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="375"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 清理最后一帧数据的缓存
    376. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="376"> class="hljs-ln-code"> class="hljs-ln-line"> * 主要是清0字节数,因为它是用来判断接收的标准
    377. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="377"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    378. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="378"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    379. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="379"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    380. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="380"> class="hljs-ln-code"> class="hljs-ln-line">void UART1_ClearRx(void)
    381. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="381"> class="hljs-ln-code"> class="hljs-ln-line">{
    382. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="382"> class="hljs-ln-code"> class="hljs-ln-line"> xUART1.usRxNum = 0 ;
    383. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="383"> class="hljs-ln-code"> class="hljs-ln-line">}
    384. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="384"> class="hljs-ln-code"> class="hljs-ln-line">#endif // endif UART1_EN
    385. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="385"> class="hljs-ln-code"> class="hljs-ln-line">
    386. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="386"> class="hljs-ln-code"> class="hljs-ln-line">
    387. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="387"> class="hljs-ln-code"> class="hljs-ln-line">
    388. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="388"> class="hljs-ln-code"> class="hljs-ln-line">
    389. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="389"> class="hljs-ln-code"> class="hljs-ln-line">
    390. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="390"> class="hljs-ln-code"> class="hljs-ln-line">// UART-2 ///
    391. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="391"> class="hljs-ln-code"> class="hljs-ln-line">/
    392. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="392"> class="hljs-ln-code"> class="hljs-ln-line">#if UART2_EN
    393. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="393"> class="hljs-ln-code"> class="hljs-ln-line">
    394. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="394"> class="hljs-ln-code"> class="hljs-ln-line">static xUSATR_TypeDef xUART2 = { 0 }; // 定义 UART2 的收发结构体
    395. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="395"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUART2RxData[UART2_RX_BUF_SIZE]; // 定义 UART2 的接收缓存
    396. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="396"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUART2TxFiFoData[UART2_TX_BUF_SIZE]; // 定义 UART2 的发送缓存
    397. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="397"> class="hljs-ln-code"> class="hljs-ln-line">
    398. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="398"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    399. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="399"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_Init
    400. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="400"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 初始化USART2的通信引脚、协议参数、中断优先级
    401. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="401"> class="hljs-ln-code"> class="hljs-ln-line"> * 引脚:TX-PA2、RX-PA3
    402. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="402"> class="hljs-ln-code"> class="hljs-ln-line"> * 协议:波特率-None-8-1
    403. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="403"> class="hljs-ln-code"> class="hljs-ln-line"> * 发送:发送中断
    404. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="404"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收:接收+空闲中断
    405. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="405"> class="hljs-ln-code"> class="hljs-ln-line"> *
    406. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="406"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ulBaudrate 通信波特率
    407. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="407"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    408. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="408"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    409. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="409"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_Init(uint32_t ulBaudrate)
    410. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="410"> class="hljs-ln-code"> class="hljs-ln-line">{
    411. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="411"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    412. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="412"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    413. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="413"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // 使能 GPIOA 时钟
    414. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="414"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // 使能 USART2 时钟
    415. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="415"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 引脚的复用功能
    416. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="416"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // 配置PA2复用功能:USART2
    417. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="417"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); // 配置PA3复用功能:USART2
    418. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="418"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 USART
    419. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="419"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE); // 使能重置
    420. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="420"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE); // 取消重置
    421. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="421"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    422. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="422"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStructure = {0}; // GPIO 初始化结构体
    423. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="423"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // 引脚编号:TX_PA2
    424. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="424"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 引脚方向: 复用功能
    425. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="425"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 输出模式:推挽
    426. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="426"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上下拉:上拉
    427. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="427"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 输出速度:50MHz
    428. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="428"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化:把上述参数,更新到芯片寄存器
    429. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="429"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    430. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="430"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // 引脚编号:RX_PA3
    431. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="431"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化:把上述参数,更新到芯片寄存器
    432. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="432"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    433. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="433"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitTypeDef USART_InitStructure; // 声明USART初始化结构体
    434. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="434"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_BaudRate = ulBaudrate; // 设置波特率
    435. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="435"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 设置字长为8位数据格式
    436. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="436"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_StopBits = USART_StopBits_1; // 设置一个停止位
    437. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="437"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Parity = USART_Parity_No; // 无奇偶校验位
    438. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="438"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制
    439. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="439"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式
    440. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="440"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Init(USART2, &USART_InitStructure); // 初始化USART
    441. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="441"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    442. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="442"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // 开启 接收中断
    443. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="443"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); // 开启 空闲中断
    444. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="444"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    445. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="445"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitTypeDef NVIC_InitStructure = {0}; // 中断优先级配置结构体
    446. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="446"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannel = USART2_IRQn; // 指定中断通道
    447. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="447"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 1; // 设置抢占优先级
    448. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="448"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelSubPriority = 1; // 设置响应优先级
    449. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="449"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelCmd = ENABLE; // 使能中断通道
    450. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="450"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_Init(&NVIC_InitStructure); // 初始化NVIC
    451. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="451"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,开启USART
    452. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="452"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Cmd(USART2, ENABLE); // 使能USART
    453. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="453"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    454. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="454"> class="hljs-ln-code"> class="hljs-ln-line">
    455. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="455"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER // HAL库 配置
    456. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="456"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    457. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="457"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOA_CLK_ENABLE(); // 使能GPIOA
    458. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="458"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART2_CLK_ENABLE(); // 使能USART2
    459. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="459"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 USART
    460. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="460"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART2_FORCE_RESET(); // 使能重置
    461. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="461"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART2_RELEASE_RESET(); // 取消重置
    462. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="462"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 GPIO引脚
    463. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="463"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStruct = {0}; // 声明初始化要用到的结构体
    464. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="464"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3; // 引脚 TX-PA2、RX-PA3
    465. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="465"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // 工作模式
    466. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="466"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_PULLUP; // 上下拉
    467. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="467"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; // 引脚速率
    468. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="468"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Alternate = GPIO_AF7_USART2; // 引脚复用功能
    469. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="469"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // 初始化引脚工作模式
    470. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="470"> class="hljs-ln-code"> class="hljs-ln-line"> // 计算波特率参数
    471. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="471"> class="hljs-ln-code"> class="hljs-ln-line"> float temp;
    472. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="472"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t mantissa, fraction;
    473. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="473"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新系统运行频率全局值; 函数SystemCoreClock( ),在标准库、HAL库通用
    474. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="474"> class="hljs-ln-code"> class="hljs-ln-line"> temp = (float)(SystemCoreClock / 4) / (ulBaudrate * 16); // 波特率公式计算; USART2挂载在APB1, 时钟为系统时钟的4分频; 全局变量SystemCoreClock,在标准库、HAL库通用;
    475. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="475"> class="hljs-ln-code"> class="hljs-ln-line"> mantissa = temp; // 整数部分
    476. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="476"> class="hljs-ln-code"> class="hljs-ln-line"> fraction = (temp - mantissa) * 16; // 小数部分
    477. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="477"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> BRR = mantissa << 4 | fraction; // 设置波特率
    478. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="478"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    479. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="479"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 = 0; // 清0
    480. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="480"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x01 << 2; // 接收使能[02]: 0=失能、1=使能
    481. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="481"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x01 << 3; // 发送使能[03]:0=失能、1=使能
    482. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="482"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x00 << 9; // 奇偶校验[09]:0=偶Even、1=奇Odd; 注意:使用奇偶校验,下面两项要置1
    483. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="483"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x00 << 10; // 校验位 [10]:0=禁用、1=使能; 注意,使用奇偶校验,此位要置1 (否则无效、数据错乱)
    484. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="484"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x00 << 12; // 数据位 [12]:0=8位、 1=9位; 注意:使用奇偶校验,此位要置1 (否则数据会发生错乱)
    485. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="485"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR2 = 0; // 数据清0
    486. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="486"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR2 &= ~(0x03 << 12); // 停止位[13:12]:00b=1个停止位、01b=0.5个停止位、10b=2个停止位、11b=1.5个停止位
    487. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="487"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR3 = 0; // 数据清0
    488. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="488"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR3 &= ~(0x01 << 6); // DMA接收[6]: 0=禁止、1=使能
    489. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="489"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR3 &= ~(0x01 << 7); // DMA发送[7]: 0=禁止、1=使能
    490. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="490"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    491. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="491"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 &= ~(0x01 << 7); // 关闭发送中断
    492. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="492"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x01 << 5; // 使能接收中断: 接收缓冲区非空
    493. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="493"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x01 << 4; // 使能空闲中断:超过1字节时间没收到新数据
    494. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="494"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> SR = ~(0x00F0); // 清理中断
    495. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="495"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    496. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="496"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_SetPriority(USART2_IRQn, 1, 1); // 设置指定中断的响应优先级; 参数:中断请求编号、抢占级、子优先级
    497. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="497"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_EnableIRQ(USART2_IRQn); // 使能、启用指定的中断
    498. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="498"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,开启USART
    499. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="499"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 -> CR1 |= 0x01 << 13; // 使能UART开始工作
    500. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="500"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    501. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="501"> class="hljs-ln-code"> class="hljs-ln-line"> // 关联缓冲区
    502. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="502"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.puRxData = uaUART2RxData; // 获取接收缓冲区的地址
    503. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="503"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.puTxFiFoData = uaUART2TxFiFoData; // 获取发送缓冲区的地址
    504. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="504"> class="hljs-ln-code"> class="hljs-ln-line"> // 输出提示
    505. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="505"> class="hljs-ln-code"> class="hljs-ln-line"> printf("UART2 初始化配置 %d-None-8-1; 已完成初始化配置、收发配置\r", ulBaudrate);
    506. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="506"> class="hljs-ln-code"> class="hljs-ln-line">}
    507. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="507"> class="hljs-ln-code"> class="hljs-ln-line">
    508. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="508"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    509. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="509"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: USART2_IRQHandler
    510. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="510"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: USART2的接收中断、空闲中断、发送中断
    511. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="511"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    512. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="512"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    513. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="513"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数,当产生中断事件时,由硬件调用。
    514. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="514"> class="hljs-ln-code"> class="hljs-ln-line"> * 如果使用本文件代码,在工程文件的其它地方,要注释同名函数,否则冲突。
    515. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="515"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    516. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="516"> class="hljs-ln-code"> class="hljs-ln-line">void USART2_IRQHandler(void)
    517. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="517"> class="hljs-ln-code"> class="hljs-ln-line">{
    518. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="518"> class="hljs-ln-code"> class="hljs-ln-line"> static uint16_t cnt = 0; // 接收字节数累计:每一帧数据已接收到的字节数
    519. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="519"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t rxTemp[UART2_RX_BUF_SIZE]; // 接收数据缓存数组:每新接收1个字节,先顺序存放到这里,当一帧接收完(发生空闲中断), 再转存到外部缓存:xUARTx.puRxData[ ]
    520. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="520"> class="hljs-ln-code"> class="hljs-ln-line">
    521. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="521"> class="hljs-ln-code"> class="hljs-ln-line"> // 发送中断:用于把环形缓冲的数据,逐字节发出
    522. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="522"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART2->SR & USART_SR_TXE) && (USART2->CR1 & USART_CR1_TXEIE)) // 检查发送寄存器空中断使能,且发送寄存器为空; TXE(发送数据寄存器空)、TXEIE(发送缓冲区空中断使能)
    523. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="523"> class="hljs-ln-code"> class="hljs-ln-line"> {
    524. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="524"> class="hljs-ln-code"> class="hljs-ln-line"> USART2->DR = xUART2.puTxFiFoData[xUART2.usTxFiFoTail++]; // 从FIFO队列中取出一个数据,放入USART的发送寄存器(硬件会自动发出),然后将FIFO的尾指针递增,指向下一个要发送的数据
    525. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="525"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART2.usTxFiFoTail == UART2_TX_BUF_SIZE) // 检查FIFO尾指针是否到达了FIFO队列的末尾
    526. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="526"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.usTxFiFoTail = 0; // 将尾指针重置为0,实现环形队列的功能
    527. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="527"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART2.usTxFiFoTail == xUART2.usTxFiFoData) // 检查FIFO尾指针是否追上了头指针,即所有数据是否都已发送完毕
    528. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="528"> class="hljs-ln-code"> class="hljs-ln-line"> USART2->CR1 &= ~USART_CR1_TXEIE; // 关闭发送寄存器空中断,防止中断服务程序被不必要地调用
    529. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="529"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    530. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="530"> class="hljs-ln-code"> class="hljs-ln-line"> }
    531. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="531"> class="hljs-ln-code"> class="hljs-ln-line">
    532. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="532"> class="hljs-ln-code"> class="hljs-ln-line"> // 接收中断:用于逐个字节接收,存放到临时缓存
    533. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="533"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART2->SR & USART_SR_RXNE) // 检查RXNE(读数据寄存器非空标志位); RXNE中断清理方法:读DR时自动清理;
    534. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="534"> class="hljs-ln-code"> class="hljs-ln-line"> {
    535. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="535"> class="hljs-ln-code"> class="hljs-ln-line"> if (cnt == UART2_RX_BUF_SIZE) // 当前帧已接收的字节量,已满缓存区的大小; 为避免溢出,本包后面接收到的数据直接舍弃;
    536. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="536"> class="hljs-ln-code"> class="hljs-ln-line"> {
    537. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="537"> class="hljs-ln-code"> class="hljs-ln-line"> printf("警告:UART2单帧接收量,已超出接收缓存大小!\r修改文件bsp_UART.h的UART2_RX_BUF_SIZE值,可解决此问题!\r");
    538. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="538"> class="hljs-ln-code"> class="hljs-ln-line"> USART2->DR; // 读取数据寄存器的数据,但不保存.主要作用:读DR时自动清理接收中断标志;
    539. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="539"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    540. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="540"> class="hljs-ln-code"> class="hljs-ln-line"> }
    541. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="541"> class="hljs-ln-code"> class="hljs-ln-line"> rxTemp[cnt++] = USART2->DR ; // 把新收到的字节数据,顺序存放到RXTemp数组中;注意:读取DR时自动清零中断位;
    542. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="542"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    543. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="543"> class="hljs-ln-code"> class="hljs-ln-line"> }
    544. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="544"> class="hljs-ln-code"> class="hljs-ln-line">
    545. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="545"> class="hljs-ln-code"> class="hljs-ln-line"> // 空闲中断:用于判断一帧数据结束,整理数据到外部备读
    546. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="546"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART2->SR & USART_SR_IDLE) // 检查IDLE(空闲中断标志位); IDLE中断标志清理方法:序列清零,USART1 ->SR; USART1 ->DR;
    547. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="547"> class="hljs-ln-code"> class="hljs-ln-line"> {
    548. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="548"> class="hljs-ln-code"> class="hljs-ln-line"> memcpy(xUART2.puRxData, rxTemp, UART2_RX_BUF_SIZE); // 把本帧接收到的数据,存入到结构体的数组成员xUARTx.puRxData中, 等待处理; 注意:复制的是整个数组,包括0值,以方便字符串输出时尾部以0作字符串结束符
    549. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="549"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.usRxNum = cnt; // 把接收到的字节数,存入到结构体变量xUARTx.usRxNum中;
    550. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="550"> class="hljs-ln-code"> class="hljs-ln-line"> cnt = 0; // 接收字节数累计器,清零; 准备下一次的接收
    551. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="551"> class="hljs-ln-code"> class="hljs-ln-line"> memset(rxTemp, 0, UART2_RX_BUF_SIZE); // 接收数据缓存数组,清零; 准备下一次的接收
    552. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="552"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 ->SR;
    553. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="553"> class="hljs-ln-code"> class="hljs-ln-line"> USART2 ->DR; // 清零IDLE中断标志位!! 序列清零,顺序不能错!!
    554. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="554"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    555. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="555"> class="hljs-ln-code"> class="hljs-ln-line"> }
    556. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="556"> class="hljs-ln-code"> class="hljs-ln-line">
    557. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="557"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    558. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="558"> class="hljs-ln-code"> class="hljs-ln-line">}
    559. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="559"> class="hljs-ln-code"> class="hljs-ln-line">
    560. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="560"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    561. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="561"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_SendData
    562. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="562"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过中断发送数据
    563. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="563"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】本函数可发送各种数据,而不限于字符串,如int,char
    564. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="564"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】注意h文件中所定义的发缓冲区大小、注意数据压入缓冲区的速度与串口发送速度的冲突
    565. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="565"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* puData 需发送数据的地址
    566. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="566"> class="hljs-ln-code"> class="hljs-ln-line"> * uint8_t usNum 发送的字节数 ,数量受限于h文件中设置的发送缓存区大小宏定义
    567. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="567"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    568. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="568"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    569. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="569"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_SendData(uint8_t *puData, uint16_t usNum)
    570. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="570"> class="hljs-ln-code"> class="hljs-ln-line">{
    571. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="571"> class="hljs-ln-code"> class="hljs-ln-line"> for (uint16_t i = 0; i < usNum; i++) // 把数据放入环形缓冲区
    572. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="572"> class="hljs-ln-code"> class="hljs-ln-line"> {
    573. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="573"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.puTxFiFoData[xUART2.usTxFiFoData++] = puData[i]; // 把字节放到缓冲区最后的位置,然后指针后移
    574. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="574"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART2.usTxFiFoData == UART2_TX_BUF_SIZE) // 如果指针位置到达缓冲区的最大值,则归0
    575. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="575"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.usTxFiFoData = 0;
    576. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="576"> class="hljs-ln-code"> class="hljs-ln-line"> }
    577. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="577"> class="hljs-ln-code"> class="hljs-ln-line">
    578. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="578"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART2->CR1 & USART_CR1_TXEIE) == 0) // 检查USART寄存器的发送缓冲区空置中断(TXEIE)是否已打开
    579. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="579"> class="hljs-ln-code"> class="hljs-ln-line"> USART2->CR1 |= USART_CR1_TXEIE; // 打开TXEIE中断
    580. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="580"> class="hljs-ln-code"> class="hljs-ln-line">}
    581. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="581"> class="hljs-ln-code"> class="hljs-ln-line">
    582. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="582"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    583. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="583"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_SendString
    584. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="584"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送字符串
    585. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="585"> class="hljs-ln-code"> class="hljs-ln-line"> * 用法请参考printf,及示例中的展示
    586. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="586"> class="hljs-ln-code"> class="hljs-ln-line"> * 注意,最大发送字节数为512-1个字符,可在函数中修改上限
    587. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="587"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: const char *pcString, ... (如同printf的用法)
    588. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="588"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    589. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="589"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    590. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="590"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_SendString(const char *pcString, ...)
    591. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="591"> class="hljs-ln-code"> class="hljs-ln-line">{
    592. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="592"> class="hljs-ln-code"> class="hljs-ln-line"> char mBuffer[512] = {0};; // 开辟一个缓存, 并把里面的数据置0
    593. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="593"> class="hljs-ln-code"> class="hljs-ln-line"> va_list ap; // 新建一个可变参数列表
    594. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="594"> class="hljs-ln-code"> class="hljs-ln-line"> va_start(ap, pcString); // 列表指向第一个可变参数
    595. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="595"> class="hljs-ln-code"> class="hljs-ln-line"> vsnprintf(mBuffer, 512, pcString, ap); // 把所有参数,按格式,输出到缓存; 参数2用于限制发送的最大字节数,如果达到上限,则只发送上限值-1; 最后1字节自动置'\0'
    596. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="596"> class="hljs-ln-code"> class="hljs-ln-line"> va_end(ap); // 清空可变参数列表
    597. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="597"> class="hljs-ln-code"> class="hljs-ln-line"> UART2_SendData((uint8_t *)mBuffer, strlen(mBuffer)); // 把字节存放环形缓冲,排队准备发送
    598. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="598"> class="hljs-ln-code"> class="hljs-ln-line">}
    599. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="599"> class="hljs-ln-code"> class="hljs-ln-line">
    600. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="600"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    601. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="601"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_SendAT
    602. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="602"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送AT命令, 并等待返回信息
    603. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="603"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char *pcString AT指令字符串
    604. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="604"> class="hljs-ln-code"> class="hljs-ln-line"> * char *pcAckString 期待的指令返回信息字符串
    605. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="605"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usTimeOut 发送命令后等待的时间,毫秒
    606. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="606"> class="hljs-ln-code"> class="hljs-ln-line"> *
    607. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="607"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回 值: 0-执行失败、1-执行正常
    608. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="608"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    609. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="609"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART2_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs)
    610. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="610"> class="hljs-ln-code"> class="hljs-ln-line">{
    611. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="611"> class="hljs-ln-code"> class="hljs-ln-line"> UART2_ClearRx(); // 清0
    612. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="612"> class="hljs-ln-code"> class="hljs-ln-line"> UART2_SendString(pcAT); // 发送AT指令字符串
    613. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="613"> class="hljs-ln-code"> class="hljs-ln-line">
    614. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="614"> class="hljs-ln-code"> class="hljs-ln-line"> while (usTimeOutMs--) // 判断是否起时(这里只作简单的循环判断次数处理)
    615. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="615"> class="hljs-ln-code"> class="hljs-ln-line"> {
    616. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="616"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART2_GetRxNum()) // 判断是否接收到数据
    617. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="617"> class="hljs-ln-code"> class="hljs-ln-line"> {
    618. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="618"> class="hljs-ln-code"> class="hljs-ln-line"> UART2_ClearRx(); // 清0接收字节数; 注意:接收到的数据内容 ,是没有被清0的
    619. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="619"> class="hljs-ln-code"> class="hljs-ln-line"> if (strstr((char *)UART2_GetRxData(), pcAckString)) // 判断返回数据中是否有期待的字符
    620. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="620"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 返回:0-超时没有返回、1-正常返回期待值
    621. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="621"> class="hljs-ln-code"> class="hljs-ln-line"> }
    622. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="622"> class="hljs-ln-code"> class="hljs-ln-line"> delay_ms(1); // 延时; 用于超时退出处理,避免死等
    623. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="623"> class="hljs-ln-code"> class="hljs-ln-line"> }
    624. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="624"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 返回:0-超时、返回异常,1-正常返回期待值
    625. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="625"> class="hljs-ln-code"> class="hljs-ln-line">}
    626. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="626"> class="hljs-ln-code"> class="hljs-ln-line">
    627. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="627"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    628. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="628"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_GetRxNum
    629. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="629"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据的字节数
    630. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="630"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    631. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="631"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 0=没有接收到数据,非0=新一帧数据的字节数
    632. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="632"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    633. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="633"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART2_GetRxNum(void)
    634. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="634"> class="hljs-ln-code"> class="hljs-ln-line">{
    635. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="635"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART2.usRxNum ;
    636. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="636"> class="hljs-ln-code"> class="hljs-ln-line">}
    637. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="637"> class="hljs-ln-code"> class="hljs-ln-line">
    638. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="638"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    639. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="639"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_GetRxData
    640. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="640"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据 (数据的地址)
    641. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="641"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    642. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="642"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 数据的地址(uint8_t*)
    643. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="643"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    644. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="644"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t *UART2_GetRxData(void)
    645. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="645"> class="hljs-ln-code"> class="hljs-ln-line">{
    646. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="646"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART2.puRxData ;
    647. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="647"> class="hljs-ln-code"> class="hljs-ln-line">}
    648. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="648"> class="hljs-ln-code"> class="hljs-ln-line">
    649. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="649"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    650. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="650"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART2_ClearRx
    651. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="651"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 清理最后一帧数据的缓存
    652. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="652"> class="hljs-ln-code"> class="hljs-ln-line"> * 主要是清0字节数,因为它是用来判断接收的标准
    653. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="653"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    654. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="654"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    655. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="655"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    656. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="656"> class="hljs-ln-code"> class="hljs-ln-line">void UART2_ClearRx(void)
    657. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="657"> class="hljs-ln-code"> class="hljs-ln-line">{
    658. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="658"> class="hljs-ln-code"> class="hljs-ln-line"> xUART2.usRxNum = 0 ;
    659. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="659"> class="hljs-ln-code"> class="hljs-ln-line">}
    660. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="660"> class="hljs-ln-code"> class="hljs-ln-line">#endif // endif UART2_EN
    661. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="661"> class="hljs-ln-code"> class="hljs-ln-line">
    662. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="662"> class="hljs-ln-code"> class="hljs-ln-line">
    663. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="663"> class="hljs-ln-code"> class="hljs-ln-line">
    664. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="664"> class="hljs-ln-code"> class="hljs-ln-line">
    665. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="665"> class="hljs-ln-code"> class="hljs-ln-line">
    666. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="666"> class="hljs-ln-code"> class="hljs-ln-line">// USART-3 //
    667. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="667"> class="hljs-ln-code"> class="hljs-ln-line">/
    668. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="668"> class="hljs-ln-code"> class="hljs-ln-line">#if UART3_EN
    669. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="669"> class="hljs-ln-code"> class="hljs-ln-line">
    670. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="670"> class="hljs-ln-code"> class="hljs-ln-line">static xUSATR_TypeDef xUART3 = { 0 }; // 定义 UART3 的收发结构体
    671. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="671"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart3RxData[UART3_RX_BUF_SIZE]; // 定义 UART3 的接收缓存
    672. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="672"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart3TxFiFoData[UART3_TX_BUF_SIZE]; // 定义 UART3 的发送缓存
    673. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="673"> class="hljs-ln-code"> class="hljs-ln-line">
    674. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="674"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    675. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="675"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_Init
    676. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="676"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 初始化USART3的通信引脚、协议参数、中断优先级
    677. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="677"> class="hljs-ln-code"> class="hljs-ln-line"> * 引脚:TX-PB10、RX-PB11
    678. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="678"> class="hljs-ln-code"> class="hljs-ln-line"> * 协议:波特率-None-8-1
    679. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="679"> class="hljs-ln-code"> class="hljs-ln-line"> * 发送:发送中断
    680. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="680"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收:接收+空闲中断
    681. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="681"> class="hljs-ln-code"> class="hljs-ln-line"> *
    682. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="682"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ulBaudrate 通信波特率
    683. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="683"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    684. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="684"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    685. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="685"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_Init(uint32_t ulBaudrate)
    686. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="686"> class="hljs-ln-code"> class="hljs-ln-line">{
    687. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="687"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    688. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="688"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    689. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="689"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // 使能 GPIOB 时钟
    690. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="690"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); // 使能 USART3 时钟
    691. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="691"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 引脚的复用功能
    692. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="692"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); // 配置PB10复用功能:USART3
    693. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="693"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3); // 配置PB11复用功能:USART3
    694. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="694"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 USART
    695. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="695"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE); // 使能重置
    696. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="696"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE); // 取消重置
    697. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="697"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    698. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="698"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStructure = {0}; // GPIO 初始化结构体
    699. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="699"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // 引脚编号
    700. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="700"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 引脚方向: 复用功能
    701. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="701"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 输出模式:推挽
    702. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="702"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上下拉:上拉
    703. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="703"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 输出速度:50MHz
    704. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="704"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOB, &GPIO_InitStructure); // 初始化:把上述参数,更新到芯片寄存器
    705. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="705"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    706. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="706"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // 引脚编号
    707. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="707"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOB, &GPIO_InitStructure); // 初始化:把上述参数,更新到芯片寄存器
    708. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="708"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    709. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="709"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitTypeDef USART_InitStructure; // 声明USART初始化结构体
    710. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="710"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_BaudRate = ulBaudrate; // 设置波特率
    711. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="711"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 设置字长为8位数据格式
    712. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="712"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_StopBits = USART_StopBits_1; // 设置一个停止位
    713. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="713"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Parity = USART_Parity_No; // 无奇偶校验位
    714. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="714"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制
    715. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="715"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式
    716. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="716"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Init(USART3, &USART_InitStructure); // 初始化USART
    717. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="717"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    718. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="718"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // 开启 接收中断
    719. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="719"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART3, USART_IT_IDLE, ENABLE); // 开启 空闲中断
    720. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="720"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    721. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="721"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitTypeDef NVIC_InitStructure = {0}; // 中断优先级配置结构体
    722. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="722"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannel = USART3_IRQn; // 指定中断通道
    723. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="723"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 1; // 设置抢占优先级
    724. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="724"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelSubPriority = 1; // 设置响应优先级
    725. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="725"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelCmd = ENABLE; // 使能中断通道
    726. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="726"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_Init(&NVIC_InitStructure); // 初始化NVIC
    727. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="727"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,开启USART
    728. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="728"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Cmd(USART3, ENABLE); // 使能USART
    729. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="729"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    730. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="730"> class="hljs-ln-code"> class="hljs-ln-line">
    731. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="731"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER // HAL库 配置
    732. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="732"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    733. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="733"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOB_CLK_ENABLE(); // 使能GPIOB
    734. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="734"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART3_CLK_ENABLE(); // 使能USART3
    735. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="735"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 USART
    736. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="736"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART3_FORCE_RESET(); // 使能重置
    737. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="737"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART3_RELEASE_RESET(); // 取消重置
    738. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="738"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 GPIO引脚
    739. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="739"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStruct = {0}; // 声明初始化要用到的结构体
    740. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="740"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11; // 引脚 TX-PB10、RX-PB11
    741. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="741"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // 工作模式
    742. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="742"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_PULLUP; // 上下拉
    743. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="743"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; // 引脚速率
    744. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="744"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Alternate = GPIO_AF7_USART3; // 引脚复用功能
    745. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="745"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); // 初始化引脚工作模式
    746. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="746"> class="hljs-ln-code"> class="hljs-ln-line"> // 计算波特率参数
    747. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="747"> class="hljs-ln-code"> class="hljs-ln-line"> float temp;
    748. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="748"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t mantissa, fraction;
    749. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="749"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新系统运行频率全局值; 函数SystemCoreClock( ),在标准库、HAL库通用
    750. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="750"> class="hljs-ln-code"> class="hljs-ln-line"> temp = (float)(SystemCoreClock / 4) / (ulBaudrate * 16); // 波特率公式计算; USART3挂载在APB1, 时钟为系统时钟的4分频; 全局变量SystemCoreClock,在标准库、HAL库通用;
    751. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="751"> class="hljs-ln-code"> class="hljs-ln-line"> mantissa = temp; // 整数部分
    752. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="752"> class="hljs-ln-code"> class="hljs-ln-line"> fraction = (temp - mantissa) * 16; // 小数部分
    753. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="753"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> BRR = mantissa << 4 | fraction; // 设置波特率
    754. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="754"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    755. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="755"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 = 0; // 清0
    756. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="756"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x01 << 2; // 接收使能[02]: 0=失能、1=使能
    757. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="757"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x01 << 3; // 发送使能[03]:0=失能、1=使能
    758. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="758"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x00 << 9; // 奇偶校验[09]:0=偶Even、1=奇Odd; 注意:使用奇偶校验,下面两项要置1
    759. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="759"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x00 << 10; // 校验位 [10]:0=禁用、1=使能; 注意,使用奇偶校验,此位要置1 (否则无效、数据错乱)
    760. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="760"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x00 << 12; // 数据位 [12]:0=8位、 1=9位; 注意:使用奇偶校验,此位要置1 (否则数据会发生错乱)
    761. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="761"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR2 = 0; // 数据清0
    762. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="762"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR2 &= ~(0x03 << 12); // 停止位[13:12]:00b=1个停止位、01b=0.5个停止位、10b=2个停止位、11b=1.5个停止位
    763. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="763"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR3 = 0; // 数据清0
    764. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="764"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR3 &= ~(0x01 << 6); // DMA接收[6]: 0=禁止、1=使能
    765. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="765"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR3 &= ~(0x01 << 7); // DMA发送[7]: 0=禁止、1=使能
    766. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="766"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置中断
    767. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="767"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 &= ~(0x01 << 7); // 关闭发送中断
    768. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="768"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x01 << 5; // 使能接收中断: 接收缓冲区非空
    769. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="769"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x01 << 4; // 使能空闲中断:超过1字节时间没收到新数据
    770. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="770"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> SR = ~(0x00F0); // 清理一次中断标志
    771. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="771"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    772. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="772"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_SetPriority(USART3_IRQn, 1, 1); // 设置指定中断的响应优先级; 参数:中断请求编号、抢占级、子优先级
    773. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="773"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_EnableIRQ(USART3_IRQn); // 使能、启用指定的中断
    774. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="774"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,开启USART
    775. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="775"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 -> CR1 |= 0x01 << 13; // 使能UART开始工作
    776. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="776"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    777. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="777"> class="hljs-ln-code"> class="hljs-ln-line"> // 关联缓冲区
    778. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="778"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.puRxData = uaUart3RxData; // 获取接收缓冲区的地址
    779. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="779"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.puTxFiFoData = uaUart3TxFiFoData; // 获取发送缓冲区的地址
    780. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="780"> class="hljs-ln-code"> class="hljs-ln-line"> // 输出提示
    781. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="781"> class="hljs-ln-code"> class="hljs-ln-line"> printf("UART3 初始化配置 %d-None-8-1; 已完成初始化配置、收发配置\r", ulBaudrate);
    782. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="782"> class="hljs-ln-code"> class="hljs-ln-line">}
    783. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="783"> class="hljs-ln-code"> class="hljs-ln-line">
    784. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="784"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    785. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="785"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: USART3_IRQHandler
    786. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="786"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: USART3的接收中断、空闲中断、发送中断
    787. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="787"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    788. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="788"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    789. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="789"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数,当产生中断事件时,由硬件调用。
    790. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="790"> class="hljs-ln-code"> class="hljs-ln-line"> * 如果使用本文件代码,在工程文件的其它地方,要注释同名函数,否则冲突。
    791. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="791"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    792. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="792"> class="hljs-ln-code"> class="hljs-ln-line">void USART3_IRQHandler(void)
    793. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="793"> class="hljs-ln-code"> class="hljs-ln-line">{
    794. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="794"> class="hljs-ln-code"> class="hljs-ln-line"> static uint16_t cnt = 0; // 接收字节数累计:每一帧数据已接收到的字节数
    795. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="795"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t rxTemp[UART3_RX_BUF_SIZE]; // 接收数据缓存数组:每新接收1个字节,先顺序存放到这里,当一帧接收完(发生空闲中断), 再转存到外部缓存:xUARTx.puRxData[ ]
    796. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="796"> class="hljs-ln-code"> class="hljs-ln-line">
    797. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="797"> class="hljs-ln-code"> class="hljs-ln-line"> // 发送中断:用于把环形缓冲的数据,逐字节发出
    798. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="798"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART3->SR & USART_SR_TXE) && (USART3->CR1 & USART_CR1_TXEIE)) // 检查发送寄存器空中断使能,且发送寄存器为空; TXE(发送数据寄存器空)、TXEIE(发送缓冲区空中断使能)
    799. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="799"> class="hljs-ln-code"> class="hljs-ln-line"> {
    800. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="800"> class="hljs-ln-code"> class="hljs-ln-line"> USART3->DR = xUART3.puTxFiFoData[xUART3.usTxFiFoTail++]; // 从FIFO队列中取出一个数据,放入USART的发送寄存器(硬件会自动发出),然后将FIFO的尾指针递增,指向下一个要发送的数据
    801. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="801"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART3.usTxFiFoTail == UART3_TX_BUF_SIZE) // 检查FIFO尾指针是否到达了FIFO队列的末尾
    802. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="802"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.usTxFiFoTail = 0; // 将尾指针重置为0,实现环形队列的功能
    803. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="803"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART3.usTxFiFoTail == xUART3.usTxFiFoData) // 检查FIFO尾指针是否追上了头指针,即所有数据是否都已发送完毕
    804. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="804"> class="hljs-ln-code"> class="hljs-ln-line"> USART3->CR1 &= ~USART_CR1_TXEIE; // 关闭发送寄存器空中断,防止中断服务程序被不必要地调用
    805. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="805"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    806. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="806"> class="hljs-ln-code"> class="hljs-ln-line"> }
    807. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="807"> class="hljs-ln-code"> class="hljs-ln-line">
    808. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="808"> class="hljs-ln-code"> class="hljs-ln-line"> // 接收中断:用于逐个字节接收,存放到临时缓存
    809. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="809"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART3->SR & USART_SR_RXNE) // 检查RXNE(读数据寄存器非空标志位); RXNE中断清理方法:读DR时自动清理;
    810. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="810"> class="hljs-ln-code"> class="hljs-ln-line"> {
    811. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="811"> class="hljs-ln-code"> class="hljs-ln-line"> if (cnt == UART3_RX_BUF_SIZE) // 当前帧已接收的字节量,已满缓存区的大小; 为避免溢出,本包后面接收到的数据直接舍弃;
    812. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="812"> class="hljs-ln-code"> class="hljs-ln-line"> {
    813. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="813"> class="hljs-ln-code"> class="hljs-ln-line"> printf("警告:UART3单帧接收量,已超出接收缓存大小!\r修改文件bsp_UART.h的UART3_RX_BUF_SIZE值,可解决此问题!\r");
    814. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="814"> class="hljs-ln-code"> class="hljs-ln-line"> USART3->DR; // 读取数据寄存器的数据,但不保存.主要作用:读DR时自动清理接收中断标志;
    815. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="815"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    816. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="816"> class="hljs-ln-code"> class="hljs-ln-line"> }
    817. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="817"> class="hljs-ln-code"> class="hljs-ln-line"> rxTemp[cnt++] = USART3->DR ; // 把新收到的字节数据,顺序存放到RXTemp数组中;注意:读取DR时自动清零中断位
    818. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="818"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    819. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="819"> class="hljs-ln-code"> class="hljs-ln-line"> }
    820. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="820"> class="hljs-ln-code"> class="hljs-ln-line">
    821. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="821"> class="hljs-ln-code"> class="hljs-ln-line"> // 空闲中断:用于判断一帧数据结束,整理数据到外部备读
    822. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="822"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART3->SR & USART_SR_IDLE) // 检查IDLE(空闲中断标志位); IDLE中断标志清理方法:序列清零,USART1 ->SR; USART1 ->DR;
    823. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="823"> class="hljs-ln-code"> class="hljs-ln-line"> {
    824. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="824"> class="hljs-ln-code"> class="hljs-ln-line"> memcpy(xUART3.puRxData, rxTemp, UART3_RX_BUF_SIZE); // 把本帧接收到的数据,存入到结构体的数组成员xUARTx.puRxData中, 等待处理; 注意:复制的是整个数组,包括0值,以方便字符串输出时尾部以0作字符串结束符
    825. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="825"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.usRxNum = cnt; // 把接收到的字节数,存入到结构体变量xUARTx.usRxNum中;
    826. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="826"> class="hljs-ln-code"> class="hljs-ln-line"> cnt = 0; // 接收字节数累计器,清零; 准备下一次的接收
    827. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="827"> class="hljs-ln-code"> class="hljs-ln-line"> memset(rxTemp, 0, UART3_RX_BUF_SIZE); // 接收数据缓存数组,清零; 准备下一次的接收
    828. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="828"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 ->SR;
    829. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="829"> class="hljs-ln-code"> class="hljs-ln-line"> USART3 ->DR; // 清零IDLE中断标志位!! 序列清零,顺序不能错!!
    830. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="830"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    831. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="831"> class="hljs-ln-code"> class="hljs-ln-line"> }
    832. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="832"> class="hljs-ln-code"> class="hljs-ln-line">
    833. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="833"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    834. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="834"> class="hljs-ln-code"> class="hljs-ln-line">}
    835. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="835"> class="hljs-ln-code"> class="hljs-ln-line">
    836. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="836"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    837. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="837"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_SendData
    838. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="838"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过中断发送数据
    839. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="839"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】本函数可发送各种数据,而不限于字符串,如int,char
    840. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="840"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】注意h文件中所定义的发缓冲区大小、注意数据压入缓冲区的速度与串口发送速度的冲突
    841. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="841"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* puData 需发送数据的地址
    842. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="842"> class="hljs-ln-code"> class="hljs-ln-line"> * uint8_t usNum 发送的字节数 ,数量受限于h文件中设置的发送缓存区大小宏定义
    843. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="843"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    844. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="844"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    845. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="845"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_SendData(uint8_t *puData, uint16_t usNum)
    846. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="846"> class="hljs-ln-code"> class="hljs-ln-line">{
    847. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="847"> class="hljs-ln-code"> class="hljs-ln-line"> for (uint16_t i = 0; i < usNum; i++) // 把数据放入环形缓冲区
    848. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="848"> class="hljs-ln-code"> class="hljs-ln-line"> {
    849. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="849"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.puTxFiFoData[xUART3.usTxFiFoData++] = puData[i]; // 把字节放到缓冲区最后的位置,然后指针后移
    850. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="850"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART3.usTxFiFoData == UART3_TX_BUF_SIZE) // 如果指针位置到达缓冲区的最大值,则归0
    851. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="851"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.usTxFiFoData = 0;
    852. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="852"> class="hljs-ln-code"> class="hljs-ln-line"> }
    853. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="853"> class="hljs-ln-code"> class="hljs-ln-line">
    854. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="854"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART3->CR1 & USART_CR1_TXEIE) == 0) // 检查USART寄存器的发送缓冲区空置中断(TXEIE)是否已打开
    855. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="855"> class="hljs-ln-code"> class="hljs-ln-line"> USART3->CR1 |= USART_CR1_TXEIE; // 打开TXEIE中断
    856. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="856"> class="hljs-ln-code"> class="hljs-ln-line">}
    857. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="857"> class="hljs-ln-code"> class="hljs-ln-line">
    858. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="858"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    859. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="859"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_SendString
    860. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="860"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送字符串
    861. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="861"> class="hljs-ln-code"> class="hljs-ln-line"> * 用法请参考printf,及示例中的展示
    862. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="862"> class="hljs-ln-code"> class="hljs-ln-line"> * 注意,最大发送字节数为512-1个字符,可在函数中修改上限
    863. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="863"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: const char *pcString, ... (如同printf的用法)
    864. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="864"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    865. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="865"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    866. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="866"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_SendString(const char *pcString, ...)
    867. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="867"> class="hljs-ln-code"> class="hljs-ln-line">{
    868. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="868"> class="hljs-ln-code"> class="hljs-ln-line"> char mBuffer[512] = {0};; // 开辟一个缓存, 并把里面的数据置0
    869. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="869"> class="hljs-ln-code"> class="hljs-ln-line"> va_list ap; // 新建一个可变参数列表
    870. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="870"> class="hljs-ln-code"> class="hljs-ln-line"> va_start(ap, pcString); // 列表指向第一个可变参数
    871. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="871"> class="hljs-ln-code"> class="hljs-ln-line"> vsnprintf(mBuffer, 512, pcString, ap); // 把所有参数,按格式,输出到缓存; 参数2用于限制发送的最大字节数,如果达到上限,则只发送上限值-1; 最后1字节自动置'\0'
    872. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="872"> class="hljs-ln-code"> class="hljs-ln-line"> va_end(ap); // 清空可变参数列表
    873. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="873"> class="hljs-ln-code"> class="hljs-ln-line"> UART3_SendData((uint8_t *)mBuffer, strlen(mBuffer)); // 把字节存放环形缓冲,排队准备发送
    874. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="874"> class="hljs-ln-code"> class="hljs-ln-line">}
    875. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="875"> class="hljs-ln-code"> class="hljs-ln-line">
    876. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="876"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    877. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="877"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_SendAT
    878. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="878"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送AT命令, 并等待返回信息
    879. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="879"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char *pcString AT指令字符串
    880. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="880"> class="hljs-ln-code"> class="hljs-ln-line"> * char *pcAckString 期待的指令返回信息字符串
    881. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="881"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usTimeOut 发送命令后等待的时间,毫秒
    882. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="882"> class="hljs-ln-code"> class="hljs-ln-line"> *
    883. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="883"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回 值: 0-执行失败、1-执行正常
    884. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="884"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    885. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="885"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART3_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs)
    886. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="886"> class="hljs-ln-code"> class="hljs-ln-line">{
    887. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="887"> class="hljs-ln-code"> class="hljs-ln-line"> UART3_ClearRx(); // 清0
    888. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="888"> class="hljs-ln-code"> class="hljs-ln-line"> UART3_SendString(pcAT); // 发送AT指令字符串
    889. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="889"> class="hljs-ln-code"> class="hljs-ln-line">
    890. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="890"> class="hljs-ln-code"> class="hljs-ln-line"> while (usTimeOutMs--) // 判断是否起时(这里只作简单的循环判断次数处理)
    891. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="891"> class="hljs-ln-code"> class="hljs-ln-line"> {
    892. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="892"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART3_GetRxNum()) // 判断是否接收到数据
    893. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="893"> class="hljs-ln-code"> class="hljs-ln-line"> {
    894. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="894"> class="hljs-ln-code"> class="hljs-ln-line"> UART3_ClearRx(); // 清0接收字节数; 注意:接收到的数据内容 ,是没有被清0的
    895. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="895"> class="hljs-ln-code"> class="hljs-ln-line"> if (strstr((char *)UART3_GetRxData(), pcAckString)) // 判断返回数据中是否有期待的字符
    896. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="896"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 返回:0-超时没有返回、1-正常返回期待值
    897. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="897"> class="hljs-ln-code"> class="hljs-ln-line"> }
    898. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="898"> class="hljs-ln-code"> class="hljs-ln-line"> delay_ms(1); // 延时; 用于超时退出处理,避免死等
    899. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="899"> class="hljs-ln-code"> class="hljs-ln-line"> }
    900. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="900"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 返回:0-超时、返回异常,1-正常返回期待值
    901. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="901"> class="hljs-ln-code"> class="hljs-ln-line">}
    902. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="902"> class="hljs-ln-code"> class="hljs-ln-line">
    903. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="903"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    904. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="904"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_GetRxNum
    905. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="905"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据的字节数
    906. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="906"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    907. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="907"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 0=没有接收到数据,非0=新一帧数据的字节数
    908. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="908"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    909. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="909"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART3_GetRxNum(void)
    910. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="910"> class="hljs-ln-code"> class="hljs-ln-line">{
    911. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="911"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART3.usRxNum ;
    912. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="912"> class="hljs-ln-code"> class="hljs-ln-line">}
    913. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="913"> class="hljs-ln-code"> class="hljs-ln-line">
    914. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="914"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    915. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="915"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_GetRxData
    916. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="916"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据 (数据的地址)
    917. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="917"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    918. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="918"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 数据的地址(uint8_t*)
    919. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="919"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    920. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="920"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t *UART3_GetRxData(void)
    921. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="921"> class="hljs-ln-code"> class="hljs-ln-line">{
    922. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="922"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART3.puRxData ;
    923. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="923"> class="hljs-ln-code"> class="hljs-ln-line">}
    924. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="924"> class="hljs-ln-code"> class="hljs-ln-line">
    925. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="925"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    926. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="926"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART3_ClearRx
    927. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="927"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 清理最后一帧数据的缓存
    928. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="928"> class="hljs-ln-code"> class="hljs-ln-line"> * 主要是清0字节数,因为它是用来判断接收的标准
    929. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="929"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    930. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="930"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    931. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="931"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    932. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="932"> class="hljs-ln-code"> class="hljs-ln-line">void UART3_ClearRx(void)
    933. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="933"> class="hljs-ln-code"> class="hljs-ln-line">{
    934. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="934"> class="hljs-ln-code"> class="hljs-ln-line"> xUART3.usRxNum = 0 ;
    935. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="935"> class="hljs-ln-code"> class="hljs-ln-line">}
    936. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="936"> class="hljs-ln-code"> class="hljs-ln-line">#endif // endif UART3_EN
    937. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="937"> class="hljs-ln-code"> class="hljs-ln-line">
    938. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="938"> class="hljs-ln-code"> class="hljs-ln-line">
    939. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="939"> class="hljs-ln-code"> class="hljs-ln-line">
    940. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="940"> class="hljs-ln-code"> class="hljs-ln-line">
    941. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="941"> class="hljs-ln-code"> class="hljs-ln-line">
    942. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="942"> class="hljs-ln-code"> class="hljs-ln-line">// UART-4 //
    943. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="943"> class="hljs-ln-code"> class="hljs-ln-line">/
    944. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="944"> class="hljs-ln-code"> class="hljs-ln-line">#if UART4_EN
    945. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="945"> class="hljs-ln-code"> class="hljs-ln-line">
    946. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="946"> class="hljs-ln-code"> class="hljs-ln-line">static xUSATR_TypeDef xUART4 = { 0 }; // 定义 UART4 的收发结构体
    947. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="947"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart4RxData[UART4_RX_BUF_SIZE]; // 定义 UART4 的接收缓存
    948. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="948"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart4TxFiFoData[UART4_TX_BUF_SIZE]; // 定义 UART4 的发送缓存
    949. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="949"> class="hljs-ln-code"> class="hljs-ln-line">
    950. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="950"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    951. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="951"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_Init
    952. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="952"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 初始化UART4的通信引脚、协议参数、中断优先级
    953. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="953"> class="hljs-ln-code"> class="hljs-ln-line"> * 引脚:TX-PC10、RX-PC11
    954. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="954"> class="hljs-ln-code"> class="hljs-ln-line"> * 协议:波特率-None-8-1
    955. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="955"> class="hljs-ln-code"> class="hljs-ln-line"> * 发送:发送中断
    956. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="956"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收:接收+空闲中断
    957. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="957"> class="hljs-ln-code"> class="hljs-ln-line"> *
    958. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="958"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ulBaudrate 通信波特率
    959. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="959"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    960. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="960"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    961. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="961"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_Init(uint32_t ulBaudrate)
    962. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="962"> class="hljs-ln-code"> class="hljs-ln-line">{
    963. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="963"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    964. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="964"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    965. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="965"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); // 使能 GPIOC 时钟
    966. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="966"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); // 使能 UART4 时钟
    967. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="967"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 引脚复用功能
    968. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="968"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4); // 配置PC10复用功能:UART4
    969. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="969"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4); // 配置PC11复用功能:UART4
    970. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="970"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 USART
    971. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="971"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE); // 使能重置
    972. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="972"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE); // 取消重置
    973. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="973"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    974. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="974"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStructure = {0}; // GPIO 初始化结构体
    975. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="975"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // 引脚编号
    976. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="976"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 引脚方向: 复用功能
    977. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="977"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 输出模式:推挽
    978. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="978"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上下拉:上拉
    979. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="979"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 输出速度:50MHz
    980. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="980"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOC, &GPIO_InitStructure); // 初始化:把上述参数,更新到芯片寄存器
    981. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="981"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    982. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="982"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // 引脚编号
    983. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="983"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOC, &GPIO_InitStructure); // 初始化:把上述参数,更新到芯片寄存器
    984. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="984"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    985. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="985"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitTypeDef USART_InitStructure; // 声明USART初始化结构体
    986. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="986"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_BaudRate = ulBaudrate; // 设置波特率
    987. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="987"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 设置字长为8位数据格式
    988. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="988"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_StopBits = USART_StopBits_1; // 设置一个停止位
    989. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="989"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Parity = USART_Parity_No; // 无奇偶校验位
    990. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="990"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制
    991. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="991"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式
    992. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="992"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Init(UART4, &USART_InitStructure); // 初始化USART
    993. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="993"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    994. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="994"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); // 开启 接收中断
    995. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="995"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(UART4, USART_IT_IDLE, ENABLE); // 开启 空闲中断
    996. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="996"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    997. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="997"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitTypeDef NVIC_InitStructure = {0}; // 中断优先级配置结构体
    998. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="998"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannel = UART4_IRQn; // 指定中断通道
    999. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="999"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 1; // 设置抢占优先级
    1000. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1000"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelSubPriority = 1; // 设置响应优先级
    1001. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1001"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelCmd = ENABLE; // 使能中断通道
    1002. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1002"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_Init(&NVIC_InitStructure); // 初始化NVIC
    1003. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1003"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,开启USART
    1004. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1004"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Cmd(UART4, ENABLE); // 使能USART
    1005. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1005"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1006. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1006"> class="hljs-ln-code"> class="hljs-ln-line">
    1007. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1007"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER // HAL库 配置
    1008. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1008"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    1009. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1009"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOC_CLK_ENABLE(); // 使能GPIOC
    1010. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1010"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_UART4_CLK_ENABLE(); // 使能UART4
    1011. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1011"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 USART
    1012. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1012"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_UART4_FORCE_RESET(); // 使能重置
    1013. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1013"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_UART4_RELEASE_RESET(); // 取消重置
    1014. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1014"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 GPIO引脚
    1015. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1015"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStruct = {0}; // 声明初始化要用到的结构体
    1016. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1016"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11; // 引脚 TX-PC10、RX-PC11
    1017. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1017"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // 工作模式
    1018. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1018"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_PULLUP; // 上下拉
    1019. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1019"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; // 引脚速率
    1020. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1020"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Alternate = GPIO_AF8_UART4; // 引脚复用功能
    1021. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1021"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); // 初始化引脚工作模式
    1022. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1022"> class="hljs-ln-code"> class="hljs-ln-line"> // 计算波特率参数
    1023. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1023"> class="hljs-ln-code"> class="hljs-ln-line"> float temp;
    1024. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1024"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t mantissa, fraction;
    1025. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1025"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新系统运行频率全局值; 函数SystemCoreClock( ),在标准库、HAL库通用
    1026. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1026"> class="hljs-ln-code"> class="hljs-ln-line"> temp = (float)(SystemCoreClock / 4) / (ulBaudrate * 16); // 波特率公式计算; UART4挂载在APB1, 时钟为系统时钟的4分频; 全局变量SystemCoreClock,在标准库、HAL库通用;
    1027. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1027"> class="hljs-ln-code"> class="hljs-ln-line"> mantissa = temp; // 整数部分
    1028. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1028"> class="hljs-ln-code"> class="hljs-ln-line"> fraction = (temp - mantissa) * 16; // 小数部分
    1029. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1029"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> BRR = mantissa << 4 | fraction; // 设置波特率
    1030. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1030"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    1031. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1031"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 = 0; // 清0
    1032. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1032"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x01 << 2; // 接收使能[02]: 0=失能、1=使能
    1033. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1033"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x01 << 3; // 发送使能[03]:0=失能、1=使能
    1034. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1034"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x00 << 9; // 奇偶校验[09]:0=偶Even、1=奇Odd; 注意:使用奇偶校验,下面两项要置1
    1035. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1035"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x00 << 10; // 校验位 [10]:0=禁用、1=使能; 注意,使用奇偶校验,此位要置1 (否则无效、数据错乱)
    1036. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1036"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x00 << 12; // 数据位 [12]:0=8位、 1=9位; 注意:使用奇偶校验,此位要置1 (否则数据会发生错乱)
    1037. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1037"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR2 = 0; // 数据清0
    1038. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1038"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR2 &= ~(0x03 << 12); // 停止位[13:12]:00b=1个停止位、01b=0.5个停止位、10b=2个停止位、11b=1.5个停止位
    1039. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1039"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR3 = 0; // 数据清0
    1040. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1040"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR3 &= ~(0x01 << 6); // DMA接收[6]: 0=禁止、1=使能
    1041. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1041"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR3 &= ~(0x01 << 7); // DMA发送[7]: 0=禁止、1=使能
    1042. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1042"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    1043. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1043"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 &= ~(0x01 << 7); // 关闭发送中断
    1044. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1044"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x01 << 5; // 使能接收中断: 接收缓冲区非空
    1045. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1045"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x01 << 4; // 使能空闲中断:超过1字节时间没收到新数据
    1046. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1046"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> SR = ~(0x00F0); // 清理中断
    1047. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1047"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优选级
    1048. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1048"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_SetPriority(UART4_IRQn, 1, 1); // 设置指定中断的响应优先级; 参数:中断请求编号、抢占级、子优先级
    1049. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1049"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_EnableIRQ(UART4_IRQn); // 使能、启用指定的中断
    1050. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1050"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,打开串口
    1051. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1051"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 -> CR1 |= 0x01 << 13; // 使能UART开始工作
    1052. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1052"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1053. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1053"> class="hljs-ln-code"> class="hljs-ln-line"> // 关联缓冲区
    1054. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1054"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.puRxData = uaUart4RxData; // 获取接收缓冲区的地址
    1055. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1055"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.puTxFiFoData = uaUart4TxFiFoData; // 获取发送缓冲区的地址
    1056. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1056"> class="hljs-ln-code"> class="hljs-ln-line"> // 输出提示
    1057. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1057"> class="hljs-ln-code"> class="hljs-ln-line"> printf("UART4 初始化配置 %d-None-8-1; 已完成初始化配置、收发配置\r", ulBaudrate);
    1058. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1058"> class="hljs-ln-code"> class="hljs-ln-line">}
    1059. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1059"> class="hljs-ln-code"> class="hljs-ln-line">
    1060. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1060"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1061. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1061"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_IRQHandler
    1062. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1062"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART4的中断处理函数
    1063. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1063"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收中断、空闲中断、发送中断
    1064. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1064"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1065. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1065"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1066. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1066"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数,当产生中断事件时,由硬件调用。
    1067. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1067"> class="hljs-ln-code"> class="hljs-ln-line"> * 如果使用本文件代码,在工程文件的其它地方,要注释同名函数,否则冲突。
    1068. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1068"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1069. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1069"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_IRQHandler(void)
    1070. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1070"> class="hljs-ln-code"> class="hljs-ln-line">{
    1071. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1071"> class="hljs-ln-code"> class="hljs-ln-line"> static uint16_t cnt = 0; // 接收字节数累计:每一帧数据已接收到的字节数
    1072. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1072"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t rxTemp[UART4_RX_BUF_SIZE]; // 接收数据缓存数组:每新接收1个字节,先顺序存放到这里,当一帧接收完(发生空闲中断), 再转存到外部缓存:xUARTx.puRxData[ ]
    1073. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1073"> class="hljs-ln-code"> class="hljs-ln-line">
    1074. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1074"> class="hljs-ln-code"> class="hljs-ln-line"> // 发送中断:用于把环形缓冲的数据,逐字节发出
    1075. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1075"> class="hljs-ln-code"> class="hljs-ln-line"> if ((UART4->SR & USART_SR_TXE) && (UART4->CR1 & USART_CR1_TXEIE)) // 检查发送寄存器空中断使能,且发送寄存器为空; TXE(发送数据寄存器空)、TXEIE(发送缓冲区空中断使能)
    1076. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1076"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1077. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1077"> class="hljs-ln-code"> class="hljs-ln-line"> UART4->DR = xUART4.puTxFiFoData[xUART4.usTxFiFoTail++]; // 从FIFO队列中取出一个数据,放入USART的发送寄存器(硬件会自动发出),然后将FIFO的尾指针递增,指向下一个要发送的数据
    1078. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1078"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART4.usTxFiFoTail == UART4_TX_BUF_SIZE) // 检查FIFO尾指针是否到达了FIFO队列的末尾
    1079. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1079"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.usTxFiFoTail = 0; // 将尾指针重置为0,实现环形队列的功能
    1080. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1080"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART4.usTxFiFoTail == xUART4.usTxFiFoData) // 检查FIFO尾指针是否追上了头指针,即所有数据是否都已发送完毕
    1081. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1081"> class="hljs-ln-code"> class="hljs-ln-line"> UART4->CR1 &= ~USART_CR1_TXEIE; // 关闭发送寄存器空中断,防止中断服务程序被不必要地调用
    1082. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1082"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1083. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1083"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1084. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1084"> class="hljs-ln-code"> class="hljs-ln-line">
    1085. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1085"> class="hljs-ln-code"> class="hljs-ln-line"> // 接收中断:用于逐个字节接收,存放到临时缓存
    1086. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1086"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART4->SR & USART_SR_RXNE) // 检查RXNE(读数据寄存器非空标志位); RXNE中断清理方法:读DR时自动清理;
    1087. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1087"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1088. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1088"> class="hljs-ln-code"> class="hljs-ln-line"> if (cnt == UART4_RX_BUF_SIZE) // 当前帧已接收的字节量,已满缓存区的大小; 为避免溢出,本包后面接收到的数据直接舍弃;
    1089. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1089"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1090. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1090"> class="hljs-ln-code"> class="hljs-ln-line"> printf("警告:UART4单帧接收量,已超出接收缓存大小!\r修改文件bsp_UART.h的UART4_RX_BUF_SIZE值,可解决此问题!\r");
    1091. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1091"> class="hljs-ln-code"> class="hljs-ln-line"> UART4->DR; // 读取数据寄存器的数据,但不保存.主要作用:读DR时自动清理接收中断标志;
    1092. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1092"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1093. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1093"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1094. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1094"> class="hljs-ln-code"> class="hljs-ln-line"> rxTemp[cnt++] = UART4->DR ; // 把新收到的字节数据,顺序存放到RXTemp数组中;注意:读取DR时自动清零中断位
    1095. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1095"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1096. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1096"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1097. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1097"> class="hljs-ln-code"> class="hljs-ln-line">
    1098. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1098"> class="hljs-ln-code"> class="hljs-ln-line"> // 空闲中断:用于判断一帧数据结束,整理数据到外部备读
    1099. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1099"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART4->SR & USART_SR_IDLE) // 检查IDLE(空闲中断标志位); IDLE中断标志清理方法:序列清零,USART1 ->SR; USART1 ->DR;
    1100. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1100"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1101. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1101"> class="hljs-ln-code"> class="hljs-ln-line"> memcpy(xUART4.puRxData, rxTemp, UART4_RX_BUF_SIZE); // 把本帧接收到的数据,存入到结构体的数组成员xUARTx.puRxData中, 等待处理; 注意:复制的是整个数组,包括0值,以方便字符串输出时尾部以0作字符串结束符
    1102. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1102"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.usRxNum = cnt; // 把接收到的字节数,存入到结构体变量xUARTx.usRxNum中;
    1103. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1103"> class="hljs-ln-code"> class="hljs-ln-line"> cnt = 0; // 接收字节数累计器,清零; 准备下一次的接收
    1104. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1104"> class="hljs-ln-code"> class="hljs-ln-line"> memset(rxTemp, 0, UART4_RX_BUF_SIZE); // 接收数据缓存数组,清零; 准备下一次的接收
    1105. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1105"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 ->SR;
    1106. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1106"> class="hljs-ln-code"> class="hljs-ln-line"> UART4 ->DR; // 清零IDLE中断标志位!! 序列清零,顺序不能错!!
    1107. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1107"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1108. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1108"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1109. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1109"> class="hljs-ln-code"> class="hljs-ln-line">
    1110. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1110"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1111. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1111"> class="hljs-ln-code"> class="hljs-ln-line">}
    1112. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1112"> class="hljs-ln-code"> class="hljs-ln-line">
    1113. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1113"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1114. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1114"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_SendData
    1115. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1115"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过中断发送数据
    1116. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1116"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】本函数可发送各种数据,而不限于字符串,如int,char
    1117. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1117"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】注意h文件中所定义的发缓冲区大小、注意数据压入缓冲区的速度与串口发送速度的冲突
    1118. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1118"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* puData 需发送数据的地址
    1119. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1119"> class="hljs-ln-code"> class="hljs-ln-line"> * uint8_t usNum 发送的字节数 ,数量受限于h文件中设置的发送缓存区大小宏定义
    1120. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1120"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1121. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1121"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1122. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1122"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_SendData(uint8_t *puData, uint16_t usNum)
    1123. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1123"> class="hljs-ln-code"> class="hljs-ln-line">{
    1124. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1124"> class="hljs-ln-code"> class="hljs-ln-line"> for (uint16_t i = 0; i < usNum; i++) // 把数据放入环形缓冲区
    1125. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1125"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1126. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1126"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.puTxFiFoData[xUART4.usTxFiFoData++] = puData[i]; // 把字节放到缓冲区最后的位置,然后指针后移
    1127. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1127"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART4.usTxFiFoData == UART4_TX_BUF_SIZE) // 如果指针位置到达缓冲区的最大值,则归0
    1128. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1128"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.usTxFiFoData = 0;
    1129. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1129"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1130. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1130"> class="hljs-ln-code"> class="hljs-ln-line">
    1131. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1131"> class="hljs-ln-code"> class="hljs-ln-line"> if ((UART4->CR1 & USART_CR1_TXEIE) == 0) // 检查USART寄存器的发送缓冲区空置中断(TXEIE)是否已打开
    1132. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1132"> class="hljs-ln-code"> class="hljs-ln-line"> UART4->CR1 |= USART_CR1_TXEIE; // 打开TXEIE中断
    1133. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1133"> class="hljs-ln-code"> class="hljs-ln-line">}
    1134. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1134"> class="hljs-ln-code"> class="hljs-ln-line">
    1135. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1135"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1136. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1136"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_SendString
    1137. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1137"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送字符串
    1138. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1138"> class="hljs-ln-code"> class="hljs-ln-line"> * 用法请参考printf,及示例中的展示
    1139. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1139"> class="hljs-ln-code"> class="hljs-ln-line"> * 注意,最大发送字节数为512-1个字符,可在函数中修改上限
    1140. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1140"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: const char *pcString, ... (如同printf的用法)
    1141. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1141"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1142. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1142"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1143. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1143"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_SendString(const char *pcString, ...)
    1144. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1144"> class="hljs-ln-code"> class="hljs-ln-line">{
    1145. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1145"> class="hljs-ln-code"> class="hljs-ln-line"> char mBuffer[512] = {0};; // 开辟一个缓存, 并把里面的数据置0
    1146. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1146"> class="hljs-ln-code"> class="hljs-ln-line"> va_list ap; // 新建一个可变参数列表
    1147. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1147"> class="hljs-ln-code"> class="hljs-ln-line"> va_start(ap, pcString); // 列表指向第一个可变参数
    1148. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1148"> class="hljs-ln-code"> class="hljs-ln-line"> vsnprintf(mBuffer, 512, pcString, ap); // 把所有参数,按格式,输出到缓存; 参数2用于限制发送的最大字节数,如果达到上限,则只发送上限值-1; 最后1字节自动置'\0'
    1149. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1149"> class="hljs-ln-code"> class="hljs-ln-line"> va_end(ap); // 清空可变参数列表
    1150. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1150"> class="hljs-ln-code"> class="hljs-ln-line"> UART4_SendData((uint8_t *)mBuffer, strlen(mBuffer)); // 把字节存放环形缓冲,排队准备发送
    1151. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1151"> class="hljs-ln-code"> class="hljs-ln-line">}
    1152. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1152"> class="hljs-ln-code"> class="hljs-ln-line">
    1153. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1153"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1154. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1154"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_SendAT
    1155. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1155"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送AT命令, 并等待返回信息
    1156. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1156"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char *pcString AT指令字符串
    1157. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1157"> class="hljs-ln-code"> class="hljs-ln-line"> * char *pcAckString 期待的指令返回信息字符串
    1158. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1158"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usTimeOut 发送命令后等待的时间,毫秒
    1159. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1159"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1160. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1160"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回 值: 0-执行失败、1-执行正常
    1161. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1161"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1162. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1162"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART4_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs)
    1163. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1163"> class="hljs-ln-code"> class="hljs-ln-line">{
    1164. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1164"> class="hljs-ln-code"> class="hljs-ln-line"> UART4_ClearRx(); // 清0
    1165. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1165"> class="hljs-ln-code"> class="hljs-ln-line"> UART4_SendString(pcAT); // 发送AT指令字符串
    1166. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1166"> class="hljs-ln-code"> class="hljs-ln-line">
    1167. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1167"> class="hljs-ln-code"> class="hljs-ln-line"> while (usTimeOutMs--) // 判断是否起时(这里只作简单的循环判断次数处理)
    1168. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1168"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1169. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1169"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART4_GetRxNum()) // 判断是否接收到数据
    1170. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1170"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1171. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1171"> class="hljs-ln-code"> class="hljs-ln-line"> UART4_ClearRx(); // 清0接收字节数; 注意:接收到的数据内容 ,是没有被清0的
    1172. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1172"> class="hljs-ln-code"> class="hljs-ln-line"> if (strstr((char *)UART4_GetRxData(), pcAckString)) // 判断返回数据中是否有期待的字符
    1173. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1173"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 返回:0-超时没有返回、1-正常返回期待值
    1174. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1174"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1175. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1175"> class="hljs-ln-code"> class="hljs-ln-line"> delay_ms(1); // 延时; 用于超时退出处理,避免死等
    1176. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1176"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1177. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1177"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 返回:0-超时、返回异常,1-正常返回期待值
    1178. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1178"> class="hljs-ln-code"> class="hljs-ln-line">}
    1179. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1179"> class="hljs-ln-code"> class="hljs-ln-line">
    1180. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1180"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1181. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1181"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_GetRxNum
    1182. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1182"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据的字节数
    1183. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1183"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1184. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1184"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 0=没有接收到数据,非0=新一帧数据的字节数
    1185. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1185"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1186. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1186"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART4_GetRxNum(void)
    1187. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1187"> class="hljs-ln-code"> class="hljs-ln-line">{
    1188. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1188"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART4.usRxNum ;
    1189. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1189"> class="hljs-ln-code"> class="hljs-ln-line">}
    1190. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1190"> class="hljs-ln-code"> class="hljs-ln-line">
    1191. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1191"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1192. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1192"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_GetRxData
    1193. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1193"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据 (数据的地址)
    1194. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1194"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1195. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1195"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 数据的地址(uint8_t*)
    1196. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1196"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1197. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1197"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t *UART4_GetRxData(void)
    1198. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1198"> class="hljs-ln-code"> class="hljs-ln-line">{
    1199. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1199"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART4.puRxData ;
    1200. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1200"> class="hljs-ln-code"> class="hljs-ln-line">}
    1201. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1201"> class="hljs-ln-code"> class="hljs-ln-line">
    1202. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1202"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1203. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1203"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART4_ClearRx
    1204. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1204"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 清理最后一帧数据的缓存
    1205. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1205"> class="hljs-ln-code"> class="hljs-ln-line"> * 主要是清0字节数,因为它是用来判断接收的标准
    1206. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1206"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1207. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1207"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1208. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1208"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1209. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1209"> class="hljs-ln-code"> class="hljs-ln-line">void UART4_ClearRx(void)
    1210. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1210"> class="hljs-ln-code"> class="hljs-ln-line">{
    1211. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1211"> class="hljs-ln-code"> class="hljs-ln-line"> xUART4.usRxNum = 0 ;
    1212. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1212"> class="hljs-ln-code"> class="hljs-ln-line">}
    1213. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1213"> class="hljs-ln-code"> class="hljs-ln-line">#endif // endif UART4_EN
    1214. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1214"> class="hljs-ln-code"> class="hljs-ln-line">
    1215. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1215"> class="hljs-ln-code"> class="hljs-ln-line">
    1216. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1216"> class="hljs-ln-code"> class="hljs-ln-line">
    1217. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1217"> class="hljs-ln-code"> class="hljs-ln-line">
    1218. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1218"> class="hljs-ln-code"> class="hljs-ln-line">// UART-5 //
    1219. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1219"> class="hljs-ln-code"> class="hljs-ln-line">/
    1220. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1220"> class="hljs-ln-code"> class="hljs-ln-line">#if UART5_EN
    1221. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1221"> class="hljs-ln-code"> class="hljs-ln-line">
    1222. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1222"> class="hljs-ln-code"> class="hljs-ln-line">static xUSATR_TypeDef xUART5 = { 0 }; // 定义 UART5 的收发结构体
    1223. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1223"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart5RxData[UART5_RX_BUF_SIZE]; // 定义 UART5 的接收缓存
    1224. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1224"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart5TxFiFoData[UART5_TX_BUF_SIZE]; // 定义 UART5 的发送缓存
    1225. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1225"> class="hljs-ln-code"> class="hljs-ln-line">
    1226. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1226"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1227. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1227"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_Init
    1228. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1228"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 初始化UART5的通信引脚、协议参数、中断优先级
    1229. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1229"> class="hljs-ln-code"> class="hljs-ln-line"> * 引脚:TX-PC12、RX-PD2
    1230. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1230"> class="hljs-ln-code"> class="hljs-ln-line"> * 协议:波特率-None-8-1
    1231. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1231"> class="hljs-ln-code"> class="hljs-ln-line"> * 发送:发送中断
    1232. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1232"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收:接收+空闲中断
    1233. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1233"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1234. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1234"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ulBaudrate 通信波特率
    1235. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1235"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1236. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1236"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1237. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1237"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_Init(uint32_t ulBaudrate)
    1238. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1238"> class="hljs-ln-code"> class="hljs-ln-line">{
    1239. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1239"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    1240. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1240"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    1241. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1241"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); // 使能 GPIOC 时钟
    1242. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1242"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); // 使能 GPIOD 时钟
    1243. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1243"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); // 使能 UART5 时钟
    1244. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1244"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 引脚复用功能
    1245. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1245"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5); // 配置PC12复用功能:UART5
    1246. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1246"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5); // 配置PD2复用功能 :UART5
    1247. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1247"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 USART
    1248. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1248"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE); // 使能重置
    1249. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1249"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE); // 取消重置
    1250. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1250"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    1251. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1251"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStructure = {0}; // GPIO 初始化结构体
    1252. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1252"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; // 引脚编号
    1253. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1253"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 引脚方向: 复用功能
    1254. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1254"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 输出模式:推挽
    1255. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1255"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上下拉:上拉
    1256. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1256"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 输出速度:50MHz
    1257. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1257"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOC, &GPIO_InitStructure); // 初始化:把上述参数,更新到芯片寄存器
    1258. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1258"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    1259. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1259"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // 引脚编号
    1260. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1260"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOD, &GPIO_InitStructure); // 初始化:把上述参数,更新到芯片寄存器
    1261. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1261"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置UART
    1262. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1262"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitTypeDef USART_InitStructure; // 声明USART初始化结构体
    1263. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1263"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_BaudRate = ulBaudrate; // 设置波特率
    1264. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1264"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 设置字长为8位数据格式
    1265. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1265"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_StopBits = USART_StopBits_1; // 设置一个停止位
    1266. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1266"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Parity = USART_Parity_No; // 无奇偶校验位
    1267. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1267"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制
    1268. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1268"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式
    1269. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1269"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Init(UART5, &USART_InitStructure); // 初始化USART
    1270. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1270"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    1271. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1271"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); // 开启 接收中断
    1272. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1272"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(UART5, USART_IT_IDLE, ENABLE); // 开启 空闲中断
    1273. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1273"> class="hljs-ln-code"> class="hljs-ln-line"> // 中断优先级配置
    1274. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1274"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitTypeDef NVIC_InitStructure = {0}; // 中断优先级配置结构体
    1275. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1275"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannel = UART5_IRQn; // 指定中断通道
    1276. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1276"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 1; // 设置抢占优先级
    1277. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1277"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelSubPriority = 1; // 设置响应优先级
    1278. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1278"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelCmd = ENABLE; // 使能中断通道
    1279. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1279"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_Init(&NVIC_InitStructure); // 初始化NVIC
    1280. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1280"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,开启USART
    1281. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1281"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Cmd(UART5, ENABLE); // 使能USART
    1282. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1282"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1283. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1283"> class="hljs-ln-code"> class="hljs-ln-line">
    1284. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1284"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER // HAL库 配置
    1285. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1285"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    1286. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1286"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOC_CLK_ENABLE(); // 使能GPIOC
    1287. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1287"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOD_CLK_ENABLE(); // 使能GPIOD
    1288. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1288"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_UART5_CLK_ENABLE(); // 使能UART5
    1289. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1289"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 UART
    1290. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1290"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_UART5_FORCE_RESET(); // 使能重置
    1291. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1291"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_UART5_RELEASE_RESET(); // 取消重置
    1292. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1292"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    1293. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1293"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStruct = {0}; // 声明初始化要用到的结构体
    1294. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1294"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_12 ; // 引脚 TX-PC12
    1295. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1295"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // 工作模式
    1296. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1296"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_NOPULL; // 上下拉
    1297. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1297"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; // 引脚速率
    1298. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1298"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Alternate = GPIO_AF8_UART5; // 引脚复用功能
    1299. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1299"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); // 初始化引脚
    1300. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1300"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    1301. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1301"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_2 ; // 引脚 RX-PD2
    1302. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1302"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); // 初始化引脚工作模式
    1303. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1303"> class="hljs-ln-code"> class="hljs-ln-line"> // 计算波特率参数
    1304. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1304"> class="hljs-ln-code"> class="hljs-ln-line"> float temp;
    1305. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1305"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t mantissa, fraction;
    1306. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1306"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新系统运行频率全局值; 函数SystemCoreClock( ),在标准库、HAL库通用
    1307. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1307"> class="hljs-ln-code"> class="hljs-ln-line"> temp = (float)(SystemCoreClock / 4) / (ulBaudrate * 16); // 波特率公式计算; UART5挂载在APB1, 时钟为系统时钟的4分频; 全局变量SystemCoreClock,在标准库、HAL库通用;
    1308. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1308"> class="hljs-ln-code"> class="hljs-ln-line"> mantissa = temp; // 整数部分
    1309. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1309"> class="hljs-ln-code"> class="hljs-ln-line"> fraction = (temp - mantissa) * 16; // 小数部分
    1310. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1310"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> BRR = mantissa << 4 | fraction; // 设置波特率
    1311. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1311"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    1312. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1312"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 = 0; // 清0
    1313. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1313"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x01 << 2; // 接收使能[02]: 0=失能、1=使能
    1314. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1314"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x01 << 3; // 发送使能[03]:0=失能、1=使能
    1315. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1315"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x00 << 9; // 奇偶校验[09]:0=偶Even、1=奇Odd; 注意:使用奇偶校验,下面两项要置1
    1316. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1316"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x00 << 10; // 校验位 [10]:0=禁用、1=使能; 注意,使用奇偶校验,此位要置1 (否则无效、数据错乱)
    1317. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1317"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x00 << 12; // 数据位 [12]:0=8位、 1=9位; 注意:使用奇偶校验,此位要置1 (否则数据会发生错乱)
    1318. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1318"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR2 = 0; // 数据清0
    1319. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1319"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR2 &= ~(0x03 << 12); // 停止位[13:12]:00b=1个停止位、01b=0.5个停止位、10b=2个停止位、11b=1.5个停止位
    1320. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1320"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR3 = 0; // 数据清0
    1321. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1321"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR3 &= ~(0x01 << 6); // DMA接收[6]: 0=禁止、1=使能
    1322. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1322"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR3 &= ~(0x01 << 7); // DMA发送[7]: 0=禁止、1=使能
    1323. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1323"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    1324. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1324"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 &= ~(0x01 << 7); // 关闭发送中断
    1325. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1325"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x01 << 5; // 使能接收中断: 接收缓冲区非空
    1326. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1326"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x01 << 4; // 使能空闲中断:超过1字节时间没收到新数据
    1327. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1327"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> SR = ~(0x00F0); // 清理中断
    1328. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1328"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    1329. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1329"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_SetPriority(UART5_IRQn, 0, 0); // 设置指定中断的响应优先级; 参数:中断请求编号、抢占级、子优先级
    1330. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1330"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_EnableIRQ(UART5_IRQn); // 使能、启用指定的中断
    1331. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1331"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,打开串口
    1332. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1332"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> CR1 |= 0x01 << 13; // 使能UART开始工作
    1333. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1333"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1334. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1334"> class="hljs-ln-code"> class="hljs-ln-line">
    1335. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1335"> class="hljs-ln-code"> class="hljs-ln-line"> // 关联缓冲区
    1336. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1336"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.puRxData = uaUart5RxData; // 获取接收缓冲区的地址
    1337. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1337"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.puTxFiFoData = uaUart5TxFiFoData; // 获取发送缓冲区的地址
    1338. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1338"> class="hljs-ln-code"> class="hljs-ln-line"> // 输出提示
    1339. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1339"> class="hljs-ln-code"> class="hljs-ln-line"> printf("UART5 初始化配置 %d-None-8-1; 已完成初始化配置、收发配置\r", ulBaudrate);
    1340. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1340"> class="hljs-ln-code"> class="hljs-ln-line">}
    1341. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1341"> class="hljs-ln-code"> class="hljs-ln-line">
    1342. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1342"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1343. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1343"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_IRQHandler
    1344. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1344"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART5的接收中断、空闲中断、发送中断
    1345. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1345"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1346. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1346"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1347. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1347"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数,当产生中断事件时,由硬件调用。
    1348. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1348"> class="hljs-ln-code"> class="hljs-ln-line"> * 如果使用本文件代码,在工程文件的其它地方,要注释同名函数,否则冲突。
    1349. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1349"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1350. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1350"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_IRQHandler(void)
    1351. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1351"> class="hljs-ln-code"> class="hljs-ln-line">{
    1352. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1352"> class="hljs-ln-code"> class="hljs-ln-line"> static uint16_t cnt = 0; // 接收字节数累计:每一帧数据已接收到的字节数
    1353. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1353"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t rxTemp[UART5_RX_BUF_SIZE]; // 接收数据缓存数组:每新接收1个字节,先顺序存放到这里,当一帧接收完(发生空闲中断), 再转存到外部缓存:xUARTx.puRxData[ ]
    1354. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1354"> class="hljs-ln-code"> class="hljs-ln-line">
    1355. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1355"> class="hljs-ln-code"> class="hljs-ln-line"> // 发送中断:用于把环形缓冲的数据,逐字节发出
    1356. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1356"> class="hljs-ln-code"> class="hljs-ln-line"> if ((UART5->SR & USART_SR_TXE) && (UART5->CR1 & USART_CR1_TXEIE)) // 检查发送寄存器空中断使能,且发送寄存器为空; TXE(发送数据寄存器空)、TXEIE(发送缓冲区空中断使能)
    1357. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1357"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1358. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1358"> class="hljs-ln-code"> class="hljs-ln-line"> UART5->DR = xUART5.puTxFiFoData[xUART5.usTxFiFoTail++]; // 从FIFO队列中取出一个数据,放入USART的发送寄存器(硬件会自动发出),然后将FIFO的尾指针递增,指向下一个要发送的数据
    1359. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1359"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART5.usTxFiFoTail == UART5_TX_BUF_SIZE) // 检查FIFO尾指针是否到达了FIFO队列的末尾
    1360. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1360"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.usTxFiFoTail = 0; // 将尾指针重置为0,实现环形队列的功能
    1361. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1361"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART5.usTxFiFoTail == xUART5.usTxFiFoData) // 检查FIFO尾指针是否追上了头指针,即所有数据是否都已发送完毕
    1362. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1362"> class="hljs-ln-code"> class="hljs-ln-line"> UART5->CR1 &= ~USART_CR1_TXEIE; // 关闭发送寄存器空中断,防止中断服务程序被不必要地调用
    1363. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1363"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1364. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1364"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1365. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1365"> class="hljs-ln-code"> class="hljs-ln-line">
    1366. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1366"> class="hljs-ln-code"> class="hljs-ln-line"> // 接收中断:用于逐个字节接收,存放到临时缓存
    1367. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1367"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART5->SR & USART_SR_RXNE) // 检查RXNE(读数据寄存器非空标志位); RXNE中断清理方法:读DR时自动清理;
    1368. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1368"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1369. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1369"> class="hljs-ln-code"> class="hljs-ln-line"> if (cnt == UART5_RX_BUF_SIZE) // 当前帧已接收的字节量,已满缓存区的大小; 为避免溢出,本包后面接收到的数据直接舍弃;
    1370. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1370"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1371. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1371"> class="hljs-ln-code"> class="hljs-ln-line"> printf("警告:UART5单帧接收量,已超出接收缓存大小!\r修改文件bsp_UART.h的UART5_RX_BUF_SIZE值,可解决此问题!\r");
    1372. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1372"> class="hljs-ln-code"> class="hljs-ln-line"> UART5->DR; // 读取数据寄存器的数据,但不保存.主要作用:读DR时自动清理接收中断标志;
    1373. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1373"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1374. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1374"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1375. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1375"> class="hljs-ln-code"> class="hljs-ln-line"> rxTemp[cnt++] = UART5->DR ; // 把新收到的字节数据,顺序存放到RXTemp数组中;注意:读取DR时自动清零中断位
    1376. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1376"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1377. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1377"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1378. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1378"> class="hljs-ln-code"> class="hljs-ln-line">
    1379. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1379"> class="hljs-ln-code"> class="hljs-ln-line"> // 空闲中断:用于判断一帧数据结束,整理数据到外部备读
    1380. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1380"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART5->SR & USART_SR_IDLE) // 检查IDLE(空闲中断标志位); IDLE中断标志清理方法:序列清零,USART1 ->SR; USART1 ->DR;
    1381. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1381"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1382. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1382"> class="hljs-ln-code"> class="hljs-ln-line"> memcpy(xUART5.puRxData, rxTemp, UART5_RX_BUF_SIZE); // 把本帧接收到的数据,存入到结构体的数组成员xUARTx.puRxData中, 等待处理; 注意:复制的是整个数组,包括0值,以方便字符串输出时尾部以0作字符串结束符
    1383. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1383"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.usRxNum = cnt; // 把接收到的字节数,存入到结构体变量xUARTx.usRxNum中;
    1384. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1384"> class="hljs-ln-code"> class="hljs-ln-line"> cnt = 0; // 接收字节数累计器,清零; 准备下一次的接收
    1385. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1385"> class="hljs-ln-code"> class="hljs-ln-line"> memset(rxTemp, 0, UART5_RX_BUF_SIZE); // 接收数据缓存数组,清零; 准备下一次的接收
    1386. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1386"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> SR;
    1387. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1387"> class="hljs-ln-code"> class="hljs-ln-line"> UART5 -> DR; // 清零IDLE中断标志位!! 序列清零,顺序不能错!!
    1388. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1388"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1389. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1389"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1390. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1390"> class="hljs-ln-code"> class="hljs-ln-line">
    1391. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1391"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1392. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1392"> class="hljs-ln-code"> class="hljs-ln-line">}
    1393. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1393"> class="hljs-ln-code"> class="hljs-ln-line">
    1394. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1394"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1395. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1395"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_SendData
    1396. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1396"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过中断发送数据
    1397. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1397"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】本函数可发送各种数据,而不限于字符串,如int,char
    1398. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1398"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】注意h文件中所定义的发缓冲区大小、注意数据压入缓冲区的速度与串口发送速度的冲突
    1399. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1399"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* pudata 需发送数据的地址
    1400. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1400"> class="hljs-ln-code"> class="hljs-ln-line"> * uint8_t usNum 发送的字节数 ,数量受限于h文件中设置的发送缓存区大小宏定义
    1401. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1401"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1402. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1402"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1403. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1403"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_SendData(uint8_t *pudata, uint16_t usNum)
    1404. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1404"> class="hljs-ln-code"> class="hljs-ln-line">{
    1405. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1405"> class="hljs-ln-code"> class="hljs-ln-line"> for (uint16_t i = 0; i < usNum; i++) // 把数据放入环形缓冲区
    1406. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1406"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1407. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1407"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.puTxFiFoData[xUART5.usTxFiFoData++] = pudata[i]; // 把字节放到缓冲区最后的位置,然后指针后移
    1408. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1408"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART5.usTxFiFoData == UART5_TX_BUF_SIZE) // 如果指针位置到达缓冲区的最大值,则归0
    1409. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1409"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.usTxFiFoData = 0;
    1410. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1410"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1411. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1411"> class="hljs-ln-code"> class="hljs-ln-line">
    1412. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1412"> class="hljs-ln-code"> class="hljs-ln-line"> if ((UART5->CR1 & USART_CR1_TXEIE) == 0) // 检查USART寄存器的发送缓冲区空置中断(TXEIE)是否已打开
    1413. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1413"> class="hljs-ln-code"> class="hljs-ln-line"> UART5->CR1 |= USART_CR1_TXEIE; // 打开TXEIE中断
    1414. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1414"> class="hljs-ln-code"> class="hljs-ln-line">}
    1415. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1415"> class="hljs-ln-code"> class="hljs-ln-line">
    1416. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1416"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1417. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1417"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_SendString
    1418. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1418"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送字符串
    1419. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1419"> class="hljs-ln-code"> class="hljs-ln-line"> * 用法请参考printf,及示例中的展示
    1420. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1420"> class="hljs-ln-code"> class="hljs-ln-line"> * 注意,最大发送字节数为512-1个字符,可在函数中修改上限
    1421. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1421"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: const char *pcString, ... (如同printf的用法)
    1422. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1422"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1423. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1423"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1424. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1424"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_SendString(const char *pcString, ...)
    1425. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1425"> class="hljs-ln-code"> class="hljs-ln-line">{
    1426. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1426"> class="hljs-ln-code"> class="hljs-ln-line"> char mBuffer[512] = {0};; // 开辟一个缓存, 并把里面的数据置0
    1427. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1427"> class="hljs-ln-code"> class="hljs-ln-line"> va_list ap; // 新建一个可变参数列表
    1428. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1428"> class="hljs-ln-code"> class="hljs-ln-line"> va_start(ap, pcString); // 列表指向第一个可变参数
    1429. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1429"> class="hljs-ln-code"> class="hljs-ln-line"> vsnprintf(mBuffer, 512, pcString, ap); // 把所有参数,按格式,输出到缓存; 参数2用于限制发送的最大字节数,如果达到上限,则只发送上限值-1; 最后1字节自动置'\0'
    1430. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1430"> class="hljs-ln-code"> class="hljs-ln-line"> va_end(ap); // 清空可变参数列表
    1431. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1431"> class="hljs-ln-code"> class="hljs-ln-line"> UART5_SendData((uint8_t *)mBuffer, strlen(mBuffer)); // 把字节存放环形缓冲,排队准备发送
    1432. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1432"> class="hljs-ln-code"> class="hljs-ln-line">}
    1433. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1433"> class="hljs-ln-code"> class="hljs-ln-line">
    1434. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1434"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1435. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1435"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_SendAT
    1436. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1436"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送AT命令, 并等待返回信息
    1437. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1437"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char *pcString AT指令字符串
    1438. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1438"> class="hljs-ln-code"> class="hljs-ln-line"> * char *pcAckString 期待的指令返回信息字符串
    1439. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1439"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usTimeOut 发送命令后等待的时间,毫秒
    1440. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1440"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1441. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1441"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回 值: 0-执行失败、1-执行正常
    1442. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1442"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1443. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1443"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART5_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs)
    1444. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1444"> class="hljs-ln-code"> class="hljs-ln-line">{
    1445. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1445"> class="hljs-ln-code"> class="hljs-ln-line"> UART5_ClearRx(); // 清0
    1446. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1446"> class="hljs-ln-code"> class="hljs-ln-line"> UART5_SendString(pcAT); // 发送AT指令字符串
    1447. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1447"> class="hljs-ln-code"> class="hljs-ln-line">
    1448. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1448"> class="hljs-ln-code"> class="hljs-ln-line"> while (usTimeOutMs--) // 判断是否起时(这里只作简单的循环判断次数处理)
    1449. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1449"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1450. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1450"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART5_GetRxNum()) // 判断是否接收到数据
    1451. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1451"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1452. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1452"> class="hljs-ln-code"> class="hljs-ln-line"> UART5_ClearRx(); // 清0接收字节数; 注意:接收到的数据内容 ,是没有被清0的
    1453. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1453"> class="hljs-ln-code"> class="hljs-ln-line"> if (strstr((char *)UART5_GetRxData(), pcAckString)) // 判断返回数据中是否有期待的字符
    1454. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1454"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 返回:0-超时没有返回、1-正常返回期待值
    1455. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1455"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1456. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1456"> class="hljs-ln-code"> class="hljs-ln-line"> delay_ms(1); // 延时; 用于超时退出处理,避免死等
    1457. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1457"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1458. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1458"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 返回:0-超时、返回异常,1-正常返回期待值
    1459. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1459"> class="hljs-ln-code"> class="hljs-ln-line">}
    1460. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1460"> class="hljs-ln-code"> class="hljs-ln-line">
    1461. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1461"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1462. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1462"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_GetRxNum
    1463. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1463"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据的字节数
    1464. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1464"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1465. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1465"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 0=没有接收到数据,非0=新一帧数据的字节数
    1466. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1466"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1467. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1467"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART5_GetRxNum(void)
    1468. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1468"> class="hljs-ln-code"> class="hljs-ln-line">{
    1469. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1469"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART5.usRxNum ;
    1470. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1470"> class="hljs-ln-code"> class="hljs-ln-line">}
    1471. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1471"> class="hljs-ln-code"> class="hljs-ln-line">
    1472. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1472"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1473. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1473"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_GetRxData
    1474. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1474"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据 (数据的地址)
    1475. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1475"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1476. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1476"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 数据的地址(uint8_t*)
    1477. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1477"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1478. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1478"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t *UART5_GetRxData(void)
    1479. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1479"> class="hljs-ln-code"> class="hljs-ln-line">{
    1480. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1480"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART5.puRxData ;
    1481. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1481"> class="hljs-ln-code"> class="hljs-ln-line">}
    1482. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1482"> class="hljs-ln-code"> class="hljs-ln-line">
    1483. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1483"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1484. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1484"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART5_ClearRx
    1485. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1485"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 清理最后一帧数据的缓存
    1486. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1486"> class="hljs-ln-code"> class="hljs-ln-line"> * 主要是清0字节数,因为它是用来判断接收的标准
    1487. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1487"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1488. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1488"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1489. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1489"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1490. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1490"> class="hljs-ln-code"> class="hljs-ln-line">void UART5_ClearRx(void)
    1491. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1491"> class="hljs-ln-code"> class="hljs-ln-line">{
    1492. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1492"> class="hljs-ln-code"> class="hljs-ln-line"> xUART5.usRxNum = 0 ;
    1493. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1493"> class="hljs-ln-code"> class="hljs-ln-line">}
    1494. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1494"> class="hljs-ln-code"> class="hljs-ln-line">#endif // endif UART5_EN
    1495. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1495"> class="hljs-ln-code"> class="hljs-ln-line">
    1496. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1496"> class="hljs-ln-code"> class="hljs-ln-line">
    1497. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1497"> class="hljs-ln-code"> class="hljs-ln-line">
    1498. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1498"> class="hljs-ln-code"> class="hljs-ln-line">
    1499. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1499"> class="hljs-ln-code"> class="hljs-ln-line">// USART-6 //
    1500. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1500"> class="hljs-ln-code"> class="hljs-ln-line">/
    1501. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1501"> class="hljs-ln-code"> class="hljs-ln-line">#if UART6_EN
    1502. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1502"> class="hljs-ln-code"> class="hljs-ln-line">
    1503. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1503"> class="hljs-ln-code"> class="hljs-ln-line">static xUSATR_TypeDef xUART6 = { 0 }; // 定义 UART6 的收发结构体
    1504. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1504"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart6RxData[UART6_RX_BUF_SIZE]; // 定义 UART6 的接收缓存
    1505. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1505"> class="hljs-ln-code"> class="hljs-ln-line">static uint8_t uaUart6TxFiFoData[UART6_TX_BUF_SIZE]; // 定义 UART6 的发送缓存
    1506. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1506"> class="hljs-ln-code"> class="hljs-ln-line">
    1507. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1507"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1508. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1508"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART6_Init
    1509. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1509"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 初始化USART6的通信引脚、协议参数、中断优先级
    1510. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1510"> class="hljs-ln-code"> class="hljs-ln-line"> * 引脚:TX-PC6、RX-PC7
    1511. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1511"> class="hljs-ln-code"> class="hljs-ln-line"> * 协议:波特率-None-8-1
    1512. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1512"> class="hljs-ln-code"> class="hljs-ln-line"> * 发送:发送中断
    1513. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1513"> class="hljs-ln-code"> class="hljs-ln-line"> * 接收:接收+空闲中断
    1514. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1514"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1515. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1515"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint32_t ulBaudrate 通信波特率
    1516. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1516"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1517. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1517"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1518. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1518"> class="hljs-ln-code"> class="hljs-ln-line">void UART6_Init(uint32_t ulBaudrate)
    1519. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1519"> class="hljs-ln-code"> class="hljs-ln-line">{
    1520. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1520"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_STDPERIPH_DRIVER
    1521. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1521"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    1522. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1522"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); // 使能 GPIOC 时钟
    1523. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1523"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); // 使能 USART6 时钟
    1524. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1524"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 引脚复用功能
    1525. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1525"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6); // 配置PC6复用功能:USART6
    1526. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1526"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6); // 配置PC7复用功能:USART6
    1527. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1527"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 USART
    1528. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1528"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE); // 使能重置
    1529. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1529"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, DISABLE); // 取消重置
    1530. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1530"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 TX引脚
    1531. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1531"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStructure = {0}; // GPIO 初始化结构体
    1532. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1532"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; // 引脚编号
    1533. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1533"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 引脚方向: 复用功能
    1534. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1534"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 输出模式:推挽
    1535. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1535"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上下拉:上拉
    1536. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1536"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 输出速度:50MHz
    1537. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1537"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOC, &GPIO_InitStructure); // 初始化:把上述参数,更新到芯片寄存器
    1538. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1538"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 RX引脚
    1539. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1539"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; // 引脚编号
    1540. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1540"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOC, &GPIO_InitStructure); // 初始化:把上述参数,更新到芯片寄存器
    1541. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1541"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置UART
    1542. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1542"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitTypeDef USART_InitStructure; // 声明USART初始化结构体
    1543. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1543"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_BaudRate = ulBaudrate; // 设置波特率
    1544. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1544"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 设置字长为8位数据格式
    1545. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1545"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_StopBits = USART_StopBits_1; // 设置一个停止位
    1546. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1546"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Parity = USART_Parity_No; // 无奇偶校验位
    1547. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1547"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制
    1548. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1548"> class="hljs-ln-code"> class="hljs-ln-line"> USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式
    1549. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1549"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Init(USART6, &USART_InitStructure); // 初始化USART
    1550. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1550"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    1551. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1551"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART6, USART_IT_RXNE, ENABLE); // 开启 接收中断
    1552. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1552"> class="hljs-ln-code"> class="hljs-ln-line"> USART_ITConfig(USART6, USART_IT_IDLE, ENABLE); // 开启 空闲中断
    1553. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1553"> class="hljs-ln-code"> class="hljs-ln-line"> // 中断优先级配置
    1554. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1554"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitTypeDef NVIC_InitStructure = {0}; // 中断优先级配置结构体
    1555. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1555"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannel = USART6_IRQn; // 指定中断通道
    1556. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1556"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 1; // 设置抢占优先级
    1557. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1557"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelSubPriority = 1; // 设置响应优先级
    1558. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1558"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_InitStructure .NVIC_IRQChannelCmd = ENABLE; // 使能中断通道
    1559. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1559"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_Init(&NVIC_InitStructure); // 初始化NVIC
    1560. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1560"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,开启USART
    1561. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1561"> class="hljs-ln-code"> class="hljs-ln-line"> USART_Cmd(USART6, ENABLE); // 使能USART
    1562. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1562"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1563. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1563"> class="hljs-ln-code"> class="hljs-ln-line">
    1564. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1564"> class="hljs-ln-code"> class="hljs-ln-line">#ifdef USE_HAL_DRIVER // HAL库 配置
    1565. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1565"> class="hljs-ln-code"> class="hljs-ln-line"> // 使能 时钟
    1566. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1566"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_GPIOC_CLK_ENABLE(); // 使能GPIOC
    1567. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1567"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART6_CLK_ENABLE(); // 使能UART6
    1568. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1568"> class="hljs-ln-code"> class="hljs-ln-line"> // 重置 UART5
    1569. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1569"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART6_FORCE_RESET(); // 使能重置
    1570. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1570"> class="hljs-ln-code"> class="hljs-ln-line"> __HAL_RCC_USART6_RELEASE_RESET(); // 取消重置
    1571. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1571"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 GPIO引脚
    1572. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1572"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStruct = {0}; // 声明初始化要用到的结构体
    1573. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1573"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; // 引脚 TX-PC6、RX-PC7
    1574. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1574"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // 工作模式
    1575. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1575"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Pull = GPIO_PULLUP; // 上下拉
    1576. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1576"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; // 引脚速率
    1577. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1577"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStruct.Alternate = GPIO_AF8_USART6; // 引脚复用功能
    1578. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1578"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); // 初始化引脚工作模式
    1579. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1579"> class="hljs-ln-code"> class="hljs-ln-line"> // 计算波特率
    1580. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1580"> class="hljs-ln-code"> class="hljs-ln-line"> float temp;
    1581. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1581"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t mantissa, fraction;
    1582. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1582"> class="hljs-ln-code"> class="hljs-ln-line"> SystemCoreClockUpdate(); // 更新系统运行频率全局值; 函数SystemCoreClock( ),在标准库、HAL库通用
    1583. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1583"> class="hljs-ln-code"> class="hljs-ln-line"> temp = (float)(SystemCoreClock / 2) / (ulBaudrate * 16); // 波特率公式计算; USART6挂载在APB2, 时钟为系统时钟的2分频; 全局变量SystemCoreClock,在标准库、HAL库通用;
    1584. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1584"> class="hljs-ln-code"> class="hljs-ln-line"> mantissa = temp; // 整数部分
    1585. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1585"> class="hljs-ln-code"> class="hljs-ln-line"> fraction = (temp - mantissa) * 16; // 小数部分
    1586. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1586"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> BRR = mantissa << 4 | fraction; // 设置波特率
    1587. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1587"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 UART
    1588. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1588"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR1 = 0; // 清0
    1589. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1589"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR1 |= 0x01 << 2; // 接收使能[02]: 0=失能、1=使能
    1590. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1590"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR1 |= 0x01 << 3; // 发送使能[03]:0=失能、1=使能
    1591. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1591"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR1 |= 0x00 << 9; // 奇偶校验[09]:0=偶Even、1=奇Odd; 注意:使用奇偶校验,下面两项要置1
    1592. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1592"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR1 |= 0x00 << 10; // 校验位 [10]:0=禁用、1=使能; 注意,使用奇偶校验,此位要置1 (否则无效、数据错乱)
    1593. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1593"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR1 |= 0x00 << 12; // 数据位 [12]:0=8位、 1=9位; 注意:使用奇偶校验,此位要置1 (否则数据会发生错乱)
    1594. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1594"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR2 = 0; // 数据清0
    1595. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1595"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR2 &= ~(0x03 << 12); // 停止位[13:12]:00b=1个停止位、01b=0.5个停止位、10b=2个停止位、11b=1.5个停止位
    1596. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1596"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR3 = 0; // 数据清0
    1597. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1597"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR3 &= ~(0x01 << 6); // DMA接收[6]: 0=禁止、1=使能
    1598. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1598"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR3 &= ~(0x01 << 7); // DMA发送[7]: 0=禁止、1=使能
    1599. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1599"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断
    1600. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1600"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR1 &= ~(0x01 << 7); // 关闭发送中断
    1601. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1601"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR1 |= 0x01 << 5; // 使能接收中断: 接收缓冲区非空
    1602. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1602"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR1 |= 0x01 << 4; // 使能空闲中断:超过1字节时间没收到新数据
    1603. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1603"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> SR = ~(0x00F0); // 清理中断
    1604. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1604"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置 中断优先级
    1605. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1605"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_SetPriority(USART6_IRQn, 1, 1); // 设置指定中断的响应优先级; 参数:中断请求编号、抢占级、子优先级
    1606. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1606"> class="hljs-ln-code"> class="hljs-ln-line"> HAL_NVIC_EnableIRQ(USART6_IRQn); // 使能、启用指定的中断
    1607. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1607"> class="hljs-ln-code"> class="hljs-ln-line"> // 配置完成,打开串口
    1608. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1608"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 -> CR1 |= 0x01 << 13; // 使能UART开始工作
    1609. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1609"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1610. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1610"> class="hljs-ln-code"> class="hljs-ln-line">
    1611. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1611"> class="hljs-ln-code"> class="hljs-ln-line"> // 关联缓冲区
    1612. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1612"> class="hljs-ln-code"> class="hljs-ln-line"> xUART6.puRxData = uaUart6RxData; // 获取接收缓冲区的地址
    1613. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1613"> class="hljs-ln-code"> class="hljs-ln-line"> xUART6.puTxFiFoData = uaUart6TxFiFoData; // 获取发送缓冲区的地址
    1614. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1614"> class="hljs-ln-code"> class="hljs-ln-line"> // 输出提示
    1615. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1615"> class="hljs-ln-code"> class="hljs-ln-line"> printf("UART6 初始化配置 %d-None-8-1; 已完成初始化配置、收发配置\r", ulBaudrate);
    1616. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1616"> class="hljs-ln-code"> class="hljs-ln-line">}
    1617. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1617"> class="hljs-ln-code"> class="hljs-ln-line">
    1618. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1618"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1619. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1619"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: USART6_IRQHandler
    1620. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1620"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: USART6的接收中断、空闲中断、发送中断
    1621. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1621"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1622. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1622"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1623. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1623"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 本函数,当产生中断事件时,由硬件调用。
    1624. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1624"> class="hljs-ln-code"> class="hljs-ln-line"> * 如果使用本文件代码,在工程文件的其它地方,要注释同名函数,否则冲突。
    1625. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1625"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1626. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1626"> class="hljs-ln-code"> class="hljs-ln-line">void USART6_IRQHandler(void)
    1627. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1627"> class="hljs-ln-code"> class="hljs-ln-line">{
    1628. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1628"> class="hljs-ln-code"> class="hljs-ln-line"> static uint16_t cnt = 0; // 接收字节数累计:每一帧数据已接收到的字节数
    1629. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1629"> class="hljs-ln-code"> class="hljs-ln-line"> static uint8_t rxTemp[UART6_RX_BUF_SIZE]; // 接收数据缓存数组:每新接收1个字节,先顺序存放到这里,当一帧接收完(发生空闲中断), 再转存到全局变量:xUARTx.puRxData[xx]中;
    1630. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1630"> class="hljs-ln-code"> class="hljs-ln-line">
    1631. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1631"> class="hljs-ln-code"> class="hljs-ln-line"> // 发送中断:用于把环形缓冲的数据,逐字节发出
    1632. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1632"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART6->SR & USART_SR_TXE) && (USART6->CR1 & USART_CR1_TXEIE)) // 检查发送寄存器空中断使能,且发送寄存器为空; TXE(发送数据寄存器空)、TXEIE(发送缓冲区空中断使能)
    1633. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1633"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1634. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1634"> class="hljs-ln-code"> class="hljs-ln-line"> USART6->DR = xUART6.puTxFiFoData[xUART6.usTxFiFoTail++]; // 从FIFO队列中取出一个数据,放入USART的发送寄存器(硬件会自动发出),然后将FIFO的尾指针递增,指向下一个要发送的数据
    1635. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1635"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART6.usTxFiFoTail == UART6_TX_BUF_SIZE) // 检查FIFO尾指针是否到达了FIFO队列的末尾
    1636. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1636"> class="hljs-ln-code"> class="hljs-ln-line"> xUART6.usTxFiFoTail = 0; // 将尾指针重置为0,实现环形队列的功能
    1637. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1637"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART6.usTxFiFoTail == xUART6.usTxFiFoData) // 检查FIFO尾指针是否追上了头指针,即所有数据是否都已发送完毕
    1638. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1638"> class="hljs-ln-code"> class="hljs-ln-line"> USART6->CR1 &= ~USART_CR1_TXEIE; // 关闭发送寄存器空中断,防止中断服务程序被不必要地调用
    1639. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1639"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1640. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1640"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1641. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1641"> class="hljs-ln-code"> class="hljs-ln-line">
    1642. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1642"> class="hljs-ln-code"> class="hljs-ln-line"> // 接收中断:用于逐个字节接收,存放到临时缓存
    1643. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1643"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART6->SR & USART_SR_RXNE) // 检查RXNE(读数据寄存器非空标志位); RXNE中断清理方法:读DR时自动清理;
    1644. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1644"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1645. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1645"> class="hljs-ln-code"> class="hljs-ln-line"> if (cnt >= UART6_RX_BUF_SIZE) // 当前帧已接收的字节量,已满缓存区的大小; 为避免溢出,本包后面接收到的数据直接舍弃;
    1646. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1646"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1647. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1647"> class="hljs-ln-code"> class="hljs-ln-line"> printf("警告:USART6单帧接收量,已超出接收缓存大小!\r修改文件bsp_UART.h的UART6_RX_BUF_SIZE值,可解决此问题!\r");
    1648. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1648"> class="hljs-ln-code"> class="hljs-ln-line"> USART6->DR; // 读取数据寄存器的数据,但不保存.主要作用:读DR时自动清理接收中断标志;
    1649. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1649"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1650. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1650"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1651. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1651"> class="hljs-ln-code"> class="hljs-ln-line"> rxTemp[cnt++] = USART6->DR ; // 把新收到的字节数据,顺序存放到RXTemp数组中;注意:读取DR时自动清零中断位
    1652. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1652"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1653. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1653"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1654. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1654"> class="hljs-ln-code"> class="hljs-ln-line">
    1655. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1655"> class="hljs-ln-code"> class="hljs-ln-line"> // 空闲中断:用于判断一帧数据结束,整理数据到外部备读
    1656. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1656"> class="hljs-ln-code"> class="hljs-ln-line"> if (USART6->SR & USART_SR_IDLE) // 检查IDLE(空闲中断标志位); IDLE中断标志清理方法:序列清零,USART1 ->SR; USART1 ->DR;
    1657. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1657"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1658. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1658"> class="hljs-ln-code"> class="hljs-ln-line"> memcpy(xUART6.puRxData, rxTemp, UART6_RX_BUF_SIZE); // 把本帧接收到的数据,存入到结构体的数组成员xUARTx.puRxData中, 等待处理; 注意:复制的是整个数组,包括0值,以方便字符串输出时尾部以0作字符串结束符
    1659. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1659"> class="hljs-ln-code"> class="hljs-ln-line"> xUART6.usRxNum = cnt; // 把接收到的字节数,存入到结构体变量xUARTx.usRxNum中;
    1660. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1660"> class="hljs-ln-code"> class="hljs-ln-line"> cnt = 0; // 接收字节数累计器,清零; 准备下一次的接收
    1661. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1661"> class="hljs-ln-code"> class="hljs-ln-line"> memset(rxTemp, 0, UART6_RX_BUF_SIZE); // 接收数据缓存数组,清零; 准备下一次的接收
    1662. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1662"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 ->SR;
    1663. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1663"> class="hljs-ln-code"> class="hljs-ln-line"> USART6 ->DR; // 清零IDLE中断标志位!! 序列清零,顺序不能错!!
    1664. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1664"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1665. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1665"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1666. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1666"> class="hljs-ln-code"> class="hljs-ln-line">
    1667. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1667"> class="hljs-ln-code"> class="hljs-ln-line"> return;
    1668. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1668"> class="hljs-ln-code"> class="hljs-ln-line">}
    1669. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1669"> class="hljs-ln-code"> class="hljs-ln-line">
    1670. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1670"> class="hljs-ln-code"> class="hljs-ln-line">
    1671. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1671"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1672. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1672"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART6_SendData
    1673. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1673"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: UART通过中断发送数据
    1674. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1674"> class="hljs-ln-code"> class="hljs-ln-line"> * 【适合场景】本函数可发送各种数据,而不限于字符串,如int,char
    1675. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1675"> class="hljs-ln-code"> class="hljs-ln-line"> * 【不 适 合】注意h文件中所定义的发缓冲区大小、注意数据压入缓冲区的速度与串口发送速度的冲突
    1676. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1676"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t *puData 需发送数据的地址
    1677. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1677"> class="hljs-ln-code"> class="hljs-ln-line"> * uint8_t usNum 发送的字节数 ,数量受限于h文件中设置的发送缓存区大小宏定义
    1678. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1678"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1679. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1679"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1680. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1680"> class="hljs-ln-code"> class="hljs-ln-line">void UART6_SendData(uint8_t *puData, uint16_t usNum)
    1681. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1681"> class="hljs-ln-code"> class="hljs-ln-line">{
    1682. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1682"> class="hljs-ln-code"> class="hljs-ln-line"> for (uint16_t i = 0; i < usNum; i++) // 把数据放入环形缓冲区
    1683. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1683"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1684. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1684"> class="hljs-ln-code"> class="hljs-ln-line"> xUART6.puTxFiFoData[xUART6.usTxFiFoData++] = puData[i]; // 把字节放到缓冲区最后的位置,然后指针后移
    1685. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1685"> class="hljs-ln-code"> class="hljs-ln-line"> if (xUART6.usTxFiFoData == UART6_TX_BUF_SIZE) // 如果指针位置到达缓冲区的最大值,则归0
    1686. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1686"> class="hljs-ln-code"> class="hljs-ln-line"> xUART6.usTxFiFoData = 0;
    1687. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1687"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1688. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1688"> class="hljs-ln-code"> class="hljs-ln-line">
    1689. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1689"> class="hljs-ln-code"> class="hljs-ln-line"> if ((USART6->CR1 & USART_CR1_TXEIE) == 0) // 检查USART寄存器的发送缓冲区空置中断(TXEIE)是否已打开
    1690. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1690"> class="hljs-ln-code"> class="hljs-ln-line"> USART6->CR1 |= USART_CR1_TXEIE; // 打开TXEIE中断
    1691. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1691"> class="hljs-ln-code"> class="hljs-ln-line">}
    1692. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1692"> class="hljs-ln-code"> class="hljs-ln-line">
    1693. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1693"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1694. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1694"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART6_SendString
    1695. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1695"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送字符串
    1696. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1696"> class="hljs-ln-code"> class="hljs-ln-line"> * 用法请参考printf,及示例中的展示
    1697. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1697"> class="hljs-ln-code"> class="hljs-ln-line"> * 注意,最大发送字节数为512-1个字符,可在函数中修改上限
    1698. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1698"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: const char *pcString, ... (如同printf的用法)
    1699. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1699"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1700. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1700"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1701. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1701"> class="hljs-ln-code"> class="hljs-ln-line">void UART6_SendString(const char *pcString, ...)
    1702. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1702"> class="hljs-ln-code"> class="hljs-ln-line">{
    1703. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1703"> class="hljs-ln-code"> class="hljs-ln-line"> char mBuffer[512] = {0};; // 开辟一个缓存, 并把里面的数据置0
    1704. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1704"> class="hljs-ln-code"> class="hljs-ln-line"> va_list ap; // 新建一个可变参数列表
    1705. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1705"> class="hljs-ln-code"> class="hljs-ln-line"> va_start(ap, pcString); // 列表指向第一个可变参数
    1706. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1706"> class="hljs-ln-code"> class="hljs-ln-line"> vsnprintf(mBuffer, 512, pcString, ap); // 把所有参数,按格式,输出到缓存; 参数2用于限制发送的最大字节数,如果达到上限,则只发送上限值-1; 最后1字节自动置'\0'
    1707. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1707"> class="hljs-ln-code"> class="hljs-ln-line"> va_end(ap); // 清空可变参数列表
    1708. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1708"> class="hljs-ln-code"> class="hljs-ln-line"> UART6_SendData((uint8_t *)mBuffer, strlen(mBuffer)); // 把字节存放环形缓冲,排队准备发送
    1709. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1709"> class="hljs-ln-code"> class="hljs-ln-line">}
    1710. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1710"> class="hljs-ln-code"> class="hljs-ln-line">
    1711. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1711"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1712. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1712"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART6_SendAT
    1713. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1713"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 发送AT命令, 并等待返回信息
    1714. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1714"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: char *pcString AT指令字符串
    1715. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1715"> class="hljs-ln-code"> class="hljs-ln-line"> * char *pcAckString 期待的指令返回信息字符串
    1716. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1716"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t usTimeOut 发送命令后等待的时间,毫秒
    1717. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1717"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1718. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1718"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回 值: 0-执行失败、1-执行正常
    1719. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1719"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1720. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1720"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t UART6_SendAT(char *pcAT, char *pcAckString, uint16_t usTimeOutMs)
    1721. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1721"> class="hljs-ln-code"> class="hljs-ln-line">{
    1722. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1722"> class="hljs-ln-code"> class="hljs-ln-line"> UART6_ClearRx(); // 清0
    1723. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1723"> class="hljs-ln-code"> class="hljs-ln-line"> UART6_SendString(pcAT); // 发送AT指令字符串
    1724. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1724"> class="hljs-ln-code"> class="hljs-ln-line">
    1725. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1725"> class="hljs-ln-code"> class="hljs-ln-line"> while (usTimeOutMs--) // 判断是否起时(这里只作简单的循环判断次数处理)
    1726. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1726"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1727. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1727"> class="hljs-ln-code"> class="hljs-ln-line"> if (UART6_GetRxNum()) // 判断是否接收到数据
    1728. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1728"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1729. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1729"> class="hljs-ln-code"> class="hljs-ln-line"> UART6_ClearRx(); // 清0接收字节数; 注意:接收到的数据内容 ,是没有被清0的
    1730. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1730"> class="hljs-ln-code"> class="hljs-ln-line"> if (strstr((char *)UART6_GetRxData(), pcAckString)) // 判断返回数据中是否有期待的字符
    1731. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1731"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 返回:0-超时没有返回、1-正常返回期待值
    1732. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1732"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1733. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1733"> class="hljs-ln-code"> class="hljs-ln-line"> delay_ms(1); // 延时; 用于超时退出处理,避免死等
    1734. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1734"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1735. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1735"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 返回:0-超时、返回异常,1-正常返回期待值
    1736. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1736"> class="hljs-ln-code"> class="hljs-ln-line">}
    1737. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1737"> class="hljs-ln-code"> class="hljs-ln-line">
    1738. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1738"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1739. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1739"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART6_GetRxNum
    1740. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1740"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据的字节数
    1741. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1741"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1742. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1742"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 0=没有接收到数据,非0=新一帧数据的字节数
    1743. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1743"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1744. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1744"> class="hljs-ln-code"> class="hljs-ln-line">uint16_t UART6_GetRxNum(void)
    1745. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1745"> class="hljs-ln-code"> class="hljs-ln-line">{
    1746. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1746"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART6.usRxNum ;
    1747. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1747"> class="hljs-ln-code"> class="hljs-ln-line">}
    1748. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1748"> class="hljs-ln-code"> class="hljs-ln-line">
    1749. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1749"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1750. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1750"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART6_GetRxData
    1751. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1751"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 获取最新一帧数据 (数据的地址)
    1752. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1752"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1753. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1753"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 数据的地址(uint8_t*)
    1754. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1754"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1755. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1755"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t *UART6_GetRxData(void)
    1756. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1756"> class="hljs-ln-code"> class="hljs-ln-line">{
    1757. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1757"> class="hljs-ln-code"> class="hljs-ln-line"> return xUART6.puRxData ;
    1758. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1758"> class="hljs-ln-code"> class="hljs-ln-line">}
    1759. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1759"> class="hljs-ln-code"> class="hljs-ln-line">
    1760. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1760"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1761. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1761"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: UART6_ClearRx
    1762. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1762"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 清理最后一帧数据的缓存
    1763. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1763"> class="hljs-ln-code"> class="hljs-ln-line"> * 主要是清0字节数,因为它是用来判断接收的标准
    1764. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1764"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: 无
    1765. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1765"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1766. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1766"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1767. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1767"> class="hljs-ln-code"> class="hljs-ln-line">void UART6_ClearRx(void)
    1768. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1768"> class="hljs-ln-code"> class="hljs-ln-line">{
    1769. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1769"> class="hljs-ln-code"> class="hljs-ln-line"> xUART6.usRxNum = 0 ;
    1770. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1770"> class="hljs-ln-code"> class="hljs-ln-line">}
    1771. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1771"> class="hljs-ln-code"> class="hljs-ln-line">#endif // endif UART6_EN
    1772. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1772"> class="hljs-ln-code"> class="hljs-ln-line">
    1773. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1773"> class="hljs-ln-code"> class="hljs-ln-line">
    1774. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1774"> class="hljs-ln-code"> class="hljs-ln-line">
    1775. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1775"> class="hljs-ln-code"> class="hljs-ln-line">
    1776. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1776"> class="hljs-ln-code"> class="hljs-ln-line">
    1777. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1777"> class="hljs-ln-code"> class="hljs-ln-line">
    1778. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1778"> class="hljs-ln-code"> class="hljs-ln-line">/ ModbusCRC16 校验 /
    1779. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1779"> class="hljs-ln-code"> class="hljs-ln-line">///
    1780. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1780"> class="hljs-ln-code"> class="hljs-ln-line">
    1781. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1781"> class="hljs-ln-code"> class="hljs-ln-line">// CRC16查表法值表数组
    1782. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1782"> class="hljs-ln-code"> class="hljs-ln-line">// 查表法相比直接计算CRC值,大约可减少一半运算时间
    1783. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1783"> class="hljs-ln-code"> class="hljs-ln-line">static const uint8_t aModbusCRC16[] =
    1784. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1784"> class="hljs-ln-code"> class="hljs-ln-line">{
    1785. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1785"> class="hljs-ln-code"> class="hljs-ln-line"> // 高位
    1786. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1786"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1787. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1787"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1788. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1788"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1789. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1789"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1790. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1790"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1791. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1791"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1792. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1792"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1793. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1793"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1794. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1794"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1795. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1795"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1796. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1796"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1797. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1797"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1798. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1798"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1799. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1799"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1800. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1800"> class="hljs-ln-code"> class="hljs-ln-line"> 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    1801. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1801"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    1802. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1802"> class="hljs-ln-code"> class="hljs-ln-line"> // 低位
    1803. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1803"> class="hljs-ln-code"> class="hljs-ln-line"> 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04,
    1804. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1804"> class="hljs-ln-code"> class="hljs-ln-line"> 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8,
    1805. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1805"> class="hljs-ln-code"> class="hljs-ln-line"> 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
    1806. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1806"> class="hljs-ln-code"> class="hljs-ln-line"> 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10,
    1807. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1807"> class="hljs-ln-code"> class="hljs-ln-line"> 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
    1808. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1808"> class="hljs-ln-code"> class="hljs-ln-line"> 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
    1809. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1809"> class="hljs-ln-code"> class="hljs-ln-line"> 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C,
    1810. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1810"> class="hljs-ln-code"> class="hljs-ln-line"> 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0,
    1811. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1811"> class="hljs-ln-code"> class="hljs-ln-line"> 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
    1812. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1812"> class="hljs-ln-code"> class="hljs-ln-line"> 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
    1813. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1813"> class="hljs-ln-code"> class="hljs-ln-line"> 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C,
    1814. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1814"> class="hljs-ln-code"> class="hljs-ln-line"> 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
    1815. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1815"> class="hljs-ln-code"> class="hljs-ln-line"> 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54,
    1816. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1816"> class="hljs-ln-code"> class="hljs-ln-line"> 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98,
    1817. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1817"> class="hljs-ln-code"> class="hljs-ln-line"> 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
    1818. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1818"> class="hljs-ln-code"> class="hljs-ln-line"> 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
    1819. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1819"> class="hljs-ln-code"> class="hljs-ln-line">};
    1820. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1820"> class="hljs-ln-code"> class="hljs-ln-line">
    1821. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1821"> class="hljs-ln-code"> class="hljs-ln-line">
    1822. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1822"> class="hljs-ln-code"> class="hljs-ln-line">/**********************************************************************************************************
    1823. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1823"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: Modbus_AddCRC16
    1824. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1824"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 在数据末尾追加两字节的ModbusCRC16校验值
    1825. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1825"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注: 1、函数将根据传入的原数据,计算CRC16值,在数据末尾追加两字节的CRC16校验值;
    1826. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1826"> class="hljs-ln-code"> class="hljs-ln-line"> * 2、注意传入的数组地址,要至少预留2字节的空间,用于追加CRC16的校验值;
    1827. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1827"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1828. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1828"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* _pcData:参与计算校验值的数据
    1829. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1829"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t _usLen :数据字节数; 长度为数据的原字节数即可,不用加2;
    1830. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1830"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1831. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1831"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回: 无
    1832. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1832"> class="hljs-ln-code"> class="hljs-ln-line">**********************************************************************************************************/
    1833. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1833"> class="hljs-ln-code"> class="hljs-ln-line">void Modbus_AddCRC16(uint8_t *_pcData, uint16_t _usLen)
    1834. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1834"> class="hljs-ln-code"> class="hljs-ln-line">{
    1835. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1835"> class="hljs-ln-code"> class="hljs-ln-line"> uint8_t ucCRCHi = 0xFF; // CRC值高位
    1836. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1836"> class="hljs-ln-code"> class="hljs-ln-line"> uint8_t ucCRCLo = 0xFF; // CRC值低位
    1837. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1837"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t usIndex; // 索引
    1838. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1838"> class="hljs-ln-code"> class="hljs-ln-line">
    1839. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1839"> class="hljs-ln-code"> class="hljs-ln-line"> while (_usLen--) // 开始逐字节查表
    1840. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1840"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1841. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1841"> class="hljs-ln-code"> class="hljs-ln-line"> usIndex = ucCRCHi ^ *_pcData++; // 注意,这里指针地址加1了
    1842. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1842"> class="hljs-ln-code"> class="hljs-ln-line"> ucCRCHi = ucCRCLo ^ aModbusCRC16[usIndex];
    1843. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1843"> class="hljs-ln-code"> class="hljs-ln-line"> ucCRCLo = aModbusCRC16[usIndex + 256];
    1844. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1844"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1845. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1845"> class="hljs-ln-code"> class="hljs-ln-line"> _pcData[0] = ucCRCHi; // 末尾第1字节,追加CRC16的高位
    1846. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1846"> class="hljs-ln-code"> class="hljs-ln-line"> _pcData[1] = ucCRCLo; // 末尾第2字节,追加CRC16的低位
    1847. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1847"> class="hljs-ln-code"> class="hljs-ln-line">}
    1848. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1848"> class="hljs-ln-code"> class="hljs-ln-line">
    1849. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1849"> class="hljs-ln-code"> class="hljs-ln-line">
    1850. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1850"> class="hljs-ln-code"> class="hljs-ln-line">
    1851. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1851"> class="hljs-ln-code"> class="hljs-ln-line">/**********************************************************************************************************
    1852. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1852"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: Modbus_CheckCRC16
    1853. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1853"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 对带ModbusCRC16校验的数据段进行校验
    1854. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1854"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1855. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1855"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t* _pcData:数据段地址
    1856. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1856"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t _usLen :数据段的长度(字节数); 长度是整包数据的字节数,即包含ModbusCRC16值的长度,不用减2;
    1857. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1857"> class="hljs-ln-code"> class="hljs-ln-line"> *
    1858. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1858"> class="hljs-ln-code"> class="hljs-ln-line"> * 返 回: 0-错误、1-正确
    1859. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1859"> class="hljs-ln-code"> class="hljs-ln-line">*******************************************************************************************/
    1860. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1860"> class="hljs-ln-code"> class="hljs-ln-line">uint8_t Modbus_CheckCRC16(uint8_t *_pcData, uint16_t _usLen)
    1861. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1861"> class="hljs-ln-code"> class="hljs-ln-line">{
    1862. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1862"> class="hljs-ln-code"> class="hljs-ln-line"> uint8_t ucCRCHi = 0xFF; // CRC值高位
    1863. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1863"> class="hljs-ln-code"> class="hljs-ln-line"> uint8_t ucCRCLo = 0xFF; // CRC值低位
    1864. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1864"> class="hljs-ln-code"> class="hljs-ln-line"> uint16_t usIndex; // 数组的索引
    1865. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1865"> class="hljs-ln-code"> class="hljs-ln-line">
    1866. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1866"> class="hljs-ln-code"> class="hljs-ln-line"> _usLen -= 2; // 字节数-2,不计算原数据末尾两字节(ModbusCRC16值)
    1867. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1867"> class="hljs-ln-code"> class="hljs-ln-line">
    1868. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1868"> class="hljs-ln-code"> class="hljs-ln-line"> while (_usLen--) // 开始逐字节查表获得ModbusCRC16值
    1869. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1869"> class="hljs-ln-code"> class="hljs-ln-line"> {
    1870. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1870"> class="hljs-ln-code"> class="hljs-ln-line"> usIndex = ucCRCHi ^ *_pcData++; // 注意,这里指针地址递增加1
    1871. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1871"> class="hljs-ln-code"> class="hljs-ln-line"> ucCRCHi = ucCRCLo ^ aModbusCRC16[usIndex];
    1872. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1872"> class="hljs-ln-code"> class="hljs-ln-line"> ucCRCLo = aModbusCRC16[usIndex + 256];
    1873. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1873"> class="hljs-ln-code"> class="hljs-ln-line"> }
    1874. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1874"> class="hljs-ln-code"> class="hljs-ln-line">
    1875. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1875"> class="hljs-ln-code"> class="hljs-ln-line"> if ((ucCRCHi == *_pcData++) && (ucCRCLo == *_pcData)) // 把数据段的CRC16校验值,与原数据末尾的CRC16值相比较
    1876. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1876"> class="hljs-ln-code"> class="hljs-ln-line"> return 1; // 成功匹配; 返回: 1
    1877. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1877"> class="hljs-ln-code"> class="hljs-ln-line">
    1878. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1878"> class="hljs-ln-code"> class="hljs-ln-line"> return 0; // 错误; 返回:0
    1879. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1879"> class="hljs-ln-code"> class="hljs-ln-line">}
    1880. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1880"> class="hljs-ln-code"> class="hljs-ln-line">
    1881. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1881"> class="hljs-ln-code"> class="hljs-ln-line">
    1882. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1882"> class="hljs-ln-code"> class="hljs-ln-line">
    1883. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1883"> class="hljs-ln-code"> class="hljs-ln-line">
    1884. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1884"> class="hljs-ln-code"> class="hljs-ln-line">
    1885. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1885"> class="hljs-ln-code"> class="hljs-ln-line">/ 辅助功能 /
    1886. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1886"> class="hljs-ln-code"> class="hljs-ln-line">///
    1887. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1887"> class="hljs-ln-code"> class="hljs-ln-line">
    1888. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1888"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1889. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1889"> class="hljs-ln-code"> class="hljs-ln-line"> * 函 数: debug_showData
    1890. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1890"> class="hljs-ln-code"> class="hljs-ln-line"> * 功 能: 经printf,发送到串口助手上,方便观察
    1891. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1891"> class="hljs-ln-code"> class="hljs-ln-line"> * 参 数: uint8_t *rxData 数据地址
    1892. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1892"> class="hljs-ln-code"> class="hljs-ln-line"> * uint16_t rxNum 字节数
    1893. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1893"> class="hljs-ln-code"> class="hljs-ln-line"> * 返回值: 无
    1894. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1894"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1895. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1895"> class="hljs-ln-code"> class="hljs-ln-line">#if 0
    1896. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1896"> class="hljs-ln-code"> class="hljs-ln-line">void debug_showData(uint8_t *puRxData, uint16_t usRxNum)
    1897. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1897"> class="hljs-ln-code"> class="hljs-ln-line">{
    1898. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1898"> class="hljs-ln-code"> class="hljs-ln-line"> printf("字节数: %d \r", usRxNum); // 显示字节数
    1899. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1899"> class="hljs-ln-code"> class="hljs-ln-line"> printf("ASCII 显示数据: %s\r", (char *)puRxData); // 显示数据,以ASCII方式显示,即以字符串的方式显示
    1900. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1900"> class="hljs-ln-code"> class="hljs-ln-line"> printf("16进制显示数据: "); // 显示数据,以16进制方式,显示每一个字节的值
    1901. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1901"> class="hljs-ln-code"> class="hljs-ln-line"> while (usRxNum--) // 逐个字节判断,只要不为'\0', 就继续
    1902. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1902"> class="hljs-ln-code"> class="hljs-ln-line"> printf("0x%X ", *puRxData++); // 格式化
    1903. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1903"> class="hljs-ln-code"> class="hljs-ln-line"> printf("\r\r"); // 显示换行
    1904. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1904"> class="hljs-ln-code"> class="hljs-ln-line">}
    1905. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1905"> class="hljs-ln-code"> class="hljs-ln-line">#endif
    1906. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1906"> class="hljs-ln-code"> class="hljs-ln-line">
    1907. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1907"> class="hljs-ln-code"> class="hljs-ln-line">
    1908. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1908"> class="hljs-ln-code"> class="hljs-ln-line">
    1909. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1909"> class="hljs-ln-code"> class="hljs-ln-line">
    1910. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1910"> class="hljs-ln-code"> class="hljs-ln-line">// printf //
    1911. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1911"> class="hljs-ln-code"> class="hljs-ln-line">///
    1912. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1912"> class="hljs-ln-code"> class="hljs-ln-line">/******************************************************************************
    1913. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1913"> class="hljs-ln-code"> class="hljs-ln-line"> * 功能说明: printf函数支持代码
    1914. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1914"> class="hljs-ln-code"> class="hljs-ln-line"> * 【特别注意】加入以下代码, 使用printf函数时, 不再需要打勾use MicroLIB
    1915. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1915"> class="hljs-ln-code"> class="hljs-ln-line"> * 备 注:
    1916. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1916"> class="hljs-ln-code"> class="hljs-ln-line"> * 最后更新: 2024年06月07日
    1917. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1917"> class="hljs-ln-code"> class="hljs-ln-line"> ******************************************************************************/
    1918. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1918"> class="hljs-ln-code"> class="hljs-ln-line">#pragma import(__use_no_semihosting)
    1919. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1919"> class="hljs-ln-code"> class="hljs-ln-line">
    1920. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1920"> class="hljs-ln-code"> class="hljs-ln-line">struct __FILE
    1921. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1921"> class="hljs-ln-code"> class="hljs-ln-line">{
    1922. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1922"> class="hljs-ln-code"> class="hljs-ln-line"> int handle;
    1923. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1923"> class="hljs-ln-code"> class="hljs-ln-line">}; // 标准库需要的支持函数
    1924. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1924"> class="hljs-ln-code"> class="hljs-ln-line">
    1925. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1925"> class="hljs-ln-code"> class="hljs-ln-line">FILE __stdout; // FILE 在stdio.h文件
    1926. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1926"> class="hljs-ln-code"> class="hljs-ln-line">void _sys_exit(int x)
    1927. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1927"> class="hljs-ln-code"> class="hljs-ln-line">{
    1928. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1928"> class="hljs-ln-code"> class="hljs-ln-line"> x = x; // 定义_sys_exit()以避免使用半主机模式
    1929. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1929"> class="hljs-ln-code"> class="hljs-ln-line">}
    1930. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1930"> class="hljs-ln-code"> class="hljs-ln-line">
    1931. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1931"> class="hljs-ln-code"> class="hljs-ln-line">int fputc(int ch, FILE *f) // 重定向fputc函数,使printf的输出,由fputc输出到UART
    1932. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1932"> class="hljs-ln-code"> class="hljs-ln-line">{
    1933. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1933"> class="hljs-ln-code"> class="hljs-ln-line"> UART1_SendData((uint8_t *)&ch, 1); // 使用队列+中断方式发送数据; 无需像方式1那样等待耗时,但要借助已写好的函数、环形缓冲
    1934. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1934"> class="hljs-ln-code"> class="hljs-ln-line"> return ch;
    1935. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1935"> class="hljs-ln-code"> class="hljs-ln-line">}
    1936. class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1936"> class="hljs-ln-code"> class="hljs-ln-line">
    class="hide-preCode-box"> class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}" onclick="hljs.signin(event)">

    data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/qq_49053936/article/details/135478892","extend1":"pc","ab":"new"}">>
    注:本文转载自blog.csdn.net的【 STM32开发 】的文章"https://blog.csdn.net/qq_49053936/article/details/135478892"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
    复制链接

    评论记录:

    未查询到任何数据!