interfatarea pe rs232.docx

(48 KB) Pobierz

4.13 EXAMPLE 11

RS232 serial communication

This example illustrates the use of the microcontroller’s EUSART module. Connection to a PC is enabled through the RS232 standard. The program works in the following manner. Every byte received via serial communication is displayed using LED diodes connected to port B and is automatically returned to the transmitter after that. If an error occurs on receive, it will be signalled by switching the LED diode on. The easiest way to test the device operation practically is by using a standard Windows program called Hyper Terminal.

EXAMPLE 11

/*Header******************************************************/

   

unsigned short i;

 

void main() {

    UART1_Init(19200);            // Initialize USART module

                                  // (8 bit, 19200 baud rate, no parity bit...)

    while (1) {

        if (UART1_Data_Ready()) { // If data has been received

            i = UART1_Read();     // read it

            UART1_Write(i);       // and send it back

        }

    }

}

In order to make this example work properly, it is necessary to tick off the UART library in the Library Manager prior to compiling:

·         UART

 

Zgłoś jeśli naruszono regulamin