Freescale 9S12 系列单片机应用笔记(SCI)2
时间:11-20
来源:互联网
点击:
这里给个利用串口收发中断的实现.
首先是头文件:
- /*sci_buffered.h*/
#ifndef_SCI_BUFFERED_H_
- #define_SCI_BUFFERED_H_
#defineSCI_RX_BUF_SIZE64/*NumberofcharactersinRxringbuffer*/
- #defineSCI_TX_BUF_SIZE64/*NumberofcharactersinTxringbuffer*/
#defineSCI_NO_ERR0/*Functioncallwassuccessful*/
- #defineSCI_BAD_CH1/*Invalidcommunicationsportchannel*/
- #defineSCI_RX_EMPTY2/*Rxbufferisempty,nocharacteravailable*/
- #defineSCI_TX_FULL3/*Txbufferisfull,couldnotdepositcharacter*/
- #defineSCI_TX_EMPTY4/*IftheTxbufferisempty.*/
- /**
- *Toobtainacharacterfromthecommunicationschannel.
- *@paramport,portcanbeSCI0/SCI1
- *@paramerr,isapointertowhereanerrorcodewillbeplaced:
- **errissettoSCI_NO_ERRifacharacterisavailable
- **errissettoSCI_RX_EMPTYiftheRxbufferisempty
- **errissettoSCI_BAD_CHifyouhavespecifiedaninvalidchannel
- *@returnThereceivedchariferr==SCI_NO_ERR
- */
- unsignedcharSCIGetCharB(unsignedcharport,unsignedchar*err);
/**
- *Tosendacharacteronthecommunicationschannel.
- *ThecharactertosendisfirstinsertedintotheTxbufferandwillbesentby
- *theTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISRwillbe
- *enabled.IftheTxbufferisfull,thecharacterwillnotbesent(i.e.itwillbelost)
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnCOMM_NO_ERRifthefunctionwassuccessful(thebufferwasnotfull)
- *COMM_TX_FULLifthebufferwasfull
- *COMM_BAD_CHifyouhavespecifiedanincorrectchannel
- */
- unsignedcharSCIPutCharB(unsignedcharport,unsignedcharc);
/**
- *Toinitializethecommunicationsmodule.
- *Youmustcallthisfunctionbeforecallinganyotherfunctions.
- */
- voidSCIBufferInit(void);
/**
- *Toseeifanycharacterisavailablefromthecommunicationschannel.
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnIfatleastonecharacterisavailable,thefunctionreturns
- *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
- */
- unsignedcharSCIBufferIsEmpty(unsignedcharport);
/**
- *ToseeifanymorecharacterscanbeplacedintheTxbuffer.
- *Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
- *
- *@paramport,portcanbeSCI0/SCI1
- *@returnIfthebufferisfull,thefunctionreturnsTRUE
- *otherwise,thefunctionreturnsFALSE.
- */
- unsignedcharSCIBufferIsFull(unsignedcharport);
#endif
然后 是 c 文件.
/**
- *SCI(SerialCommunicationInterface)BufferedSerialI/O
- *@filesci_buffered.c
- *@authorLiYuan
- *@platformmc9s12XX
- *@date2012-7-22
- *@version1.0.1
- */
- #include"derivative.h"/*derivative-specificdefinitions*/
- #include
- #include"sci.h"
- #include"sci_buffered.h"
//#defineOS_ENTER_CRITICAL()_asm("pshc;sei")
- //#defineOS_EXIT_CRITICAL()_asm("pulc")
#defineOS_ENTER_CRITICAL()_asm("tpa;sei;staacpu_sr")
- #defineOS_EXIT_CRITICAL()_asm("ldaacpu_sr;tap")
/**
- *DATATYPES
- */
- typedefstruct{
- shortRingBufRxCtr;/*NumberofcharactersintheRxringbuffer*/
- unsignedchar*RingBufRxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
- unsignedchar*RingBufRxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
- unsignedcharRingBufRx[SCI_RX_BUF_SIZE];/*Ringbuffercharacterstorage(Rx)*/
- shortRingBufTxCtr;/*NumberofcharactersintheTxringbuffer*/
- unsignedchar*RingBufTxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
- unsignedchar*RingBufTxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
- unsignedcharRingBufTx[SCI_TX_BUF_SIZE];/*Ringbuffercharacterstorage(Tx)*/
- }SCI_RING_BUF;
/**
- *GLOBALVARIABLES
- */
SCI_RING_BUFSCI0Buf;
- SCI_RING_BUFSCI1Buf;
- /**
- *Toobtainacharacterfromthecommunicationschannel.
- *@paramport,portcanbeSCI0/SCI1
- *@paramerr,isapointertowhereanerrorcodewillbeplaced:
- **errissettoSCI_NO_ERRifacharacterisavailable
- **errissettoSCI_RX_EMPTYiftheRxbufferisempty
- **errissettoSCI_BAD_CHifyouhavespecifiedaninvalidchannel
- *@returnThereceivedchariferr==SCI_NO_ERR
- */
- unsignedcharSCIGetCharB(unsignedcharport,unsignedchar*err)
- {
- unsignedcharcpu_sr;
- unsignedcharc;
- SCI_RING_BUF*pbuf;
- /*Obtainpointertocommunicationschannel*/
- switch(port)
- {
- c