Cortex-M3 (NXP LPC1788)之UART用法
下面介紹Uart相關(guān)系統(tǒng)配置和Uart模塊的配置。Uart的時(shí)鐘采用PCLK,我們配置系統(tǒng)的CCLK為120M,PCLK為60M,后面設(shè)置串口的波特率就采用PLCK進(jìn)行計(jì)算。要使用串口2的功能需要使能系統(tǒng)時(shí)鐘控制PCONP,以及配置GPIO管腳為Uart2的RXD和TXD功能。要實(shí)現(xiàn)通信,我們需要設(shè)置數(shù)據(jù)的格式,包括傳輸?shù)牟ㄌ芈?,?shù)據(jù)長(zhǎng)度,停止位,以及校驗(yàn)等,這些數(shù)據(jù)在線性控制寄存器UnLCR中控制。波特率的產(chǎn)生需要經(jīng)過(guò)分?jǐn)?shù)波特率分頻器UnFDR和主分頻器DLL,DLM。計(jì)數(shù)公式如下圖。
本文引用地址:http://2s4d.com/article/201611/318449.htm根據(jù)計(jì)算,當(dāng)PLCK=60M,波特率為115200,數(shù)據(jù)位為8,停止位為1,無(wú)校驗(yàn),則DLL = 22, DLM =0, DivAddVal =1, MulVal = 2 ,線性控制寄存器中的值為0x3。
要通過(guò)串口發(fā)送數(shù)據(jù)時(shí),只需要把要發(fā)送的數(shù)據(jù)寫(xiě)入發(fā)送保持寄存器UnTHR,系統(tǒng)就會(huì)通過(guò)移位寄存器將數(shù)據(jù)通過(guò)串口發(fā)送。為了了解系統(tǒng)的發(fā)送狀態(tài),還需要線性狀態(tài)寄存器UnLSR,例如程序中使用該該寄存器的第5位判斷發(fā)慫保持寄存器是否為空,防止數(shù)據(jù)溢出。
如果需要進(jìn)行串口的中斷操作,還需要對(duì)串口中斷進(jìn)行配置,如串口中斷使能寄存器UnIER和串口中斷標(biāo)識(shí)寄存器UnIIR。程序中使用到了串口2的接收中斷,為此在中斷使能設(shè)置寄存器ISER中使能UART2中斷,在串口中斷使能寄存器UnIER中使能串口的接收中斷,該中斷同時(shí)使能了字符接收超時(shí)中斷。UART2的RXD管腳接收到數(shù)據(jù)將存放在FIFO中,程序中配置接收FIFO的觸發(fā)條件為1個(gè)字節(jié),即有接收到數(shù)據(jù)就觸發(fā)。中斷觸發(fā)后,我們可以根據(jù)中斷標(biāo)識(shí)寄存器UnIIR判斷到底是串口的接收中斷,超時(shí)中斷,發(fā)送中斷等。進(jìn)入中斷以后,接收中斷和超時(shí)中斷,都可以通過(guò)讀取接收緩存寄存器UnRBR進(jìn)行中斷復(fù)位,使下次中斷可以發(fā)生。
下面的程序例子,程序開(kāi)始打印菜單,PC串口軟件發(fā)送一個(gè)字節(jié)數(shù)據(jù)給開(kāi)發(fā)板,開(kāi)發(fā)板接收到數(shù)據(jù)后將讀取UnRBR前后的中斷標(biāo)識(shí)寄存器IIR的值,以及接收到的值發(fā)送回給PC。如果是0x5a或者0xa5還可以打開(kāi)或者關(guān)閉LED指示燈。
- #defineCCLK120000000
- #definePCLK60000000
- #definerFIO1DIR(*(volatileunsigned*)(0x20098020))
- #definerFIO1MASK(*(volatileunsigned*)(0x20098030))
- #definerFIO1PIN(*(volatileunsigned*)(0x20098034))
- #definerFIO1SET(*(volatileunsigned*)(0x20098038))
- #definerFIO1CLR(*(volatileunsigned*)(0x2009803c))
- #definerISER0(*(volatileunsigned*)(0xE000E100))
- #definerCLKSRCSEL(*(volatileunsigned*)(0x400FC10C))//時(shí)鐘源選擇寄存器
- #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時(shí)鐘選擇寄存器
- #definerUSBCLKSEL(*(volatileunsigned*)(0x400FC108))//USB時(shí)鐘選擇寄存器
- #definerPCLKSEL(*(volatileunsigned*)(0x400FC1A8))//外設(shè)時(shí)鐘寄存器
- #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;//波特率設(shè)置115200
- rU2DLM=0;
- rU2DLL=22;
- rU2LCR&=~(0x1<<7);//DLAB=0
- rU2LCR|=0x3;//8位數(shù)據(jù)位,無(wú)校驗(yàn),1個(gè)停止位
- rU2TER|=0x1<<7;//使能串口2的發(fā)送
- rU2IER|=0x1;//使能串口2的接收中斷
- rU2FCR|=0x1;//復(fù)位FIFO,設(shè)置接收1個(gè)字符觸發(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);
- }
運(yùn)行結(jié)果如下圖所示
評(píng)論