CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Strings and usb_cdc_putc()

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
lankybits



Joined: 12 Nov 2009
Posts: 2

View user's profile Send private message

Strings and usb_cdc_putc()
PostPosted: Thu Nov 12, 2009 4:15 pm     Reply with quote

Hi,
I'm new to CCS and PIC programming (but not to C programming) and was copying a co-worker's code for printing debugging strings in HyperTerminal over a USB link using usb_cdc_putc(). He is using the function to print a string (e.g., "usb_cdc_putc("Green LED is set here.\n\r");")

I want to print the value of a variable in my string, so I looked in usb_cdc.h to see if usb_cdc_putc() supports format specifiers (like %i) the way printf() does. I was very surprised to see that usb_cdc_putc() takes a character as its only argument... there is no mention anywhere of passing a string to it. I can't figure out how his code is working, and it's driving me crazy. Is it common usage to pass strings to usb_cdc_putc()? Is there a version that takes a string defined somewhere else, and I'm just looking at the wrong definition? I can't believe that the compiler doesn't even warn that there's a type mismatch, nevermind that it actually seems to work. If you can demystify this for me, I'd be grateful!

Thanks,
_________________
Anthony
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 12, 2009 4:35 pm     Reply with quote

See the CCS manual. It says:
Quote:

A (non-standard) feature has been added to the compiler to help get
around the problems created by the fact that pointers cannot be created
to constant strings. A function that has one CHAR parameter will accept a
constant string where it is called. The compiler will generate a loop that
will call the function once for each character in the string.


Also see the manual on printf:
Quote:

printf (fname, cstring, values...)

fname is a function name to be used for outputting
(default is putc if none is specified).


Look at examples in the various USB example files:
Quote:

printf(usb_cdc_putc, "\r\nNew value: ");
printf(usb_cdc_putc, "%2x ", read_eeprom( i*16+j ) );
lankybits



Joined: 12 Nov 2009
Posts: 2

View user's profile Send private message

PostPosted: Fri Nov 13, 2009 9:08 am     Reply with quote

Thanks for the response, PCM programmer. I think I understand the need for it now... the PIC uses Harvard architecture, so string constants are stored in instruction memory. It's a nice feature to have, it's just weird that this usage isn't mentioned in the function's comments or in its entry in the help file. Then again, it's not really a property of the function itself, it's a special enhancement of the compiler. Sounds like the manual is the best source of info, and I should probably get my hands on one.

Thanks again!
_________________
Anthony
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Tue Nov 23, 2010 2:37 pm     Reply with quote

On the same topic, I have been playing with the FLEX_LCD driver and in there, I notice the same basic thing ... lcd_putc("some_string");
The question I have (I have looked at the man page for the printf() - is this an implied version of the printf(lcd_putc("some_string") ??? I could not find that documented (I was searching for the same answer when I found this thread - how do they pass a string to function that says it expects a char). I'm happy to play by the rules, I just need to know what they are :-)

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Tue Nov 23, 2010 3:18 pm     Reply with quote

The 'rule' is in the manual section PCM programmer has posted.

_Any_ function that accepts a single char (or an int8), can be called with a constant string in CCS. What happens is the function is automatically called repeatedly with each character in turn. This applies to your functions, as well as CCS ones.

So:

Code:

void demo(char c) {
    static int8 ctr=1;
    Printf("Char %d is %c\n\r",ctr++,c);
}

main {
    demo("Demo string");
    while (TRUE) ;
}

Obviously this needs an RS232 setup to output the values.....

Best Wishes
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Tue Nov 23, 2010 3:28 pm     Reply with quote

Ah, thanks - I had somehow missed reading that part of PCM's post. I have the pdf version of the manual, but my hard copy should be arriving tomorrow where I can put tabs on it :-) Last time I used the CCS compiler was back in 2000 or so with version 2.xxx for a cub scout project. New features !! Looks like it is pg 50 in the pdf manual dated "June 2010"

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group