Page 1 of 1

Long numbers

Posted: Fri Jun 02, 2017 9:00 pm
by EtsDriver
Hi!
Has anyone else encountered that the "long" type variables roll over well before they reach the max value of it?

I've been trying to implement simple calibration to certain values (temperature), but i need to show some decimals too, and when i multiply calibration values by 10 and adc by 10 and calculate the corrected values, the results are unexpected. Then i started to break down the calculation to see where it fails and why...

In simulation everything works like it should, but when on real HW, the numbers seem to roll over:

This is from real HW test on 16F18877 target:

Code: Select all

DEBUG: ADC0: 1023
DEBUG: ADC1: 293

KEY press= 0
DEBUG: VAL_TEMP before calculation: -39
DEBUG: ADC_OFFSET: 10230
DEBUG: CAL_RANGE: 10230
DEBUG: REF_RANGE: 1400
DEBUG: RANGE_OFFSET: 35152
DEBUG: UNCORRECTED VALUE:  3
DEBUG: CORRECTED VALUE: -397
DEBUG: VAL_TEMP AFTER CALC: -39
DEBUG: VAL_TEMP_PART AFTER CALC: 81
DEBUG: .temp string: ERR
DEBUG: ADC0: 1023
DEBUG: ADC1: 513
Same thing on 16F877A target:

Code: Select all

DEBUG: VAL_TEMP before calculation: -39
DEBUG: ADC_OFFSET: 6570
DEBUG: CAL_RANGE: 10230
DEBUG: REF_RANGE: 1400
DEBUG: RANGE_OFFSET: 22960
DEBUG: UNCORRECTED VALUE:  2
DEBUG: CORRECTED VALUE: -398
DEBUG: VAL_TEMP AFTER CALC: -39
DEBUG: VAL_TEMP_PART AFTER CALC: 81
I was not able to test this on DSpic as it seems that i dont anymore know how to use the usb serial on the night :D And lost motivation to Arduino it.

Re: Long numbers

Posted: Fri Jun 02, 2017 10:02 pm
by LeighM
On the PIC platform calculations will be 16 bit unless forced otherwise,
So multiplying two 16 bit variables will only give a 16 bit result, even if you a assign that to a 32 bit variable.
On tablet just now so cannot look at details of your file, so hope that helps.

Re: Long numbers

Posted: Sun Jun 04, 2017 2:56 pm
by Jay Dee
If
.RANGE_OFFSET = ULONG
.ADC_OFFSET = UINT
.REF_RANGE = UINT

To achieve this,
.RANGE_OFFSET = .ADC_OFFSET * .REF_RANGE

try,
.RANGE_OFFSET = .ADC_OFFSET
.RANGE_OFFSET = .RANGE_OFFSET * .REF_RANGE

I found that to get the result of calculations properly applied to a ULong variable, you had to have a ULong involved in the calculation.
Ben pointed mein this direction, I think its somethign to do with the way the compiler handles calcs.
Give it a try. :)

Re: Long numbers

Posted: Mon Jun 05, 2017 8:32 am
by LeighM
Yes, you have to have a 32 bit (long) on the right hand side of the equation.
(The same goes for any intermediate calculations such as those in brackets)

Re: Long numbers

Posted: Wed Jun 07, 2017 6:34 pm
by EtsDriver
Got it working, thanks Jay and Leigh for help!