Cortex-M3 (NXP LPC1788)之UART用法
下面介紹Uart相關系統(tǒng)配置和Uart模塊的配置。Uart的時鐘采用PCLK,我們配置系統(tǒng)的CCLK為120M,PCLK為60M,后面設置串口的波特率就采用PLCK進行計算。要使用串口2的功能需要使能系統(tǒng)時鐘控制PCONP,以及配置GPIO管腳為Uart2的RXD和TXD功能。要實現(xiàn)通信,我們需要設置數(shù)據(jù)的格式,包括傳輸?shù)牟ㄌ芈?,?shù)據(jù)長度,停止位,以及校驗等,這些數(shù)據(jù)在線性控制寄存器UnLCR中控制。波特率的產(chǎn)生需要經(jīng)過分數(shù)波特率分頻器UnFDR和主分頻器DLL,DLM。計數(shù)公式如下圖。
本文引用地址:http://www.ex-cimer.com/article/201611/318449.htm根據(jù)計算,當PLCK=60M,波特率為115200,數(shù)據(jù)位為8,停止位為1,無校驗,則DLL = 22, DLM =0, DivAddVal =1, MulVal = 2 ,線性控制寄存器中的值為0x3。
要通過串口發(fā)送數(shù)據(jù)時,只需要把要發(fā)送的數(shù)據(jù)寫入發(fā)送保持寄存器UnTHR,系統(tǒng)就會通過移位寄存器將數(shù)據(jù)通過串口發(fā)送。為了了解系統(tǒng)的發(fā)送狀態(tài),還需要線性狀態(tài)寄存器UnLSR,例如程序中使用該該寄存器的第5位判斷發(fā)慫保持寄存器是否為空,防止數(shù)據(jù)溢出。
如果需要進行串口的中斷操作,還需要對串口中斷進行配置,如串口中斷使能寄存器UnIER和串口中斷標識寄存器UnIIR。程序中使用到了串口2的接收中斷,為此在中斷使能設置寄存器ISER中使能UART2中斷,在串口中斷使能寄存器UnIER中使能串口的接收中斷,該中斷同時使能了字符接收超時中斷。UART2的RXD管腳接收到數(shù)據(jù)將存放在FIFO中,程序中配置接收FIFO的觸發(fā)條件為1個字節(jié),即有接收到數(shù)據(jù)就觸發(fā)。中斷觸發(fā)后,我們可以根據(jù)中斷標識寄存器UnIIR判斷到底是串口的接收中斷,超時中斷,發(fā)送中斷等。進入中斷以后,接收中斷和超時中斷,都可以通過讀取接收緩存寄存器UnRBR進行中斷復位,使下次中斷可以發(fā)生。
下面的程序例子,程序開始打印菜單,PC串口軟件發(fā)送一個字節(jié)數(shù)據(jù)給開發(fā)板,開發(fā)板接收到數(shù)據(jù)后將讀取UnRBR前后的中斷標識寄存器IIR的值,以及接收到的值發(fā)送回給PC。如果是0x5a或者0xa5還可以打開或者關閉LED指示燈。
- #defineCCLK120000000
- #definePCLK60000000
- #definerFIO1DIR(*(volatileunsigned*)(0x20098020))
- #definerFIO1MASK(*(volatileunsigned*)(0x20098030))
- #definerFIO1PIN(*(volatileunsigned*)(0x20098034))
- #definerFIO1SET(*(volatileunsigned*)(0x20098038))
- #definerFIO1CLR(*(volatileunsigned*)(0x2009803c))
- #definerISER0(*(volatileunsigned*)(0xE000E100))
- #definerCLKSRCSEL(*(volatileunsigned*)(0x400FC10C))//時鐘源選擇寄存器
- #definerPLL0CON(*(volatileunsigned*)(0x400FC080))//PLL0控制寄存器
- #definerPLL0CFG(*(volatileunsigned*)(0x400FC084))//PLL0配置寄存器
- #definerPLL0STAT(*(volatileunsigned*)(0x400FC088))//PLL0狀態(tài)寄存器
- #definerPLL0FEED(*(volatileunsigned*)(0x400FC08C))//PLL0饋送寄存器
- #definerPLL1CON(*(volatileunsigned*)(0x400FC0A0))
- #definerPLL1CFG(*(volatileunsigned*)(0x400FC0A4))
- #definerPLL1STAT(*(volatileunsigned*)(0x400FC0A8))
- #definerPLL1FEED(*(volatileunsigned*)(0x400FC0AC))
- #definerCCLKSEL(*(volatileunsigned*)(0x400FC104))//CPU時鐘選擇寄存器
- #definerUSBCLKSEL(*(volatileunsigned*)(0x400FC108))//USB時鐘選擇寄存器
- #definerPCLKSEL(*(volatileunsigned*)(0x400FC1A8))//外設時鐘寄存器
- #definerPCON(*(volatileunsigned*)(0x400FC0C0))
- #definerPXCONP(*(volatileunsigned*)(0x400FC0C4))
- #definerSCS(*(volatileunsigned*)(0x400FC1A0))//系統(tǒng)控制和狀態(tài)寄存器
- #definerCLKOUTCFG(*(volatileunsigned*)(0x400FC1C8))
- #definerIOCON_P0_10(*(volatileunsigned*)(0x4002C028))
- #definerIOCON_P0_11(*(volatileunsigned*)(0x4002C02C))
- #definerPCONP(*(volatileunsigned*)(0x400FC0C4))
- #definerU2LCR(*(volatileunsigned*)(0x4009800C))
- #definerU2FDR(*(volatileunsigned*)(0x40098028))
- #definerU2DLL(*(volatileunsigned*)(0x40098000))
- #definerU2DLM(*(volatileunsigned*)(0x40098004))
- #definerU2TER(*(volatileunsigned*)(0x40098030))
- #definerU2THR(*(volatileunsigned*)(0x40098000))
- #definerU2RBR(*(volatileunsigned*)(0x40098000))
- #definerU2FCR(*(volatileunsigned*)(0x40098008))
- #definerU2IIR(*(volatileunsigned*)(0x40098008))
- #definerU2LSR(*(volatileunsigned*)(0x40098014))
- #definerU2IER(*(volatileunsigned*)(0x40098004))
- #definerU2ACR(*(volatileunsigned*)(0x40098020))
- voidUART2_IRQHandler()
- {
- unsignedintintId;
- chartmp_char;
- intId=rU2IIR&0xf;
- rU2THR=intId;
- if(intId==0xc||intId==0x4)//RDA或者CTI中斷
- {
- rU2LCR&=~(0x1<<7);//DLAB=0
- tmp_char=rU2RBR&0xff;
- rU2THR=tmp_char;
- }
- intId=rU2IIR&0xf;
- rU2THR=intId;
- if(tmp_char==0xa5)
- rFIO1PIN|=(1<<18);
- elseif(tmp_char==0x5a)
- rFIO1PIN&=~(1<<18);
- }
- voidSystemInit()
- {
- rSCS&=~(0x1<<4);//頻率12M
- rSCS|=(0x1<<5);//使能主振蕩器
- while(0==(rSCS&(0x1<<6)));//等待主振蕩器穩(wěn)定
- rCLKSRCSEL=0x1;
- rPLL0CFG=0x9;//配置CCLK=120M
- rPLL0CON=0x01;
- rPLL0FEED=0xAA;
- rPLL0FEED=0x55;
- while(0==(rPLL0STAT&(0x1<<10)));
- rCCLKSEL=(0x1|(0x1<<8));
- rPCLKSEL=0x2;//配置PCLK=60M
- rCLKOUTCFG=0x0|(0xb<<4)|(0x1<<8);
- }
- voidInit_Uart2()
- {
- rPCONP|=0x1<<24;//使能UART2功率控制
- rIOCON_P0_10=(rIOCON_P0_10&(~0x7))|0x1;//P0.10P0.11做UART2的發(fā)送和接收管腳
- rIOCON_P0_11=(rIOCON_P0_11&(~0x7))|0x1;
- rU2LCR|=0x1<<7;//DLAB=1
- rU2FDR=1|2<<4;//波特率設置115200
- rU2DLM=0;
- rU2DLL=22;
- rU2LCR&=~(0x1<<7);//DLAB=0
- rU2LCR|=0x3;//8位數(shù)據(jù)位,無校驗,1個停止位
- rU2TER|=0x1<<7;//使能串口2的發(fā)送
- rU2IER|=0x1;//使能串口2的接收中斷
- rU2FCR|=0x1;//復位FIFO,設置接收1個字符觸發(fā)中斷
- rU2FCR|=0x1<<1|0x1<<2;
- rISER0|=0x1<<7;//使能串口2中斷
- }
- voidUart2SendC(charc)
- {
- rU2THR=c&0xff;
- while(!(rU2LSR&(0x1<<5)));//等待rU2THR中的數(shù)據(jù)發(fā)送完成,防止數(shù)據(jù)溢出
- }
- voidUart2SendS(char*s)
- {
- while(*s)
- {
- Uart2SendC(*s);
- s++;
- }
- }
- intmain(void)
- {
- charstr[]={"nr1,DisplaytheU2IIR[3:0]+Data+U2IIR[3:0]nr2,Send0x5a--->TurnontheLEDnr3,Send0xa5--->TurnofftheLEDnr"};
- rFIO1DIR|=(1<<18);//GPIO1.18->OUTPUT
- Init_Uart2();
- Uart2SendS(str);
- while(1);
- }
運行結(jié)果如下圖所示
評論