欢迎来到天天文库
浏览记录
ID:20124439
大小:39.00 KB
页数:4页
时间:2018-10-10
《uart操作程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、UART操作的程序佚名/2008-09-18T3C25-V5仅售880元UART操作的程序作者:转载#include"config.h"/*defineUARTsdatastructure*/typedefstructUartMode{uint8datab;//bytelengthuint8stopb;//stopbit,1/2uint8parity;//paritybitsetting}UARTMODE;uint8rcv_buf[8];uint8rcv_byte[8];volatileuint8add_data_num;volatileuint8rcv_new;//f
2、lagforrcvingnewdatavolatileuint8add_data;void__irqIRQ_UART0(void){uint8i;if((U0IIR&0x0F)==0x04){rcv_new=1;for(i=0;i<8;i++){rcv_buf[i]=U0RBR;}}//readallthedatainFIFOonce.Thenthereisnotoomuchinterruptionsif((U0IIR&0xF)==0x0C){add_data_num=0;while(U0LSR&0x00000001)//ReadUARTsLSRregisterfirs
3、t,{//anddeterminewhetherFIFOisemptyadd_data=1;//ornot.rcv_byte[add_data_num]=U0RBR;add_data_num++;}}VICVectAddr=0x00;}voidUART0_SendByte(uint8dat){U0THR=dat;}voidUART0_SendBuf(void){uint8i;for(i=0;i<8;i++)UART0_SendByte(rcv_buf[i]);while((U0LSR&0x20)==0);}voidUART0_SendAddByte(uint8j){ui
4、nt8i;for(i=0;i5、6、(baud>115200))return(0);if((set.datab<5)7、8、(set.datab>8))return(0);if((set.stopb==0)9、10、(set.stopb>2))return(0);if(set.parity>4)return(0);/*setUARTsbaudrate11、*/U0LCR=0x80;//DLAB=1bak=(Fpclk>>4)/baud;U0DLM=bak>>8;U0DLL=bak&0xFF;/*setUARTsmode*/bak=set.datab-5;if(set.stopb==2)bak12、=0x04;if(set.parity!=0){set.parity=set.parity-1;bak13、=0x08;}bak14、=set.parity<<4;U0LCR=bak;return(1);}intmain(void){//addusersourcecodeUARTMODEset;set.datab=8;set.stopb=115、;set.parity=0;rcv_new=0;PINSEL0=(PINSEL0&0xFFFFFFF0)16、0x5;UART0_Init(115200,set);U0FCR=0x81;U0IER=0x01;IRQEnable();/*EnableUART0Interruption*/VICIntSelect=0x00000000;VICVectCntl0=0x2017、0x06;VICVectAddr0=(uint32)IRQ_UART0;VICIntEnable=1<<0x06;//EnableUART0sInterruptionwhile(1){if(rcv_new==118、){rcv_new=0;UART0_SendBuf();}if(add_data==1){add_data=0;UART0_SendAddByte(add_data_num);}}return0;}
5、
6、(baud>115200))return(0);if((set.datab<5)
7、
8、(set.datab>8))return(0);if((set.stopb==0)
9、
10、(set.stopb>2))return(0);if(set.parity>4)return(0);/*setUARTsbaudrate
11、*/U0LCR=0x80;//DLAB=1bak=(Fpclk>>4)/baud;U0DLM=bak>>8;U0DLL=bak&0xFF;/*setUARTsmode*/bak=set.datab-5;if(set.stopb==2)bak
12、=0x04;if(set.parity!=0){set.parity=set.parity-1;bak
13、=0x08;}bak
14、=set.parity<<4;U0LCR=bak;return(1);}intmain(void){//addusersourcecodeUARTMODEset;set.datab=8;set.stopb=1
15、;set.parity=0;rcv_new=0;PINSEL0=(PINSEL0&0xFFFFFFF0)
16、0x5;UART0_Init(115200,set);U0FCR=0x81;U0IER=0x01;IRQEnable();/*EnableUART0Interruption*/VICIntSelect=0x00000000;VICVectCntl0=0x20
17、0x06;VICVectAddr0=(uint32)IRQ_UART0;VICIntEnable=1<<0x06;//EnableUART0sInterruptionwhile(1){if(rcv_new==1
18、){rcv_new=0;UART0_SendBuf();}if(add_data==1){add_data=0;UART0_SendAddByte(add_data_num);}}return0;}
此文档下载收益归作者所有