|
|
View previous topic :: View next topic |
Author |
Message |
thomas Guest
|
Problem with printf(); |
Posted: Tue Jul 15, 2003 1:47 am |
|
|
Hello,
I have a problem with printf() function:
float VBAT;
int VADC1 = 0xff;
VBAT = VADC1*5/255;
printf(lcd_putc,"\fVBAT: \%1.2f",VBAT);
I expect the display to show "VBAT: 5.00", but it shows "VBAT: 0.00". Do you know what the problem is?
Regards,
THomas
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515977 |
|
|
SebDey Guest
|
Re: Problem with printf(); |
Posted: Tue Jul 15, 2003 1:53 am |
|
|
:=Hello,
:=I have a problem with printf() function:
:=
:=float VBAT;
:=int VADC1 = 0xff;
:=VBAT = VADC1*5/255;
:=printf(lcd_putc,"\fVBAT: \%1.2f",VBAT);
:=
:=I expect the display to show "VBAT: 5.00", but it shows "VBAT: 0.00". Do you know what the problem is?
:=
:=Regards,
:=THomas
Hi,
I suspect the problem to be a in the conversion between data formats.
When you write VBAT = VADC1*5/255;, the program will evaluate 5/255 as an integer (like VADC1 in fact), returning a value of 0, i think that writing VBAT= float(5*VADC1/255); will solve the problem.
Hope this helps and that I'm not completely wrong...
SebD
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515978 |
|
|
thomas Guest
|
Re: Problem with printf(); |
Posted: Tue Jul 15, 2003 2:21 am |
|
|
Thank you for your help. Writing the way you suggested below return this error message "A numeric expression must appear here" :(
Thomas
:=Hi,
:=I suspect the problem to be a in the conversion between data formats.
:=When you write VBAT = VADC1*5/255;, the program will evaluate 5/255 as an integer (like VADC1 in fact), returning a value of 0, i think that writing VBAT= float(5*VADC1/255); will solve the problem.
:=
:=Hope this helps and that I'm not completely wrong...
:=
:=SebD
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515980 |
|
|
R.J.Hamlett Guest
|
Re: Problem with printf(); |
Posted: Tue Jul 15, 2003 2:27 am |
|
|
:=:=Hello,
:=:=I have a problem with printf() function:
:=:=
:=:=float VBAT;
:=:=int VADC1 = 0xff;
:=:=VBAT = VADC1*5/255;
:=:=printf(lcd_putc,"\fVBAT: \%1.2f",VBAT);
:=:=
:=:=I expect the display to show "VBAT: 5.00", but it shows "VBAT: 0.00". Do you know what the problem is?
:=:=
:=:=Regards,
:=:=THomas
:=
:=Hi,
:=I suspect the problem to be a in the conversion between data formats.
:=When you write VBAT = VADC1*5/255;, the program will evaluate 5/255 as an integer (like VADC1 in fact), returning a value of 0, i think that writing VBAT= float(5*VADC1/255); will solve the problem.
:=
:=Hope this helps and that I'm not completely wrong...
:=
:=SebD
You are right, but he may have to go a little 'further' than this.
Generally the compiler does pre-solve 'constant' terms, and in integer arithmetic 5/255=0. Hence the equation becomes the integer result of multiplying '0xFF', by 0 as you say...
However there is a second problem, that 'VADC1', is also an integer. Hence even with your 'rewrite', the term inside the brackets (which only contains 8 bit values), will not necessarily evaluate correctly. Your cast, occurs after the arithmetic, not before... :-(
To force the correct result, requires casting VADC1 to a float (forcing floating point arithmetic), and for simplicity, it is probably also better to 'force' the constant term to be a float as well.
Hence:
VBAT = VADC1*5.0/255.0
Should work (since now the constant term is forced to a float). However, the CCS compiler is notoriously a little 'week', at automatically casting number, and given that 5/255, is a constant:
VBAT = (float)VADC1*0.019608;
would be 'certain' (both solving the 5/255, storing it as a constant, and forcing casting of the incoming number as well!).
However I also slightly 'query' the arithmetic, and the possibility of improving the speed. Assuming the A-D, is returning an 8bit value, scaled over 5v, this normally corresponds not to '255=5', but '256=5', with the top value (256), never being returned. This depends on the design of the AD, but most common designs work this way. In either case, the speed can be significantly improved (floating point arithmetic is slow), by doing the maths as:
int16 VBAT;
int16 VADC1 = 0xff;
VBAT = (VADC1*251)>>7;
//Here 'VBAT', is now an integer, scaled 0-500 - use '250',
//if the AD, does scale as 0-256, with 256 never returned.
The 'integer' answer, will evaluate much faster, and the code will be smaller, and it is even possible to use 'sprintf', and add a decimal point to the output, to make the number 'resemble' a floating point value. It may be worth looking into this type of solution, if speed/space is important. :-)
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515981 |
|
|
R.J.Hamlett Guest
|
Re: Problem with printf(); |
Posted: Tue Jul 15, 2003 2:28 am |
|
|
:=Thank you for your help. Writing the way you suggested below return this error message "A numeric expression must appear here" <img src="http://www.ccsinfo.com/pix/forum/sad.gif" border="0">
:=Thomas
:=
:=:=Hi,
:=:=I suspect the problem to be a in the conversion between data formats.
:=:=When you write VBAT = VADC1*5/255;, the program will evaluate 5/255 as an integer (like VADC1 in fact), returning a value of 0, i think that writing VBAT= float(5*VADC1/255); will solve the problem.
:=:=
:=:=Hope this helps and that I'm not completely wrong...
:=:=
:=:=SebD
Yes, the 'cast', needs to be bracketted, so as given, the line is syntactically invalid...
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515982 |
|
|
SebDey Guest
|
Re: Problem with printf(); |
Posted: Tue Jul 15, 2003 2:38 am |
|
|
:=:=Thank you for your help. Writing the way you suggested below return this error message "A numeric expression must appear here" <img src="http://www.ccsinfo.com/pix/forum/sad.gif" border="0">
:=:=Thomas
:=:=
:=:=:=Hi,
:=:=:=I suspect the problem to be a in the conversion between data formats.
:=:=:=When you write VBAT = VADC1*5/255;, the program will evaluate 5/255 as an integer (like VADC1 in fact), returning a value of 0, i think that writing VBAT= float(5*VADC1/255); will solve the problem.
:=:=:=
:=:=:=Hope this helps and that I'm not completely wrong...
:=:=:=
:=:=:=SebD
:=Yes, the 'cast', needs to be bracketted, so as given, the line is syntactically invalid...
:=
:=Best Wishes
Thanks Hamlett,
My fault, correct syntax is (float)(VADC1)*5/255; , which works for me (well, it shows me 5.00...)
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515983 |
|
|
R.J.Hamlett Guest
|
Re: Problem with printf(); |
Posted: Tue Jul 15, 2003 3:29 am |
|
|
:=:=:=Thank you for your help. Writing the way you suggested below return this error message "A numeric expression must appear here" <img src="http://www.ccsinfo.com/pix/forum/sad.gif" border="0">
:=:=:=Thomas
:=:=:=
:=:=:=:=Hi,
:=:=:=:=I suspect the problem to be a in the conversion between data formats.
:=:=:=:=When you write VBAT = VADC1*5/255;, the program will evaluate 5/255 as an integer (like VADC1 in fact), returning a value of 0, i think that writing VBAT= float(5*VADC1/255); will solve the problem.
:=:=:=:=
:=:=:=:=Hope this helps and that I'm not completely wrong...
:=:=:=:=
:=:=:=:=SebD
:=:=Yes, the 'cast', needs to be bracketted, so as given, the line is syntactically invalid...
:=:=
:=:=Best Wishes
:=
:=Thanks Hamlett,
:=My fault, correct syntax is (float)(VADC1)*5/255; , which works for me (well, it shows me 5.00...)
Yes. with the brackets this way, you are forcing 'VADC1', up to a float, before the maths is done, which should give the right answer. :-)
The 'caveat' though, is that it will be relatively slow (floating point arithmetic...).
There is a sort of 'general problem', that runs in all groups like this. You (correctly) pointed out the 'core' of the problem (that it wasn't to do with printf, but the arithmetic before this), and realised that a 'cast' was required, and posted a 'schema' of what was needed. Most posters who had any experience of 'C', would at that point, have happily realised what was needed, and made the 'tweaks' themselves. Unfortunately, the range of questions, and people posting here, extends from those with a lot of experience, who occasionally run into problems with the CCS compiler, through to people who are really just learning 'C'. In the latter cases, the answers need to be detailed... This is why sometimes the threads can get very long, and the answers rather verbose. :-(
I decided to post a somewhat longer reply, pointing out why there might still be problems (the CCS compiler at times, does some very odd things when casting between types), and some routes that could be used to give faster/smaller arithmetic, since this is a very 'general' problem, with this type of arithmetic. :-)
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515985 |
|
|
thomas Guest
|
Re: Problem with printf(); |
Posted: Tue Jul 15, 2003 12:53 pm |
|
|
Thank you very much SebDey and R.J. for your help! I really appreciate it.
Best wishes and regards,
Thomas
:=:=:=:=Thank you for your help. Writing the way you suggested below return this error message "A numeric expression must appear here" <img src="http://www.ccsinfo.com/pix/forum/sad.gif" border="0">
:=:=:=:=Thomas
:=:=:=:=
:=:=:=:=:=Hi,
:=:=:=:=:=I suspect the problem to be a in the conversion between data formats.
:=:=:=:=:=When you write VBAT = VADC1*5/255;, the program will evaluate 5/255 as an integer (like VADC1 in fact), returning a value of 0, i think that writing VBAT= float(5*VADC1/255); will solve the problem.
:=:=:=:=:=
:=:=:=:=:=Hope this helps and that I'm not completely wrong...
:=:=:=:=:=
:=:=:=:=:=SebD
:=:=:=Yes, the 'cast', needs to be bracketted, so as given, the line is syntactically invalid...
:=:=:=
:=:=:=Best Wishes
:=:=
:=:=Thanks Hamlett,
:=:=My fault, correct syntax is (float)(VADC1)*5/255; , which works for me (well, it shows me 5.00...)
:=Yes. with the brackets this way, you are forcing 'VADC1', up to a float, before the maths is done, which should give the right answer. :-)
:=The 'caveat' though, is that it will be relatively slow (floating point arithmetic...).
:=There is a sort of 'general problem', that runs in all groups like this. You (correctly) pointed out the 'core' of the problem (that it wasn't to do with printf, but the arithmetic before this), and realised that a 'cast' was required, and posted a 'schema' of what was needed. Most posters who had any experience of 'C', would at that point, have happily realised what was needed, and made the 'tweaks' themselves. Unfortunately, the range of questions, and people posting here, extends from those with a lot of experience, who occasionally run into problems with the CCS compiler, through to people who are really just learning 'C'. In the latter cases, the answers need to be detailed... This is why sometimes the threads can get very long, and the answers rather verbose. :-(
:=I decided to post a somewhat longer reply, pointing out why there might still be problems (the CCS compiler at times, does some very odd things when casting between types), and some routes that could be used to give faster/smaller arithmetic, since this is a very 'general' problem, with this type of arithmetic. :-)
:=
:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516009 |
|
|
|
|
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
|