|
|
View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
Strange printf behavior |
Posted: Thu Jun 18, 2015 2:00 pm |
|
|
Hello guys.
PIC16F1508
Compiler version PCM: 5.025
IDE version: 5.025
This code:
Code: | #include <16F1508.h>
#FUSES INTRC_IO, NOWDT, PUT, NOMCLR, NOPROTECT, NOBROWNOUT, NOCLKOUT, NOIESO, NOFCMEN, NOWRT, NOLPBOR, NODEBUG, NOLVP
#use delay(internal = 16M)
#use rs232(baud=9600, UART1)
void main()
{
setup_oscillator(OSC_16MHZ);
printf("status=%u\n",14);
} | prints:
Now when I put delay after the printf
Code: | #include <16F1508.h>
#FUSES INTRC_IO, NOWDT, PUT, NOMCLR, NOPROTECT, NOBROWNOUT, NOCLKOUT, NOIESO, NOFCMEN, NOWRT, NOLPBOR, NODEBUG, NOLVP
#use delay(internal = 16M)
#use rs232(baud=9600, UART1)
void main()
{
setup_oscillator(OSC_16MHZ);
printf("status=%u\n",14);
delay_ms(no_matter_how_many);
} |
I get the correct printing:
The test environment is real hardware using FTDI chip and Hercules (serial terminal). Some ideas? _________________ A person who never made a mistake never tried anything new. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19587
|
|
Posted: Thu Jun 18, 2015 2:11 pm |
|
|
It's what you'd expect.....
You are letting the code 'drop off the end'. When this happens the processor goes to sleep. Any character in the UART that has not yet sent, will then not be sent.
You should never let the code drop off the end. There is no operating system to go 'back' to. Your code is the entire program, and should stay running:
Code: |
void main()
{
setup_oscillator(OSC_16MHZ);
printf("status=%u\n",14);
do ;
while(TRUE);
}
|
or something similar to stop the code falling off the end. |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Fri Jun 19, 2015 8:09 am |
|
|
Thank you Ttelmah for the reply.
I thought that the processor will make sure that the UART TX buffer is empty before go to sleep. Something like this pseudo code:
Code: | int putchar(int c)
{
UARTTXBUF = (char)c;
while(!(TXBUFFER_IS_EMTY_FLAG))
;
return c;
}
|
Can you please tell me how can I find the (CCS)definition of the putchar function? _________________ A person who never made a mistake never tried anything new. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19587
|
|
Posted: Fri Jun 19, 2015 8:14 am |
|
|
It works the other way round. It tests if the buffer is empty, and if it is, writes the character, and returns immediately. That way the code can be getting on with other things while the hardware handles sending the character. Waiting for the character to be sent, would mean that you were wasting the hardware buffer.... |
|
|
|
|
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
|