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
#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
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:
#chip16F877A,4.00#optionExplicit#configOSC=XT,;MCLRE_on,PWRT_ON;-----LCDconnectionsettings#defineLCD_IO8'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 Meduimdim HX711_RAW as long ;Raw Sampledim HX711_AVG as long ;Cumulative averagedim HX711_DEC as long ;2'sComplementconvertedtodecimalCLSLocate0,0Print"START"'Prints subroutine info on LCDwait 1 sMAIN:Do foreverHX711_DEC = 16735960;HX711_DEC = 11111111'1stSampleHX711_AVG=HX711_DEC'2nd SampleHX711_AVG = HX711_AVG + HX711_DEC'3rdSampleHX711_AVG=HX711_AVG+HX711_DEC'4thSampleHX711_AVG=HX711_AVG+HX711_DECCLSPrint"Seed = d"PrintHX711_DECLocate1,0Print"AVG = h"Printhex(HX711_AVG_E)PrintHex(HX711_AVG_U)Printhex(HX711_AVG_H)Printhex(HX711_AVG)HX711_AVG=HX711_AVG/4Locate2,0Print"AVG/4= h"Printhex(HX711_AVG_E)PrintHex(HX711_AVG_U)Printhex(HX711_AVG_H)Printhex(HX711_AVG)Locate3,0Print"AVG/4= d"PrintHX711_AVGwait1sLoop
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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)
#chip 16F877A, 4.00
#config OSC = XT, ;MCLRE_on, PWRT_ON
#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
CLS
Locate 0,0
Print "START" 'Prints subroutine info on LCD
wait 1 s
MAIN:
Do forever
;HX711_DEC = 16735960
HX711_DEC = 11111111
Loop`
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.
My program:
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?
Pleasure.
It is the Real Simulator. It is OK for simulation. But, @caroper is the real expert on Sims.