Hallo everybody,
got myself another problem.... not exactly a problem... i think The thing is i would like to know whats happening in this output:
printf("AIN[%d][%d] = %u.%.3u V\n",m,n,(int)(AD_ARRAY[m][n]/V_SCALE),(int)(AD_ARRAY[m][n]%V_SCALE)*R_SCALE);
The preprocessor declarations are :
ine V_REF 5 #define V_SCALE (0xFF/V_REF) #define R_SCALE (99/V_REF)
I am using a 80C535 µC and the AD_ARRAY[m][n] saves the actual ADC value of the sample and im using µVision2
Thanks for the help Andy
Do you understand how printf works?
That line of code is pretty hard to read, being all crammed together with no white space - you could make your life a lot easier by improving the laytout; eg,
printf( "AIN[%d][%d] = %u.%.3u V\n", // Format string m, // 1st value n, // 2nd value (int)(AD_ARRAY[m][n]/V_SCALE), // 3rd value (int)(AD_ARRAY[m][n]%V_SCALE)*R_SCALE // 4th value );
It would also be easier to read if you calculated the 3rd & 4th values separately, rather than cramming inline expressions into the printf call. With modern optimising compilers, it is unlikely that this would actually add to the code.
It is a false assumption that reducing the number of source lines (eg, by cramming as much into each line as possible) will reduce the size of the generated code!
Thaks for the advice but it is not my code... I would like to know what tis code does. i know it prints something out but how it is done is my delirium What is this for example:
%u.%.3u V
????
Andy
Normally, you would emit the measurement as a floating point value. However, embedded chips normally don't like floating point numbers.
So, it is first calculating and emitting the integer part - the digits before the decimal point. Then it is removing the integer part by using the modulo operator % and multiplying the remaining value (which represents the fractional part) and emits that as a second number.
Hi,
Sometimes I forget the format identifier.
I use this page to remember / learn how they are used.
www.phim.unibe.ch/.../format.html
Ed
You'd be better to refer to the Manual for the specific compiler that you're using - in this case, http://www.keil.com/support/man/docs/c51/c51_printf.htm
Yes, you are right.
I was very sure that was formatting was ANSI-C but I was just wishfully thinking.
Must think compiler specific. You are right on the spot.
Ed.