新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > PIC C18自帶串口程序

        PIC C18自帶串口程序

        作者: 時間:2016-11-13 來源:網絡 收藏
        官方寫的串口程序,值得學習一下!
        (串口無論接收還是發送都有輪詢法和中斷法)

        讀一個字節
        #include
        #include
        /********************************************************************
        * Function Name: ReadUSART *
        * Return Value: char: received data *
        * Parameters: void *
        * Description: This routine reads the data from the USART *
        * and records the status flags for that byte *
        * in USART_Status (Framing and Overrun). *
        ********************************************************************/
        #if defined (AUSART_V1) || defined (EAUSART_V3) || defined (EAUSART_V4) || defined (EAUSART_V5)
        char ReadUSART(void) //this function can be removed by macro #define ReadUSART RCREG
        {
        char data; // Holds received data
        USART_Status.val &= 0xf2; // Clear previous status flags
        if(RCSTAbits.RX9) // If 9-bit mode
        {
        USART_Status.RX_NINE = 0; // Clear the receive bit 9 for USART
        if(RCSTAbits.RX9D) // according to the RX9D bit
        USART_Status.RX_NINE = 1;
        }
        if(RCSTAbits.FERR) // If a framing error occured
        USART_Status.FRAME_ERROR = 1; // Set the status bit
        if(RCSTAbits.OERR) // If an overrun error occured
        USART_Status.OVERRUN_ERROR = 1; // Set the status bit
        data = RCREG; // Read data
        return (data); // Return the received data
        }
        #endif
        獲取字符串
        #include
        #include
        /********************************************************************
        * Function Name: getsUSART *
        * Return Value: void *
        * Parameters: buffer: pointer to string *
        * len: length of characters to receive *
        * Description: This routine receives a string of characters *
        * from the USART of length specified by len. *
        ********************************************************************/
        #if defined (AUSART_V1) || defined (EAUSART_V3) || defined (EAUSART_V4) || defined (EAUSART_V5)
        void getsUSART(char *buffer, unsigned char len)
        {
        char i; // Length counter
        unsigned char data;
        for(i=0;i
        {
        while(!DataRdyUSART());// Wait for data to be received
        data = getcUSART(); // Get a character from the USART =ReadUSART(void)
        // and save in the string
        *buffer = data;
        buffer++; // Increment the string pointer
        }
        }
        #endif

        寫一個字符
        #include
        #include
        /********************************************************************
        * Function Name: WriteUSART *
        * Return Value: none *
        * Parameters: data: data to transmit *
        * Description: This routine transmits a byte out the USART. *
        ********************************************************************/
        #if defined (AUSART_V1) || defined (EAUSART_V3) || defined (EAUSART_V4) || defined (EAUSART_V5)
        void WriteUSART(char data)
        {
        if(TXSTAbits.TX9) // 9-bit mode?
        {
        TXSTAbits.TX9D = 0; // Set the TX9D bit according to the
        if(USART_Status.TX_NINE) // USART Tx 9th bit in status reg
        TXSTAbits.TX9D = 1;
        }
        TXREG = data; // Write the data byte to the USART
        }
        #endif
        寫一個字符串
        #include
        #include
        /**********************************************************************
        * Function Name: putsUSART *
        * Return Value: void *
        * Parameters: data: pointer to string of data *
        * Description: This routine transmits a string of characters *
        * to the USART including the null. *
        **********************************************************************/
        #if defined (AUSART_V1) || defined (EAUSART_V3) || defined (EAUSART_V4) || defined (EAUSART_V5)
        void putsUSART( char *data)
        {
        do
        { // Transmit a byte
        while(BusyUSART());
        putcUSART(*data); //=WriteUSART
        } while( *data++ );
        }
        #endif



        關鍵詞: PIC串口程

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 郎溪县| 铜川市| 尉犁县| 渭南市| 确山县| 虎林市| 东丰县| 太康县| 灌云县| 蒙自县| 汾阳市| 商城县| 昌宁县| 铁岭市| 阿坝县| 勐海县| 玉溪市| 平南县| 铜川市| 奉节县| 云龙县| 防城港市| 盐池县| 丹巴县| 利川市| 绥棱县| 广西| 泸溪县| 独山县| 六安市| 壶关县| 绿春县| 诸暨市| 府谷县| 大安市| 稻城县| 瓦房店市| 安仁县| 沙湾县| 宝坻区| 安溪县|