Problem of variable format reading in MPLAB

Discussion in 'Embedded Systems and Microcontrollers' started by ecaits, Feb 21, 2014.

  1. ecaits

    Thread Starter Member

    Jan 6, 2014
    52
    0
    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???
     
  2. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    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!!
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Take a look here for the definition of what an integer is http://en.wikipedia.org/wiki/Integer The division 200/6.8 can not be done as is working with integers. You must also remember that your PIC work with binary numbers. only and not decimal numbers.
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,362
    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.
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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).
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    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.
     
  7. ecaits

    Thread Starter Member

    Jan 6, 2014
    52
    0
    But I cannot perform multiplication and division operation like decimal 200/3 or 300*4 .... why???
     
  8. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    Post your code!!! There is no reason why you cannot...
     
  9. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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 ;))
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    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.
     
Loading...