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

STM32F10x 学习笔记7(USART实现串口通讯 3)

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

  1. ARTx);
  2. return(USARTx->DR&0xff);
  3. }


commrtos.h 的代码如下:

  1. #ifndef_COMMRTOS_H_
  2. #define_COMMRTOS_H_
  3. #ifndefCFG_H
  4. #defineCOMM_RX_BUF_SIZE64/*NumberofcharactersinRxringbuffer*/
  5. #defineCOMM_TX_BUF_SIZE64/*NumberofcharactersinTxringbuffer*/
  6. #endif
  7. /*
  8. *********************************************************************************************************
  9. *CONSTANTS
  10. *********************************************************************************************************
  11. */
  12. #ifndefFALSE
  13. #defineFALSE0x00
  14. #endif
  15. #ifndefTRUE
  16. #defineTRUE0xff
  17. #endif
  18. #ifndefNUL
  19. #defineNUL0x00
  20. #endif
  21. #defineCOM10
  22. #defineCOM21
  23. /*ERRORCODES*/
  24. #defineCOMM_NO_ERR0/*Functioncallwassuccessful*/
  25. #defineCOMM_BAD_CH1/*Invalidcommunicationsportchannel*/
  26. #defineCOMM_RX_EMPTY2/*Rxbufferisempty,nocharacteravailable*/
  27. #defineCOMM_TX_FULL3/*Txbufferisfull,couldnotdepositcharacter*/
  28. #defineCOMM_TX_EMPTY4/*IftheTxbufferisempty.*/
  29. #defineCOMM_RX_TIMEOUT5/*Ifatimeoutoccurredwhilewaitingforacharacter*/
  30. #defineCOMM_TX_TIMEOUT6/*Ifatimeoutoccurredwhilewaitingtosendachar.*/
  31. #defineCOMM_PARITY_NONE0/*Definesforsettingparity*/
  32. #defineCOMM_PARITY_ODD1
  33. #defineCOMM_PARITY_EVEN2
  34. unsignedcharCommGetChar(unsignedcharch,unsignedshortto,unsignedchar*err);
  35. voidCOMInit(void);
  36. unsignedcharCommIsEmpty(unsignedcharch);
  37. unsignedcharCommIsFull(unsignedcharch);
  38. unsignedcharCommPutChar(unsignedcharch,unsignedcharc,unsignedshortto);
  39. voidCommPutStr(unsignedcharch,uint8_t*str);
  40. #endif

commrtos.c 的代码如下:

  1. #include"stm32f10x_usart.h"
  2. #include"includes.h"
  3. #include"COMMRTOS.H"
  4. /*
  5. *DATATYPES
  6. */
  7. typedefstruct{
  8. unsignedshortRingBufRxCtr;/*NumberofcharactersintheRxringbuffer*/
  9. OS_EVENT*RingBufRxSem;/*PointertoRxsemaphore*/
  10. unsignedchar*RingBufRxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
  11. unsignedchar*RingBufRxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
  12. unsignedcharRingBufRx[COMM_RX_BUF_SIZE];/*Ringbuffercharacterstorage(Rx)*/
  13. unsignedshortRingBufTxCtr;/*NumberofcharactersintheTxringbuffer*/
  14. OS_EVENT*RingBufTxSem;/*PointertoTxsemaphore*/
  15. unsignedchar*RingBufTxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
  16. unsignedchar*RingBufTxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
  17. unsignedcharRingBufTx[COMM_TX_BUF_SIZE];/*Ringbuffercharacterstorage(Tx)*/
  18. }COMM_RING_BUF;
  19. /*
  20. *GLOBALVARIABLES
  21. */
  22. COMM_RING_BUFComm1Buf;
  23. COMM_RING_BUFComm2Buf;
  24. staticvoidCOMEnableTxInt(unsignedcharport)
  25. {
  26. staticUSART_TypeDef*map[2]={USART1,USART2};
  27. //USART_ITConfig(map[port],USART_IT_TXE,ENABLE);
  28. map[port]->CR1|=USART_FLAG_TXE;
  29. }
  30. /*
  31. *********************************************************************************************************
  32. *REMOVECHARACTERFROMRINGBUFFER
  33. *
  34. *
  35. *Description:Thisfunctioniscalledbyyourapplicationtoobtainacharacterfromthecommunications
  36. *channel.Thefunctionwillwaitforacharactertobereceivedontheserialchannelor
  37. *untilthefunctiontimesout.
  38. *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
  39. *COMM1
  40. *COMM2
  41. *toistheamountoftime(inclockticks)thatthecallingfunctioniswillingto
  42. *waitforacharactertoarrive.Ifyouspecifyatimeoutof0,thefunctionwill
  43. *waitforeverforacharactertoarrive.
  44. *errisapointertowhereanerrorcodewillbeplaced:
  45. **errissettoCOMM_NO_ERRifacharacterhasbeenreceived
  46. **errissettoCOMM_RX_TIMEOUTifatimeoutoccurred
  47. **errissettoCOMM_BAD_CHifyouspecifyaninvalidchannelnumber
  48. *Returns:Thecharacterinthebuffer(orNULifatimeoutoccurred)
  49. *********************************************************************************************************
  50. */
  51. unsignedcharCommGetChar(unsignedcharch,unsignedshortto,unsignedchar*err)
  52. {
  53. unsignedcharc;
  54. unsignedcharoserr;
  55. COMM_RING_BUF*pbuf;
  56. OS_CPU_SRcpu_sr;
  57. switch(ch)
  58. {/*Obtainpointertocommunicationschannel*/
  59. caseCOM1:
  60. pbuf=&Comm1Buf;
  61. break;
  62. caseCOM2:
  63. pbuf=&Comm2Buf;
  64. break;
  65. defau

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

网站地图

Top