View previous topic :: View next topic |
Author |
Message |
Dale Botkin Guest
|
printf() to a function - can somebody 'splain to me? |
Posted: Wed Nov 28, 2001 11:46 am |
|
|
OK, I see you can use printf(fname, cstring, values) to send a formatted string to a function. Does this call the function once for each character, passing that character as a parameter, or what? That's the only way I can think of that it would make sense, but the docs seem to be very vague on this.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1355 |
|
![](templates/subSilver/images/spacer.gif) |
John Goss Guest
|
Re: printf() to a function - can somebody 'splain to me? |
Posted: Wed Nov 28, 2001 6:10 pm |
|
|
Yes, fname is the name of the function fname(). fname() would be a function that does some routine with one char (1byte).
The typical application may be a routine to send one data byte to a LCD. The printf() function takes care of the recursive funtion calls to your fname() function.
void PutCharLCD(char Alpha)
{
code to send 'Alpha' to LCD
}
main()
{
byte voltage = 12;
printf(PutCharLCD,"This is a test.");
printf(PutCharLCD,"Voltage = \%u",voltage);
}
:=OK, I see you can use printf(fname, cstring, values) to send a formatted string to a function. Does this call the function once for each character, passing that character as a parameter, or what? That's the only way I can think of that it would make sense, but the docs seem to be very vague on this.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1362 |
|
![](templates/subSilver/images/spacer.gif) |
Dale Botkin Guest
|
Re: printf() to a function - can somebody 'splain to me? |
Posted: Thu Nov 29, 2001 12:03 pm |
|
|
:=Yes, fname is the name of the function fname(). fname() would be a function that does some routine with one char (1byte).
Thanks, that's what I thought. After posting I re-read the function description in the help file and found the magic cookie crumb... the help file says if you don't specify fname, it defaults to putc(). Since putc() takes a char parameter, I guess that'd mean printf() calls it once for each character to be printed. Thanks for the confirmation!
Dale
___________________________
This message was ported from CCS's old forum
Original Post ID: 1375 |
|
![](templates/subSilver/images/spacer.gif) |
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
Re: printf() to a function - can somebody 'splain to me? |
Posted: Mon Sep 08, 2008 7:48 pm |
|
|
John Goss wrote: | Yes, fname is the name of the function fname(). |
It's interesting that some canned library functions cause the code not to compile, when they are passed to printf as fname. For example:
Code: | printf(i2c_write, "*"); |
generates a compile-time error 12 "undefined identifier i2c_write". A canned library function can be wrapped by a user-defined function. For example, the following compiles just fine:
Code: | void wrapped_i2c_write(int8 iByte) {i2c_write(iByte);}
...
printf(wrapped_i2c_write, "*"); |
PCWH 4.071 _________________ Read the label, before opening a can of worms. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Tue Sep 09, 2008 2:58 am |
|
|
Not surprising really. A _lot_ of the 'library functions', are really '#define' macro operations, rather than true 'functions'. It is a pity that this is not better documented, but it has been reported here in other threads before, where people have trouble using them like this...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
dbotkin
Joined: 08 Sep 2003 Posts: 197 Location: Omaha NE USA
|
|
Posted: Thu Oct 23, 2008 12:21 pm |
|
|
Interesting. I go looking for another answer, find one of my own REALLY OLD threads, and notice it's been recently resurrected. :)
OK... now does anyone have a way to pass a variable fname to printf() - OR call printf() either with or without an fname? I have a pretty big function that currently does a bunch of printfs to a function. I need to replicate that functionality with everything being sent to a serial port. I'd rather re-use the existing code, and either use printf() with no fname, or with a different fname depending on where the output needs to go.
Clear as mud? Anyone got a solution? I know I can use an if/else to select one of two printf() calls to use, but that will burn a whole lot more code space. |
|
![](templates/subSilver/images/spacer.gif) |
frequentguest Guest
|
|
Posted: Thu Oct 23, 2008 12:48 pm |
|
|
How about having a global flag indicating which device to output to and only have two printf() statements in your fname() function instead of two printf() statements in every place that you need an output? |
|
![](templates/subSilver/images/spacer.gif) |
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Thu Oct 23, 2008 2:03 pm |
|
|
dbotkin wrote: | Clear as mud? |
Are you, effectively, suggesting to replicate the printf() ?
The ANSI C source code for it should be out there. _________________ Read the label, before opening a can of worms. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 23, 2008 2:08 pm |
|
|
CCS doesn't compile a run-time module for the printf() function.
The printf line is parsed at compile time and only the needed code is
compiled into the HEX file. For example, you can't have a variable format
string in CCS. There is no run-time module to interpret it. The reason:
PICs traditionally (16F-series) have a limited program space. |
|
![](templates/subSilver/images/spacer.gif) |
dbotkin
Joined: 08 Sep 2003 Posts: 197 Location: Omaha NE USA
|
|
Posted: Thu Oct 23, 2008 2:31 pm |
|
|
frequentguest wrote: | How about having a global flag indicating which device to output to and only have two printf() statements in your fname() function instead of two printf() statements in every place that you need an output? |
That's the route I'm taking. It turns out to be only a few places that way, not as many as I thought.
Quote: | CCS doesn't compile a run-time module for the printf() function.
The printf line is parsed at compile time and only the needed code is
compiled into the HEX file. For example, you can't have a variable format
string in CCS. There is no run-time module to interpret it. The reason:
PICs traditionally (16F-series) have a limited program space. |
Yeah, I know... I was hoping there was a way around it. Sigh... oh well, there's always the 18F45K20. Thanks, though. |
|
![](templates/subSilver/images/spacer.gif) |
|