您的位置 首页 国产IC

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

这里给个利用串口收发中断的实现.首先是头文件:/*sci_buffered.h*/p>#ifndef_SCI_BUFFERED_H_#define_SCI_BUFFERED_H_/p>p&

这儿给个使用串口收发中止的完成.

首先是头文件:

  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. caseSCI0:
  48. pbuf=&SCI0Buf;
  49. break;

    caseSCI1:

  50. pbuf=&SCI1Buf;
  51. break;

    default:

  52. *err=SCI_BAD_CH;
  53. return(0);
  54. }
  55. OS_ENTER_CRITICAL();
  56. if(pbuf->RingBufRxCtr>0)/*Seeifbufferisempty*/
  57. {
  58. pbuf->RingBufRxCtr–;/*No,decrementcharactercount*/
  59. c=*pbuf->RingBufRxOutPtr++;/*Getcharacterfrombuffer*/
  60. if(pbuf->RingBufRxOutPtr==&pbuf->RingBufRx[SCI_RX_BUF_SIZE])
  61. {
  62. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];/*WrapOUTpointer*/
  63. }
  64. OS_EXIT_CRITICAL();
  65. *err=SCI_NO_ERR;
  66. return(c);
  67. }
  68. else
  69. {
  70. OS_EXIT_CRITICAL();
  71. *err=SCI_RX_EMPTY;
  72. c=0;/*Bufferisempty,return0*/
  73. return(c);
  74. }
  75. }

  76. /**
  77. *Tosendacharacteronthecommunicationschannel.
  78. *ThecharactertosendisfirstinsertedintotheTxbufferandwillbesentby
  79. *theTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISRwillbe
  80. *enabled.IftheTxbufferisfull,thecharacterwillnotbesent(i.e.itwillbelost)
  81. *
  82. *@paramport,portcanbeSCI0/SCI1
  83. *@returnCOMM_NO_ERRifthefunctionwassuccessful(thebufferwasnotfull)
  84. *COMM_TX_FULLifthebufferwasfull
  85. *COMM_BAD_CHifyouhavespecifiedanincorrectchannel
  86. */
  87. unsignedcharSCIPutCharB(unsignedcharport,unsignedcharc)
  88. {
  89. unsignedcharcpu_sr;
  90. SCI_RING_BUF*pbuf;
  91. /*Obtainpointertocommunicationschannel*/
  92. switch(port)
  93. {
  94. caseSCI0:
  95. pbuf=&SCI0Buf;
  96. break;

    caseSCI1:

  97. pbuf=&SCI1Buf;
  98. break;

    default:

  99. return(SCI_BAD_CH);
  100. }

    OS_ENTER_CRITICAL();

  101. if(pbuf->RingBufTxCtr
  102. {
  103. pbuf->RingBufTxCtr++;/*No,incrementcharactercount*/
  104. *pbuf->RingBufTxInPtr++=c;/*Putcharacterintobuffer*/
  105. if(pbuf->RingBufTxInPtr==&pbuf->RingBufTx[SCI_TX_BUF_SIZE])
  106. {
  107. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];/*WrapINpointer*/
  108. }
  109. if(pbuf->RingBufTxCtr==1)
  110. {/*Seeifthisisthefirstcharacter*/
  111. SCIEnableTxInt(port);/*Yes,EnableTxinterrupts*/
  112. OS_EXIT_CRITICAL();
  113. }
  114. else
  115. {
  116. OS_EXIT_CRITICAL();
  117. }
  118. return(SCI_NO_ERR);
  119. }
  120. else
  121. {
  122. OS_EXIT_CRITICAL();
  123. return(SCI_TX_FULL);
  124. }
  125. }

    /**

  126. *Toinitializethecommunicationsmodule.
  127. *Youmustcallthisfunctionbeforecallinganyotherfunctions.
  128. */
  129. voidSCIBufferInit(void)
  130. {
  131. SCI_RING_BUF*pbuf;
  132. /*InitializetheringbufferforSCI0*/
  133. pbuf=&SCI0Buf;
  134. pbuf->RingBufRxCtr=0;
  135. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  136. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  137. pbuf->RingBufTxCtr=0;
  138. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  139. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  140. /*InitializetheringbufferforSCI1*/
  141. pbuf=&SCI1Buf;
  142. pbuf->RingBufRxCtr=0;
  143. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  144. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  145. pbuf->RingBufTxCtr=0;
  146. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  147. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  148. }

  149. /**
  150. *Toseeifanycharacterisavailablefromthecommunicationschannel.
  151. *
  152. *@paramport,portcanbeSCI0/SCI1
  153. *@returnIfatleastonecharacterisavailable,thefunctionreturns
  154. *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
  155. */
  156. unsignedcharSCIBufferIsEmpty(unsignedcharport)
  157. {
  158. unsignedcharcpu_sr;
  159. unsignedcharempty;
  160. SCI_RING_BUF*pbuf;
  161. /*Obtainpointertocommunicationschannel*/
  162. switch(port)
  163. {
  164. caseSCI0:
  165. pbuf=&SCI0Buf;
  166. break;

    caseSCI1:

  167. pbuf=&SCI1Buf;
  168. break;

    default:

  169. return(1);
  170. }
  171. OS_ENTER_CRITICAL();
  172. if(pbuf->RingBufRxCtr>0)/*Seeifbufferisempty*/
  173. {
  174. empty=0;/*BufferisNOTempty*/
  175. }
  176. else
  177. {
  178. empty=1;/*Bufferisempty*/
  179. }
  180. OS_EXIT_CRITICAL();
  181. return(empty);
  182. }

  183. /**
  184. *ToseeifanymorecharacterscanbeplacedintheTxbuffer.
  185. *Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
  186. *
  187. *@paramport,portcanbeSCI0/SCI1
  188. *@returnIfthebufferisfull,thefunctionreturnsTRUE
  189. *otherwise,thefunctionreturnsFALSE.
  190. */
  191. unsignedcharSCIBufferIsFull(unsignedcharport)
  192. {
  193. unsignedcharcpu_sr;
  194. charfull;
  195. SCI_RING_BUF*pbuf;
  196. /*Obtainpointertocommunicationschannel*/
  197. switch(port)
  198. {
  199. caseSCI0:
  200. pbuf=&SCI0Buf;
  201. break;

    caseSCI1:

  202. pbuf=&SCI1Buf;
  203. break;

    default:

  204. return(1);
  205. }
  206. OS_ENTER_CRITICAL();
  207. if(pbuf->RingBufTxCtr
  208. {/*Seeifbufferisfull*/
  209. full=0;/*BufferisNOTfull*/
  210. }
  211. else
  212. {
  213. full=1;/*Bufferisfull*/
  214. }
  215. OS_EXIT_CRITICAL();
  216. return(full);
  217. }

  218. //ThisfunctioniscalledbytheRxISRtoinsertacharacterintothereceiveringbuffer.
  219. staticvoidSCIPutRxChar(unsignedcharport,unsignedcharc)
  220. {
  221. SCI_RING_BUF*pbuf;
  222. /*Obtainpointertocommunicationschannel*/
  223. switch(port)
  224. {
  225. caseSCI0:
  226. pbuf=&SCI0Buf;
  227. break;

    caseSCI1:

  228. pbuf=&SCI1Buf;
  229. break;

    default:

  230. return;
  231. }
  232. if(pbuf->RingBufRxCtr
  233. {
  234. pbuf->RingBufRxCtr++;/*No,incrementcharactercount*/
  235. *pbuf->RingBufRxInPtr++=c;/*Putcharacterintobuffer*/
  236. if(pbuf->RingBufRxInPtr==&pbuf->RingBufRx[SCI_RX_BUF_SIZE])
  237. {/*WrapINpointer*/
  238. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  239. }
  240. }
  241. }

  242. //ThisfunctioniscalledbytheTxISRtoextractthenextcharacterfromtheTxbuffer.
  243. //ThefunctionreturnsFALSEifthebufferisemptyafterthecharacterisextractedfrom
  244. //thebuffer.ThisisdonetosignaltheTxISRtodisableinterruptsbecausethisisthe
  245. //lastcharactertosend.
  246. staticunsignedcharSCIGetTxChar(unsignedcharport,unsignedchar*err)
  247. {
  248. unsignedcharc;
  249. SCI_RING_BUF*pbuf;

    switch(port)

  250. {
  251. caseSCI0:
  252. pbuf=&SCI0Buf;
  253. break;

    caseSCI1:

  254. pbuf=&SCI1Buf;
  255. break;

    default:

  256. *err=SCI_BAD_CH;
  257. return(0);
  258. }
  259. /*Seeifbufferisempty*/
  260. if(pbuf->RingBufTxCtr>0)
  261. {
  262. pbuf->RingBufTxCtr–;/*No,decrementcharactercount*/
  263. c=*pbuf->RingBufTxOutPtr++;/*Getcharacterfrombuffer*/
  264. if(pbuf->RingBufTxOutPtr==&pbuf->RingBufTx[SCI_TX_BUF_SIZE])
  265. {
  266. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];/*WrapOUTpointer*/
  267. }
  268. *err=SCI_NO_ERR;
  269. return(c);/*Charactersarestillavailable*/
  270. }else{
  271. *err=SCI_TX_EMPTY;
  272. return(0);/*Bufferisempty*/
  273. }
  274. }

  275. interruptVectorNumber_Vsci0voidSCI0_ISR(void)
  276. {
  277. charstatus;
  278. unsignedchardata;
  279. unsignedcharerr;
  280. status=SCI0SR1;
  281. if(status&0x0F)//0x1F=00011111,ifstatusisnotReceiveDataRegFullFlag
  282. {
  283. //Seeifwehavesomekindoferror
  284. //Clearinterrupt(donothingaboutit!)
  285. data=SCI0DRL;
  286. }
  287. elseif(status&0x20)//ReceiveDataRegFullFlag
  288. {
  289. data=SCI0DRL;
  290. SCIPutRxChar(SCI0,data);//Insertreceivedcharacterintobuffer
  291. }
  292. elseif(status&0x80)
  293. {
  294. data=SCIGetTxChar(SCI0,&err);//Getnextcharactertosend.
  295. if(err==SCI_TX_EMPTY)
  296. {//Dowehaveanymorecharacterstosend?
  297. //No,DisableTxinterrupts
  298. SCI0CR2_SCTIE=0;
  299. SCI0CR2_TCIE=0;
  300. }
  301. else
  302. {
  303. SCI0DRL=data;//Yes,Sendcharacter
  304. }
  305. }
  306. }

    interruptVectorNumber_Vsci1voidSCI1_ISR(void)

  307. {
  308. charstatus;
  309. unsignedchardata;
  310. unsignedcharerr;
  311. status=SCI1SR1;
  312. if(status&0x0F)//0x1F=00011111,ifstatusisnotReceiveDataRegFullFlag
  313. {
  314. //Seeifwehavesomekindoferror
  315. //Clearinterrupt(donothingaboutit!)
  316. data=SCI1DRL;
  317. }
  318. elseif(status&0x20)//ReceiveDataRegFullFlag
  319. {
  320. data=SCI1DRL;
  321. SCIPutRxChar(SCI1,data);//Insertreceivedcharacterintobuffer
  322. }
  323. elseif(status&0x80)
  324. {
  325. data=SCIGetTxChar(SCI1,&err);//Getnextcharactertosend.
  326. if(err==SCI_TX_EMPTY)
  327. {//Dowehaveanymorecharacterstosend?
  328. //No,DisableTxinterrupts
  329. SCI1CR2_SCTIE=0;
  330. SCI1CR2_TCIE=0;
  331. }
  332. else
  333. {
  334. SCI1DRL=data;//Yes,Sendcharacter
  335. }
  336. }
  337. }

最终给个比如阐明用法:

  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 上完成串口驱动。

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/bandaoti/ic/264179.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部