| |||||
Technical Support Support Resources
Product Information | RTX166 TINY: USING PRINTF, SPRINTF, SCANF, AND SSCANFInformation in this article applies to:
QUESTIONI have a very simple program that uses Round-Robin task scheduling and the printf function. Sometimes the output from sprintf and printf is corrupted as demonstrated by the following test code.
void task1() _task_ 2 {
static char buffer[20]; // static because of RTX166 Tiny stack
while (1) {
sprintf(buffer, "TEST %d;", (int)255);
if (strcmp(buffer, "TEST 255;") != 0) {
_nop_(); // program reaches this point, but
// this should never be the case.
}
}
}
void task2() _task_ 1 {
while (1) printf("a");
}
How can I solve this problem. ANSWERThe RTX166 Tiny README.TXT file found in the \KEIL\C166\RTX_TINY\ folder describes that using pointers to stack-based variables is not safe. The printf, sprintf, scanf, and sscanf library routines (as well as any other routines with variable-length argument lists) use pointers to access the parameters (which are passed on the stack). Since RTX166 may reorganize the stack after a task switch, you see the strange output results that you do. The result is that any function which uses variable-length argument lists (like printf, scanf, and so on) will not work as expected. RESOLUTIONWe recommed that you disable the RTX Tiny Hardware Timer Interrupt for those routines that access the stack via pointer. For example:
.
.
.
T0IE = 0; // disable RTX Timer interrupt
sprintf(buffer, "TEST %d;", (int)255);
T0IE = 1; // enable RTX Timer interrupt
.
.
.
T0IE = 0; // disable RTX Timer interrupt
printf("a");
T0IE = 1; // enable RTX Timer interrupt }
.
.
.
Note that this problem does not exist with RTX166 Full. So you may want to consider using RTX166 Full. MORE INFORMATION
Last Reviewed: Thursday, October 20, 2005 | ||||
| |||||