新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > LPC21XX 串口的接收和發(fā)送中斷(MDK)

        LPC21XX 串口的接收和發(fā)送中斷(MDK)

        作者: 時(shí)間:2016-11-09 來源:網(wǎng)絡(luò) 收藏
        由于使用檢測方式僅適合于數(shù)據(jù)量少的設(shè)備中使用,當(dāng)需要發(fā)送或者接收大量數(shù)據(jù)時(shí)應(yīng)該采用串口中斷接收或發(fā)送的方式。

        /*============================================================
        LPC21XX 串口使用接收發(fā)送中斷
        ==============================================================
        本例程的CCLK=60M.
        晶振采用12M
        倍頻系數(shù)為:5
        分頻系數(shù)為:2
        以上設(shè)置在Startup.s中設(shè)置
        */
        #include
        #include "Config.H"
        #define UART_BAUD(baud) (unsigned int)(Fpclk/(baud * 16))
        uint8 buffer[100];
        uint8 SendLen;//發(fā)送數(shù)據(jù)長度
        uint8 SendCount;//數(shù)據(jù)發(fā)送計(jì)數(shù)
        uint8 SendData[256];
        void Init_Uart0(unsigned int Baud)
        {
        /* initialize the serial interface */
        PINSEL0 = 0x00000005; /* Enable RxD0 and TxD0 */
        U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
        U0DLM=(unsigned char)(Baud>>8);
        U0DLL = (unsigned char)Baud;
        U0LCR = 0x03; /* DLAB = 0 */
        }
        void delay (unsigned int i)
        { /* Delay function */
        unsigned int n;
        while(i>1)
        {
        for(n=65535;n>1;n--);
        i--;
        }
        }
        //發(fā)送普通二進(jìn)制數(shù)據(jù)
        void Sent_Byte(uint8 *da,uint8 len)
        {
        uint8 i;
        for(i=0;i
        SendData[i]=da[i];
        SendLen=len;
        U0THR = SendData[0];
        SendCount=1;
        }
        // 發(fā)送字符
        void Sent_Str(unsigned char const *str)
        {
        uint8 i=0;
        while(1)
        {
        SendData[i]=*str;
        if( *str == /0 ) break;
        str++;
        i++;
        }
        SendLen=i;
        U0THR = SendData[0];
        SendCount=1;
        }
        //串口中斷
        void __irq uart_int()
        {
        uint8 i;
        if((U0IIR&0x0f)==0x02) //發(fā)送中斷
        {
        if(SendCount!=SendLen)
        {
        U0THR = SendData[SendCount];
        SendCount++;
        }
        }
        // 接收中斷
        else if(((U0IIR & 0x0F) == 0x0C)||((U0IIR & 0x0F) == 0x04))
        { // 如果中斷是由FIFO數(shù)據(jù)達(dá)到觸發(fā)點(diǎn)引起的
        for (i=0; i<14; i++)
        {
        buffer[i] = U0RBR;
        }
        Sent_Byte(buffer,8);
        }
        /*
        else if ((U0IIR & 0x0F) == 0x0C)
        { // 如果中斷是由FIFO超時(shí)引起的
        buffer[0] = U0RBR;
        } */
        VICVectAddr = 0; // 清中斷標(biāo)志,不是外部中斷不必復(fù)位EXTINT
        }
        int main(void)
        {
        Init_Uart0(UART_BAUD(115200));
        U0FCR = 0xc1; // 開啟UART FIFO模式 1個(gè)字符觸發(fā)0x01,4-0x41,8-0x81,14-0xc1
        U0IER = 0x03; // 使能RBR中斷,禁止THRE Rx線狀態(tài)中斷(DLAB=0時(shí))
        VICIntSelect = 0; // 選擇所有中斷為IRQ中斷
        VICVectCntl0 = 0x20 | 0x06; // slot0 對應(yīng)中斷源為UART
        VICVectAddr0 = (uint32)uart_int; // slot0 中斷服務(wù)程序
        VICIntEnable = 1 << 0x06; // 使能UART中斷源
        Sent_Str("http://blog.csdn.net/cy757//n") ;
        for(;;)
        {
        delay(200);
        }
        }


        評論


        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 南澳县| 乌海市| 汝阳县| 健康| 陆良县| 东乡族自治县| 岑巩县| 元江| 股票| 榆树市| 隆安县| 合江县| 郁南县| 焦作市| 开阳县| 汝城县| 陆河县| 修水县| 盐城市| 泰安市| 雷州市| 治多县| 托克逊县| 苏尼特右旗| 丹东市| 元阳县| 祁东县| 东辽县| 弥勒县| 襄汾县| 汕尾市| 温宿县| 许昌市| 乐山市| 邳州市| 应城市| 田东县| 沐川县| 丰顺县| 四会市| 儋州市|