usart.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #include "usart.h"
  2. #include "mmodbus.h"
  3. #include "dlt645.h"
  4. #include "gateway_message.h"
  5. uint8_t UART6_RX_BUF[BUFF_SIZE];
  6. uint8_t UART6_RX_STAT=0;
  7. uint32_t UART6_RX_NUM = 0;
  8. /**
  9. * @brief DEBUG_USART GPIO 配置,工作模式配置。115200 8-N-1 ,中断接收模式
  10. * @param 无
  11. * @retval 无
  12. */
  13. void DEBUG_USART_Config(void)
  14. {
  15. USART_InitTypeDef USART_InitStruct;
  16. GPIO_InitTypeDef GPIO_InitStruct;
  17. // Enable USART6 clock
  18. RCC_APB2PeriphClockCmd(DEBUG_USART_CLK, ENABLE);
  19. // Enable GPIOC clock
  20. RCC_AHB1PeriphClockCmd(DEBUG_USART_RX_GPIO_CLK, ENABLE);
  21. // Configure USART6 pins
  22. GPIO_InitStruct.GPIO_Pin = DEBUG_USART_RX_PIN | DEBUG_USART_TX_PIN; // USART6_TX (PC6) and USART6_RX (PC7)
  23. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  24. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  25. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  26. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  27. GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStruct);
  28. // Connect USART6 pins to AF8
  29. GPIO_PinAFConfig(DEBUG_USART_RX_GPIO_PORT, DEBUG_USART_RX_SOURCE, DEBUG_USART_RX_AF);
  30. GPIO_PinAFConfig(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_SOURCE, DEBUG_USART_TX_AF);
  31. // Configure USART6
  32. USART_InitStruct.USART_BaudRate = DEBUG_USART_BAUDRATE;
  33. USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  34. USART_InitStruct.USART_StopBits = USART_StopBits_1;
  35. USART_InitStruct.USART_Parity = USART_Parity_No;
  36. USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  37. USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  38. USART_Init(DEBUG_USART, &USART_InitStruct);
  39. NVIC_InitTypeDef NVIC_InitStructure;
  40. NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART_IRQ; //IRQ通道
  41. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //主优先级
  42. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级
  43. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能通道
  44. NVIC_Init(&NVIC_InitStructure);
  45. // Enable USART6
  46. USART_ITConfig(DEBUG_USART, USART_IT_IDLE, ENABLE); //开启空闲中断
  47. DMA_Cmd(DEBUG_USART_DMA_STREAM, ENABLE); //使能DMA接收
  48. USART_Cmd(DEBUG_USART, ENABLE);
  49. USART_ClearFlag(DEBUG_USART, USART_FLAG_TC);
  50. }
  51. void DEBUG_USART_IRQHandler(void)
  52. {
  53. uint16_t clear;
  54. if(USART_GetITStatus(DEBUG_USART,USART_IT_IDLE)!=RESET)
  55. {
  56. //清除空闲中断
  57. clear=USART6->SR;
  58. clear=USART6->DR;
  59. //关闭DMA
  60. DMA_Cmd(DEBUG_USART_DMA_STREAM,DISABLE);
  61. UART6_RX_NUM=BUFF_SIZE-DMA_GetCurrDataCounter(DEBUG_USART_DMA_STREAM);
  62. //清除DMA中断
  63. DMA_ClearFlag(DEBUG_USART_DMA_STREAM,DMA_IT_TCIF1);
  64. DMA_Cmd(DEBUG_USART_DMA_STREAM, ENABLE);
  65. //接收状态置位
  66. UART6_RX_STAT=1;
  67. }
  68. }
  69. /**
  70. * @brief USART1 TX DMA 配置,内存到外设(USART1->DR)
  71. * @param 无
  72. * @retval 无
  73. */
  74. void USART_DMA_Config(void)
  75. {
  76. // 使能串口和DMA时钟
  77. RCC_APB2PeriphClockCmd(DEBUG_USART_CLK, ENABLE);
  78. RCC_AHB1PeriphClockCmd(DEBUG_USART_DMA_CLK, ENABLE);
  79. DMA_InitTypeDef DMA_InitStructure;
  80. DMA_InitStructure.DMA_Channel = DEBUG_USART_DMA_CHANNEL;
  81. /*设置DMA源:串口数据寄存器地址*/
  82. DMA_InitStructure.DMA_PeripheralBaseAddr = DEBUG_USART_DR_BASE;
  83. /*内存地址(要传输的变量的指针)*/
  84. DMA_InitStructure.DMA_Memory0BaseAddr = (u32)UART6_RX_BUF;
  85. /*方向:从外设到内存*/
  86. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  87. /*传输大小DMA_BufferSize=SENDBUFF_SIZE*/
  88. DMA_InitStructure.DMA_BufferSize = BUFF_SIZE;
  89. /*外设地址不增*/
  90. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  91. /*内存地址自增*/
  92. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  93. /*外设数据单位*/
  94. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  95. /*内存数据单位 8bit*/
  96. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  97. /*DMA模式:不断循环*/
  98. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  99. /*优先级:中*/
  100. DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  101. /*禁用FIFO*/
  102. DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  103. DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  104. /*存储器突发传输 16个节拍*/
  105. DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  106. /*外设突发传输 1个节拍*/
  107. DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  108. /*配置DMA2的数据流*/
  109. DMA_Init(DEBUG_USART_DMA_STREAM, &DMA_InitStructure);
  110. USART_DMACmd(DEBUG_USART,USART_DMAReq_Rx,ENABLE);
  111. DMA_ITConfig(DEBUG_USART_DMA_STREAM,DMA_IT_TC,ENABLE);
  112. NVIC_InitTypeDef NVIC_InitStructure;
  113. NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream5_IRQn ;//串口1发送中断通道
  114. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级
  115. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级
  116. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  117. NVIC_Init(&NVIC_InitStructure);
  118. }
  119. void DMA2_Stream5_IRQHandler() {
  120. if (DMA_GetITStatus(DMA2_Stream1, DMA_IT_TCIF1))
  121. {
  122. DMA_ClearITPendingBit(DMA2_Stream1, DMA_IT_TCIF1);
  123. }
  124. }
  125. /*
  126. 485串口de控制端口
  127. */
  128. void USART_485_DE_init()
  129. {
  130. GPIO_InitTypeDef GPIO_InitStruct;
  131. RCC_AHB1PeriphClockCmd(USART_485_DE_GPIO_CLK, ENABLE);
  132. GPIO_InitStruct.GPIO_Pin = USART_485_DE_PIN;
  133. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  134. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  135. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  136. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  137. GPIO_Init(USART_485_DE_GPIO_PORT, &GPIO_InitStruct);
  138. }
  139. void USART_485_DE_TX()
  140. {
  141. GPIO_WriteBit(USART_485_DE_GPIO_PORT,USART_485_DE_PIN,1);
  142. }
  143. void USART_485_DE_RX()
  144. {
  145. GPIO_WriteBit(USART_485_DE_GPIO_PORT,USART_485_DE_PIN,0);
  146. }
  147. void USART_485_Send(uint8_t *message,uint16_t size)
  148. {
  149. uint16_t i=0;
  150. USART_485_DE_TX();
  151. for(i=0;i<size;i++)
  152. {
  153. USART_SendData(USART_232,*(message+i));
  154. while (USART_GetFlagStatus(USART_232, USART_FLAG_TC) == RESET);
  155. }
  156. USART_485_DE_RX();
  157. }
  158. //void _485_Config(void)
  159. //{
  160. // GPIO_InitTypeDef GPIO_InitStructure;
  161. // USART_InitTypeDef USART_InitStructure;
  162. // /* config USART clock */
  163. // RCC_AHB1PeriphClockCmd(_485_USART_RX_GPIO_CLK|_485_USART_TX_GPIO_CLK|_485_RE_GPIO_CLK, ENABLE);
  164. // RCC_APB1PeriphClockCmd(_485_USART_CLK, ENABLE);
  165. //
  166. // /* Connect PXx to USARTx_Tx*/
  167. // GPIO_PinAFConfig(_485_USART_RX_GPIO_PORT,_485_USART_RX_SOURCE, _485_USART_RX_AF);
  168. // /* Connect PXx to USARTx_Rx*/
  169. // GPIO_PinAFConfig(_485_USART_TX_GPIO_PORT,_485_USART_TX_SOURCE,_485_USART_TX_AF);
  170. //
  171. // /* USART GPIO config */
  172. // /* Configure USART Tx as alternate function push-pull */
  173. // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  174. // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  175. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  176. // GPIO_InitStructure.GPIO_Pin = _485_USART_TX_PIN ;
  177. // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  178. // GPIO_Init(_485_USART_TX_GPIO_PORT, &GPIO_InitStructure);
  179. // /* Configure USART Rx as alternate function */
  180. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  181. // GPIO_InitStructure.GPIO_Pin = _485_USART_RX_PIN;
  182. // GPIO_Init(_485_USART_RX_GPIO_PORT, &GPIO_InitStructure);
  183. //
  184. // /* 485收发控制管脚 */
  185. // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  186. // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  187. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  188. // GPIO_InitStructure.GPIO_Pin = _485_RE_PIN ;
  189. // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  190. // GPIO_Init(_485_RE_GPIO_PORT, &GPIO_InitStructure);
  191. //
  192. // /* USART mode config */
  193. // USART_InitStructure.USART_BaudRate = _485_USART_BAUDRATE;
  194. // USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  195. // USART_InitStructure.USART_StopBits = USART_StopBits_1;
  196. // USART_InitStructure.USART_Parity = USART_Parity_No ;
  197. // USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  198. // USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  199. // USART_Init(_485_USART, &USART_InitStructure);
  200. // USART_Cmd(_485_USART, ENABLE);
  201. //
  202. // NVIC_Configuration();
  203. // /* 使能串口接收中断 */
  204. // USART_ITConfig(_485_USART, USART_IT_RXNE, ENABLE);
  205. //
  206. // GPIO_ResetBits(_485_RE_GPIO_PORT,_485_RE_PIN); //默认进入接收模式
  207. //}
  208. /*
  209. 485串口在未配置情况下,初始化的参数
  210. */
  211. void USART_485_config()
  212. {
  213. USART_485_DE_init();
  214. USART_InitTypeDef USART_InitStruct;
  215. GPIO_InitTypeDef GPIO_InitStruct;
  216. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  217. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  218. GPIO_PinAFConfig(USART_485_RX_GPIO_PORT, USART_485_RX_SOURCE, USART_485_RX_AF);
  219. GPIO_PinAFConfig(USART_485_TX_GPIO_PORT, USART_485_TX_SOURCE, USART_485_TX_AF);
  220. GPIO_InitStruct.GPIO_Pin = USART_485_TX_PIN ;
  221. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  222. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  223. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  224. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  225. GPIO_Init(GPIOC, &GPIO_InitStruct);
  226. GPIO_InitStruct.GPIO_Pin = USART_485_RX_PIN;
  227. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  228. GPIO_Init(GPIOC, &GPIO_InitStruct);
  229. USART_InitStruct.USART_BaudRate = USART_485_BAUDRATE;
  230. USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  231. USART_InitStruct.USART_StopBits = USART_StopBits_1;
  232. USART_InitStruct.USART_Parity = USART_Parity_No;
  233. USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  234. USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  235. USART_Init(USART_485, &USART_InitStruct);
  236. USART_Cmd(USART_485, ENABLE);
  237. USART_ClearFlag(USART_485, USART_FLAG_TC);
  238. USART_ITConfig(USART_485,USART_IT_RXNE,ENABLE);
  239. USART_ITConfig(USART_485,USART_IT_IDLE,ENABLE);
  240. NVIC_InitTypeDef NVIC_InitStruct;
  241. NVIC_InitStruct.NVIC_IRQChannel = USART_485_IRQ;
  242. NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  243. NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  244. NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  245. NVIC_Init(&NVIC_InitStruct);
  246. }
  247. /*
  248. * 函数名:void Config_485_Port(uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity, uint8_t flowcontrol)
  249. * 输入参数:baudrate,databits,stopbits,parity,flowcontol 串口的配置参数
  250. * 输出参数:无
  251. * 返回值:无
  252. * 函数作用:根据输入参数进行配置485串口参数
  253. */
  254. void Config_485_Port(uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity, uint8_t flowcontrol)
  255. {
  256. uint8_t wordLength;
  257. if(parity!=0)
  258. {
  259. wordLength=databits+1;
  260. }else wordLength=databits;
  261. USART_InitTypeDef USART_InitStruct;
  262. GPIO_InitTypeDef GPIO_InitStruct;
  263. RCC_AHB1PeriphClockCmd(USART_485_CLK, ENABLE);
  264. RCC_APB2PeriphClockCmd(USART_485_RX_GPIO_CLK, ENABLE);
  265. GPIO_InitStruct.GPIO_Pin = USART_485_RX_PIN | USART_485_TX_PIN;
  266. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  267. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  268. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  269. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  270. GPIO_Init(GPIOC, &GPIO_InitStruct);
  271. GPIO_PinAFConfig(USART_485_RX_GPIO_PORT, USART_485_RX_SOURCE, USART_485_RX_AF);
  272. GPIO_PinAFConfig(USART_485_TX_GPIO_PORT, USART_485_TX_SOURCE, USART_485_TX_AF);
  273. USART_InitStruct.USART_BaudRate = baudrate;
  274. USART_InitStruct.USART_WordLength = (wordLength== 9) ? USART_WordLength_9b : USART_WordLength_8b;
  275. USART_InitStruct.USART_StopBits = USART_StopBits_1;
  276. USART_InitStruct.USART_Parity = USART_Parity_No;
  277. USART_InitStruct.USART_Parity =(parity == 1) ? (USART_Parity_Odd) : (parity == 2 ? USART_Parity_Even : USART_Parity_No);
  278. USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  279. USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  280. USART_Init(USART_485, &USART_InitStruct);
  281. USART_Cmd(USART_485, ENABLE);
  282. USART_ClearFlag(USART1, USART_FLAG_TC);
  283. USART_ITConfig(USART_485,USART_IT_RXNE,ENABLE);
  284. USART_ITConfig(USART_485,USART_IT_IDLE,ENABLE);
  285. // 配置485接收中断优先级
  286. NVIC_InitTypeDef NVIC_InitStruct;
  287. NVIC_InitStruct.NVIC_IRQChannel = USART_485_IRQ;
  288. NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  289. NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  290. NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  291. NVIC_Init(&NVIC_InitStruct);
  292. }
  293. /*
  294. 232串口配置
  295. */
  296. void USART_232_config()
  297. {
  298. USART_InitTypeDef USART_InitStruct;
  299. GPIO_InitTypeDef GPIO_InitStruct;
  300. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  301. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  302. GPIO_PinAFConfig(USART_232_RX_GPIO_PORT, USART_232_RX_SOURCE, USART_232_RX_AF);
  303. GPIO_PinAFConfig(USART_232_TX_GPIO_PORT, USART_232_TX_SOURCE, USART_232_TX_AF);
  304. GPIO_InitStruct.GPIO_Pin = USART_232_RX_PIN | USART_232_TX_PIN;
  305. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  306. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  307. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  308. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  309. GPIO_Init(GPIOA, &GPIO_InitStruct);
  310. USART_InitStruct.USART_BaudRate = USART_232_BAUDRATE;
  311. USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  312. USART_InitStruct.USART_StopBits = USART_StopBits_1;
  313. USART_InitStruct.USART_Parity = USART_Parity_No;
  314. USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  315. USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  316. USART_Init(USART_232, &USART_InitStruct);
  317. USART_Cmd(USART_232, ENABLE);
  318. USART_ClearFlag(USART1, USART_FLAG_TC);
  319. USART_ITConfig(USART_232,USART_IT_RXNE,ENABLE);
  320. USART_ITConfig(USART_232,USART_IT_IDLE,ENABLE);
  321. // 配置USART1接收中断优先级
  322. NVIC_InitTypeDef NVIC_InitStruct;
  323. NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
  324. NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  325. NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  326. NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  327. NVIC_Init(&NVIC_InitStruct);
  328. }
  329. /***************** 发送一个字符 **********************/
  330. void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
  331. {
  332. /* 发送一个字节数据到USART */
  333. USART_SendData(pUSARTx,ch);
  334. /* 等待发送数据寄存器为空 */
  335. while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
  336. }
  337. /***************** 发送字符串 **********************/
  338. void Usart_SendString( USART_TypeDef * pUSARTx, char *str)
  339. {
  340. unsigned int k=0;
  341. do
  342. {
  343. Usart_SendByte( pUSARTx, *(str + k) );
  344. k++;
  345. } while(*(str + k)!='\0');
  346. /* 等待发送完成 */
  347. while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
  348. {}
  349. }
  350. /***************** 输出Hex字符串可能包含0x00**********************/
  351. void Usart_SendHex( USART_TypeDef * pUSARTx, uint8_t *str,uint16_t hexLength)
  352. {
  353. unsigned int k=0;
  354. do
  355. {
  356. Usart_SendByte( pUSARTx, *(str + k) );
  357. k++;
  358. } while(k==hexLength);
  359. /* 等待发送完成 */
  360. while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
  361. {}
  362. }
  363. /***************** 发送一个16位数 **********************/
  364. void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
  365. {
  366. uint8_t temp_h, temp_l;
  367. /* 取出高八位 */
  368. temp_h = (ch&0XFF00)>>8;
  369. /* 取出低八位 */
  370. temp_l = ch&0XFF;
  371. /* 发送高八位 */
  372. USART_SendData(pUSARTx,temp_h);
  373. while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
  374. /* 发送低八位 */
  375. USART_SendData(pUSARTx,temp_l);
  376. while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
  377. }
  378. void USART3_IRQHandler(){
  379. GATEWAY_PARAMS *get;
  380. get= get_gateway_config_params();
  381. if(get->device_params->protocol == 3)
  382. mmodbus_callback();
  383. else if(get->device_params->protocol == 1 || get->device_params->protocol == 2)
  384. dlt_callback();
  385. }
  386. /*********************************************END OF FILE**********************/