View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
PrintF issues - SOLVED |
Posted: Tue May 11, 2010 6:14 am |
|
|
Hello,
I can't get this to print properly.
First print statement prints 1023(@5v) as expected and pads with ceros
so it goes 0000 to 1023.
The second print statement fails miserably.
The result of my multiplication (3FF*1E8)is 79E18. When I try to print as a hex number it only prints 9E18. My volts variable is a int32 type.
Why is it chopping off the 7 ?
My expected result is "Volts = 499224".
volts is int32
ADC_value is int16, the multiplier constant is also 16.
Code: |
while(1)
{
set_adc_channel(0);
delay_ms(300);
ADC_value=read_adc();
volts=(ADC_value*0X01E8);
printf("A/D value = %04Lu\n\r", ADC_value);
printf("Volts = %06Lu\n\r", volts);
}
|
Thanks.
Sorry for the dumb question.
g _________________ CCS PCM 5.078 & CCS PCH 5.093
Last edited by Gabriel on Tue May 11, 2010 8:02 am; edited 1 time in total |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue May 11, 2010 6:55 am |
|
|
As mentioned in many places in this forum :-
volts=(ADC_value*0X01E8);
ADC_value is a 16 bit value.
0x01E8 is alos a 16 bit value.
the compiler will use 16 bit maths on the equation (ADC_vallue * 0x01E8) and the result will be 0x9E18.
type cast the variable ADC_value to force 32bit math.
volts=((int32)ADC_value*0X01E8);
your code does not have a printf statement that outputs the hex value! |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
SOLVED |
Posted: Tue May 11, 2010 8:01 am |
|
|
thanks you so much.... that solved it. i really had no idea.
Quote: | your code does not have a printf statement that outputs the hex value! |
i know, i just said that when i *try* (meant to say tried)....refering to previous attempts.
.... thank you so much.
g _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Wed May 12, 2010 7:12 am |
|
|
CCS provide a slightly more efficient way of doing that: Code: | volts = _mul(ADC_value, 0x01E8); |
_________________ Andrew |
|
|
|