I coded this in an old assembler for the 68hc912b32
Note each coefficient is placed in a 32 bit variable.
I am sure there is room for optimizations.
.include "globals.s"
.include "hc12.s"
c12x2: .blkb 4
a1: .blkb 4
a1x1: .blkb 4
y1: .blkb 4
a2x2: .blkb 4
PComp32: .blkb 4
baro_fltr: .blkb 3
;***************************************************************************************************************
; Initialize and read coefficients
movb #0b00000000,sp0br ; set spi to 4 MHz clock
ibaro: bclr portb,#0b10000000 ; Set CS low
movb #$88,sp0dr ; read a0 msb
brclr sp0sr,#SPIF,. ; wait for spi
movb #0,sp0dr ; clearing byte
brclr sp0sr,#SPIF,. ; wait for spi
ldaa sp0dr ; get a0 msb
movb #$8A,sp0dr ; read a0 LSB
brclr sp0sr,#SPIF,. ; wait for spi
movb #0,sp0dr ; clearing byte
brclr sp0sr,#SPIF,. ; wait for spi
ldab sp0dr ; get a0 LSB
std BaroCoa0 ; save baro coefficient a0
movb #$8C,sp0dr ; read b1 MSB
brclr sp0sr,#SPIF,. ; wait for spi
movb #0,sp0dr ; clearing byte
brclr sp0sr,#SPIF,. ; wait for spi
ldaa sp0dr ; get b1 MSB
movb #$8e,sp0dr ; read b1 LSB
brclr sp0sr,#SPIF,. ; wait for spi
movb #0,sp0dr ; clearing byte
brclr sp0sr,#SPIF,. ; wait for spi
ldab sp0dr ; get b1 LSB
std BaroCob1 ; save baro coeffiecient b1
movb #$90,sp0dr ; read b2 msb
brclr sp0sr,#SPIF,. ; wait for spi
movb #0,sp0dr ; clearing byte
brclr sp0sr,#SPIF,. ; wait for spi
ldaa sp0dr ; get b2 msb
movb #$92,sp0dr ; read b2 LSB
brclr sp0sr,#SPIF,. ; wait for spi
movb #0,sp0dr ; clearing byte
brclr sp0sr,#SPIF,. ; wait for spi
ldab sp0dr ; get b2 LSB
std BaroCob2 ; save baro Coefficient b2
movb #$94,sp0dr ; read c12 MSB
brclr sp0sr,#SPIF,. ; wait for spi
movb #0,sp0dr ; clearing byte
brclr sp0sr,#SPIF,. ; wait for spi
ldaa sp0dr ; get c12 MSB
movb #$96,sp0dr ; read c12 LSB
brclr sp0sr,#SPIF,. ; wait for spi
movb #0,sp0dr ; clearing byte
brclr sp0sr,#SPIF,. ; wait for spi
ldab sp0dr ; get c12 LSB
movb #0,sp0dr ; extra clearing byte
brclr sp0sr,#SPIF,. ; wait for spi
bset portb,#0b10000000 ; Set CS hi
std BaroCoc12 ; save baro coeffiecient c12
rts
CompBaro::
emuls ;result in Y:D needs shift 11 bits
;or divide by 2^11 which is 2048.
ldx #2048 ; **** TODO or could do 3 shifts then load from mid byte
; to acomplishs the other 8 bits.
sty c12x2+2 ; only save as 16 bit **** ;/ *
adca a1 ;preserve sign bit
ldd a1+2 ;get lower 16 bits
ror PComp32 ;Shift 1 bit
; todo check this for best resolution
ldd PComp32+1 ; Get mid bytes effectly shifts 8 bit
ldx #16394 ; 1023... times 16 for binary fraction
;use IRR filter with an index of 15 to smooth out noise
subd AmbBaro ; subtract last filtered Baro value
psha ; save sign
addd baro_fltr+1 ; add accumulator lsword
std baro_fltr+1 ; update
pulb ; msbyte difference
tfr b,d ; extend sign to acca
adca baro_fltr ; add with carry to msbyte accumulator
staa baro_fltr ; update
ldd baro_fltr ; IIR filtered result in msword
pshd ; to temp on stack
ldab baro_fltr+2 ; lsbyte filter for left shift
ldaa #15 ; filter control, 0 = min fltr, 15= max fltr
; flip bits for decrement??
eora #$F ; *** could use Higer if this is not enough
; less filtering => more shifts
bfc1: beq bfc2 ; begin shift loop for less filtering
lslb ; left shift lsbyte
rol 1,s ; shift into filtered pressure
rol ,s ; msbyte
deca ; dec shift count by subtracting bit 4
bra bfc1 ; repeat
bfc2: puld ; retrieve result
std AmbBaro ;Results in kPa= mBar*10
Jack