当前位置:   article > 正文

STM32多字节发送与接收(附淘晶驰串口屏教程)_陶晶驰串口屏全双工

陶晶驰串口屏全双工

由于本人做题目时发现正点原子的串口发送教程是单字节,也在网上找了许多案例,但都不是很满意,这里借鉴了一下B站江科大UP主的代码,结合自己的需求,改了一下,大家可以参考一下,讲的很好,话不多说,直接上代码,希望帮助更多小伙伴。同时哪里不对的地方望大神多多指教。

usart.c

头文件自己添加

  1. #if 1
  2. #pragma import(__use_no_semihosting)
  3. uint8_t Serial_TxPacket[4]; //FF 01 02 03 04 FE
  4. uint8_t Serial_RxPacket[4];
  5. uint8_t Serial_RxFlag;
  6. //标准库需要的支持函数
  7. struct __FILE
  8. {
  9. int handle;
  10. };
  11. FILE __stdout;
  12. //定义_sys_exit()以避免使用半主机模式
  13. void _sys_exit(int x)
  14. {
  15. x = x;
  16. }
  17. //重定义fputc函数
  18. int fputc(int ch, FILE *f)
  19. {
  20. while ((USART1->SR & 0X40) == 0); //循环发送,直到发送完毕
  21. USART1->DR = (u8) ch;
  22. return ch;
  23. }
  24. #endif
  25. /*使用microLib的方法*/
  26. /*
  27. int fputc(int ch, FILE *f)
  28. {
  29. USART_SendData(USART1, (uint8_t) ch);
  30. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
  31. return ch;
  32. }
  33. int GetKey (void) {
  34. while (!(USART1->SR & USART_FLAG_RXNE));
  35. return ((int)(USART1->DR & 0x1FF));
  36. }
  37. */
  38. //#if EN_USART1_RX //如果使能了接收
  39. //串口1中断服务程序
  40. //注意,读取USARTx->SR能避免莫名其妙的错误
  41. u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.
  42. //接收状态
  43. //bit15, 接收完成标志
  44. //bit14, 接收到0x0d
  45. //bit13~0, 接收到的有效字节数目
  46. u16 USART_RX_STA = 0; //接收状态标记
  47. char rxarray[4];
  48. uint8_t rxlength=0;
  49. //字符串发送函数
  50. void HMISends(char *buf1)
  51. {
  52. u8 i=0;
  53. while(1)
  54. {
  55. if(buf1[i] != 0)
  56. {
  57. USART_SendData(USART1,buf1[i]); //发送一个字节
  58. while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET){};//等待发送结束
  59. i++;
  60. }
  61. else
  62. {
  63. return ;
  64. }
  65. }
  66. }
  67. //字节发送函数
  68. void HMISendb(u8 k)
  69. {
  70. u8 i;
  71. for(i=0; i<3; i++)
  72. {
  73. if(k != 0)
  74. {
  75. USART_SendData(USART1,k); //发送一个字节
  76. while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET){};//等待发送结束
  77. }
  78. else
  79. {
  80. return ;
  81. }
  82. }
  83. }
  84. void uart_init(u32 bound)
  85. {
  86. //GPIO端口设置
  87. GPIO_InitTypeDef GPIO_InitStructure;
  88. USART_InitTypeDef USART_InitStructure;
  89. NVIC_InitTypeDef NVIC_InitStructure;
  90. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
  91. //USART1_TX GPIOA.9
  92. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  93. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  94. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  95. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
  96. //USART1_RX GPIOA.10初始化
  97. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
  98. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  99. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
  100. //Usart1 NVIC 配置
  101. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  102. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3 ; //抢占优先级3
  103. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
  104. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  105. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  106. //USART 初始化设置
  107. USART_InitStructure.USART_BaudRate = bound;//串口波特率
  108. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  109. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  110. USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  111. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  112. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  113. USART_Init(USART1, &USART_InitStructure); //初始化串口1
  114. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
  115. USART_Cmd(USART1, ENABLE); //使能串口1
  116. }
  117. void Serial_SendByte(uint8_t Byte)
  118. {
  119. USART_SendData(USART1, Byte);
  120. while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
  121. }
  122. void Serial_SendArray(uint8_t *Array, uint16_t Length)
  123. {
  124. uint16_t i;
  125. for (i = 0; i < Length; i ++)
  126. {
  127. Serial_SendByte(Array[i]);
  128. }
  129. }
  130. void Serial_SendPacket(void) //发送字头
  131. {
  132. Serial_SendByte(0xFF);
  133. Serial_SendArray(Serial_TxPacket, 4);
  134. Serial_SendByte(0xFE);
  135. }
  136. uint8_t Serial_GetRxFlag(void) //读取状态位
  137. {
  138. if (Serial_RxFlag == 1)
  139. {
  140. Serial_RxFlag = 0;
  141. return 1;
  142. }
  143. return 0;
  144. }
  145. char tjcstr[100];
  146. void USART1_IRQHandler(void)
  147. {
  148. int a=9;
  149. static uint8_t RxState = 0;
  150. static uint8_t pRxPacket = 0;
  151. if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
  152. {
  153. uint8_t RxData = USART_ReceiveData(USART1);
  154. // printf ("%d",RxData);
  155. if (RxState == 0)
  156. {
  157. if (RxData == 0xFF)
  158. {
  159. RxState = 1;
  160. pRxPacket = 0;
  161. }
  162. }
  163. else if (RxState == 1)
  164. {
  165. Serial_RxPacket[pRxPacket] = RxData;
  166. pRxPacket ++;
  167. if (pRxPacket >= 4)
  168. {
  169. RxState = 2;
  170. }
  171. }
  172. else if (RxState == 2)
  173. {
  174. if (RxData == 0xFE)
  175. {
  176. RxState = 0;
  177. Serial_RxFlag = 1;
  178. }
  179. }
  180. USART_ClearITPendingBit(USART1, USART_IT_RXNE);
  181. }
  182. }

usart.h

  1. #ifndef __USART_H
  2. #define __USART_H
  3. #include "stdio.h"
  4. #include "sys.h"
  5. #define USART_REC_LEN 200 //定义最大接收字节数 200
  6. #define EN_USART1_RX 1 //使能(1)/禁止(0)串口1接收
  7. extern u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.末字节为换行符
  8. extern u16 USART_RX_STA; //接收状态标记
  9. //如果想串口中断接收,请不要注释以下宏定义
  10. void uart_init(u32 bound);
  11. extern char rxarray[4];
  12. extern uint8_t rxlength;
  13. extern uint8_t Serial_TxPacket[];
  14. extern uint8_t Serial_RxPacket[];
  15. void Serial_Init(void);
  16. void Serial_SendByte(uint8_t Byte);
  17. void Serial_SendArray(uint8_t *Array, uint16_t Length);
  18. void Serial_SendString(char *String);
  19. void Serial_SendNumber(uint32_t Number, uint8_t Length);
  20. void Serial_Printf(char *format, ...);
  21. void Serial_SendPacket(void);
  22. uint8_t Serial_GetRxFlag(void);
  23. #endif

大家需要注意的是发送数据格式,当然包头包尾自己可以改下哈。

格式:FF 12 45 67 89 FE

我们发送的数据都被数组接收了,那怎样打印出来尼,看大招。

  1. while(1)
  2. {
  3. if (Serial_GetRxFlag() == 1)
  4. {
  5. for(i=0;i<4;i++)
  6. {
  7. printf ("%x", Serial_RxPacket[i]);
  8. }
  9. }
  10. }

方法2

  1. if(rxlength == 4)
  2. {
  3. //int rxint = (rxarray[0] << 24) | (rxarray[1] << 16) | (rxarray[2] << 8) | (rxarray[3]);
  4. //printf("get %c,%c,%c,%c",rxarray[0],rxarray[1],rxarray[2],rxarray[3]);
  5. union tmpa2i
  6. {
  7. char tmpa[4];
  8. int tmpi;
  9. }
  10. tmpa2i1;
  11. tmpa2i1.tmpa[0] = rxarray[0];
  12. tmpa2i1.tmpa[1] = rxarray[1];
  13. tmpa2i1.tmpa[2] = rxarray[2];
  14. tmpa2i1.tmpa[3] = rxarray[3];
  15. int rxint = tmpa2i1.tmpi;
  16. printf("\x01\xff\xff\xffpage2.n7.val=%d\xff\xff\xff",rxint);
  17. rxlength = 0;
  18. }

他们的原理都差不多,都是数组接收,打印的方式不一样罢了,方法2是串口屏给单片机发送数据时写的,大家如果使用的是串口屏的话推荐看方法2。

如果还是看不懂,建议大家看看江科大的视频。

需要源代码的伙伴评论区聊吧。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/839070
推荐阅读
相关标签
  

闽ICP备14008679号