Cypress Perform

Home > Support
support.cypress.com     Bookmark and Share
Support

Knowledge Base Article



Using the printf Function in PSoC® 3 - KBA83472

Last Updated: 01/08/2013

Version: *A
Question: How do I use the printf function in stdio.h to send data to UART?

Answer: Keil’s printf function calls the putchar() to send characters but the default putchar uses an UART based on the 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 to 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):

void main() {
UART_Start();
while (1) {
printf("Hello world"); // uses the new putchar() function to stream data to the UART
while(!(UART_ReadTxStatus() & UART_TX_STS_COMPLETE)); // wait until transfer is complete
}
}

Related Categories: PSoC® 3




Provide feedback on this item to help us improve:

How likely are you to recommend this article to a friend or colleague?

Not at all likely
0
1
2
3
4
5
6
7
8
9
10
Extremely likely

Was this item helpful?
Yes
No
Maybe


Additional comments:

Email:

Sunset Owner: KUMR; Secondary Owner: KUMR; Sunset Date: 05/23/13