Printing Non-String Variables to the PSoC UART

    Q: How do I print an integer (or other non-string variable) to the PSoC UART?

    A: According to the PSoC UART data sheet, the UART only outputs strings and characters. Therefore, we need to convert the integer to a string first, and then output it using the UART. Solution: the sprintf() function in stdio.h

    The following C code uses printf() (which does not output to the UART):

    int a;          // declare variable
    a = 5;          // initialize variable
    float b;            // declare variable
    b = 6.7;            // initialize variable
    printf(Value = %i,%f\n, a,b); // print to screen (not UART)
    

    The following C code uses sprintf() to create a string and outputs it using the UART):

    #include <stdio.h>  // include the library with sprintf()
    int a = 5;      // declare and initialize variable
    float b = 6.7;      // declare and initialize variable
    char obuffer[100];  // declare output buffer string
    UART_Start();       // only need this once in the program
    
    // print to string using a similar syntax to the printf statement
    sprintf( obuffer, Value = %i,%f\n, a, b );
    
    UART_UartPutString( obuffer ); // output the string to the UART