1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

[MPLAB] Multiplication (*) doesn't work

Discussion in 'Microcontrollers' started by John27, Jul 27, 2014.

  1. John27

    John27 New Member

    Joined:
    Jul 27, 2014
    Messages:
    1
    Likes:
    0
    Hello everyone !

    MPLAB (8.9) makes me crazy and I would like to get your help in order to fix it.
    My program is very simple :

    Code (text):

    #include "33EP512MU810.h"
    #DEVICE ICD = true, ICD = 3

    #FUSES NOPROTECT, NOWRT, NOGSSK, NOAPLK, NOWDT, noPUT,HS, CKSFSM, PLLWAIT, NOIOL1WAY, noIESO, NOJTAG,debug

    void main(void){
    float z=5.0;
    z=z*10.0;
    }

    I use MPLAB SIM and in the end, the value of z is 100 instead of 50 (I monitored the value via the Watch)
    Why ? Can you help me ?

    Thanks
     
  2. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    It is generally undefined what happens with local variables after you leave "main".

    I would insert

    Code (C):
    while(1);
    at the end of the "main" and put a breakpoint in there.

    I would also make sure that z is used somewhere, because otherwise it doesn't matter what value gets assigned to it, so the compiler could have optimized your manipulations away.[/CODE]
     
  3. heydonms

    heydonms Member

    Joined:
    Jul 1, 2012
    Messages:
    100
    Likes:
    13
    An easy fix for this is to declare the variable volatile. On a PC you could just output it to the screen, but in a uC it can a bit harder to use a variable in a way that doesn't effect the operation of the code, but also can't be optimized out by the compiler. By marking it as volatile the compiler knows there is more to the variable than there appears and any code that references it will be run as written.
     

Share This Page