Need advice on 32bit signed values in SPIN
T Chap
Posts: 4,223
PUB iTRipSet(tr) 'must REPEAT to work**** iTRipSet(audiofrq)** older Dira[CurrentTripSetPin]~~ outa[CurrentTripSetPin]~ ctrb := %00110* << 26 +* CurrentTripSetPin repeat '''' This line below has values that can range from -2B to 2B required for the range of voltage output on the ''''' pin of 0 - 3v3. 'frqb := ((TripLevel * TripLevelBias) - (tempnew * TempBias)) #> 500_000_000 '''''' test values, produces voltages as shown 'frqb := -1 '3.265V***** 4.294B 'frqb := -2_147483646 '1.655V***** 2.147B 'frqb := 2_147483647 '1.655V***** 2.147B 'frqb := 0 '0.000V
The formula in the code above derives user input values from TRIPLEVEL, and a motor current value derived from a formula in a PID loop, relative to a PWM value.
The problem is when the values rolls over beyond 2147483647. If I output the value 2147483647 to and LCD, it shows the same number. If I output 2147483647 + 1 to an LCD, the LCD shows a 0. 2147483647 + 2 shows -2147483647. 2147483647 + 3 shows -2147483646.
The current checking software is getting thrown off when it sees the 0 just beyond 2147483647, as it expects -2147483646 which is the logical next value up, picking up at 1.655V onwards up to 3v3.
Why is this 0 there? How to work about this without more code to filter it?
The second part of the problem is the Limin Min of 500_000_000. I need the values from -2147483646 to -1 to get the output from 1.65v to 3v3. The Limit Min seems to prevent any negative values, which it should be doing but you get what I am trying to accomplish here.
Comments
This seems to solve the problems.
Lawson
right, that's why you always left shift one when setting the counter. This sacrifices 1 lsb of precision, but you're only likely to get 12 or the 32 bits of counter precision anyway so this isn't much of a loss.
Lawson