Serial Port initialization program and Application

Source: Internet
Author: User

// ------ When Fcpu = 32 m, filter rate setting ------------ # defineUART_RATE_1200 (uint16) 6661 # average (uint16) 3331 # average (uint16) 1666 # define (uint16) 833 # defineUART_RATE_19200 (uint16) 417 //************************************* // Function Name: init_Uart // function: Serial Port initialization // entry parameter: // exit parameter: // baud rate formula: Fosc/[4 (n + 1)] //************************************** * void Init_Uart (uint16 Tcon) {SPBRGH = (Tcon> 8 );/ /Set the baud rate SPBRGL = (Tcon & 0XFF); TXSTA = 0X04; // enable serial transmission, select high-speed baud rate RCSTA = 0X80; // enable serial operation, continuous receiving enable receiver BAUDCON = (1 <3); // 16-bit baud rate generator TXEN = 1; // enable sending // RX9 = 1; // The ninth-digit receiving enable CREN = 1; // enable receiving RCIE = 1; // enable receiving interruption }//******************************* * ***** // Function Name: uartSendByte // function: Send one byte of data through the serial port // entry parameter: data to be sent // exit parameter: NONE // return value: none //************************************* ** void Uart1SendByte (uint8 Byte) {while (! TRMT); // when the sending data register is full, wait for TXREG = Byte ;} //*************************************/ /function name: uartSendByte // function: Send a series of data via serial port // entry parameter: data to be sent // exit parameter: NONE // return value: none //************************************* ** void Uart1SendString (uint8 *, uint8 DatLong) {uint8 I = 0; for (I = DatLong; I> 0; I --, a ++) {while (! TRMT); // The data sending register is not empty. Wait TXREG = * a ;}// ---- application instance ---- // serial port initialization Init_Uart (UART_RATE_1200); // initialize UART baud rate 1200

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.