Split from: Is there a way to use rprintf to print a 16-bit decimal ?

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My code is as follows:

 

#define min_count   64985

unsigned int motor_position;

 

rprintf("\n\rmin_count is %d\n\r", min_count);
rprintf("min_count is %x\n\r", min_count);

 

motor_position = 32768;
rprintf("\n\rmotor position is %d\n\r", motor_position);
rprintf("rmotor position is %x\n\r", motor_position);

 

motor_position = 32765;
rprintf("\n\rmotor position is %d\n\r", motor_position);

 

motor_position = 30000;
rprintf("\n\rmotor position is %d\n\r", motor_position);

 

   The results are:

 

min_count is -551
min_count is FDD9

 

motor position is -32768
motor position is 8000

 

motor position is 32765

 

motor position is 30000

 

I tried this code on my PC with lcc-win32 and it produced the
correct answers - no negative answers. It doesn't seem like
the "rprintf" see the 16 bit number as a unsigned number and
prints it like a signed number.

Any help would be appreciated. Thanks.

Last Edited: Sat. Apr 16, 2016 - 08:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

%u not %d for unsigned int! 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And the reason it's not happening on the PC is because there an int is 32 bits wide.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Sat. Apr 16, 2016 - 06:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

see stdint.h

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Per the above problem:

 

%u not %d for unsigned int!:
%u is an undefined format for "rprintf". What gets printed out is just a "u" for the data.

 

And the reason it's not happening on the PC is because there an int is 32 bits wide.:
AVR Studio V4.19 has an int at 2 bytes and we are talking unsigned numbers so that
shouldn't matter.

 

see stdint.h:
All that file is, is type defines which redefines what I did and should have no effect
on my answers. My data types are not the perferred types but they are still legal.

 

I submit that "rprintf" as used in my test cannot handle 5 digit decimal numbers. Hex
numbers print out fine per my post. If anyone would put a wrapper around my exact code
to verify my answers and if correct, suggest a new approach to this problem, I would be
thankful. I guess why I have never noticed this problem before was I never printed out
large decimal numbers and usually use hex in my programs.

Thanks again for all your help.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use printf()

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

quick look :

A simple printf routine. Called by rprintf() - does a simple printf (supports d, x, c). Supports:

  • d - decimal
  • x - hex
  • c - character

Definition at line 320 of file rprintf.c.

So if you can't live with the overhead of printf you will need to make a small function that will do the job, there have been many threads about that in the past.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does the library have a puts()? If so then either utoa() or sprintf() then puts()