CodeVision, printf(), and floats

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

Hello,
I am trying to implement a simple code in at Atmega16 where I read an analog value and output the voltage to the USART. I am programming through CodeVision.

Ex.
int word_value;
float volts;
word_value=read_adc(0); // this is word value from adc0
volts=0.0103*word_value-2.54;
printf("channel 1 = %.2f =",volts);

This doesn't work. In fact, I couldn't get any non-integer value to print. I can get this to work by using integer math, but I would rather not. Does anyone know why printf won't allow non-integer values? And does anyone know how to get around this?

thanks

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

Quote:

Does anyone know why printf won't allow non-integer values? And does anyone know how to get around this?

As with the other popular compilers for AVR >>micro<<controllers, several flavours of printf() in varying sizes are available. Since these are micros and a loaded printf() that handles all possible transformations can suck up half of a Mega88, these different versions are available.

Go to Project/Configure/C Compiler and pick a flavour that contains what you need.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Also, I think the format spec is %totalwidthincludingdecimal.numberoffracfiondigitsf

Imagecraft compiler user

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

Quote:

I think the format spec is ...

I >>know<< that the stated, documented CodeVision format string is
Quote:
%[flags][width][.precision][l]type_char

which quickly shows that all fields are optional except "type".

What purpose is even making your post, Bob, if you "think" there might be a problem with the OP's code? Is that going to help to OP to go searching for red herrings? I realize that this may not make your "three popular" but there are a lot of places to look up printf() format strings.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Quote:

I think the format spec is %totalwidthincludingdecimal.numberoffracfiondigitsf

Yes, but IIRC the width can be left out in which case printf will use just enough positions to show the number with the reqested decimals. Eg

printf( "%.2f", 3.141592654);

will yield "3.14".

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]

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

I'll be darned. All these years I just figgered it worked like fortran. Never knew the width was optional till today. Is this a great board or what?

Imagecraft compiler user

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

If folks aren't moved to have a flick through K&R from time to time perhaps this two page summary of the C language might prove useful (if I ever use scanf() I always end up looking at this!)

Attachment(s):