UART stands for universal asynchronous receiver/transmitter, or in laymen’s terms, the serial port. This tutorial will teach you how to use the serial port to send and receive data trough a USB to serial converter. To connect the UART to a serial port the Rx from the Simplecortex has to be connected with the Tx from the serial port and the Tx from the Simplecortex to the Rx from the serial port. Some sensors also use this interface. The library and examples can be downloaded here.
The libraries for the microcontroller itself and the variables have to be included to:
For the UART the UART library has to be included:
3. Write data to UART
To write data to a UART port first the port has to be declared with the UART[num]_Init(baud); command. Num stands for the UART port that is used, there are 4 UART ports, port0 to port3. Baud stands for the used baudrate, the most used value is 9600 but higher speeds are no problem. To use UART2 with a 19200 baudrate the correct command is:
There are two ways to send data, send a byte at a time or send a text string. To send one byte at a time the command is UART[num]_Sendchar(char to be send); . To send the character A to UART2 the command is:
65 is the ASCII code of A, it is also possible to use hexadecimal numbers, 0x41 is equal to the decimal 65 or the ascii code A.
To send a string of text there is the UART[num]_PrintString("Hello world\r\n"); Command.
When sending a line of text add \r\n on the end to make it jump to a new line.
4. Read data from UART
To read data from a UART port the instruction UART[num]_Getchar();. With this instruction the Simplecortex waits until data is send trough the Simplecortex with UART. To receive a byte of data from UART1 the correct code is:
uint8_t databyte; //declare an 8 bit variable
databyte = UART1_Getchar();
The UART port receives a data byte at a time; therefore an 8 bit variable is used. The data that has been read is stored in the databyte variable.