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

Passing a string to a function
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
slandry



Joined: 12 Mar 2007
Posts: 7
Location: New Hampshire

View user's profile Send private message

Passing a string to a function
PostPosted: Thu May 31, 2007 1:30 pm     Reply with quote

Does anyone have a good way to pass a string to a function? In my application, there are many places where I want to display a message and then get a value from the operator. I would like to create a function that displays the text, inputs a response in ASCII and then returns the int. The call would look something like "i = GetInt("Enter Var 1");". I have no problem creating the function itself, but not being able to create a pointer to a constant (to pass the string) is making this difficult. There must be a clean way around it. BTW In section 7.4 of the PIC C intro book there is a perfect example of what I want to do, but it violates the pointer to constant limitation.

Thanks for any ideas! - Steve
kevcon



Joined: 21 Feb 2007
Posts: 142
Location: Michigan, USA

View user's profile Send private message

PostPosted: Thu May 31, 2007 1:59 pm     Reply with quote

What version are you using?

4.035 and up allow pointers to constants.
slandry



Joined: 12 Mar 2007
Posts: 7
Location: New Hampshire

View user's profile Send private message

PostPosted: Thu May 31, 2007 2:25 pm     Reply with quote

Thanks kevcon! you hit it on the head. I'm running 4.020 and I remember reading about the newer versions allowing pointers too. Guess I'm working too hard.

I'll give it a try and thanks again for the quick reply.

Best Regards,

- Steve
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 31, 2007 2:46 pm     Reply with quote

Quote:
4.035 and up allow pointers to constants.

Can you post a demo program to show how this works ?

Can you make some modifications to the test program
that I posted in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=27714&start=103
What do I have to do to make this test program work with
PCH vs. 4.039 ?
slandry



Joined: 12 Mar 2007
Posts: 7
Location: New Hampshire

View user's profile Send private message

PostPosted: Thu May 31, 2007 3:01 pm     Reply with quote

FYI, I'm now running 4.035 and I still get the same error message on the following line: "i = GetInt(" Enter Gain Value - ");" Things are never as simple as they seem ;-) - Steve
kevcon



Joined: 21 Feb 2007
Posts: 142
Location: Michigan, USA

View user's profile Send private message

PostPosted: Thu May 31, 2007 3:06 pm     Reply with quote

Go higher.

this works in 4.038

Code:

#include "18F65J10.h"
#include "string.h"

#case

#fuses DEBUG         //Enable Dedicated Debugger Pins
#fuses NOWDT         //Disable Watchdog Timer
#fuses STVREN         //Enable Stack Over/Underflow Reset
#fuses NOXINST         //Disable Extended Instructions

#fuses NOPROTECT      //Disable Code Protection

#fuses NOIESO         //Disable Two Speed Start-up
#fuses FCMEN         //Enable Fail Safe Clock Monitor
#fuses PRIMARY         //Set Defaul Clock as PRIMARY
#fuses H4_SW         //High Speed Crystal and PLL

#fuses WDT32768         //Set WDT Postscaler to MAX

#fuses CCP2C1         //ECCP2/P2A is Multiplexed with RC1

#use delay( clock = 29,491,200, crystal = 7,372,800 )
#use rs232( stream = UART1, baud = 115,200, UART1, parity = N, BITS = 8 )


const unsigned int8 romString[ 20 ] = { "Hello World" };
unsigned int8 ramString[ 20 ];


void main( void )
{
   fprintf( UART1, "\n\r" );
   fprintf( UART1, romString );

   strcpy( ramString, romString );

   fprintf( UART1, "\n\r" );
   fprintf( UART1, ramString );

   delay_ms( 2000 );
}


SIM Uart1 output

Quote:

Hello World

Hello World
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 31, 2007 3:23 pm     Reply with quote

That method will work in vs. 3.249.

I want to see how vs. 4.039 can create a pointer to a constant string
stored in ROM and pass this pointer as a parameter to a function,
just as if it were a pointer to a RAM array. I'm not directing this
criticism at you. Until I see this in a demo program, I don't think
it's working. I mean full-blown pointers to constant strings, that I
can use just like a RAM pointer.
kevcon



Joined: 21 Feb 2007
Posts: 142
Location: Michigan, USA

View user's profile Send private message

PostPosted: Thu May 31, 2007 3:54 pm     Reply with quote

Ok, don't I look stupid with this egg on my face. Very Happy

Seems I have been misled again by CCS saying something works when it doesn't.

BTW PCM no offence taken, I will gladly admit when I'm wrong.

Kevin
slandry



Joined: 12 Mar 2007
Posts: 7
Location: New Hampshire

View user's profile Send private message

PostPosted: Fri Jun 01, 2007 6:23 am     Reply with quote

In the book "PIC C An introduction to programming the Microchip PIC in CCS C" which I purchased from CCS some time ago, there is an example in section 7.4, "Passing Pointers to Functions". I typed in the example below, the comments are mine:

Code:

#include <16C74.h>

void puts(char *p);

void main(void)
{
    puts("Microchip is great!");        // This line is where I get the error.
}

void puts(char *p)
{
    while(*p)
    {
        printg("%c", *p);
        p++;
    }
    printf("\n");
}


This is exactly what I would like to do, but add some code to the "puts()" function above to return an int from a keyboard entry. Like the old "input" statement from basic. One call and you send a prompt and get back the input.

Thanks to everyone that has contributed to this thread. - Steve
kevcon



Joined: 21 Feb 2007
Posts: 142
Location: Michigan, USA

View user's profile Send private message

PostPosted: Fri Jun 01, 2007 6:56 am     Reply with quote

You can achieve the result you are looking for in a roundabout way.

Code:

#include "18F65J10.h"
#include "string.h"

#case

#fuses DEBUG         //Enable Dedicated Debugger Pins
#fuses NOWDT         //Disable Watchdog Timer
#fuses STVREN         //Enable Stack Over/Underflow Reset
#fuses NOXINST         //Disable Extended Instructions

#fuses NOPROTECT      //Disable Code Protection

#fuses NOIESO         //Disable Two Speed Start-up
#fuses FCMEN         //Enable Fail Safe Clock Monitor
#fuses PRIMARY         //Set Defaul Clock as PRIMARY
#fuses H4_SW         //High Speed Crystal and PLL

#fuses WDT32768         //Set WDT Postscaler to MAX

#fuses CCP2C1         //ECCP2/P2A is Multiplexed with RC1

#use delay( clock = 29,491,200, crystal = 7,372,800 )
//#use delay( clock = 36,684,000, crystal = 9,216,000 )

#use rs232( stream = UART1, baud = 115,200, UART1, parity = N, BITS = 8 )

void display_string(char *ptr);

const unsigned int8 buffer[ 2 ][ * ] = { "Hello World\n", "Goodbye World\n" };
unsigned int8 ramBuffer[ 32 ];


void main( void )
{

   strcpy( ramBuffer, buffer[ 0 ] );
   display_string( ramBuffer );

   strcpy( ramBuffer, buffer[ 1 ] );
   display_string( ramBuffer );


   while( 1 );
}

void display_string(unsigned int8 *ptr)
{
   unsigned int8 c;

   while( c = *ptr++ )
      putc( c );
}



Sim window output
Quote:

Hello World
Goodbye World
Ttelmah
Guest







PostPosted: Fri Jun 01, 2007 7:33 am     Reply with quote

It actually works quite well now!.

'delay_cycles' calls added to allow debugging when I was testing.
Code:

//Add processor, clock, and RS232 definitions here

const char cstr[] = "test string";
char space[20];
char *ptr;

void main( void ) {
  sprintf(space,"%s",cstr); //can I treat a constant string as normal?.
  delay_cycles(1);
  ptr=cstr; //works OK.
  delay_cycles(1);
  sprintf(space,"%s",ptr); //Works OK. Interesting. The pointer
  //here, only holds '14', yet the system correctly pulls the data from the
  //array in ROM!... space contains 'test string'.
  delay_cycles(1);
  sprintf(space,"%s","test constant");
  //OK 'space', again contains 'test constant'.
  puts(cstr);
  //Ok outputs 'test string'
  delay_cycles(1);
  puts(ptr);
  //Again works.
  delay_cycles(1);
  puts("test embedded constant");
  //Yahoo. Also works. Outputs 'test embedded constant'.
 
  while (true) ;
}

Much to my suprise, it does now work. Not as documented (you can't declare the pointer as 'const'), and it handles being aware of the 'type' internally.

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 01, 2007 10:46 am     Reply with quote

Quote:
puts(cstr);
//Ok outputs 'test string'
delay_cycles(1);
puts(ptr);
//Again works.
delay_cycles(1);
puts("test embedded constant");

The 2nd puts() doesn't give any output when I compile the program
with PCH 4.039 for an 18F452 and run it in MPLAB simulator.
This is the output:
Quote:

test string

test embedded constant
slandry



Joined: 12 Mar 2007
Posts: 7
Location: New Hampshire

View user's profile Send private message

PostPosted: Fri Jun 01, 2007 11:44 am     Reply with quote

I just tested the "puts("test embedded constant");" statement as you did only on real hardware using a PIC18F4680 with v4.039 compiler and the string does go out the serial port. Puts() is a standard built-in function that passes each character in the constant string to the putc() function (also built-in).

I fear there may be some confusion because we have been using "puts()" as the test function name. Below, is more of what I'm trying to compile and maybe someone will see the error of my ways.

Code:

void test(char *p)
{
    while(*p)
    {
        printf("%c", *p);
        p++;
    }
    printf("\n");
}

test("Test the function");



I get the following error message on the last line above that calls the test() function:

: Attempt to create a pointer to a constant

- Steve
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 01, 2007 12:09 pm     Reply with quote

Email CCS support and ask them what is necessary to make that
routine compile and work, with vs. 4.039.
slandry



Joined: 12 Mar 2007
Posts: 7
Location: New Hampshire

View user's profile Send private message

PostPosted: Fri Jun 01, 2007 12:33 pm     Reply with quote

Done, I'll post the reply when I get it.

Thanks
_________________
- Steve
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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