Jump to content

Recommended Posts

Hi

I am trying to print a rom string to the serial port, the strings are defined thus:

 

rom char *test_txt = "\n\rMatrix Controller Test mode\n\r";
rom char *txt1 = "Connect an 80R 10W resistor across pins 11 and 19\n\r";
rom char *txt2 = "Press Enter when ready\n\r";

 

i use the following call:

 

puts (test_txt);

 

where puts() is:

 

void puts(rom char* c)
{
char cc;
char i;
for( i = 0; cc = c[ i ]; i++ )
putc( cc );
}

 

void putc(char c)
{
txsta.TXEN = 1;
txreg = c;
while (txsta.TRMT == 0) clear_wdt();
}

 

But all of the strings are printed one after the other and then looping to the start again until the 255 char limit is reached, it appears that the routine isn't detecting the end of string \0 - i also tried putting a \0 at the end os each string, I have checked the prog mem and each rom strings ends in a retlw 0

 

Can anyone tell me what I'm doing wrong, i'm using a pic16F887 processor, the above code works fine on a pic 18F25 series chip

btw im using v7.20, mplab 8.92, win 7

regards

rob carter

Share this post


Link to post
Share on other sites

Hi Rob,

 

Is your problem really sorted ?

 

You have no expression to test for the terminating null and are assuming the compiler will test the assigned value for zero or non-zero.

 

For a simple char or integer the compiler may (or may not) throw in a test and set the Z flag accordingly.

 

It is better you tell the compiler exactly what you want to do such as:

for (char i = 0; (cc = array[i]) != '\0'; ++i)

Here you are telling the compiler (and reader) to perform the for loop statements if cc is not equal to end of string null.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites

Hi

 

thanks, I hadn't thought about adding in the extra check, I presumed that the expression cc = c[ i ] would return false when the \0 at the end of the string was encountered, but as you say it appears that the compiler doesn't always check for zero, which was what seemed to be happening with V7.2

 

Regards

Rob

Share this post


Link to post
Share on other sites

Hi Ted,

 

It may well be that condition testing is not required for simple data types but should you rely on it for all compilers.

 

The condition part should be evaluated and not assumed the compilers has done some behind the scenes testing for a zero value.

 

If the assignment had been a float or an object, what result would you get then ?

 

Be explicit of your intentions for the compiler, lint testing and the reader of your code.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites

Hi All,

 

Just to clarify, using an assignment as a condition test is valid ("Assignment used as truth value") but not obvious that a test for zero is performed.

 

The compiler maybe should have squeaked out a warning of "possible unintended assignment" or something.

Who hasn't typed in a single = when they meant a double == ?...... Oooooook, just me then :)

 

gcc issues such a warning recommending parenthesis around the assignment, just to confirm that was your intention.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...