微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32F10x 学习笔记6(USART实现串口通讯 2)

STM32F10x 学习笔记6(USART实现串口通讯 2)

时间:11-20 来源:互联网 点击:

  1. _EMPTY;
  2. c=0;/*Bufferisempty,return0*/
  3. return(c);
  4. }
  5. }
  6. /************************************************************
  7. *function:COMPutCharB
  8. *parameter:charport,portcanbeCOM1/COM2
  9. *return:COMM_NO_ERRifthefunctionwassuccessful(thebufferwasnotfull)
  10. *COMM_TX_FULLifthebufferwasfull
  11. *COMM_BAD_CHifyouhavespecifiedanincorrectchannel
  12. *usage:Thisfunctioniscalledbyyourapplicationtosendacharacteronthecommunications
  13. *channel.ThecharactertosendisfirstinsertedintotheTxbufferandwillbesentby
  14. *theTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISRwillbe
  15. *enabled.IftheTxbufferisfull,thecharacterwillnotbesent(i.e.itwillbelost)
  16. *changelog:
  17. *1.firstimplimentedbyliyuan2010.11.5
  18. *************************************************************/
  19. unsignedcharCOMPutCharB(unsignedcharport,unsignedcharc)
  20. {
  21. //unsignedcharcpu_sr;
  22. COM_RING_BUF*pbuf;
  23. switch(port)
  24. {/*Obtainpointertocommunicationschannel*/
  25. caseCOM1:
  26. pbuf=&COM1Buf;
  27. break;
  28. caseCOM2:
  29. pbuf=&COM2Buf;
  30. break;
  31. default:
  32. return(COM_BAD_CH);
  33. }
  34. OS_ENTER_CRITICAL();
  35. if(pbuf->RingBufTxCtr
  36. pbuf->RingBufTxCtr++;/*No,incrementcharactercount*/
  37. *pbuf->RingBufTxInPtr++=c;/*Putcharacterintobuffer*/
  38. if(pbuf->RingBufTxInPtr==&pbuf->RingBufTx[COM_TX_BUF_SIZE]){/*WrapINpointer*/
  39. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  40. }
  41. if(pbuf->RingBufTxCtr==1){/*Seeifthisisthefirstcharacter*/
  42. COMEnableTxInt(port);/*Yes,EnableTxinterrupts*/
  43. OS_EXIT_CRITICAL();
  44. }else{
  45. OS_EXIT_CRITICAL();
  46. }
  47. return(COM_NO_ERR);
  48. }else{
  49. OS_EXIT_CRITICAL();
  50. return(COM_TX_FULL);
  51. }
  52. }
  53. /************************************************************
  54. *function:COMBufferInit
  55. *parameter:
  56. *return:
  57. *usage:Thisfunctioniscalledbyyourapplicationtoinitializethecommunicationsmodule.You
  58. *mustcallthisfunctionbeforecallinganyotherfunctions.
  59. *changelog:
  60. *************************************************************/
  61. voidCOMBufferInit(void)
  62. {
  63. COM_RING_BUF*pbuf;
  64. pbuf=&COM1Buf;/*InitializetheringbufferforCOM0*/
  65. pbuf->RingBufRxCtr=0;
  66. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  67. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  68. pbuf->RingBufTxCtr=0;
  69. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  70. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  71. pbuf=&COM2Buf;/*InitializetheringbufferforCOM1*/
  72. pbuf->RingBufRxCtr=0;
  73. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  74. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  75. pbuf->RingBufTxCtr=0;
  76. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  77. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  78. }
  79. /************************************************************
  80. *function:COMBufferIsEmpty
  81. *parameter:charport,portcanbeCOM1/COM2
  82. *return:char
  83. *usage:Thisfunctioniscalledbyyourapplicationtosee
  84. *ifanycharacterisavailablefromthecommunicationschannel.
  85. *Ifatleastonecharacterisavailable,thefunctionreturns
  86. *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
  87. *changelog:
  88. *************************************************************/
  89. unsignedcharCOMBufferIsEmpty(unsignedcharport)
  90. {
  91. //unsignedcharcpu_sr;
  92. unsignedcharempty;
  93. COM_RING_BUF*pbuf;
  94. switch(port)
  95. {/*Obtainpointertocommunicationschannel*/
  96. caseCOM1:
  97. pbuf=&COM1Buf;
  98. break;
  99. caseCOM2:
  100. pbuf=&COM2Buf;
  101. break;
  102. default:
  103. return(1);
  104. }
  105. OS_ENTER_CRITICAL();
  106. if(pbuf->RingBufRxCtr>0)
  107. {/*Seeifbufferisempty*/
  108. empty=0;/*BufferisNOTempty*/
  109. }
  110. else
  111. {
  112. empty=1;/*Bufferisempty*/
  113. }
  114. OS_EXIT_CRITICAL();
  115. return(empty);
  116. }
  117. /************************************************************
  118. *function:COMBufferIsFull
  119. *parameter:charport,portcanbeCOM1/COM2
  120. *return:char
  121. *usage:Thisfunctioniscalledbyyourapplicationtoseeifanymorecharacterscanbeplaced
  122. *intheTxbuffer.Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
  123. *Ifthebufferisfull,thefunctionreturnsTRUEotherwise,thefunctionreturnsFALSE.
  124. *changelog:
  125. *************************************************************/
  126. unsignedcharCOMBufferIsFull(unsignedcharport)
  127. {
  128. //unsignedcharcpu_sr;

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top