Thanks Timmers that's a big improvement.
I'm using a pic 16F886 so can't use long variables sadly.
Anyone else any other ideas?
Thanks Timmers that's a big improvement.
I'm using a pic 16F886 so can't use long variables sadly.
Anyone else any other ideas?
Hi,
Not tested but how about:/Henrik.Code:ADC_VALUE VAR WORD AMPS VAR WORD i VAR BYTE AMPS = 0 For i = 0 to 3 ' Sample 4 times ADCIN 0,ADC_VALUE ' Get actual value AMPS = AMPS + ADC_VALUE ' Accumulate NEXT AMPS = AMPS >> 1 ' Divide by 2, AMPS is now 0-2048 AMPS = AMPS ** 64000 ' Multiply by ~0.97656, AMPS is now 0-2000 AMPS = AMPS - 1000 ' AMPS is now +/-1000, change to AMPS=1000-AMPS to invert. LCDOUT $FE,$1, "Current: ", SDEC AMPS/10, ".", DEC ABS(AMPS // 10)
Clever Henrik. I actually do capture ten samples and average them to get a reliable figure. But your maths after that is interesting. Wish i understood all the funny symbols >> etc
Thanks i might try that.
Hi,
The >> is the shift right operator, it shifts the word, in this case, one bit to the right which effectively is the same as dividing by 2. It's really not needed as you might as well change 64000 to 32000 to get the same end result (a value ranging from 0-2000)
In your case, with ten accumulating samples you could remove the shift operation and do Amps = Amps ** 12800 to get value ranging from 0-2000.
The ** operator multiplies by the value you specify and then returns the top 16bits of the intermediate 32bit result (you don't need LONGS for this). In effect this is the same as first multiplying by your value and then dividing by 65536. 12800/65536=0.1953 and 10240*0.1953=2000.
Good luck!
/Henrik.
Bookmarks