Problem of variable format reading in MPLAB

Thread Starter

ecaits

Joined Jan 6, 2014
56
Hello Sirs,

I am working on PIC16F877 using hi-tech c compiler.

if i declare one integer variable say i = 200. then when i observe it in watch window of mplab then it shows equivalent hex value i.e. C8.

If I want to do division operation with i, say a = i/6.82; then it always take hex value in i but i want to do division operation with decimal only.

How i can solve this problem???
 

Ian Rogers

Joined Dec 12, 2012
1,136
In MPLAB debbugging window If you right click at the top of the window.. You can add a new column... Just add "decimal" and you will be able to see it!

On another note... is "a" a float!! as I'm not sure it shows floats!!
 

MrChips

Joined Oct 2, 2009
30,824
Binary, decimal and hexadecimal numbers are all the same as far as the PIC cares, i.e. they are all stored in binary.

There are only 10 types of people in the world, those who understand binary and those who don't.
 

tshuck

Joined Oct 18, 2012
3,534
[...]i want to do division operation with decimal only.
If you wasn't to do integer division, you need to divide by an integer.

If you want an integer result, then you can round the result (which will probably be a floating point number).
 

ErnieM

Joined Apr 24, 2011
8,377
When it comes times for me to perform operations such as:

a = i/6.82

I oft want to keep the math simple and just use integers, because here if i=1 then a = 0.1466... obviously a floating point number.

But every quantity has a finite number of significant digits. Say here you with to see 3 digits: so start with i * 1,000,000 and 6.82*100. For i=1 you then get:

1,000,000 / 682 = 1466

So now add 5 (to round the result), divide by 10:

(1466 + 5) / 10 = 147

Somewhere you need account for the number being scaled by 1,000,000/100/10 = 1,000, and how that happens depends on what you are doing with the result.
 

tshuck

Joined Oct 18, 2012
3,534
But I cannot perform multiplication and division operation like decimal 200/3 or 300*4 .... why???
Odds are that you are not paying attention to variable types - dividing an integer by a floating point number results in....(look at your compiler's manual for the answer ;))
 

ErnieM

Joined Apr 24, 2011
8,377
But I cannot perform multiplication and division operation like decimal 200/3 or 300*4 .... why???
I assume you mean the Watch Window is telling you that it's actually dividing 0xC8 by 0x3 and multiplying 0x12C by 0x4?

Wait a sec... I thing that means something. I think that is something important.
 
Top