Patch Level Update: PICC-18 STD
Patch Level Update: HI-TECH C PRO for the PIC10/12/16 MCU Family
NEW - PRO Compiler supporting Microchip PIC32 microcontrollers
Omniscient Code Generation: as featured in EDN Hot 100 Products of 2007.
|
The C standard library includes a family of functions useful for generating formatted output: printf(), sprintf(), fprintf(), and others. They are very powerful, but to get that power they suffer from the twin problems of size (usually vital in embedded systems) and complexity (which contributes to the size problem, but also makes it very easy to mis-use them). Usually, an embedded program does not need the full functionality and so can get away with smaller, custom-tailored routines that can be easier to use, too. Let's assume that you already have a function called putch() that takes a single char argument and sends it to your output device, whatever that may be. The simplest step up from that is a function to send a string to that same output device:
/* Send 'len' characters from 's' to 'putch()';
if 'len'==0, keep sending until you hit a '\0'. */
void put_string(const char* s, unsigned len)
{
if (!s) return;
if (len) while (len--) putch(*(s++));
else while (*s) putch(*(s++));
}
(This function will, if you ask it nicely, send only part of an array of chars to putch()). Code to output other data types can be written either to call putch() for every character, or (with appropriate precautions to avoid buffer overflows) can generate strings which are then fed to put_string(). In this article, the second approach will be used. To
output integers in decimal, the following code is much smaller than a
full sprintf()
implementation:
|
![]() |
But remember, as you add complexity, you generally add code size and bugs. |
|
March 2006. |
Copyright © 2008 HI-TECH Software • Trademarks • Forum
Site Map