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 support@ccsinfo.com

Strange printf behavior

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

Strange printf behavior
PostPosted: Thu Jun 18, 2015 2:00 pm     Reply with quote

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:
Quote:
status=1

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:
Quote:
status=14

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: 19451

View user's profile Send private message

PostPosted: Thu Jun 18, 2015 2:11 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 19, 2015 8:09 am     Reply with quote

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: 19451

View user's profile Send private message

PostPosted: Fri Jun 19, 2015 8:14 am     Reply with quote

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....
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