Basic use of UART stdio

In this tutorial i will show how to use the UART to transmit data to the computer. It's useful to see the values read by the ADC and other peripherals.

There is this example that use the UART to transmit the distance measured by a SRF04:Timer Periodic Mode - SRF04

This uses not only the UART API from TivaWare, but also the UART stdio utility from TivaWare™ Utilities Library.

First, like any other peripheral if we want to use the GPIO as outputs/inputs we have to configure them. We will use the UART0 which uses the pins PA0 and PA1. UART0 is connected to the ICDI so it can communicate with the computer by a COM

Enable the GPIO module as always and then configure the pins

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlDelay(3); GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX);

That was for the GPIO side, now for the UART:

SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); SysCtlDelay(3); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

This is just to enable the UART0 peripheral and finish configuring the PA0 and PA1 pins.

Let's configure now the UART clock and baudrate:

UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); UARTStdioConfig(0, 115200, 16000000);

UARTClockSourceSet sets the clock to use, it can be either the system clock or the precision internal oscillator, for this case we use the second option. Remember that if you change the clock source you have to reconfigure the baud rate.

Now UARTStdioConfig, this is the first function that is not from the UART API but it's instead from the UART stdio utility. The first parameter selects the UART module, in this case it's UART0. The second parameter selects the baud rate, 115200 in here. And last is the system clock or 16Mhz if you sellect using the PIOSC as source clock for the UART.

Now it's all configured to output data to the computer. Simply use UARTprintf. It works like the printf in the console when programming in C. Here is a example of it's use, taken from Timer Periodic Mode - SRF04

UARTprintf("distance = %2dcm \n" , pulse);

If pulse equals 20 then this outputs: distance = 20cm

And it creates a new line in the serial monitor.

And that's it for this tutorial. Later there will be more examples on how to use the UART stdio and UART. Most of them will consist of a more in depth explanation of the UART when used for debugging other peripherals like the ADC, Timers (for capturing or edge counting), mostly so the user can see data.

I hope you liked it, goodbye and i leave you with the function that i will mostly use to debug values:

void InitConsole(void){ // // Enable GPIO port A which is used for UART0 pins. // TODO: change this to whichever GPIO port you are using. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // // Configure the pin muxing for UART0 functions on port A0 and A1. // This step is not necessary if your part does not support pin muxing. // TODO: change this to select the port/pin you are using. // GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); // // Enable UART0 so that we can configure the clock. // SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); // // Use the internal 16MHz oscillator as the UART clock source. // UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); // // Select the alternate (UART) function for these pins. // TODO: change this to select the port/pin you are using. // GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); // // Initialize the UART for console I/O. // UARTStdioConfig(0, 115200, 16000000);}