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

String control needed

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



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

String control needed
PostPosted: Sat Oct 31, 2009 6:23 am     Reply with quote

Hi All,

I am using a 18F452 with compiler V 4.073. I need to send a string of characters out the usart to a speech module (V8601) without over running its input buffer. There is a control pin available on the speech module and I can wire it to any port pin (RB0 as an example). The pin is active low. Is there any controls in printf or in putc? In short all I need to do is test the pin before sending each character in the string. But that requires string management and I don't know how to do that in C.

I could easily do this in asm, but would rather stay in C as I am trying to learn C.
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sat Oct 31, 2009 1:16 pm     Reply with quote

Just a thought. printf allows printf to a function ex printf(mycode,"hello");
Now the CCS compiler has a specific feature to deal with this. The compiler calls mycode ( or the specific name you use) for each and every char it wants to print. It is if it does the printf to your function char by char. Now in your function you can test the possiblity that your receiver is ready and do a putc if it is. Now if it isn't you can hold in a loop until it is. Now this will be blocking on your code if the receiver goes off line but you could create a timer interrupt to break the blocking loop or go out with the watchdog timer.
frierray



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

PostPosted: Sun Nov 01, 2009 7:12 am     Reply with quote

Hi Douglas,

Thanks for your help with this. If I could get this working is should do just what I need. However I must have made some kind of simple coding error. It all works, but the message does not print. We I run the code (see below) it toggles the pin OK. The pin toggle is just a visual indicator during the testing. I'll convert it to a input test later. My code looks like this:
Code:

#include <18f452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void my_code(void)                           //
   {
   output_toggle(PIN_B4);                    // Show change of pin
   delay_ms(50);                             // kill some time
   }
   
// start of code
void main (void)
{
   printf(my_code, "\n\rprint this msg\n\r");   // my message
   //
   printf("\n\rEnd of Test\n\r");               // show end if test
   
   while(1) {};                            // Loop here
}

When I run it, RB4 toggles for each character in the string. However "print this msg" is not sent out.

Any help would be much appreciated.

Ray
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Nov 01, 2009 8:35 am     Reply with quote

Obviously, my_code() must have a char argument and send the data ...

Code:
void my_code(char c) //
{
int try = 50;
while (input(PIN_B0))
{
  delay_ms(1);
  if (!try--) return;
}
putc(c);
}
frierray



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

PostPosted: Mon Nov 02, 2009 1:52 pm     Reply with quote

That's very helpful. However it was not obvious. I'm new to C programming and could not find any info on how this would work. My assumption was that printf would still be responsible for sending the characters. It now looks like printf is only responsible for the string management and putc is doing the output.

Thanks, Ray
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Nov 02, 2009 2:03 pm     Reply with quote

Quote:
However it was not obvious.
Not particularly.

The manual isn't verbose in this regard, although it mentions the concept
Quote:
printf (fname, cstring, values...)
fname is a function name to be used for outputting (default is putc is none is specified).
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Mon Nov 02, 2009 3:30 pm     Reply with quote

A really good example of this is in the LCD drivers that come with PIC-C.


printf(lcd_putc, "hello");

allows printf to call a routine called lcd_putc() which DOES know how to send a char to an LCD while printf() has no idea.


You could model lcd_putc after what YOU need to send data to your text->speech chip and now everything printf() is available to you.


-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
frierray



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

PostPosted: Fri Nov 06, 2009 2:26 pm     Reply with quote

Thanks, it worked great!
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