微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > Freescale 9S12 系列单片机应用笔记(SCI)2

Freescale 9S12 系列单片机应用笔记(SCI)2

时间:11-20 来源:互联网 点击:
这里给个利用串口收发中断的实现.

首先是头文件:

  1. /*sci_buffered.h*/
  2. #ifndef_SCI_BUFFERED_H_

  3. #define_SCI_BUFFERED_H_

    #defineSCI_RX_BUF_SIZE64/*NumberofcharactersinRxringbuffer*/

  4. #defineSCI_TX_BUF_SIZE64/*NumberofcharactersinTxringbuffer*/

    #defineSCI_NO_ERR0/*Functioncallwassuccessful*/

  5. #defineSCI_BAD_CH1/*Invalidcommunicationsportchannel*/
  6. #defineSCI_RX_EMPTY2/*Rxbufferisempty,nocharacteravailable*/
  7. #defineSCI_TX_FULL3/*Txbufferisfull,couldnotdepositcharacter*/
  8. #defineSCI_TX_EMPTY4/*IftheTxbufferisempty.*/

  9. /**
  10. *Toobtainacharacterfromthecommunicationschannel.
  11. *@paramport,portcanbeSCI0/SCI1
  12. *@paramerr,isapointertowhereanerrorcodewillbeplaced:
  13. **errissettoSCI_NO_ERRifacharacterisavailable
  14. **errissettoSCI_RX_EMPTYiftheRxbufferisempty
  15. **errissettoSCI_BAD_CHifyouhavespecifiedaninvalidchannel
  16. *@returnThereceivedchariferr==SCI_NO_ERR
  17. */
  18. unsignedcharSCIGetCharB(unsignedcharport,unsignedchar*err);

    /**

  19. *Tosendacharacteronthecommunicationschannel.
  20. *ThecharactertosendisfirstinsertedintotheTxbufferandwillbesentby
  21. *theTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISRwillbe
  22. *enabled.IftheTxbufferisfull,thecharacterwillnotbesent(i.e.itwillbelost)
  23. *
  24. *@paramport,portcanbeSCI0/SCI1
  25. *@returnCOMM_NO_ERRifthefunctionwassuccessful(thebufferwasnotfull)
  26. *COMM_TX_FULLifthebufferwasfull
  27. *COMM_BAD_CHifyouhavespecifiedanincorrectchannel
  28. */
  29. unsignedcharSCIPutCharB(unsignedcharport,unsignedcharc);

    /**

  30. *Toinitializethecommunicationsmodule.
  31. *Youmustcallthisfunctionbeforecallinganyotherfunctions.
  32. */
  33. voidSCIBufferInit(void);

    /**

  34. *Toseeifanycharacterisavailablefromthecommunicationschannel.
  35. *
  36. *@paramport,portcanbeSCI0/SCI1
  37. *@returnIfatleastonecharacterisavailable,thefunctionreturns
  38. *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
  39. */
  40. unsignedcharSCIBufferIsEmpty(unsignedcharport);

    /**

  41. *ToseeifanymorecharacterscanbeplacedintheTxbuffer.
  42. *Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
  43. *
  44. *@paramport,portcanbeSCI0/SCI1
  45. *@returnIfthebufferisfull,thefunctionreturnsTRUE
  46. *otherwise,thefunctionreturnsFALSE.
  47. */
  48. unsignedcharSCIBufferIsFull(unsignedcharport);

    #endif

然后 是 c 文件.

  1. /**

  2. *SCI(SerialCommunicationInterface)BufferedSerialI/O
  3. *@filesci_buffered.c
  4. *@authorLiYuan
  5. *@platformmc9s12XX
  6. *@date2012-7-22
  7. *@version1.0.1
  8. */

  9. #include"derivative.h"/*derivative-specificdefinitions*/
  10. #include
  11. #include"sci.h"
  12. #include"sci_buffered.h"

    //#defineOS_ENTER_CRITICAL()_asm("pshc;sei")

  13. //#defineOS_EXIT_CRITICAL()_asm("pulc")

    #defineOS_ENTER_CRITICAL()_asm("tpa;sei;staacpu_sr")

  14. #defineOS_EXIT_CRITICAL()_asm("ldaacpu_sr;tap")

    /**

  15. *DATATYPES
  16. */
  17. typedefstruct{
  18. shortRingBufRxCtr;/*NumberofcharactersintheRxringbuffer*/
  19. unsignedchar*RingBufRxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
  20. unsignedchar*RingBufRxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
  21. unsignedcharRingBufRx[SCI_RX_BUF_SIZE];/*Ringbuffercharacterstorage(Rx)*/
  22. shortRingBufTxCtr;/*NumberofcharactersintheTxringbuffer*/
  23. unsignedchar*RingBufTxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
  24. unsignedchar*RingBufTxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
  25. unsignedcharRingBufTx[SCI_TX_BUF_SIZE];/*Ringbuffercharacterstorage(Tx)*/
  26. }SCI_RING_BUF;

    /**

  27. *GLOBALVARIABLES
  28. */

    SCI_RING_BUFSCI0Buf;

  29. SCI_RING_BUFSCI1Buf;

  30. /**
  31. *Toobtainacharacterfromthecommunicationschannel.
  32. *@paramport,portcanbeSCI0/SCI1
  33. *@paramerr,isapointertowhereanerrorcodewillbeplaced:
  34. **errissettoSCI_NO_ERRifacharacterisavailable
  35. **errissettoSCI_RX_EMPTYiftheRxbufferisempty
  36. **errissettoSCI_BAD_CHifyouhavespecifiedaninvalidchannel
  37. *@returnThereceivedchariferr==SCI_NO_ERR
  38. */
  39. unsignedcharSCIGetCharB(unsignedcharport,unsignedchar*err)
  40. {
  41. unsignedcharcpu_sr;
  42. unsignedcharc;
  43. SCI_RING_BUF*pbuf;
  44. /*Obtainpointertocommunicationschannel*/
  45. switch(port)
  46. {
  47. c

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

网站地图

Top