```
#Picaxe 08M2
#Terminal 4800
#No_Data
; Comment the line below to disable SERTXD output
;#Define USE_SERTXD
Symbol reserveW0 = w0
Symbol reserveW1 = w1
Symbol h.average = w2 ; b5:b4
Symbol m.average = w3 ; b7:b6
Symbol l.average = w4 ; b9:b8
Symbol h.average.lsb = b4
Symbol m.average.lsb = b6
Symbol l.average.lsb = b8
Symbol h.average.msb = b5
Symbol m.average.msb = b7
Symbol l.average.msb = b9
Symbol m.last = w5 ; b11:b10
Symbol l.last = w6 ; b13:b12
Symbol diff = w7 ; b15:b14
Symbol h.byte = b16
Symbol m.byte = b17
Symbol l.byte = b18
Symbol counter = b19
Symbol SAMPLES = 16
Symbol CMD_RESET = $1E
Symbol CMD_D1_4096 = $48
Symbol CMD_ADC = $00
#Define Negative(wVar) wVar >= $8000
#Define Positive(wVar) wVar < $8000
PowerOnReset:
HI2cSetup I2CMASTER, %11101110, I2CSLOW, I2CBYTE
MainLoop:
Do
h.average = 0
m.average = 0
l.average = 0
For counter = 1 To SAMPLES
HI2cOut ( CMD_D1_4096 )
Pause 10
HI2cOut ( CMD_ADC )
Pause 10
HI2cIn ( h.byte, m.byte, l.byte )
#IfDef SIMULATING
Random diff
h.byte = $AB
m.byte = $CD ^ diff
l.byte = $EF + diff
#EndIf
h.average = h.average + h.byte
m.average = m.average + m.byte
l.average = l.average + l.byte
#IfDef USE_SERTXD
SerTxd( #counter, TAB, " " )
b0 = h.byte : Gosub HexByte
b0 = m.byte : Gosub HexByte
b0 = l.byte : Gosub HexByte
SerTxd( CR, LF )
#EndIf
Next
#IfDef USE_SERTXD
; Show the raw averages
SerTxd( "h", TAB )
w0 = h.average : Gosub HexWord
SerTxd( CR,LF, "m", TAB, " " )
w0 = m.average : Gosub HexWord
SerTxd( CR,LF, "l", TAB, " " )
w0 = l.average : Gosub HexWord
SerTxd( CR,LF )
#EndIf
; Combine the numbers
m.average = m.average + l.average.msb
h.average = h.average + m.average.msb
; Create m:l sum
l.average.msb = m.average.lsb
m.average = h.average
#IfDef USE_SERTXD
; show the sum
SerTxd( "Sum", TAB )
w0 = m.average : Gosub HexWord
w0 = l.average : Gosub HexWord
SerTxd( CR,LF )
#EndIf
; Find the average
m.average = $100 / SAMPLES * m.average
m.average = l.average / SAMPLES / 256 + m.average
l.average = $100 / SAMPLES * l.average
; At this point the integer 24-bit average
; held in the m.average and l.average pair
; as -
; m.average l.average
;.---------------------. .---------------------.
;| xwvutsrq | ponmlkji | | hgfedcba | 00000000 |
;`---------------------' `---------------------'
; m.avg.msb m.avg.lsb l.avg.msb l.avg.lsb
; Now make the numbers lsb aligned -
;
; m.average l.average
;.---------------------. .---------------------.
;| 00000000 | xwvutsrq | | ponmlkji | hgfedcba |
;`---------------------' `---------------------'
; m.avg.msb m.avg.lsb l.avg.msb l.avg.lsb
l.average.lsb = l.average.msb
l.average.msb = m.average.lsb
m.average.lsb = m.average.msb
m.average.msb = 0
; Determine which is highest, this average or last
If m.average > m.last Then ThisIsHigher
If m.average < m.last Then LastIsHigher
If l.average > l.last Then ThisIsHigher
If l.average < l.last Then LastIsHigher
BothTheSame:
diff = 0
Goto CalculatedDifference
ThisIsHigher:
; Calculate diff = average - last
m.last = m.average - m.last
diff = l.average - l.last
; Handle subtraction borrow
If l.average < l.last Then
m.last = m.last - 1
End If
; Limit to maximum positive value
If m.last > 0 Then
diff = $7FFF
Else
diff = diff max $7FFF
End If
; Reult is correctly positive
Goto CalculatedDifference
LastIsHigher:
; Calculate diff = last - average
m.last = m.last - m.average
diff = l.last - l.average
; Handle subtraction borrow
If l.last < l.average Then
m.last = m.last - 1
End If
; Limit to maximum positive value
If m.last > 0 Then
diff = $7FFF
Else
diff = diff max $7FFF
End If
; Make negative, because average - last is
diff = -diff
Goto CalculatedDifference
CalculatedDifference:
; last becomes latest average
m.last = m.average
l.last = l.average
; Show the 24-bit hex averages
SerTxd( "Avg(", #SAMPLES, ")", TAB )
b0 = m.average.lsb : Gosub HexByte
w0 = l.average : Gosub HexWord
; Show the 16-bit decimal average
SerTxd( TAB, #l.average, TAB )
; Show the signed difference
If diff = 0 Then
SerTxd( "=")
Else
w0 = diff : Gosub SignedWord
End If
SerTxd( CR, LF )
#IfDef USE_SERTXD
SerTxd( "===", TAB, "========", CR,LF )
#EndIf
Pause 1000
Loop
HexWord:
b2 = b1 / 16 : Gosub HexNibble
b2 = b1 : Gosub HexNibble
HexByte:
b2 = b0 / 16 : Gosub HexNibble
b2 = b0
HexNibble:
b2 = b2 & $0F + "0"
If b2 > "9" Then : b2 = b2 + 7 : End If
SerTxd( b2 )
Return
SignedWord:
If Negative(w0) Then
w0 = -w0
SerTxd( "-", #w0 )
Else
SerTxd( "+", #w0 )
End If
Return
```