Long numbers

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

Post Reply
EtsDriver
Posts: 444
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 345 times
Been thanked: 227 times
Contact:

Long numbers

Post 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.
Attachments
2POINT_CALIBRATION_EXCEL_SHEET.xlsx
(11.01 KiB) Downloaded 184 times
16f877A TEST BIG NUMBERS.fcfx
(25.04 KiB) Downloaded 204 times
Ill just keep the good work up!

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Long numbers

Post 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.

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: Long numbers

Post 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. :)

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Long numbers

Post 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)

EtsDriver
Posts: 444
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 345 times
Been thanked: 227 times
Contact:

Re: Long numbers

Post by EtsDriver »

Got it working, thanks Jay and Leigh for help!
Ill just keep the good work up!

Post Reply