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 CCS Technical Support

multiplication a floating point number to a hex number

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



Joined: 05 Dec 2004
Posts: 31

View user's profile Send private message

multiplication a floating point number to a hex number
PostPosted: Wed Dec 07, 2005 1:32 am     Reply with quote

I need help of using pic16f877 to multiply hex number e.g. 0x34 to a
floating point number like 61x10-6(0.000061) and display the result to
two to three variables,each are 8 bits,anyone knows the c source ??????
Ttelmah
Guest







PostPosted: Wed Dec 07, 2005 3:21 am     Reply with quote

I think you need to understand numbers. Inside a computer, there is no such thing as either a fp number, or a hex number. Everything is binary. Hex, is just a way of 'representing' these values, and fp, is another (more complex) way of using these values to cover a wider 'range' of values.
Now if you code:
Code:


int8 x=0x34;
float y=6.1E-5;
float z;

z=y*(float)x;

'z', will now be the fp representation of 0.003172. In 'hex', it should be 0x764FE154. If you wanted 3 hex digits (003), then use sprintf, to output it as:
Code:

char out[5];

sprintf(out,"%03lx",(int16)(z*1000.0));

Which will place the digits '0', '0', '3', into the array 'out'. The digits beng placed are 'hex', in that if you had a result that was 0.011, the output would be 00B.
Now the point is that 'hex', is just a way of representing binary values. You could output the fp value in hex (will give 8 characters as shown above), or convert just part of the number, back to integr, and output this to hex 'text' output values (as shown using the printf), or just output the bytes themselves (four 8 bit characters). All can be represented in 'hex'. You need to work out what format you actually want the data in, then rephrase the question.

Best Wishes
lgeorge123



Joined: 05 Dec 2004
Posts: 31

View user's profile Send private message

multiplication a floating point number to a hex number
PostPosted: Thu Dec 08, 2005 8:51 pm     Reply with quote

Hi Ttelmah, actually I wish to use ADC to measure analog current across
a 1 ohm resistor and display the data to LCD. Suppose 0.61A drops in
1 ohm resistor would produce 0.61V, according to ADC datasheet, 0.61V result in 0x27 in highbyte and 0x10 in low byte or 0x2710. Every 61x10-6
(0.000061v) output a 1bit, 0x2710 means 0x2710*0.000061 or 0.61V,so I knows that 0.61A has been measured, but the problem is how to multiply
two numbers and 'generate' a meaningful output , just like '0', '.' , '6',
'1' , could someone or you help me ????????
ttelmah
Guest







PostPosted: Fri Dec 09, 2005 5:13 am     Reply with quote

Read your two bytes into an int16 (use make16, to put the result into a single 16bit integer value). You now have a value of '10000' in this integer (0x2710, is just a way of representing this). Your conversion factor, is then 0.000061. So:
Code:

//In 'psuedo code'

float voltage;
int16 val;

//Read your adc value into 'val' here
val=get_adc_reading();

voltage=val*0.00061;

//Now voltage contains the fp representation of 0.610

//What you do now, depends on what you want.
//So, to display this:

printf("%5.3g",voltage)

//This will output 0.610 - adjust format as needed
//The 'justification', is faulty on older compilers, and the latest
//few versions have developed fp bugs, so you want something
//like 3.236


Best Wishes
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