|
|
View previous topic :: View next topic |
Author |
Message |
Pilt1 Guest
|
String Routine |
Posted: Wed May 24, 2006 3:34 pm |
|
|
I wish to pass a string to a function and send each character seperatly
This is my function at the moment
Code: |
void send_string(char string[10]) //need to use pointers
{
int8 i;
for(i=0;string[i] != '/n' ;i++)
{
send(string[i]);
}
|
I wish to call the function like this
Code: |
send_string("Hello");
|
As you can see i wish to send the first character first and stop after the last character. That why i used the /n character to represent the character after last although i dont think it is correct?
How can i do this?
Thanks
Pilt |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 24, 2006 3:45 pm |
|
|
You can't pass a pointer to a constant string in CCS. You have to
copy the string to a RAM array first, and then pass the address of
the array. Use the CCS function strcpy() to do this.
But maybe you don't need to do that. It seems that you want to
send characters to a special function that will transmit them.
CCS has a special feature of the printf() function, where the output
of printf is sent to a specified function, one character at a time.
In the example program below, the output of printf() is sent to
the send_char() function. (I have renamed your "send_string()"
function to "send_char").
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)
//-------------------------
send(char c)
{
putc(c);
}
//-------------------------
void send_char(char c)
{
send(c);
}
//====================================
void main()
{
printf(send_char, "Hello");
while(1);
} |
|
|
|
pilt1 Guest
|
|
Posted: Wed May 24, 2006 3:58 pm |
|
|
Yeah sorry was ment to delete the comments regarding pointers.
The reason i am doing it using this method is because i am passing the value to a 8 bit wide port on the uP which connects to another chip (Usb Chip)
The send method places the 8 bit wide data on the port and triggers another ouptut to tell the other chip to accept the data. Therefore to send a string i pass each byte using the send(byte); function.
Thanks
Pilt |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 24, 2006 4:17 pm |
|
|
In that case, the program below will show how to do 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)
#define STROBE PIN_C0
//-------------------------
send(char c)
{
output_d(c); // Write char to port D
delay_us(1);
output_high(STROBE); // Create short pulse
delay_us(1);
output_low(STROBE);
}
//====================================
void main()
{
printf(send, "Hello");
while(1);
} |
|
|
|
pilt1 Guest
|
|
Posted: Thu May 25, 2006 12:39 pm |
|
|
Thanks that is exactally what i needed!
Cheers
Pilt |
|
|
|
|
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
|