• AVR Freaks

Hot!Incorrect result with simple subtraction calculation

Author
bpowers
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2003/11/07 12:39:51
  • Status: offline
2020/06/10 12:05:18 (permalink)
0

Incorrect result with simple subtraction calculation

Using: MPLABX v5.40, XC8 v2.20, PIC18F47K42 Curiosity Nano Evaluation kit
I have a large array containing approx 636 integers:
 
#define DEMAND 91
#define FEEDBACK 95
#define ERROR 98
int16_t param[636];
 
In one of my interrupts, I have the following calculation but it is not computed correctly.
 
param[ERROR]=param[DEMAND]-param[FEEDBACK];
 
For example, if param[DEMAND]=500 and param[FEEDBACK]=104, param[ERROR] is assigned 500!
 
However, if I substitute the following in place of the line above, I get the correct value:
 
int16_t Temp1 = param[DEMAND]-param[FEEDBACK]; 
param[ERROR]=Temp1;
 
#1

6 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3787
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Incorrect result with simple subtraction calculation 2020/06/10 13:08:20 (permalink)
    +2 (2)

    ... if ..., I get the correct answer.

     
    Let me guess: with the code modification, you're checking Temp1.
    Most likely, param[ERROR] won't bevupdated with this value.
     
    To cut things very short: make param[636] "volatile int16_t"!

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    1and0
    Access is Denied
    • Total Posts : 10906
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Incorrect result with simple subtraction calculation 2020/06/10 13:45:38 (permalink)
    +1 (1)
    bpowers
    In one of my interrupts, I have the following calculation but it is not computed correctly.
     
    param[ERROR]=param[DEMAND]-param[FEEDBACK];

     
    Post the disassembly listing of this.
     
    #3
    bpowers
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2003/11/07 12:39:51
    • Status: offline
    Re: Incorrect result with simple subtraction calculation 2020/06/10 14:03:19 (permalink)
    0
    !
    ! param[ERROR] = param[DEMAND]-param[FEEDBACK];
    0x121A6: LFSR 2, 0x487
    0x121A8: NOP
    0x121AA: MOVFFL POSTINC2, 0x34
    0x121AC: NOP
    0x121AE: NOP
    0x121B0: MOVFFL POSTDEC2, 0x35
    0x121B2: NOP
    0x121B4: NOP
    0x121B6: MOVF 0x8F, W, BANKED
    0x121B8: SUBWF 0x34, F, ACCESS
    0x121BA: MOVF 0x90, W, BANKED
    0x121BC: SUBWFB 0x35, F, ACCESS
    0x121BE: MOVFF 0x34, 0x495
    0x121C0: NOP
    0x121C2: MOVFF 0x35, 0x496
    0x121C4: NOP
    !
    !
    #4
    1and0
    Access is Denied
    • Total Posts : 10906
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Incorrect result with simple subtraction calculation 2020/06/10 14:28:44 (permalink)
    0
    Put a breakpoint on that statement. What halted on that breakpoint, what is the value in the BSR register? If BSR != 4 then you have a banking error.
     
    Edit: If I was to code that in assembly (of course using symbols) it would be this:
            movlb   4
            movf    0x48F,w
            subwf   0x487,w
            movwf   0x495
            movf    0x490,w
            subwfb  0x488,w
            movwf   0x496

    which is a saving of 63% code space.
    post edited by 1and0 - 2020/06/10 15:22:13
    #5
    NKurzman
    A Guy on the Net
    • Total Posts : 18785
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Incorrect result with simple subtraction calculation 2020/06/10 19:37:04 (permalink)
    +2 (2)
    After you get it working you will still have an issue.
    16bit operations are not atomic.
    If you are only reading, You may get occasional bad data if the Interrupts happens between the time the two bytes are read.
     
    If you are writing, You will get data corruption.
    #6
    bpowers
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2003/11/07 12:39:51
    • Status: offline
    Re: Incorrect result with simple subtraction calculation 2020/06/24 13:07:27 (permalink)
    0
    Quick update.  I have submitted this issue to Microchip and they have acknowledged that it appears to be an issue with the compiler.
    #7
    Jump to:
    © 2020 APG vNext Commercial Version 4.5