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

Total beginner - problem with printf missing characters

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
lindsay.wilson.88



Joined: 11 Sep 2024
Posts: 40

View user's profile Send private message

Total beginner - problem with printf missing characters
PostPosted: Wed Sep 11, 2024 12:34 pm     Reply with quote

Hello,

Been using mikroC and mikroBasic for ages, but now looking around at other compilers and trying out CCS. I'm stumped at the first hurdle! printf() seems to miss the last couple of characters of the string.

Here's my main.c:

Code:
#include <main.h>
void main()
{
   printf("Hello World");
   printf("Hello World");
}


and my main.h:

Code:
#include <16F887.h>
#device ADC=10
#use delay(crystal=18.432MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


I'm using a 16F887 with an FT232 USB-serial module. (I know all the hardware works fine because I've previously written code in mikroBasic for it.)

If I program and run that, it outputs "Hello WorldHelloWor" to the serial terminal. I.e. the last printf() statement appears to miss the final two characters. Some sort of buffer problem?

If I use puts() instead, then it works without any problems.

I'm sure there's something obvious I'm missing, but I'd apprecate any suggestions!

Thanks,
Lindsay
gaugeguy



Joined: 05 Apr 2011
Posts: 303

View user's profile Send private message

PostPosted: Wed Sep 11, 2024 12:42 pm     Reply with quote

You should never allow your program to reach the end of the main. Add a while(1); before the closing brace of main.

Code:

main()
{
   printf("Hello World");
   printf("Hello World");
   while(1);
}
jeremiah



Joined: 20 Jul 2010
Posts: 1354

View user's profile Send private message

Re: Total beginner - problem with printf missing characters
PostPosted: Wed Sep 11, 2024 6:11 pm     Reply with quote

lindsay.wilson.88 wrote:

If I use puts() instead, then it works without any problems.


gaugeguy already provided a solution, but wanted to address this part for your understanding.

the puts() function is much much quicker than printf (puts() doesn't have to parse a format string after all), so when you fall off the edge of the world (leave main), the puts is able to eek out the data before things go dark. The printf is just taking a big longer so it doesn't get to finish.
lindsay.wilson.88



Joined: 11 Sep 2024
Posts: 40

View user's profile Send private message

PostPosted: Wed Sep 11, 2024 7:40 pm     Reply with quote

Hi,

Many thanks to both of you for the tips - that did the trick, things are working great now. I guess I was still thinking in terms of a program on the computer which has a definite "end" ;-0

Really liking CCS compared with what I've been using and what I've tried.

Lindsay
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Thu Sep 12, 2024 1:35 am     Reply with quote

Worth just explaining what was happening. Very Happy

Historically a 'C' program was normally called from a command line of
something like Unix. When you dropped off the end, you returned to the
OS. Now on the PIC there is no OS to return to, so really the program
should be written to never drop off the end. CCS be default fills
after the code with a 'sleep' instruction. So if you let the code drop of
the end, this is what is hit. Now when the processor sleeps, normally the
UART stops working. There is one buffered character, and the one in the
actual transmit register. So two characters don't send.
Voila!...

I have to say 'well done'. You had clear code, which everyone could see
exactly what was happening, and posed the question simply.
lindsay.wilson.88



Joined: 11 Sep 2024
Posts: 40

View user's profile Send private message

PostPosted: Thu Sep 12, 2024 4:10 pm     Reply with quote

Haha thanks! And thanks for the explanation, that makes perfect sense now 👍👍
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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