CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PrintF issues - SOLVED

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PrintF issues - SOLVED
PostPosted: Tue May 11, 2010 6:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue May 11, 2010 6:55 am     Reply with quote

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

View user's profile Send private message

SOLVED
PostPosted: Tue May 11, 2010 8:01 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Wed May 12, 2010 7:12 am     Reply with quote

CCS provide a slightly more efficient way of doing that:
Code:
volts = _mul(ADC_value, 0x01E8);

_________________
Andrew
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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