Freescale 9S12 系列单片机应用笔记(SCI)1
时间:11-20
来源:互联网
点击:
SCI模块应用笔记(1)
UART,也就是异步串行通讯接口是单片机中最常见的外设,几乎每种类型的单片机都必备1到2个UART接口,9S12系列单片机也不例外。不过,摩托罗拉给他自己的单片机的串口起了个很有个性的缩写名称SCI(serialcommunicationinterface),其实就是我们常说的UART。
各种单片机串口的编程都大同小异,无非就是设置波特率、起始位、停止位、校验位等等。下面通过给出编程例子的方式分别介绍。
设置波特率
- /**
- *SettheBaudRateoftheSCI.
- *@paramport,portcanbeSCI0/SCI1
- *@parambaudRate,thewantedbaudrate.
- *@parambusClk,TheSCImoduleclock.
- */
- voidSCISetBaudRate(unsignedcharport,unsignedlongbaudRate,unsignedlongbusClk)
- {
- unsignedshortbaudRateReg;
- baudRateReg=(unsignedshort)(busClk/baudRate/16);
- if(port==SCI0)
- {
- //HerewemustwriteBDHfirst!
- SCI0BDH=(0x1f&(baudRateReg>>8));
- SCI0BDL=(0xff&baudRateReg);
- }
- elseif(port==SCI1)
- {
- SCI1BDH=(0x1f&(baudRateReg>>8));
- SCI1BDL=(0xff&baudRateReg);
- }
- else
- {
- //Somethingmustgowrong.Donothinghere!
- }
- }
设置奇偶校验位
- /**
- *Enable/DisableparityfunctionandsettheParitytype
- *@paramportportcanbeSCI0/SCI1
- *@paramisEnable0fordisableparityfunction,1forenable
- *@paramtype0forEvenParity,1forOddParity
- */
- voidSCISetParity(unsignedcharport,unsignedcharisEnable,unsignedchartype)
- {
- if(port==SCI0)
- {
- SCI0CR1_PE=(isEnable&0x01);
- SCI0CR1_PT=(type&0x01);
- }
- elseif(port==SCI1)
- {
- SCI1CR1_PE=(isEnable&0x01);
- SCI1CR1_PT=(type&0x01);
- }
- else
- {
- //Ifcoderunhere,somethingmustgowrong.Donothinghere!
- }
- }
使能数据位的长度
- /**
- *SettheDataFormatModeBit
- *@paramportportcanbeSCI0/SCI1
- *@parambitsmustbe8or9
- */
- voidSCISetDataBit(unsignedcharport,unsignedcharbits)
- {
- if(port==SCI0)
- {
- switch(bits)
- {
- case8:
- SCI0CR1_M=0;/*1startbit,8databits,1stopbit*/
- break;
- case9:
- SCI0CR1_M=1;/*1startbit,9databits,1stopbit*/
- break;
- default:
- //Ifcoderunhere,somethingmustgowrong.Donothinghere!
- break;
- }
- }
- elseif(port==SCI1)
- {
- switch(bits)
- {
- case8:
- SCI1CR1_M=0;/*1startbit,8databits,1stopbit*/
- break;
- case9:
- SCI1CR1_M=1;/*1startbit,9databits,1stopbit*/
- break;
- default:
- //Ifcoderunhere,somethingmustgowrong.Donothinghere!
- break;
- }
- }
- else
- {
- //Ifcoderunhere,somethingmustgowrong.Donothinghere!
- }
- }
设置串口的工作模式
NORMAL_MODE是我们通常用的模式
LOOP_MODE是自发自收模式
SING_WIRE_MODE就是收发公用一条数据线的模式
- /**
- *Settheworkmodeofoperation
- *@paramportportcanbeSCI0/SCI1
- *@parammodemodecanbeNORMAL_MODE/LOOP_MODE/SING_WIRE_MODE
- */
- voidSCISetWorkMode(unsignedcharport,unsignedcharmode)
- {
- if(port==SCI0)
- {
- switch(mode)
- {
- caseNORMAL_MODE:
- SCI0CR1_LOOPS=0;
- SCI0CR1_RSRC=0;
- break;
- caseLOOP_MODE:
- SCI0CR1_LOOPS=1;
- SCI0CR1_RSRC=0;
- break;
- caseSING_WIRE_MODE:
- SCI0CR1_LOOPS=1;
- SCI0CR1_RSRC=1;
- break;
- default:
- //Ifcoderunhere,somethingmustgowrong.Donothinghere!
- break;
- }
- }
- elseif(port==SCI1)
- {
- switch(mode)
- {
- caseNORMAL_MODE:
- SCI1CR1_LOOPS=0;
- SCI1CR1_RSRC=0;
- break;
- caseLOOP_MODE:
- SCI1CR1_LOOPS=1;
- SCI1CR1_RSRC=0;
- break;
- caseSING_WIRE_MODE:
- SCI1CR1_LOOPS=1;
- SCI1CR1_RSRC=1;
- break;
- default:
- //Ifcoderunhere,somethingmustgowrong.Donothinghere!
- break;
- }
- }
- else
- {
- //Ifcoderunhere,somethingmustgowrong.Donothinghere!
- }
- }
设置SCI模块是否在Wait模式下工作
- /**
- *Enable/DisabletheSCIinwaitmode
- *@paramportportcanbeSCI0/SCI1
- *@parammodemodecanbeRUN_MODE/WAIT_MODE
- */
- voidSCISetPowerMode(unsignedcharport,unsignedcharmode)
- {
- if(port==SCI0)
- {
- switch(mode)
- {
- caseRUN_MODE:
- SCI0CR1_SCISWAI=0;
- break;
- caseWAIT_MODE:
- SCI0CR1_SCISWAI=1;
- break;
- default:
- //Ifcoderunhere,somethingmustgowrong.Donothinghere!
- break;
- }
- }
- elseif(port==SCI1)
- {
- switch(mode)
- {
- caseRUN_MODE:
- SCI1CR1_SCISWAI=0;
- break;
- caseWAIT_MODE:
- SCI1CR1_SCISWAI=1;
- break;
- default:
- //Ifcoderunhere,somethingmustgowrong.Donothinghere!
- break;
- }
- }
- else
- {
- //Ifcoderunhere,somethingmustgowrong.Donothinghere!
- }
- }
使能SCI模块的接收功能
- /**
- *Enable/DisableSCIReceiver
- *@paramportportcanbeSCI0/SCI1
- *@paramisEnable0disable1enable
- */
- voidSCIEnableRecv(unsignedcharport,unsignedcharisEnable)
- {
- if(port==SCI0)
- {
- SC