usart.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include "sys.h"
  2. #include "usart.h"
  3. #include "string.h"
  4. #include "led.h"
  5. #include "stdlib.h"
  6. #include "stdio.h"
  7. char Usart1ReadBuff[Usart1ReadLen]={0}; //接收数据缓存
  8. u16 Usart1ReadCnt = 0;//串口1接收到的数据个数
  9. u16 Usart1IdleCnt = 0;//空闲检测用
  10. char Usart2ReadBuff[Usart2ReadLen]={0}; //接收数据缓存
  11. u16 Usart2ReadCnt = 0;//串口1接收到的数据个数
  12. u16 Usart2IdleCnt = 0;//空闲检测用
  13. char Usart3ReadBuff[Usart3ReadLen]={0}; //接收数据缓存
  14. u16 Usart3ReadCnt = 0;//串口1接收到的数据个数
  15. u16 Usart3IdleCnt = 0;//空闲检测用
  16. //////////////////////////////////////////////////////////////////////////////////
  17. //如果使用ucos,则包括下面的头文件即可.
  18. #if SYSTEM_SUPPORT_OS
  19. #include "includes.h" //ucos 使用
  20. #endif
  21. void sendData(char *p,unsigned char n);
  22. //加入以下代码,支持printf函数,而不需要选择use MicroLIB
  23. #if 1
  24. #pragma import(__use_no_semihosting)
  25. //标准库需要的支持函数
  26. struct __FILE
  27. {
  28. int handle;
  29. };
  30. FILE __stdout;
  31. //定义_sys_exit()以避免使用半主机模式
  32. _sys_exit(int x)
  33. {
  34. x = x;
  35. }
  36. //重定义fputc函数
  37. int fputc(int ch, FILE *f)
  38. {
  39. while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
  40. USART1->DR = (u8) ch;
  41. return ch;
  42. }
  43. #endif
  44. /*使用microLib的方法*/
  45. /*
  46. int fputc(int ch, FILE *f)
  47. {
  48. USART_SendData(USART1, (uint8_t) ch);
  49. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
  50. return ch;
  51. }
  52. int GetKey (void) {
  53. while (!(USART1->SR & USART_FLAG_RXNE));
  54. return ((int)(USART1->DR & 0x1FF));
  55. }
  56. */
  57. void uart_init(u32 bound){
  58. //GPIO端口设置
  59. GPIO_InitTypeDef GPIO_InitStructure;
  60. USART_InitTypeDef USART_InitStructure;
  61. NVIC_InitTypeDef NVIC_InitStructure;
  62. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
  63. //USART1_TX GPIOA.9
  64. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  65. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  66. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  67. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
  68. //USART1_RX GPIOA.10初始化
  69. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
  70. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  71. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
  72. //Usart1 NVIC 配置
  73. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  74. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级3
  75. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
  76. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  77. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  78. //USART 初始化设置
  79. USART_InitStructure.USART_BaudRate = bound;//串口波特率
  80. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  81. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  82. USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  83. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  84. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  85. USART_Init(USART1, &USART_InitStructure); //初始化串口1
  86. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
  87. USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
  88. USART_Cmd(USART1, ENABLE); //使能串口1
  89. }
  90. void uart3_init(u32 bound)
  91. {
  92. USART_InitTypeDef USART_InitStructure;
  93. NVIC_InitTypeDef NVIC_InitStructure;
  94. GPIO_InitTypeDef GPIO_InitStructure;
  95. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //使能USART3,
  96. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  97. // GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);//设置端口部分重映射 功能
  98. /* Enable the USART1 Interrupt */
  99. NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; // 通道设置为串口3中断
  100. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2 ; // 抢占优先级
  101. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // 中断响应优先级0
  102. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 打开中断
  103. NVIC_Init(&NVIC_InitStructure);
  104. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10
  105. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  106. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  107. GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB10
  108. //USART3_RX PB.11
  109. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  110. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  111. GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB11
  112. /* 第4步:配置USART3参数
  113. - BaudRate = 115200 baud
  114. - Word Length = 8 Bits
  115. - One Stop Bit
  116. - No parity
  117. - Hardware flow control disabled (RTS and CTS signals)
  118. - Receive and transmit enabled
  119. */
  120. USART_InitStructure.USART_BaudRate = bound;
  121. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  122. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  123. USART_InitStructure.USART_Parity = USART_Parity_No ;
  124. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  125. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  126. USART_Init(USART3, &USART_InitStructure);
  127. USART_Cmd(USART3,ENABLE);// 使能U3
  128. //USART_IT_IDLE
  129. USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
  130. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // 接收中断使能
  131. }
  132. void uart2_init(u32 bound)
  133. {
  134. GPIO_InitTypeDef GPIO_InitStructure;
  135. USART_InitTypeDef USART_InitStructure;
  136. NVIC_InitTypeDef NVIC_InitStructure;
  137. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能USART2,GPIOA时钟
  138. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  139. // USART_DeInit(USART2); //复位串口2
  140. //USART2_TX PA.2
  141. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
  142. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  143. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  144. GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA2
  145. //USART2_RX PA.3
  146. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  147. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  148. GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA3
  149. //USART 初始化设置
  150. USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
  151. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  152. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  153. USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  154. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  155. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  156. USART_Init(USART2, &USART_InitStructure); //初始化串口2
  157. // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  158. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  159. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级0
  160. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级2
  161. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  162. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  163. USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);
  164. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
  165. USART_Cmd(USART2, ENABLE); //使能串口
  166. }
  167. void USART2_SendByte(u8 data)
  168. {
  169. while((USART2->SR&0X40)==0);//循环发送,直到发送完毕
  170. USART2->DR = (u8) data;
  171. }
  172. /****************************************************
  173. * 函数名称 :void String_Sub(char *pcBuf, char *pcRes,char *pStart,char *pEnd)
  174. * 函数功能 : 获取两个特殊字符串之间的字符串 NB-iot数据解析用
  175. * 编写日期 :2019/7/26
  176. * 编写人 :小浩电子科技
  177. * 参数 ; *pcBuf 源字符串 *pcRes目标字符串 *pStart字符串1 *pEnd字符串2
  178. * 版本记录 :V1.0
  179. * :
  180. ****************************************************/
  181. char String_Sub(char *pcBuf, char *pcRes,const char *pStart,const char *pEnd)
  182. {
  183. char *pcBegin = NULL;
  184. char *pcEnd = NULL;
  185. pcBegin = strstr((char*)pcBuf,pStart);
  186. pcEnd = strstr((char*)pcBegin+strlen(pStart)+1, pEnd);
  187. if(pcBegin == NULL || pcEnd == NULL || pcBegin > pcEnd)
  188. {
  189. return 0;//错误返回
  190. }
  191. else
  192. {
  193. pcBegin += strlen(pStart);
  194. // memcpy(pcRes, pcBegin, pcEnd-pcBegin);//内存拷贝法
  195. strncpy(pcRes, pcBegin, pcEnd-pcBegin);//开始拷贝数据
  196. // pcRes[(pcEnd-pcBegin)+1]='\0';
  197. }
  198. return 1;
  199. }
  200. // 往串口发送字符串
  201. void UsartSendData(u8 *p,char len)
  202. {
  203. while(len--)
  204. {
  205. while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  206. USART_SendData(USART1, *p++);
  207. }
  208. }
  209. void USART1_IRQHandler(void) //串口1中断服务程序
  210. {
  211. u8 Res=0,rxes;
  212. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断
  213. {
  214. Res =USART_ReceiveData(USART1); //读取接收到的数据
  215. if(Usart1ReadCnt < Usart1ReadLen-1)
  216. {
  217. Usart1ReadBuff[Usart1ReadCnt] = Res;
  218. }
  219. else
  220. {
  221. Usart1ReadCnt=0;
  222. }
  223. Usart1ReadCnt ++; //数据个数
  224. }
  225. if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)//空闲中断
  226. {
  227. //用读SR和DR的方法清除IDLE
  228. rxes = USART1->SR;
  229. rxes = USART1->DR;
  230. // analyse_data();
  231. Usart1ReadCnt=0;
  232. }
  233. }
  234. void USART2_IRQHandler(void) //串口1中断服务程序
  235. {
  236. u8 Res=0,rxes;
  237. if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断
  238. {
  239. Res =USART_ReceiveData(USART2); //读取接收到的数据
  240. if(Usart2ReadCnt < Usart2ReadLen-1)
  241. {
  242. Usart2ReadBuff[Usart2ReadCnt] = Res;
  243. }
  244. else
  245. {
  246. Usart2ReadCnt=0;
  247. }
  248. Usart2ReadCnt ++; //数据个数
  249. }
  250. if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)//空闲中断
  251. {
  252. //用读SR和DR的方法清除IDLE
  253. rxes = USART2->SR;
  254. rxes = USART2->DR;
  255. // analyse_data();
  256. Usart2ReadCnt=0;
  257. }
  258. }
  259. void USART3_IRQHandler(void) //串口1中断服务程序
  260. {
  261. u8 Res=0,rxes;
  262. if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中断
  263. {
  264. Res =USART_ReceiveData(USART3); //读取接收到的数据
  265. if(Usart3ReadCnt < Usart3ReadLen-1)
  266. {
  267. Usart1ReadBuff[Usart3ReadCnt] = Res;
  268. }
  269. else
  270. {
  271. Usart3ReadCnt=0;
  272. }
  273. Usart3ReadCnt ++; //数据个数
  274. }
  275. if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET)//空闲中断
  276. {
  277. //用读SR和DR的方法清除IDLE
  278. rxes = USART3->SR;
  279. rxes = USART3->DR;
  280. // analyse_data();
  281. Usart3ReadCnt=0;
  282. }
  283. }