Not performing multiplication and division operation with decimal number in PIC

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

  1. ecaits

    Thread Starter Member

    Jan 6, 2014
    52
    0
    Dear All,

    I cannot perform multiplication and division with integer number in pic 16f877 using hi-tech c compiler.


    What is reason behind that???
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,368
    A number of reasons.

    Give us some examples of numbers and the results.
    Show us your code.

    How did you declare your variables, i.e. how many bits did you assign to the variable?
     
    Anirban Raha likes this.
  3. ecaits

    Thread Starter Member

    Jan 6, 2014
    52
    0
    Plz find code


    #include<pic.h>
    #include<htc.h>
    #include<stdlib.h>
    #include<math.h>

    void main()
    {

    int i=12,d=15,t=0,k=0,b=0,m=0;
    float j=0;

    t=i;
    b=(i*2);
    k=i/3;
    j=i;
    m=i+d;

    }
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    And you verified the answers how?

    Since nothing is actually done with these numbers, the compiler may optimize them out - putting volatile ints and floats tells the compiler not to remove them.

    Also, your code should have an infinite loop to prevent the controller from getting into undefined operations (typically done with a while (1){} loop).

    Also, use code tags when posting code (button that looks like "#", which will put your code between two tags: [CODE ] and [/CODE ] (use without spaces in the tags)).
     
  5. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    That's exactly what's happening!!! Testing your code and making the variables volatile you get to see it working!!

    Code ( (Unknown Language)):
    1.  
    2. #include<xc.h>  // I use XC8.
    3.  
    4. #include<stdlib.h>
    5. #include<math.h>
    6.  
    7. void main()
    8.     {
    9.     volatile int i=12,d=15,t=0,k=0,b=0,m=0;
    10.     volatile float j=0;
    11.  
    12.     t=i;
    13.     b=(i*2);
    14.     k=i/3;
    15.     j=i;
    16.     m=i+d;
    17.  
    18.     }
    19.  
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Setting the Hi-Tech C or XC8 compiler in free mode(or lite mode) also works. If you just want to single step your code and see the result while debugging
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,017

    Not so sure. I have seen the free XC8 still do some crazy things in debug which I can only guess is due to optimization.
     
Loading...