|
|
View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 27, 2005 9:41 pm |
|
|
There are several things about your program that could be improved.
For example, you're not using #INT_RDA in the proper way. There is
also no reason to use interrupts in your current program.
To answer your question about strtok():
The CCS manual has a good example of how use strtok().
I made it into a test program, as shown below, and it
displays the following output:
Whenever you have a coding problem you should always make a
short test program that only tests that problem.
When you make a short program, it's much easier to study the
problem, and to solve it.
Code: | #include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <string.h>
//--------------------------------------------
void main()
{
char *ptr;
char string[20];
char term[10];
strcpy(string, "X:10;Y:10;");
strcpy(term, ":;");
ptr = strtok(string, term);
while(ptr!=0)
{
puts(ptr);
ptr = strtok(0, term);
}
while(1); // Prevent the PIC from going to sleep
} |
|
|
|
Christian Guest
|
|
Posted: Sun Feb 27, 2005 10:01 pm |
|
|
thank you programmer for ur post, however i have used the tokenizer sucessful in a program similar to urs above, but i cant get it work whenever i'm trying to tokenize an input from a pc, i get garbage. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 27, 2005 11:41 pm |
|
|
OK, I took a closer look at your program in that area, and there are
several things wrong with it. The major thing is that your input
array is defined with a length = 1 byte. There's no room in it
to store your input string. You need to define a larger array.
There are a few other things wrong with your program.
You're using HS mode in your #fuses, but you have 40 MHz
specified in #use delay(). That's not right. You should
have NOLVP specified in your #fuses, unless you're using
low voltage programming (which is rare).
I think you have the demo. Maybe you're just using the MPLAB
simulator. Then these fuse settings don't matter. So I won't
say any more about them.
Here is a very simple test program which just gets a string
from your terminal program, and then displays it when
you press the Enter key on your PC.
Code: | #include <18F458.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000) // 4 MHz crystal
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS, stream=PC)
//===================================
void main()
{
char command_string[80]; // Allow enough space for a long string.
fgets(command_string, PC);
printf("Command string = %s \n\r", command_string);
while(1);
} |
|
|
|
Christian Guest
|
|
Posted: Sun Feb 27, 2005 11:53 pm |
|
|
hey programmer, not intending to be bashful but i have tried setting commandstring[30], but the code just wont work, and ur right i am using the hyperterminal, but i cant tokenize my inputs, does your code do this?
Thanks |
|
|
Christian Guest
|
|
Posted: Mon Feb 28, 2005 12:26 am |
|
|
problem solved
"You need to define a larger array"
thanks for that tip |
|
|
|
|
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
|