Using the printf Function in PSoC® 3 - KBA83472

Version: *B



How do I use the printf function in stdio.h to send data to UART in PSoC® 3 device?



Keil’s printf function calls the putchar() to send characters but the default putchar() uses UART based Special Function Register (SFR), which PSoC® 3 does not have. Therefore, to use printf, the program must override Keil's built-in putchar function.

For example, if ‘UART’ is the instance name of the UART component in your project, then write the following function in the main.c file to override Keil’s built-in putchar function:

char putchar(char c) {         UART_WriteTxData((uint8)c);         return c; }

Then, printf can be used to send data over UART in the following manner (note that the putchar() function should either be defined or declared before the first call to the printf() function for proper execution): 

int main()
            printf(“Hello world”); // uses the new putchar() function to stream data over UART
            while(!(UART_ReadTxStatus() & UART_TX_STS_COMPLETE)); // wait until transfer is complete