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

Print formating changes value PCW v. 3.219

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








Print formating changes value PCW v. 3.219
PostPosted: Fri Nov 11, 2005 3:41 pm     Reply with quote

I've got a simple program that times the interval between when a button is pushed and when it is released using an ISR on Timer_1. The program looks like it works as it sits. When I try to format the output of the printf statement at the end of the program the value that I am formating changes drastically.

The line as it works.
Code:

   printf("Time is %lu.%lu seconds.\r\n",
           time/5000000,(time%5000000));


And the line with formating and wrong values.
Code:

   printf("Time is %lu.%06u seconds.\r\n",
           time/5000000,(time%5000000));


Code:
#include <prototype.h>

int16 overflow_count;

#int_timer1
timer1_isr()
{
 overflow_count++;
}

main()
{
 int32 time;
 
 setup_timer_1(T1_INTERNAL |T1_DIV_BY_1);
 enable_interrupts(int_timer1);
  while(true)
  {
   enable_interrupts(global);
   while(input(PUSH_BUTTON));
   set_timer1(0);
   overflow_count=0;
   while(!input(PUSH_BUTTON));
   disable_interrupts(global);
   time=get_timer1();
   time+=((int32)overflow_count<<16);
   time-= 15;
   printf("Time is %lu.%lu seconds.\r\n",
           time/5000000,(time%5000000));
  }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 11, 2005 3:54 pm     Reply with quote

Show us what you expect to see, and show us what you're getting.

In other words, post an example program like the one below,
with some suitable value loaded into the time variable, and then
post what's displayed, and also, what you wish it would be.


Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use RS232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS) 

//=============================
void main()
{
int32 time;

time= 10000000;

printf("Time is %lu.%lu seconds.\r\n", time/5000000,(time%5000000));

printf("Time is %lu.%06u seconds.\r\n", time/5000000,(time%5000000));


while(1);
}


---------
Edited to put the NOLVP fuse back in. I had taken it out while
testing some 16F690 code.


Last edited by PCM programmer on Fri Nov 11, 2005 6:08 pm; edited 1 time in total
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

PostPosted: Fri Nov 11, 2005 5:51 pm     Reply with quote

I tried compiling your code with #use rs232(debugger) and #device ICD=TRUE statements and I now get an error message stating:

Could not start target. The target was not halted after reset. Check the target Oscillator and MCLR. MCLR goes from 5v to 0v when I press the RESET button and there are sine waves at both OSC pins.

I also get the error message with the program that worked previously.

Thanks....Harry
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 11, 2005 6:09 pm     Reply with quote

Harry, I don't know. Just start back with some known good hardware
and a known good development environment.
Ttelmah
Guest







PostPosted: Sat Nov 12, 2005 3:51 am     Reply with quote

The obvious fault in the 'wrong' line, is that it has the 'L' missing. Even though you specify 6 digits for the output, the compiler _still_ must be told to treat the value as a 'long'. As posted, it'll output the value of the low byte only.
Code:

printf("Time is %lu.%06lu seconds.\r\n",
           time/5000000,(time%5000000));


Best Wishes
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

PostPosted: Sat Nov 12, 2005 9:22 am     Reply with quote

Thanks guys! That solves 95% of my problem.

PCM, when I ran your suggested program I still didn't get the formating that I expected. The program as I ran it:
Code:
#include <16F877A.h>
#device ICD=TRUE
#fuses HS, NOLVP, NOWDT, PUT
#use delay (clock = 20000000)
#use rs232 (DEBUGGER)
//#use RS232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

void main()
{
int32 time;

time= 11000056;

printf("Time is %lu.%lu seconds.\r\n", time/5000000,(time%5000000));

printf("Time is %lu.%06lu seconds.\r\n", time/5000000,(time%5000000));


while(1);
}


Although both printf statement give the correct answer, Time is 1.1000056 seconds, I would have expected the second printf statement to show only 6 places behind the decimal point ie. 1.100005.

Thanks....Harry
Ttelmah
Guest







PostPosted: Sat Nov 12, 2005 9:34 am     Reply with quote

The problem is that you are thinking about the DP, the 'wrong' way.
Your number is an integer. It doesn't actually have a 'decimal point'. The %06 definition means 'print at least six digits'. Now the number contains 1000056 _integer_, which needs seven digits to show, hence this many digits get printed. If fact, if there is anything in the top digit at all, this will always be a problem. If you 'pre divided' 'time' by five, and then split using %1000000, which can only give a six digit result, things would work.
The other way to trim the number the way you expect, you would be to check for it exceeding 6 digits (if (val>=1000000)), and divide it by ten if it does.

Best Wishes
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

PostPosted: Sat Nov 12, 2005 12:20 pm     Reply with quote

Thanks for a great explanation....Harry
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