在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

            EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > Mega128的串口通訊程序

            Mega128的串口通訊程序

            作者: 時間:2016-11-10 來源:網(wǎng)絡(luò) 收藏
            #i nclude
            #i nclude
            #i nclude "uart.h"
            /* USART Buffer Defines */
            #define USART_RX_BUFFER_SIZE 16 /* 1,2,4,8,16,32,64,128 or 256
            bytes */
            #define USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 )
            #define USART_TX_BUFFER_SIZE 16 /* 1,2,4,8,16,32,64,128 or 256
            bytes */
            #define USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 )
            #if ( USART_RX_BUFFER_SIZE & USART_RX_BUFFER_MASK )
            #error RX buffer size is not a power of 2
            #endif
            /* Static Variables */
            static unsigned char USART_RxBuf[USART_RX_BUFFER_SIZE];
            static volatile unsigned char USART_RxHead;
            static volatile unsigned char USART_RxTail;
            static unsigned char USART_TxBuf[USART_TX_BUFFER_SIZE];
            static volatile unsigned char USART_TxHead;
            static volatile unsigned char USART_TxTail;
            /* 串口初始化 */
            void InitUSART(INT8U baudrate)
            {
            INT8U x;
            UBRR0L = baudrate;
            UCSR0B = ( (1<Complete
            Interrupt Enable, Receiver Enable and Transmitter Enable*/
            x = 0; /* flush receive buffer */
            USART_RxTail = x;
            USART_RxHead = x;
            USART_TxTail = x;
            USART_TxHead = x;
            }
            /* 接收中斷 */
            void USART_RX_interrupt( void )
            {
            unsigned char data;
            unsigned char tmphead;
            data = UDR0; /* read the received data */
            tmphead = ( USART_RxHead + 1 ) & USART_RX_BUFFER_MASK;/*
            calculate
            buffer index */
            USART_RxHead = tmphead; /* store new index */
            if ( tmphead == USART_RxTail )
            {
            return;/* ERROR! Receive buffer overflow, you can use
            flux cortorl */
            }
            USART_RxBuf[tmphead] = data; /* store received data in buffer
            */
            }
            /* 發(fā)送中斷 */
            void USART_TX_interrupt( void )
            {
            unsigned char tmptail;
            if ( USART_TxHead != USART_TxTail )/* check if all data is
            transmitted */
            {
            tmptail = ( USART_TxTail + 1 ) & USART_TX_BUFFER_MASK;/*
            calculate buffer index */
            USART_TxTail = tmptail; /* store new index */
            UDR0 = USART_TxBuf[tmptail]; /* start transmition */
            }
            else
            {
            UCSR0B &= ~(1<}
            }
            /* 讀取數(shù)據(jù)函數(shù) */
            unsigned char ReceiveByte( void )
            {
            unsigned char tmptail;
            while ( USART_RxHead == USART_RxTail ) /* wait for incomming
            data */
            ;
            tmptail = ( USART_RxTail + 1 ) & USART_RX_BUFFER_MASK;/*
            calculate
            buffer index */
            USART_RxTail = tmptail; /* store new index */
            return USART_RxBuf[tmptail]; /* return data */
            }
            /* 發(fā)送數(shù)據(jù)函數(shù) */
            void TransmitByte( unsigned char data )
            {
            unsigned char tmphead;
            tmphead = ( USART_TxHead + 1 ) & USART_TX_BUFFER_MASK; /*
            calculate
            buffer index */
            while ( tmphead == USART_TxTail )/* wait for free space in
            buffer */
            ;
            USART_TxBuf[tmphead] = data; /* store data in buffer */
            USART_TxHead = tmphead; /* store new index */
            UCSR0B |= (1<}
            /* 查詢是否有數(shù)據(jù) */
            unsigned char DataInReceiveBuffer( void )
            {
            return ( USART_RxHead != USART_RxTail ); /* return 0 (FALSE)
            if the
            receive buffer is empty */
            }
            /* 中斷中只做很少的事情,通常我是在接收中斷中post一個信號量,觸發(fā)別的任務(wù)讀數(shù)據(jù)
            */


            關(guān)鍵詞: Mega128串口通

            評論


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

            關(guān)閉