fl { Pressure meter Using Pressure sensor(MPL115A1) PropForth/5.2 15/09/2012 20:23:04 } hex \ Propeller MPL115A1 0 wconstant _DOUT \ P0 <--- DOUT 1 wconstant _CLK \ P1 ---> SCLK 2 wconstant _DIN \ P2 ---> DIN 3 wconstant _CS \ P3 ---> CS : clk_out_l _CLK pinlo ; : clk_out_h _CLK pinhi ; : din_out_l _DIN pinlo ; : din_out_h _DIN pinhi ; : cs_out_l _CS pinlo ; : cs_out_h _CS pinhi ; \ saving area for coefficient (8byte) wvariable a0 6 allot \ Pressure and Temperature variable data \ SPI protocol \ ( n1 -- n2) n1:command code n2:response : spi 80 0 8 0 do clk_out_l \ response data \ 1 lshift 1 _DOUT lshift ina COG@ and if 1+ ." 1" else ." 0" then rot2 1 lshift _DOUT px? if 1+ then rot2 \ transmit data 2dup and if din_out_h \ ." 1" else din_out_l \ ." 0" then clk_out_h 1 rshift rot loop rot2 2drop \ cr ; \ Setup port to initial \ _DOUT:input \ _CLK _DIN _CS:output level:hi \ ( -- ) : setup _CLK 3 0 do dup dup pinout pinhi 1+ loop drop ; \ Send command \ ( n1 -- n2 ) n1:command data 2n:receive data : command spi drop 0 spi ; \ Setup and Read coefficient \ ( -- n1 n2 n3 n4 ) n1:c12 n2:b2 n3;b1 n4:a0 : init setup cs_out_l 96 94 92 90 8E 8C 8A 88 4 0 do 2 0 do command i 0= if 8 lshift then swap loop or a0 i 2 * + W! loop 0 spi drop cs_out_h ; \ Get Pressure and Temperature \ ( -- ) : convert cs_out_l 24 command drop 0 spi drop cs_out_h 3 delms cs_out_l 86 84 82 80 2 0 do 2 0 do command i 0= if 8 lshift then swap loop or 6 rshift data i 2 * + W! loop 0 spi drop cs_out_h ; \ Convert coefficient to floating-point \ ( n1 n2 -- n3 ) n1:decimal-point n2:coefficient n3:floating point : float_format dup F rshift 1 = if invert 1+ FFFF and 1 else 0 then rot2 \ Get sign \ ( sign decimal-point coefficient ) 2dup bit_enc 1- swap - 7F + \ Get exponent rot2 21 over bit_enc - lshift \ msb-justify mantissa nip \ bit29-justify mantissa \ ( n1 n2 n3 ) n1:sign n2:exponent n3:mantissa 9 rshift \ mantissa swap 17 lshift \ exponent or swap 1F lshift \ sign or ; \ Calculate pressure : P_calc \ c12x2 = c12 * Tadc 18 a0 6 + W@ float_format data 2+ W@ ffloat fmult \ a1 = b1 + c12x2 D a0 2+ W@ float_format fadd \ a1x1 = a1 * Padc data W@ ffloat fmult \ y1 = a0 + a1x1 3 a0 W@ float_format fadd \ a2x2 = b2 * Tadc E a0 4+ W@ float_format data 2+ W@ ffloat fmult \ PComp = y1 + a2x2 fadd ; : divider over u/mod dup 0= if \ quotient is 0 e W@ 0= if drop else 30 + emit then else \ quotient is not 0 30 + emit 1 e W! then swap A u/ swap ; wvariable fraction -2 allot 1388 w, 9C4 w, 4E2 w, 271 w, 138 w, 9C w, 4E w, 27 w, 13 w, : make_1s 0 swap 0 do 1 lshift 1+ loop ; \ Display pressure(decimal) \ ( n -- ) n:pressure(floating point) : disp_press \ 447AA7D7 dup 17 rshift 7F - \ Get exponent swap 8 lshift 80000000 or \ Get mantissa \ (exponent mantissa) 2dup swap 1+ lshift E0000000 and \ fraction part rot2 1F rot - rshift \ integer part \ calculate integer 3E8 swap 0 e W! 4 0 do divider loop 2drop 2E emit \ calculate fraction 80000000 0 rot2 A 0 do 2dup and 0<> if \ rot fraction 2+ i 2 u* + W@ + rot2 rot fraction i 2 u* + W@ + rot2 then 1 rshift loop 2drop 3E8 swap 1 e W! 3 0 do divider loop 2drop ; \ Display each cofficient and Padc and Tadc and calculated presseure : MPL115A1 init cr \ Display coefficient 4 0 do i 0 over = if ." a0=" else 1 over = if ." b1=" else 2 over = if ." b2=" else ." c12=" thens drop a0 i 2 u* + W@ hex . cr loop convert \ Display Padc and Tadc ." Padc=" data W@ . cr ." Tdc=" data 2+ W@ . cr cr begin convert P_calc \ hPa = PComp * (650/1023) + 500 28A ffloat fmult 3FF ffloat fdiv 1F4 ffloat fadd disp_press ." hPa" cr 3E8 delms fkey? swap drop until ; { 18 33C8 float_format 1FB ffloat fmult \ 3ECD_1A30 D B3F9 float_format fadd \ BFFC_D574 -1.975263 19B ffloat fmult \ C44A_F559 -811.83312957 3 3ECE float_format fadd \ 4495_BD54 1197.916503 E C517 float_format 1FB ffloat fmult \ C3E9_56E6 -466.678893535 fadd \ 4436_CF35 731.237609675 } decimal