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

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

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

  1. nsignedcharport,unsignedcharc)
  2. {
  3. SCI_RING_BUF*pbuf;
  4. /*Obtainpointertocommunicationschannel*/
  5. switch(port)
  6. {
  7. caseSCI0:
  8. pbuf=&SCI0Buf;
  9. break;

    caseSCI1:

  10. pbuf=&SCI1Buf;
  11. break;

    default:

  12. return;
  13. }
  14. if(pbuf->RingBufRxCtr
  15. {
  16. pbuf->RingBufRxCtr++;/*No,incrementcharactercount*/
  17. *pbuf->RingBufRxInPtr++=c;/*Putcharacterintobuffer*/
  18. if(pbuf->RingBufRxInPtr==&pbuf->RingBufRx[SCI_RX_BUF_SIZE])
  19. {/*WrapINpointer*/
  20. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  21. }
  22. }
  23. }

  24. //ThisfunctioniscalledbytheTxISRtoextractthenextcharacterfromtheTxbuffer.
  25. //ThefunctionreturnsFALSEifthebufferisemptyafterthecharacterisextractedfrom
  26. //thebuffer.ThisisdonetosignaltheTxISRtodisableinterruptsbecausethisisthe
  27. //lastcharactertosend.
  28. staticunsignedcharSCIGetTxChar(unsignedcharport,unsignedchar*err)
  29. {
  30. unsignedcharc;
  31. SCI_RING_BUF*pbuf;

    switch(port)

  32. {
  33. caseSCI0:
  34. pbuf=&SCI0Buf;
  35. break;

    caseSCI1:

  36. pbuf=&SCI1Buf;
  37. break;

    default:

  38. *err=SCI_BAD_CH;
  39. return(0);
  40. }
  41. /*Seeifbufferisempty*/
  42. if(pbuf->RingBufTxCtr>0)
  43. {
  44. pbuf->RingBufTxCtr--;/*No,decrementcharactercount*/
  45. c=*pbuf->RingBufTxOutPtr++;/*Getcharacterfrombuffer*/
  46. if(pbuf->RingBufTxOutPtr==&pbuf->RingBufTx[SCI_TX_BUF_SIZE])
  47. {
  48. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];/*WrapOUTpointer*/
  49. }
  50. *err=SCI_NO_ERR;
  51. return(c);/*Charactersarestillavailable*/
  52. }else{
  53. *err=SCI_TX_EMPTY;
  54. return(0);/*Bufferisempty*/
  55. }
  56. }

  57. interruptVectorNumber_Vsci0voidSCI0_ISR(void)
  58. {
  59. charstatus;
  60. unsignedchardata;
  61. unsignedcharerr;
  62. status=SCI0SR1;
  63. if(status&0x0F)//0x1F=00011111,ifstatusisnotReceiveDataRegFullFlag
  64. {
  65. //Seeifwehavesomekindoferror
  66. //Clearinterrupt(donothingaboutit!)
  67. data=SCI0DRL;
  68. }
  69. elseif(status&0x20)//ReceiveDataRegFullFlag
  70. {
  71. data=SCI0DRL;
  72. SCIPutRxChar(SCI0,data);//Insertreceivedcharacterintobuffer
  73. }
  74. elseif(status&0x80)
  75. {
  76. data=SCIGetTxChar(SCI0,&err);//Getnextcharactertosend.
  77. if(err==SCI_TX_EMPTY)
  78. {//Dowehaveanymorecharacterstosend?
  79. //No,DisableTxinterrupts
  80. SCI0CR2_SCTIE=0;
  81. SCI0CR2_TCIE=0;
  82. }
  83. else
  84. {
  85. SCI0DRL=data;//Yes,Sendcharacter
  86. }
  87. }
  88. }

    interruptVectorNumber_Vsci1voidSCI1_ISR(void)

  89. {
  90. charstatus;
  91. unsignedchardata;
  92. unsignedcharerr;
  93. status=SCI1SR1;
  94. if(status&0x0F)//0x1F=00011111,ifstatusisnotReceiveDataRegFullFlag
  95. {
  96. //Seeifwehavesomekindoferror
  97. //Clearinterrupt(donothingaboutit!)
  98. data=SCI1DRL;
  99. }
  100. elseif(status&0x20)//ReceiveDataRegFullFlag
  101. {
  102. data=SCI1DRL;
  103. SCIPutRxChar(SCI1,data);//Insertreceivedcharacterintobuffer
  104. }
  105. elseif(status&0x80)
  106. {
  107. data=SCIGetTxChar(SCI1,&err);//Getnextcharactertosend.
  108. if(err==SCI_TX_EMPTY)
  109. {//Dowehaveanymorecharacterstosend?
  110. //No,DisableTxinterrupts
  111. SCI1CR2_SCTIE=0;
  112. SCI1CR2_TCIE=0;
  113. }
  114. else
  115. {
  116. SCI1DRL=data;//Yes,Sendcharacter
  117. }
  118. }
  119. }

最后给个例子说明用法:

  1. #include/*commondefinesandmacros*/
  2. #include"derivative.h"/*derivative-specificdefinitions*/
  3. #include"sci.h"
  4. #include"sci_buffered.h"
  5. voidmain(void)

  6. {
  7. /*putyourowncodehere*/
  8. unsignedcharC,err;
  9. longl=0x1234L;
  10. inti=0x5678;
  11. CRGInit();
  12. SCIInit(SCI0);
  13. SCIInit(SCI1);
  14. SCIBufferInit();
  15. SCISetIEBit(SCI0,SCI_RIE);
  16. SCISetIEBit(SCI1,SCI_RIE);

    EnableInterrupts;

  17. SCIPutShortBigEndian(SCI1,i);
  18. SCIPutShortLittleEndian(SCI1,i);
  19. SCIPutLongBigEndian(SCI1,l);
  20. SCIPutLongLittleEndian(SCI1,l);
  21. for(;;)
  22. {
  23. _FEED_COP();/*feedsthedog*/
  24. C=SCIGetCharB(SCI1,&err);
  25. if(err==SCI_NO_ERR)
  26. {
  27. SCIPutCharB(SCI1,C);
  28. }
  29. }/*loopforever*/
  30. /*pleasemakesurethatyouneverleavemain*/
  31. }

  32. 例子很简单,就不多解释了。

下一篇介绍在实时操作系统 uC/OS-II 上实现串口驱动。

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

网站地图

Top