{ Franklin Lightning Sensor Module(AE-AS3935) PropForth 5.5 2015/05/03 10:09:57 Franklin Lightning Sensor Module Propeller (AE-AS3935) SDA ------ SDA SCL ------ SCL IRQ ------ P0 GND - A0 GND - A1 Vdd ------ 3.3V GND ------ GND } \ ( n1 n2 -- n3 ) n1:LCO_FDIV n2:IRQ pin n3:frequency lockdict create a_FREQcount forthentry $C_a_lxasm w, h123 h113 1- tuck - h9 lshift or here W@ alignl h10 lshift or l, z2WiPZB l, z1SyLI[ l, z2WyPb4 l, z20iPeB l, z2Wiy4X l, z20iy3C l, z2WyyW0 l, z2WiPnY l, z20iPqk l, z2WyyG1 l, z3ryPj0 l, z2WiPVv l, zfiPRD l, z1SV01X l, zb0000 l, z4kBG0 l, freedict \ =========================================================================== \ Constants \ =========================================================================== \ Slave addres 0 for AS3935 0 wconstant AS3935 0 wconstant IRQ \ register 0 wconstant reg0 \ Reserved[7:6] AFE_GB[5:1] PWD[0] 1 wconstant reg1 \ Reserved[7] NF_LEV[6:4] WDTH[3:0] 2 wconstant reg2 \ Reserved[7] CL_STAT[6] MIN_MUM_LIGH[5:4] SREJ[3:0] 3 wconstant reg3 \ LCO_FDIV[7:6] MASK_DIST[5] Reserved[4] INT[3:0] 4 wconstant reg4 \ S_LIG_L[7:0] 5 wconstant reg5 \ S_LIG_M[7:0] 6 wconstant reg6 \ Reserved[7:5] S_LIG_MM[4:0] 7 wconstant reg7 \ Reserved[7:6] DISTANCE[5:0] 8 wconstant reg8 \ DISP_LCO[7] DISP_SRCO[6] DISP_TRCO[5] Reserved[4] TUN_CAP[3:0] \ 9 -h32 Lightning Detection Look-up table h3A wconstant reg58 \ TRCO_CALIB_DONE[7] TRCO_CALIB_NOK[6] Reserved[5:0] h3B wconstant reg59 \ SRCO_CALIB_DONE[7] SRCO_CALIB_NOK[6] Reserved[5:0] h3C wconstant reg60 \ Preset Default h3D wconstant reg61 \ Calib_RCO \ initial value h1F wconstant AFE_GB \ AFE(Analog front end) Boost Gain [<=h12:Indoor >=h0E:Outdoor] 2 wconstant NF_LVL \ NoisFloor Threshold [0 - 7] 2 wconstant WDTH \ Watchdog Threshold [0 - hF] 0 wconstant DIV \ Frequency Division Ratio \ =========================================================================== \ Variables \ =========================================================================== wvariable old \ Frequency difference wvariable cap \ capacitance index \ =========================================================================== \ Main \ =========================================================================== \ Read 1byte \ ( n1 -- n2 ) n1:register n2:data : AS3935_rd AS3935 i2c_rd err? ; \ Write 1byte \ ( n1 n2 -- ) n1:data n2:register : AS3935_wr AS3935 i2c_wr err? ; \ Display all registers from mode1 to ALLCALLADR \ ( -- ) : rd_allreg cr hex ." register number[hex]:value[hex]" cr cr hex 8 0 do 8 0 do j 8 * i + d62 < if j 8 * i + AS3935_rd j 8 * i + .byte ." :" .byte 2 spaces else 8 seti then loop cr loop decimal cr ; \ Caribrating LCO \ ( -- ) : calib_LCO d16 0 do \ Write Disp_LCO-bit and TUN_CAP h80 i or reg8 AS3935_wr d50 delms DIV IRQ a_FREQcount dup ." CAP= " i . space ." frequency=" . cr d500000 - abs dup old W@ < \ Get difference between obtained value and 500kHz if old W! i cap W! \ Update old and cap else drop then loop ; \ Initialize AS3935 \ ( -- ) : init_AS3935 d10000 old W! h96 reg60 AS3935_wr \ Preset Default h96 reg61 AS3935_wr \ Caliblate RCO \ Write AFE_GB AFE_GB 1 lshift reg0 AS3935_wr \ Write NF_LVL and WDTH NF_LVL 4 lshift WDTH or reg1 AS3935_wr \ Write DIV DIV 6 lshift reg3 AS3935_wr \ Caribrate LCO calib_LCO \ Display Frequency and Capacitance h80 cap W@ or reg8 AS3935_wr DIV IRQ a_FREQcount ." SetFrequency:" . ." Hz" space ." Capacitance:" cap W@ 8 * . ." pF" cr \ Save TUN_CAP cap W@ reg8 AS3935_wr h96 reg61 AS3935_wr \ Caliblate RCO \ TRCO reg8 AS3935_rd h20 or reg8 AS3935_wr \ DISP_TRCO=1 2 delms reg58 AS3935_rd hC0 and h80 = \ Check bit7=1[TRCO_CALIB_DONE] bit6=0[TRCO_CALIB_NOK] if ." TRCO ok" DIV IRQ a_FREQcount 4 rshift space . ." Hz" else ." TRCO NG" then reg8 AS3935_rd hF and reg8 AS3935_wr \ DISP_TRCO=0 cr \ SRCO reg8 AS3935_rd h40 or reg8 AS3935_wr \ DISP_SRCO=1 2 delms reg59 AS3935_rd hC0 and h80 = \ Check bit7=1[SRCO_CALIB_DONE] bit6=0[SRCO_CALIB_NOK] if ." SRCO ok" DIV IRQ a_FREQcount 4 rshift space . ." Hz" else ." TRCO NG" then reg8 AS3935_rd hF and reg8 AS3935_wr \ DISP_SRCO=0 cr cr ; \ Noise level too high \ ( -- ) : INT_NH ." Noise level too high" cr ; \ Disturb detected \ ( -- ) : INT_D ." Disturb detected" cr ; \ Ligtning interrupt \ ( -- ) : INT_L ." Ligtning detected" cr \ energy reg6 AS3935_rd hF and d65536 * reg5 AS3935_rd d256 * + reg4 AS3935_rd + ." Energy:" . 5 spaces ." Distance:" \ Distance reg7 AS3935_rd h1F and 1 over = if ." Storm is Overhead" else h3F over = if ." Out of range" else . thens drop cr ; \ Detect interrupt \ ( -- ) : Detect_int 2 delms reg3 AS3935_rd hF and \ Read Interrupt 1 over = if INT_NH \ Noise level too high else 4 over = if INT_D \ Disturb detected else 8 over = if INT_L \ Ligtning interrupt thens drop ; \ \ ( -- ) : demo cr \ Initialize AS3935 init_AS3935 begin 1 IRQ lshift ina COG@ and if Detect_int then fkey? swap drop until ; { \ Measurement for frequency on irp pin \ ( n1 n2 -- n3 ) n1:LCO_FDIV n2:IRQ pin n3:frequency \ $C_treg1 - IRQ pin \ $C_treg2 - shift bit \ $C_treg3 - waiting cnt fl h1F8 wconstant ctra h1FA wconstant frqa h1FC wconstant phsa build_BootOpt :rasm \ Get IRQ pin mov $C_treg1 , $C_stTOS spop mov $C_treg2 , # 4 add $C_treg2 , $C_stTOS \ Counter mode mov ctra , __POSEDGE add ctra , $C_treg1 mov phsa , # 0 mov $C_treg3 , __1sec add $C_treg3 , cnt mov frqa , # 1 waitcnt $C_treg3 , # 0 mov $C_stTOS , phsa shl $C_stTOS , $C_treg2 jexit __POSEDGE h28000000 __1sec d80000000 ;asm a_FREQcount }