usart.c 10 KB

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