这次讲讲利用串口收发中断来进行串口通讯。STM32 上为每个串口分配了一个中断。也就是说无论是发送完成还是收到数据或是数据溢出都产生同一个中断。程序需在中断处理函数中读取状态寄存器(USART_SR)来判断当前的是什么中断。下面的中断映像图给出了这些中断源是如何汇合成最终的中断信号的。图中也给出了如何控制每一个单独的中断源是否起作用。

另外,Cortex-M3 内核中还有个NVIC,可以控制这里的中断信号是否触发中断处理函数的执行,还有这些外部中断的级别。关于NVIC 可以参考《ARM CortexM3 权威指南》,里面讲解的非常详细。

简单的说,为了开启中断,我们需要如下的代码:

  1. NVIC_InitTypeDef NVIC_InitStructure;
  2. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  3. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ;
  4. NVIC_InitStructure.NVIC_IRQChannelSubPriority = ;
  5. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  6. NVIC_Init(&NVIC_InitStructure);
  7.  
  8. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开启接收中断
  9. USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // 开启发送中断

这里多说一句,串口的发送中断有两个,分别是:

  1. l发送数据寄存器空中断(TXE)
  2. l发送完成中断(TC)

一般来说我们会使用发送数据寄存器空中断,用这个中断发送的效率会高一些。

中断处理函数的框架如下,如果检测到错误就清除错误,收到数了就处理。发完当前数据了就发下一个。

  1. void USART1_IRQHandler(void)
  2. {
  3. unsigned int data;
  4.  
  5. if(USART1->SR & 0x0F)
  6. {
  7. // See if we have some kind of error, Clear interrupt
  8. data = USART1->DR;
  9. }
  10. else if(USART1->SR & USART_FLAG_RXNE) //Receive Data Reg Full Flag
  11. {
  12. data = USART1->DR;
  13. // 对收到的数据进行处理,或者干些其他的事
  14. }
  15. else if(USART1->SR & USART_FLAG_TXE)
  16. {
  17. { // 可以发送数据了,如果没有数据需要发送,就在这里关闭发送中断
  18. USART1->DR = something; // Yes, Send character
  19. }
  20. }
  21. }

下面给一个利用环形缓冲区的串口驱动程序。

  1. #ifndef _COM_BUFFERED_H_
  2. #define _COM_BUFFERED_H_
  3.  
  4. #define COM1 0
  5. #define COM2 1
  6.  
  7. #define COM_RX_BUF_SIZE 64 /* Number of characters in Rx ring buffer */
  8. #define COM_TX_BUF_SIZE 64 /* Number of characters in Tx ring buffer */
  9.  
  10. #define COM_NO_ERR 0 /* Function call was successful */
  11. #define COM_BAD_CH 1 /* Invalid communications port channel */
  12. #define COM_RX_EMPTY 2 /* Rx buffer is empty, no character available */
  13. #define COM_TX_FULL 3 /* Tx buffer is full, could not deposit character */
  14. #define COM_TX_EMPTY 4 /* If the Tx buffer is empty. */
  15.  
  16. /************************************************************
  17. * function : COMGetCharB
  18. * parameter: char port, port can be COM1 / COM2
  19. * parameter: char* err is a pointer to where an error code will be placed:
  20. * *err is set to COM_NO_ERR if a character is available
  21. * *err is set to COM_RX_EMPTY if the Rx buffer is empty
  22. * *err is set to COM_BAD_CH if you have specified an invalid channel
  23. * return : char
  24. * usage : This function is called by your application to obtain a character from the communications
  25. * channel.
  26. * changelog:
  27. *************************************************************/
  28. unsigned char COMGetCharB (unsigned char ch, unsigned char *err);
  29.  
  30. /************************************************************
  31. * function : COMPutCharB
  32. * parameter: char port, port can be COM1 / COM2
  33. * return : COMM_NO_ERR if the function was successful (the buffer was not full)
  34. * COMM_TX_FULL if the buffer was full
  35. * COMM_BAD_CH if you have specified an incorrect channel
  36.  
  37. * usage : This function is called by your application to send a character on the communications
  38. * channel. The character to send is first inserted into the Tx buffer and will be sent by
  39. * the Tx ISR. If this is the first character placed into the buffer, the Tx ISR will be
  40. * enabled. If the Tx buffer is full, the character will not be sent (i.e. it will be lost)
  41. * changelog:
  42. *************************************************************/
  43. unsigned char COMPutCharB (unsigned char port, unsigned char c);
  44.  
  45. /************************************************************
  46. * function : COMBufferInit
  47. * parameter:
  48. * return :
  49. * usage : This function is called by your application to initialize the communications module. You
  50. * must call this function before calling any other functions.
  51. * changelog:
  52. *************************************************************/
  53. void COMBufferInit (void);
  54.  
  55. /************************************************************
  56. * function : COMBufferIsEmpty
  57. * parameter: char port, port can be COM1 / COM2
  58. * return : char
  59. * usage : This function is called by your application to see
  60. * if any character is available from the communications channel.
  61. * If at least one character is available, the function returns
  62. * FALSE(0) otherwise, the function returns TRUE(1).
  63. * changelog:
  64. *************************************************************/
  65. unsigned char COMBufferIsEmpty (unsigned char port);
  66.  
  67. /************************************************************
  68. * function : COMBufferIsFull
  69. * parameter: char port, port can be COM1 / COM2
  70. * return : char
  71. * usage : This function is called by your application to see if any more characters can be placed
  72. * in the Tx buffer. In other words, this function check to see if the Tx buffer is full.
  73. * If the buffer is full, the function returns TRUE otherwise, the function returns FALSE.
  74. * changelog:
  75. *************************************************************/
  76. unsigned char COMBufferIsFull (unsigned char port);
  77.  
  78. #endif
  1. /*
  2. * file: com_buffered.c
  3. * author: Li Yuan
  4. * platform: STM32F107
  5. * date: 2013-5-5
  6. * version: 0.0.1
  7. * description: UART Ring Buffer
  8. **/
  9.  
  10. #include "stm32f10x_usart.h"
  11. #include "com_buffered.h"
  12.  
  13. #define OS_ENTER_CRITICAL() __set_PRIMASK(1)
  14. #define OS_EXIT_CRITICAL() __set_PRIMASK(0)
  15.  
  16. /**
  17. * Enables Transmiter interrupt.
  18. **/
  19. static void COMEnableTxInt(unsigned char port)
  20. {
  21. ] = {USART1, USART2};
  22. USART_ITConfig(map[port], USART_IT_TXE, ENABLE);
  23. }
  24. /*
  25. *********************************************************************************************************
  26. * DATA TYPES
  27. *********************************************************************************************************
  28. */
  29. typedef struct {
  30. short RingBufRxCtr; /* Number of characters in the Rx ring buffer */
  31. unsigned char *RingBufRxInPtr; /* Pointer to where next character will be inserted */
  32. unsigned char *RingBufRxOutPtr; /* Pointer from where next character will be extracted */
  33. unsigned char RingBufRx[COM_RX_BUF_SIZE]; /* Ring buffer character storage (Rx) */
  34. short RingBufTxCtr; /* Number of characters in the Tx ring buffer */
  35. unsigned char *RingBufTxInPtr; /* Pointer to where next character will be inserted */
  36. unsigned char *RingBufTxOutPtr; /* Pointer from where next character will be extracted */
  37. unsigned char RingBufTx[COM_TX_BUF_SIZE]; /* Ring buffer character storage (Tx) */
  38. } COM_RING_BUF;
  39.  
  40. /*
  41. *********************************************************************************************************
  42. * GLOBAL VARIABLES
  43. *********************************************************************************************************
  44. */
  45.  
  46. COM_RING_BUF COM1Buf;
  47. COM_RING_BUF COM2Buf;
  48.  
  49. /************************************************************
  50. * function : COMGetCharB
  51. * parameter: char port, port can be COM1 / COM2
  52. * parameter: char* err is a pointer to where an error code will be placed:
  53. * *err is set to COM_NO_ERR if a character is available
  54. * *err is set to COM_RX_EMPTY if the Rx buffer is empty
  55. * *err is set to COM_BAD_CH if you have specified an invalid channel
  56. * return : char
  57. * usage : This function is called by your application to obtain a character from the communications
  58. * channel.
  59. * changelog:
  60. *************************************************************/
  61. unsigned char COMGetCharB (unsigned char port, unsigned char *err)
  62. {
  63. // unsigned char cpu_sr;
  64.  
  65. unsigned char c;
  66. COM_RING_BUF *pbuf;
  67.  
  68. switch (port)
  69. { /* Obtain pointer to communications channel */
  70. case COM1:
  71. pbuf = &COM1Buf;
  72. break;
  73.  
  74. case COM2:
  75. pbuf = &COM2Buf;
  76. break;
  77.  
  78. default:
  79. *err = COM_BAD_CH;
  80. );
  81. }
  82. OS_ENTER_CRITICAL();
  83. ) /* See if buffer is empty */
  84. {
  85. pbuf->RingBufRxCtr--; /* No, decrement character count */
  86. c = *pbuf->RingBufRxOutPtr++; /* Get character from buffer */
  87. if (pbuf->RingBufRxOutPtr == &pbuf->RingBufRx[COM_RX_BUF_SIZE])
  88. {
  89. pbuf->RingBufRxOutPtr = &pbuf->RingBufRx[]; /* Wrap OUT pointer */
  90. }
  91. OS_EXIT_CRITICAL();
  92. *err = COM_NO_ERR;
  93. return (c);
  94. } else {
  95. OS_EXIT_CRITICAL();
  96. *err = COM_RX_EMPTY;
  97. c = ; /* Buffer is empty, return 0 */
  98. return (c);
  99. }
  100. }
  101.  
  102. /************************************************************
  103. * function : COMPutCharB
  104. * parameter: char port, port can be COM1 / COM2
  105. * return : COMM_NO_ERR if the function was successful (the buffer was not full)
  106. * COMM_TX_FULL if the buffer was full
  107. * COMM_BAD_CH if you have specified an incorrect channel
  108.  
  109. * usage : This function is called by your application to send a character on the communications
  110. * channel. The character to send is first inserted into the Tx buffer and will be sent by
  111. * the Tx ISR. If this is the first character placed into the buffer, the Tx ISR will be
  112. * enabled. If the Tx buffer is full, the character will not be sent (i.e. it will be lost)
  113. * changelog:
  114. * 1.first implimented by liyuan 2010.11.5
  115. *************************************************************/
  116. unsigned char COMPutCharB (unsigned char port, unsigned char c)
  117. {
  118. // unsigned char cpu_sr;
  119.  
  120. COM_RING_BUF *pbuf;
  121. switch (port)
  122. { /* Obtain pointer to communications channel */
  123. case COM1:
  124. pbuf = &COM1Buf;
  125. break;
  126.  
  127. case COM2:
  128. pbuf = &COM2Buf;
  129. break;
  130.  
  131. default:
  132. return (COM_BAD_CH);
  133. }
  134.  
  135. OS_ENTER_CRITICAL();
  136. if (pbuf->RingBufTxCtr < COM_TX_BUF_SIZE) { /* See if buffer is full */
  137. pbuf->RingBufTxCtr++; /* No, increment character count */
  138. *pbuf->RingBufTxInPtr++ = c; /* Put character into buffer */
  139. if (pbuf->RingBufTxInPtr == &pbuf->RingBufTx[COM_TX_BUF_SIZE]) { /* Wrap IN pointer */
  140. pbuf->RingBufTxInPtr = &pbuf->RingBufTx[];
  141. }
  142. ) { /* See if this is the first character */
  143. COMEnableTxInt(port); /* Yes, Enable Tx interrupts */
  144. OS_EXIT_CRITICAL();
  145. } else {
  146. OS_EXIT_CRITICAL();
  147. }
  148. return (COM_NO_ERR);
  149. } else {
  150. OS_EXIT_CRITICAL();
  151. return (COM_TX_FULL);
  152. }
  153. }
  154.  
  155. /************************************************************
  156. * function : COMBufferInit
  157. * parameter:
  158. * return :
  159. * usage : This function is called by your application to initialize the communications module. You
  160. * must call this function before calling any other functions.
  161. * changelog:
  162. *************************************************************/
  163. void COMBufferInit (void)
  164. {
  165. COM_RING_BUF *pbuf;
  166.  
  167. pbuf = &COM1Buf; /* Initialize the ring buffer for COM0 */
  168. pbuf->RingBufRxCtr = ;
  169. pbuf->RingBufRxInPtr = &pbuf->RingBufRx[];
  170. pbuf->RingBufRxOutPtr = &pbuf->RingBufRx[];
  171. pbuf->RingBufTxCtr = ;
  172. pbuf->RingBufTxInPtr = &pbuf->RingBufTx[];
  173. pbuf->RingBufTxOutPtr = &pbuf->RingBufTx[];
  174.  
  175. pbuf = &COM2Buf; /* Initialize the ring buffer for COM1 */
  176. pbuf->RingBufRxCtr = ;
  177. pbuf->RingBufRxInPtr = &pbuf->RingBufRx[];
  178. pbuf->RingBufRxOutPtr = &pbuf->RingBufRx[];
  179. pbuf->RingBufTxCtr = ;
  180. pbuf->RingBufTxInPtr = &pbuf->RingBufTx[];
  181. pbuf->RingBufTxOutPtr = &pbuf->RingBufTx[];
  182. }
  183.  
  184. /************************************************************
  185. * function : COMBufferIsEmpty
  186. * parameter: char port, port can be COM1 / COM2
  187. * return : char
  188. * usage : This function is called by your application to see
  189. * if any character is available from the communications channel.
  190. * If at least one character is available, the function returns
  191. * FALSE(0) otherwise, the function returns TRUE(1).
  192. * changelog:
  193. *************************************************************/
  194. unsigned char COMBufferIsEmpty (unsigned char port)
  195. {
  196. // unsigned char cpu_sr;
  197.  
  198. unsigned char empty;
  199. COM_RING_BUF *pbuf;
  200. switch (port)
  201. { /* Obtain pointer to communications channel */
  202. case COM1:
  203. pbuf = &COM1Buf;
  204. break;
  205.  
  206. case COM2:
  207. pbuf = &COM2Buf;
  208. break;
  209.  
  210. default:
  211. );
  212. }
  213. OS_ENTER_CRITICAL();
  214. )
  215. { /* See if buffer is empty */
  216. empty = ; /* Buffer is NOT empty */
  217. }
  218. else
  219. {
  220. empty = ; /* Buffer is empty */
  221. }
  222. OS_EXIT_CRITICAL();
  223. return (empty);
  224. }
  225.  
  226. /************************************************************
  227. * function : COMBufferIsFull
  228. * parameter: char port, port can be COM1 / COM2
  229. * return : char
  230. * usage : This function is called by your application to see if any more characters can be placed
  231. * in the Tx buffer. In other words, this function check to see if the Tx buffer is full.
  232. * If the buffer is full, the function returns TRUE otherwise, the function returns FALSE.
  233. * changelog:
  234. *************************************************************/
  235. unsigned char COMBufferIsFull (unsigned char port)
  236. {
  237. // unsigned char cpu_sr;
  238.  
  239. char full;
  240. COM_RING_BUF *pbuf;
  241. switch (port)
  242. { /* Obtain pointer to communications channel */
  243. case COM1:
  244. pbuf = &COM1Buf;
  245. break;
  246.  
  247. case COM2:
  248. pbuf = &COM2Buf;
  249. break;
  250.  
  251. default:
  252. );
  253. }
  254. OS_ENTER_CRITICAL();
  255. if (pbuf->RingBufTxCtr < COM_TX_BUF_SIZE) { /* See if buffer is full */
  256. full = ; /* Buffer is NOT full */
  257. } else {
  258. full = ; /* Buffer is full */
  259. }
  260. OS_EXIT_CRITICAL();
  261. return (full);
  262. }
  263.  
  264. // This function is called by the Rx ISR to insert a character into the receive ring buffer.
  265. static void COMPutRxChar (unsigned char port, unsigned char c)
  266. {
  267. COM_RING_BUF *pbuf;
  268.  
  269. switch (port)
  270. { /* Obtain pointer to communications channel */
  271. case COM1:
  272. pbuf = &COM1Buf;
  273. break;
  274.  
  275. case COM2:
  276. pbuf = &COM2Buf;
  277. break;
  278.  
  279. default:
  280. return;
  281. }
  282. if (pbuf->RingBufRxCtr < COM_RX_BUF_SIZE) { /* See if buffer is full */
  283. pbuf->RingBufRxCtr++; /* No, increment character count */
  284. *pbuf->RingBufRxInPtr++ = c; /* Put character into buffer */
  285. if (pbuf->RingBufRxInPtr == &pbuf->RingBufRx[COM_RX_BUF_SIZE]) { /* Wrap IN pointer */
  286. pbuf->RingBufRxInPtr = &pbuf->RingBufRx[];
  287. }
  288. }
  289. }
  290.  
  291. // This function is called by the Tx ISR to extract the next character from the Tx buffer.
  292. // The function returns FALSE if the buffer is empty after the character is extracted from
  293. // the buffer. This is done to signal the Tx ISR to disable interrupts because this is the
  294. // last character to send.
  295. static unsigned char COMGetTxChar (unsigned char port, unsigned char *err)
  296. {
  297. unsigned char c;
  298. COM_RING_BUF *pbuf;
  299.  
  300. switch (port)
  301. { /* Obtain pointer to communications channel */
  302. case COM1:
  303. pbuf = &COM1Buf;
  304. break;
  305.  
  306. case COM2:
  307. pbuf = &COM2Buf;
  308. break;
  309.  
  310. default:
  311. *err = COM_BAD_CH;
  312. );
  313. }
  314. ) { /* See if buffer is empty */
  315. pbuf->RingBufTxCtr--; /* No, decrement character count */
  316. c = *pbuf->RingBufTxOutPtr++; /* Get character from buffer */
  317. if (pbuf->RingBufTxOutPtr == &pbuf->RingBufTx[COM_TX_BUF_SIZE])
  318. {
  319. pbuf->RingBufTxOutPtr = &pbuf->RingBufTx[]; /* Wrap OUT pointer */
  320. }
  321. *err = COM_NO_ERR;
  322. return (c); /* Characters are still available */
  323. } else {
  324. *err = COM_TX_EMPTY;
  325. ); /* Buffer is empty */
  326. }
  327. }
  328.  
  329. void USART1_IRQHandler(void)
  330. {
  331. unsigned int data;
  332. unsigned char err;
  333.  
  334. if(USART1->SR & 0x0F)
  335. {
  336. // See if we have some kind of error
  337. // Clear interrupt (do nothing about it!)
  338. data = USART1->DR;
  339. }
  340. else if(USART1->SR & USART_FLAG_RXNE) //Receive Data Reg Full Flag
  341. {
  342. data = USART1->DR;
  343. COMPutRxChar(COM1, data); // Insert received character into buffer
  344. }
  345. else if(USART1->SR & USART_FLAG_TXE)
  346. {
  347. data = COMGetTxChar(COM1, &err); // Get next character to send.
  348. if (err == COM_TX_EMPTY)
  349. { // Do we have anymore characters to send ?
  350. // No, Disable Tx interrupts
  351. //USART_ITConfig(USART1, USART_IT_TXE| USART_IT_TC, ENABLE);
  352. USART1->CR1 &= ~USART_FLAG_TXE | USART_FLAG_TC;
  353. }
  354. else
  355. {
  356. USART1->DR = data; // Yes, Send character
  357. }
  358. }
  359. }
  360.  
  361. void USART2_IRQHandler(void)
  362. {
  363. unsigned int data;
  364. unsigned char err;
  365.  
  366. if(USART2->SR & 0x0F)
  367. {
  368. // See if we have some kind of error
  369. // Clear interrupt (do nothing about it!)
  370. data = USART2->DR;
  371. }
  372. else if(USART2->SR & USART_FLAG_RXNE) //Receive Data Reg Full Flag
  373. {
  374. data = USART2->DR;
  375. COMPutRxChar(COM2, data); // Insert received character into buffer
  376. }
  377. else if(USART2->SR & USART_FLAG_TXE)
  378. {
  379. data = COMGetTxChar(COM2, &err); // Get next character to send.
  380. if (err == COM_TX_EMPTY)
  381. { // Do we have anymore characters to send ?
  382. // No, Disable Tx interrupts
  383. //USART_ITConfig(USART2, USART_IT_TXE| USART_IT_TC, ENABLE);
  384. USART2->CR1 &= ~USART_FLAG_TXE | USART_FLAG_TC;
  385. }
  386. else
  387. {
  388. USART2->DR = data; // Yes, Send character
  389. }
  390. }
  391. }

下面给个例子主程序,来演示如何使用上面的串口驱动代码。

  1. #include "misc.h"
  2. #include "stm32f10x.h"
  3. #include "com_buffered.h"
  4.  
  5. void UART_PutStrB (unsigned char port, uint8_t *str)
  6. {
  7. != *str)
  8. {
  9. COMPutCharB(port, *str);
  10. str++;
  11. }
  12. }
  13.  
  14. void USART1_Init(void)
  15. {
  16. GPIO_InitTypeDef GPIO_InitStructure;
  17. USART_InitTypeDef USART_InitStructure;
  18. NVIC_InitTypeDef NVIC_InitStructure;
  19.  
  20. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
  21.  
  22. /* Configure USART Tx as alternate function push-pull */
  23. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  24. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  25. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  26. GPIO_Init(GPIOA, &GPIO_InitStructure);
  27.  
  28. /* Configure USART Rx as input floating */
  29. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  30. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  31. GPIO_Init(GPIOA, &GPIO_InitStructure);
  32.  
  33. USART_InitStructure.USART_BaudRate = ;
  34. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  35. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  36. USART_InitStructure.USART_Parity = USART_Parity_No;
  37. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  38. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  39. USART_Init(USART1, &USART_InitStructure );
  40.  
  41. USART_Cmd(USART1, ENABLE);
  42.  
  43. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  44. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ;
  45. NVIC_InitStructure.NVIC_IRQChannelSubPriority = ;
  46. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  47. NVIC_Init(&NVIC_InitStructure);
  48. }
  49.  
  50. void USART2_Init(void)
  51. {
  52. GPIO_InitTypeDef GPIO_InitStructure;
  53. USART_InitTypeDef USART_InitStructure;
  54. NVIC_InitTypeDef NVIC_InitStructure;
  55.  
  56. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
  57.  
  58. /* Configure USART Tx as alternate function push-pull */
  59. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  60. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  61. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  62. GPIO_Init(GPIOD, &GPIO_InitStructure);
  63.  
  64. /* Configure USART Rx as input floating */
  65. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  66. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  67. GPIO_Init(GPIOD, &GPIO_InitStructure);
  68.  
  69. GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
  70. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  71.  
  72. USART_InitStructure.USART_BaudRate = ;
  73. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  74. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  75. USART_InitStructure.USART_Parity = USART_Parity_No;
  76. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  77. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  78. USART_Init(USART2, &USART_InitStructure );
  79.  
  80. USART_Cmd(USART2, ENABLE);
  81.  
  82. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  83. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ;
  84. NVIC_InitStructure.NVIC_IRQChannelSubPriority = ;
  85. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  86. NVIC_Init(&NVIC_InitStructure);
  87. }
  88.  
  89. int main(void)
  90. {
  91. unsigned char c;
  92. unsigned char err;
  93.  
  94. USART1_Init();
  95. USART2_Init();
  96. COMBufferInit();
  97. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  98. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  99.  
  100. UART_PutStrB(COM1, "Hello World!\n");
  101. for(;;)
  102. {
  103. c = COMGetCharB(COM1, &err);
  104. if(err == COM_NO_ERR)
  105. {
  106. COMPutCharB(COM1, c);
  107. }
  108. }
  109. }

STM32F10x 学习笔记6(USART实现串口通讯 2)的更多相关文章

  1. STM32学习笔记(四)——串口控制LED(中断方式)

    目录: 一.时钟使能,包括GPIO的时钟和串口的时钟使能 二.设置引脚复用映射 三.GPIO的初始化配置,注意要设置为复用模式 四.串口参数初始化配置 五.中断分组和中断优先级配置 六.设置串口中断类 ...

  2. STM32F10x 学习笔记5(USART实现串口通讯 1)

    STM32F10x 系列单片机中都包含了USART 模块,所谓USART,就是通用同步异步收发器.通用同步异步收发器(USART)提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间 ...

  3. ARM-LINUX学习笔记-(虚拟机linux串口终端以及USB程序下载,基于TQ2440)

    昨天安装了ssh服务之后今天在windows上用xshell登陆发现登录不上,原因是使用了virtualbox的NAT模式,在NAT模式下,客户机可以很方便地上网,但是想要链接宿主机就需要打开网络地址 ...

  4. STM32学习笔记(二)——串口控制LED

    开发板芯片:STM32F407ZGT6 PA9-USART1_TX,PA10-USART1_RX; PF9-LED0,PF10-LED1; 一.串口1配置过程(不使用串口中断): 1.使能时钟,包括G ...

  5. 串口(USART)通信-串口通讯协议简介

    物理层:规定通讯系统中具有机械.电子功能部分的特性,确保原始数据在物理媒体的传输.其实就是硬件部分. 协议层:协议层主要规定通讯逻辑,统一收发双方的数据打包.解包标准.其实就是软件部分. 简单来说物理 ...

  6. STM32学习笔记(五) USART异步串行口输入输出(轮询模式)

    学习是一个简单的过程,只要有善于发掘的眼睛,总能学到新知识,然而如何坚持不懈的学习却很困难,对我亦如此,生活中有太多的诱惑,最后只想说一句勿忘初心.闲话不多扯,本篇讲诉的是异步串行口的输入输出,串口在 ...

  7. 【STM32学习笔记】USART 硬件流控

    流控的概念源于 RS232 这个标准,在 RS232 标准里面包含了串口.流控的定义.大家一定了解,RS232 中的"RS"是Recommend Standard 的缩写,即&qu ...

  8. CC2540开发板学习笔记(五)——串口通信

    (一)串口发送 一.实验现象: 开发板实现功能发送 二.实验过程 1.PL2303 USB转串口电路图 2.串口发送 (1)查看用户手册有: UART0 对应的外部设备 IO 引脚关系为: P0_2 ...

  9. Arduino101学习笔记(十)—— 串口通信

    //打开串口 Serial.begin(); //获取串口上可读取的数据的字节数.该数据是指已经到达并存储在接收缓存(共有64字节)中 Serial.available(); //读串口数据,串口上第 ...

随机推荐

  1. 在LaTeX里插入全页的pdf 分类: LaTex 2015-02-04 17:20 142人阅读 评论(0) 收藏

    要帮女友排版毕业论文,需要插入封面,省时省力的方法就是把学校给的Word封面保存成PDF然后插入到Latex文档中. 首先添加下面的宏: \usepackage[final]{pdfpages} 然后 ...

  2. [AngularJS + Webpack] Uglifying your JavaScript

    Angular requires some careful consideration when uglifying your code because of how angular's depend ...

  3. 手机相机ISO是什么

    要说什么是ISO还要从传统胶片相机说起,ISO被 称为感光度,它是衡量传统相机所使用胶片感光速度的国际统一指标,其数值反映了胶片感光时的速度(其实是银元素与光线的光化学反应速率).而对于现在并不 使用 ...

  4. 08_android入门_android-async-http开源项目介绍及用法

    android-async-http开源项目可以是我们轻松的获取网络数据或者向server发送数据.使用起来很easy,关于android-async-http开源项目的介绍内容来自于官方:http: ...

  5. Fix java version mismatch in windows---stackoverflow

    Question: I have the 64bit version of the jdk installed on windows 7. I installed the 32 bit version ...

  6. mac 终端常见指令

    基本命令 1.列出文件 ls 参数 目录名        例: 看看驱动目录下有什么:ls /System/Library/Extensions参数 -w 显示中文,-l 详细信息, -a 包括隐藏文 ...

  7. Python之路【第十七篇】:Django之【进阶篇】

    Python之路[第十七篇]:Django[进阶篇 ]   Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接 ...

  8. 关于黑名单IP的设置

    最近在做一个项目的时候,需要做一个自动的黑名单设置,也就是将一天内重复出错的超过一定次数的手机号,和IP给加入黑名单里面,下次请求的时候先判断是否在黑名单里. 这个是获取IP地址的方法 private ...

  9. Android 开发笔记——通过 Intent 传递类对象

    Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...

  10. 获取C++类成员变量的地址偏移

    今天有在校学生问怎么获取类中的成员变量的地址偏移量,这个应该是很多初学C++的人很好奇的问题.以前我在学校的时候,也有过这种需求.忘了当时是要写什么“奇怪的程序”了,反正需要获取一个类的成员变量的地址 ...