Menu

Error in LONG variable addition?

2020-03-30
2020-03-31
  • James Whyte

    James Whyte - 2020-03-30

    I am using a HX711 Strain Gauge amplifier that has a 24 bit output, so I am using LONG variables and trying to do some averaging, but I have been having problems doing addition with LONG variables.

    The plan is to take 4 x 24bit samples, add them, then divide by 4 to give a 4 sample average.
    However the math does not come out right.
    Rather than actual samples which will changed over time I have set my code to a constant for testing.
    For example if I set;
    HX711_DEC = 16735960
    I get 02fd7b60 = 50166624 (x3), when it should be 66,943,840 (x4)

    However if I set;
    HX711_DEC = 11111111
    I get 02a62b1c = 44444444 (x 4 = Correct)
    
    What am I missing here?
    
    `; ----- Configuration
    

    #chip 16F877A, 4.00
    #config OSC = XT, ;MCLRE_on, PWRT_ON

    ; ----- LCD connection settings
    

    #define LCD_IO 8 'Sets up ports and pins to be used with LCD screen
    #define LCD_RS PORTC.3
    #define LCD_RW PORTC.4
    #define LCD_Enable PORTC.5
    #define LCD_DATA_PORT PORTD
    #define LCD_Speed Meduim

    dim HX711_RAW as long ;Raw Sample
    dim HX711_AVG as long ;Cumulative average
    dim HX711_DEC as long ;2's Complement converted to decimal
    

    CLS
    Locate 0,0
    Print "START" 'Prints subroutine info on LCD
    wait 1 s

    MAIN:
    Do forever
    ;HX711_DEC = 16735960
    HX711_DEC = 11111111

    '1st Sample
    HX711_AVG = HX711_DEC
    '2nd Sample
    HX711_AVG = HX711_AVG + HX711_DEC
    '3rd Sample
    HX711_AVG = HX711_AVG + HX711_DEC
    '4th Sample
    HX711_AVG = HX711_AVG + HX711_DEC
    
    CLS
    Locate 0,0
    ;Print hex(HX711_DEC)
    Print hex(HX711_AVG_E)
    Locate 0,2
    Print Hex(HX711_AVG_U)
    Locate 0,4
    Print hex(HX711_AVG_H)
    Locate 0,6
    Print hex(HX711_AVG)
    
    wait 1 s
    

    Loop`

     
  • Anobium

    Anobium - 2020-03-30

    There is nothing wrong with you code. I get correct result here. I adpated your code to give me more debug but I get 66,943,840 (x4)

    Post the the top line of your (program).asm file. It will look something like this.

    ;Program compiled by Great Cow BASIC (0.98.<<>> 2020-03-28 (Windows 32 bit))
    

    My program:

    #chip 16F877A, 4.00
    #option Explicit
    #config OSC = XT, ;MCLRE_on, PWRT_ON
    ; ----- LCD connection settings
    #define LCD_IO 8 'Sets up ports and pins to be used with LCD screen
    #define LCD_RS PORTC.3
    #define LCD_RW PORTC.4
    #define LCD_Enable PORTC.5
    #define LCD_DATA_PORT PORTD
    #define LCD_Speed Meduim
    dim HX711_RAW as long ;Raw Sample
    dim HX711_AVG as long ;Cumulative average
    dim HX711_DEC as long ;2's Complement converted to decimal
    CLS
    Locate 0,0
    Print "START" 'Prints subroutine info on LCD
    wait 1 s
    MAIN:
    Do forever
    HX711_DEC = 16735960
    ;HX711_DEC = 11111111
    '1st Sample
    HX711_AVG = HX711_DEC
    
    '2nd Sample
    HX711_AVG = HX711_AVG + HX711_DEC
    '3rd Sample
    HX711_AVG = HX711_AVG + HX711_DEC
    '4th Sample
    HX711_AVG = HX711_AVG + HX711_DEC
    
    CLS
    Print "Seed = d"
    Print HX711_DEC
    Locate 1,0
    Print "AVG  = h"
    Print hex(HX711_AVG_E)
    Print Hex(HX711_AVG_U)
    Print hex(HX711_AVG_H)
    Print hex(HX711_AVG)
    
    HX711_AVG = HX711_AVG/4
    
    Locate 2,0
    Print "AVG/4= h"
    Print hex(HX711_AVG_E)
    Print Hex(HX711_AVG_U)
    Print hex(HX711_AVG_H)
    Print hex(HX711_AVG)
    
    Locate 3,0
    Print "AVG/4= d"
    Print HX711_AVG
    
    
    wait 1 s
    Loop
    
     
  • James Whyte

    James Whyte - 2020-03-31

    Yes, that was the problem.
    Was: (0.98.00 2017-09-23)
    Now: (0.98.06 2019-06-12 (Windows 32 bit))

    I should have chcked that first.... well I did actually. The problem is the installer that I checked does not have the full version number. Even now the latest installer is GCB_Installer-98.exe and I thought I had updated, but must have not used the latest installer even hough I thought I had.

    Anyhow, with latest version the problem does go away. Thanks for your help and keep up the good work.

    Off topic: Is that some sort of virtual LCD screen capture you posted?

     
    • Anobium

      Anobium - 2020-03-31

      Pleasure.

      It is the Real Simulator. It is OK for simulation. But, @caroper is the real expert on Sims.

       

Log in to post a comment.