欢迎来到天天文库
浏览记录
ID:42677347
大小:116.44 KB
页数:11页
时间:2019-09-20
《网上_USART的接口程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、网±_USART的接口程序一般教科书上提供的UART收发的程序往往是一段采用轮循(Polling)方式完成收发的简单代码。但对于高速的AVR来讲,采用这种方式大大降低了MUC的效率。在使用AVR时,应根据芯片本身的特点(片内大容量数据存储器RAM,更适合采用高级语言编写系统程序),编写高效可靠的UART收发接口(低层)程序。下面是一个典型的USART的接口程序。#ineludettdefineRXB81ttdefineTXB80ttdefineUPE2ttdefineOVR3ttdefineFE4ttdefineUDRE5ttdefineRXC
2、7ttdefineFRAMING_ERROR(1«FE)ttdefinePARITY_ERROR(1«UPE)ttdefineDATAOVERRUN(1«OVR)ttdefineDATA_REGISTER_EMPTY(1«UDRE)ftdefineRX_COMPLETE(1«RXC)//USARTOReceiverbufferttdefineRX_BUFFER_SIZEO8charrx_bufferO[RX_BUFFER_SIZEO];unsignedcharrxwrindexO,rxrdindexO,rxcounterO;//ThisflagissetonUSART
3、OReceiverbufferoverflowbitrxbufferoverflowO://USARTOReceiverinterruptserviceroutinettpragmasavereginterrupt[USARTORXC]voiduartO_rx_isr(void){charstatus,data;#a.smpushr26pushr27pushr30pushr31inr26,sregpushr26ttendasmstatus二UCSROA;data二UDRO;if((status&(FRAMINGERROR
4、PARITYERROR
5、DATA_OVERR
6、UN))==0)rrx_bufferO[rx_wr_indexO]二data;if(++rxwrindexO=RXBUFFERSIZEO)rxwrindex0=0;if(++rxcounterO二二RXBUFFERSIZEO){rx_counter0=0;rxbufferoverflowO=l:};};#a.smpopr26outsreg,r26popr31popr30popr27popr26#endasm}ttpragmasavereg+ttifndef_DEBUG.TERMINAL」。—//GetacharacterfromtheUSARTOReceiverbuf
7、ferftdefine_ALTERNATE_GETCIIAR_ttpragmaused+chargetchar(void){chardata;while(rx_counter0=0);data=rx_bufferO[rx_rd_indexO];if(++rxrdindexO二二RXBUFFERSIZEO)rxrdindexO二0;#asm(,,cli,z)一-rx_counterO;#asm(,,sei,z)returndata;}ttpragmaused-ttendif//USARTOTransmitterbufferttdefineTX_BUFFER_SIZEO8
8、chartx_bufferO[TX_BUFFER_SIZEO];unsignedchartx_wr_indexO,tx_rd_indexO,tx_counterO;//USARTOTransmitterinterruptserviceroutinettpragmasavereg-interrupt[USARTOTXC]voiduartO_tx_isr(void){#asmpushr26pushr27pushr30pushr31inr26,sregpushr26ttedasmif(tx_counterO){--tx_counterO;UDRO二txbufferO[txr
9、dindexO];if(++txrdindexO=TXBUFFERSIZEO)txrdindexO二0;};#asmpopr26outsreg,r26popr31popr30popr27popr26#endasm#pragmasavereg+ftifndef_DEBUG_TERMINAL_IO_//WriteacharactertotheUSARTOTransmitterbufferftdefine_ALTERNATE_PUTCIIAR_#pragmaused+voidputchar(charc){while(txcounterO二二TXBUFFER
此文档下载收益归作者所有