View previous topic :: View next topic |
Author |
Message |
frierray
Joined: 11 Nov 2008 Posts: 29
|
String control needed |
Posted: Sat Oct 31, 2009 6:23 am |
|
|
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
|
|
Posted: Sat Oct 31, 2009 1:16 pm |
|
|
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
|
|
Posted: Sun Nov 01, 2009 7:12 am |
|
|
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
|
|
Posted: Sun Nov 01, 2009 8:35 am |
|
|
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
|
|
Posted: Mon Nov 02, 2009 1:52 pm |
|
|
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
|
|
Posted: Mon Nov 02, 2009 2:03 pm |
|
|
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
|
|
Posted: Mon Nov 02, 2009 3:30 pm |
|
|
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
|
|
Posted: Fri Nov 06, 2009 2:26 pm |
|
|
Thanks, it worked great! |
|
|
|