Freescale 9S12 系列单片机应用笔记(SCI)1
时间:11-20
来源:互联网
点击:
- I0CR2_RE=(isEnable&0x01);
- }
- elseif(port==SCI1)
- {
- SCI1CR2_RE=(isEnable&0x01);
- }
- else
- {
- //Ifcoderunhere,somethingmustgowrong.Donothinghere!
- }
- }
使能SCI模块的发送功能
- /**
- *Enable/DisableSCITransmitter
- *@paramportportcanbeSCI0/SCI1
- *@paramisEnable0disable1enable
- */
- voidSCIEnableTrans(unsignedcharport,unsignedcharisEnable)
- {
- if(port==SCI0)
- {
- SCI0CR2_TE=(isEnable&0x01);
- }
- elseif(port==SCI1)
- {
- SCI1CR2_TE=(isEnable&0x01);
- }
- else
- {
- //Ifcoderunhere,somethingmustgowrong.Donothinghere!
- }
- }
发送数据
- /**
- *SendacharthrounghSCImodule.
- *@paramportportcanbeSCI0/SCI1
- *@paramsthedatatobesent
- */
- voidSCIPutChar(unsignedcharport,unsignedchars)
- {
- if(port==SCI0)
- {
- while(SCI0SR1_TDRE==0);//SCI0SR1_TC是发送完成
- SCI0DRL=s;
- }
- else
- {
- while(SCI1SR1_TDRE==0);//SCI1SR1_TC
- SCI1DRL=s;
- }
- }
- /**
- *SendacharstringthrounghSCImodule.
- *@paramportportcanbeSCI0/SCI1
- *@param*strthestringtobesent
- */
- voidSCIPutStr(unsignedcharport,unsignedchar*str)
- {
- while(0!=*str)
- {
- SCIPutChar(port,*str);
- str++;
- }
- }
- /**
- *SenddatathrounghSCImodule.
- *@paramportportcanbeSCI0/SCI1
- *@param*ppointertothedatatobesent
- *@paramsizethesize(byte)ofthedata
- */
- voidSCIWrite(unsignedcharport,void*p,intsize)
- {
- unsignedchar*str=(unsignedchar*)p;
- while(size>0)
- {
- SCIPutChar(port,*str);
- str++;
- size--;
- }
- }
- /**
- *Sendashortintvalue(BigEndian)throunghSCImodule.
- *@paramportportcanbeSCI0/SCI1
- *@paramithedatatobesent
- */
- voidSCIPutShortBigEndian(unsignedcharport,shorti)
- {
- char*p=(char*)&i;
- SCIPutChar(port,p[0]);
- SCIPutChar(port,p[1]);
- }
- /**
- *Sendashortintvalue(LittleEndian)throunghSCImodule.
- *@paramportportcanbeSCI0/SCI1
- *@paramithedatatobesent
- */
- voidSCIPutShortLittleEndian(unsignedcharport,shorti)
- {
- char*p=(char*)&i;
- SCIPutChar(port,p[1]);
- SCIPutChar(port,p[0]);
- }
- /**
- *Sendalongintvalue(BigEndian)throunghSCImodule.
- *@paramportportcanbeSCI0/SCI1
- *@paramithedatatobesent
- */
- voidSCIPutLongBigEndian(unsignedcharport,longi)
- {
- char*p=(char*)&i;
- SCIPutChar(port,p[0]);
- SCIPutChar(port,p[1]);
- SCIPutChar(port,p[2]);
- SCIPutChar(port,p[3]);
- }
- /**
- *Sendalongintvalue(LittleEndian)throunghSCImodule.
- *@paramportportcanbeSCI0/SCI1
- *@paramithedatatobesent
- */
- voidSCIPutLongLittleEndian(unsignedcharport,longi)
- {
- char*p=(char*)&i;
- SCIPutChar(port,p[3]);
- SCIPutChar(port,p[2]);
- SCIPutChar(port,p[1]);
- SCIPutChar(port,p[0]);
- }
接收数据
- /**
- *ReceiveachardatafromSCImodule,noreply.
- *@paramportportcanbeSCI0/SCI1
- *@returnthereceivedchar
- */
- unsignedcharSCIGetChar(unsignedcharport)
- {
- if(port==SCI0)
- {
- while(SCI0SR1_RDRF==0);
- returnSCI0DRL;
- }
- else
- {
- while(SCI1SR1_RDRF==0);
- returnSCI1DRL;
- }
- }
相应的头文件
- /**
- *\filesci.h
- *\authorLiYuan
- *platform:mc9s12dp256B
- *date:2012-4-16
- *version:1.0.0
- *description:SCI(SerialCommunicationInterface)SupportCode
- */
- #ifndef_SCI_H_
- #define_SCI_H_
- #defineSCI00
- #defineSCI11
- #defineIDLE_LINE0
- #defineADDRESS_MARK1
- #defineNORMAL_MODE0
- #defineLOOP_MODE1
- #defineSING_WIRE_MODE2
- #defineRUN_MODE0
- #defineWAIT_MODE1
- /*FunctionDeclaration*/
- /**
- *SettheBaudRateoftheSCI.
- *@paramport,portcanbeSCI0/SCI1
- *@parambaudRate,thewantedbaudrate.
- *@parambusClk,TheSCImoduleclock.
- */
- voidSCISetBaudRate(unsignedcharport,unsignedlongbaudRate,unsignedlongbusClk);
- /**
- *SettheInterruptEnableBit
- *@paramportportcanbeSCI0/SCI1
- *@paramtieTransmitterInterruptEnableBIt
- *@paramtcieTransmissionCompleteInterruptEnableBIt
- *@paramrieReceiverFullInterruptEnableBIt
- *@paramilieIdleLineInterruptEnableBIt
- *0InterruptrequestsDisabled
- *1InterruptrequestsEnabled
- */
- voidSCISetIEBit(unsignedcharport,unsignedchartie,unsignedchartcie,unsignedcharrie,unsignedcharilie);
- /**
- *EnableTheTxinterrupt(TransmitterInterruptEnableBIt)
- *@paramport,portcanbeSCI0/SCI1
- */
- voidSCIEnableTxInt(unsignedcharport);
- /**
- *DisableTheTxinterrupt(TransmitterInterruptEnableBIt)
- *@paramport,portcanbeSCI0/SCI1
- */
- voidSCIDisTxInt(unsignedcharport);
- /**
- *Enable/DisableSCIReceiver
- *@paramportportcanbeSCI0/S