View previous topic :: View next topic |
Author |
Message |
lindsay.wilson.88
Joined: 11 Sep 2024 Posts: 40
|
Total beginner - problem with printf missing characters |
Posted: Wed Sep 11, 2024 12:34 pm |
|
|
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
|
|
Posted: Wed Sep 11, 2024 12:42 pm |
|
|
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: 1345
|
Re: Total beginner - problem with printf missing characters |
Posted: Wed Sep 11, 2024 6:11 pm |
|
|
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
|
|
Posted: Wed Sep 11, 2024 7:40 pm |
|
|
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: 19495
|
|
Posted: Thu Sep 12, 2024 1:35 am |
|
|
Worth just explaining what was happening.
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
|
|
Posted: Thu Sep 12, 2024 4:10 pm |
|
|
Haha thanks! And thanks for the explanation, that makes perfect sense now 👍👍 |
|
|
|