Freescale 9S12 系列单片机应用笔记(SCI)2
时间:11-20
来源:互联网
点击:
- nsignedcharport,unsignedcharc)
- {
- SCI_RING_BUF*pbuf;
- /*Obtainpointertocommunicationschannel*/
- switch(port)
- {
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- return;
- }
- if(pbuf->RingBufRxCtr
- {
- pbuf->RingBufRxCtr++;/*No,incrementcharactercount*/
- *pbuf->RingBufRxInPtr++=c;/*Putcharacterintobuffer*/
- if(pbuf->RingBufRxInPtr==&pbuf->RingBufRx[SCI_RX_BUF_SIZE])
- {/*WrapINpointer*/
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- }
- }
- }
- //ThisfunctioniscalledbytheTxISRtoextractthenextcharacterfromtheTxbuffer.
- //ThefunctionreturnsFALSEifthebufferisemptyafterthecharacterisextractedfrom
- //thebuffer.ThisisdonetosignaltheTxISRtodisableinterruptsbecausethisisthe
- //lastcharactertosend.
- staticunsignedcharSCIGetTxChar(unsignedcharport,unsignedchar*err)
- {
- unsignedcharc;
- SCI_RING_BUF*pbuf;
switch(port)
- {
- caseSCI0:
- pbuf=&SCI0Buf;
- break;
caseSCI1:
- pbuf=&SCI1Buf;
- break;
default:
- *err=SCI_BAD_CH;
- return(0);
- }
- /*Seeifbufferisempty*/
- if(pbuf->RingBufTxCtr>0)
- {
- pbuf->RingBufTxCtr--;/*No,decrementcharactercount*/
- c=*pbuf->RingBufTxOutPtr++;/*Getcharacterfrombuffer*/
- if(pbuf->RingBufTxOutPtr==&pbuf->RingBufTx[SCI_TX_BUF_SIZE])
- {
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];/*WrapOUTpointer*/
- }
- *err=SCI_NO_ERR;
- return(c);/*Charactersarestillavailable*/
- }else{
- *err=SCI_TX_EMPTY;
- return(0);/*Bufferisempty*/
- }
- }
- interruptVectorNumber_Vsci0voidSCI0_ISR(void)
- {
- charstatus;
- unsignedchardata;
- unsignedcharerr;
- status=SCI0SR1;
- if(status&0x0F)//0x1F=00011111,ifstatusisnotReceiveDataRegFullFlag
- {
- //Seeifwehavesomekindoferror
- //Clearinterrupt(donothingaboutit!)
- data=SCI0DRL;
- }
- elseif(status&0x20)//ReceiveDataRegFullFlag
- {
- data=SCI0DRL;
- SCIPutRxChar(SCI0,data);//Insertreceivedcharacterintobuffer
- }
- elseif(status&0x80)
- {
- data=SCIGetTxChar(SCI0,&err);//Getnextcharactertosend.
- if(err==SCI_TX_EMPTY)
- {//Dowehaveanymorecharacterstosend?
- //No,DisableTxinterrupts
- SCI0CR2_SCTIE=0;
- SCI0CR2_TCIE=0;
- }
- else
- {
- SCI0DRL=data;//Yes,Sendcharacter
- }
- }
- }
interruptVectorNumber_Vsci1voidSCI1_ISR(void)
- {
- charstatus;
- unsignedchardata;
- unsignedcharerr;
- status=SCI1SR1;
- if(status&0x0F)//0x1F=00011111,ifstatusisnotReceiveDataRegFullFlag
- {
- //Seeifwehavesomekindoferror
- //Clearinterrupt(donothingaboutit!)
- data=SCI1DRL;
- }
- elseif(status&0x20)//ReceiveDataRegFullFlag
- {
- data=SCI1DRL;
- SCIPutRxChar(SCI1,data);//Insertreceivedcharacterintobuffer
- }
- elseif(status&0x80)
- {
- data=SCIGetTxChar(SCI1,&err);//Getnextcharactertosend.
- if(err==SCI_TX_EMPTY)
- {//Dowehaveanymorecharacterstosend?
- //No,DisableTxinterrupts
- SCI1CR2_SCTIE=0;
- SCI1CR2_TCIE=0;
- }
- else
- {
- SCI1DRL=data;//Yes,Sendcharacter
- }
- }
- }
最后给个例子说明用法:
- #include
/*commondefinesandmacros*/ - #include"derivative.h"/*derivative-specificdefinitions*/
- #include"sci.h"
- #include"sci_buffered.h"
voidmain(void)
- {
- /*putyourowncodehere*/
- unsignedcharC,err;
- longl=0x1234L;
- inti=0x5678;
- CRGInit();
- SCIInit(SCI0);
- SCIInit(SCI1);
- SCIBufferInit();
- SCISetIEBit(SCI0,SCI_RIE);
- SCISetIEBit(SCI1,SCI_RIE);
EnableInterrupts;
- SCIPutShortBigEndian(SCI1,i);
- SCIPutShortLittleEndian(SCI1,i);
- SCIPutLongBigEndian(SCI1,l);
- SCIPutLongLittleEndian(SCI1,l);
- for(;;)
- {
- _FEED_COP();/*feedsthedog*/
- C=SCIGetCharB(SCI1,&err);
- if(err==SCI_NO_ERR)
- {
- SCIPutCharB(SCI1,C);
- }
- }/*loopforever*/
- /*pleasemakesurethatyouneverleavemain*/
- }
- 例子很简单,就不多解释了。
下一篇介绍在实时操作系统 uC/OS-II 上实现串口驱动。