Created
October 28, 2012 18:27
-
-
Save TAUTIC/3969372 to your computer and use it in GitHub Desktop.
AS3935 Demo for the Parallax Propeller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' AS3935 Lightning Detector -- lostcauz 10/17/2012 | |
' Thanks Jayson Tautic [www.tautic.com] for the tuning code and creating this awesome board! | |
' Propeller pins 1..5 == CS - IRQ - SCLK - MISO - MOSI || (connect SI to GND) | |
' write - 00 pull CS low, write, pull CS high | |
' read - 01 register_addr(6 bits) register_data(8 bits) | |
' read - (CS high,low,high ends read) | |
' on_events, AS3935 pulls IRQ high and displays the interrupt in the REG0x03[3:0] | |
con | |
_clkmode = xtal1+pll16x | |
_clkfreq = 80_000_000 | |
obj | |
pst : "Parallax Serial Terminal" | |
var | |
long distance | |
long disturber | |
long lightning | |
pub main | |
pst.Start(115200) | |
pst.str(string(13,"Lightning Detector ON")) | |
cognew(@init,@distance) | |
repeat | |
case distance | |
1: pst.str(string(13,"Storm is overhead, Lightning #")) | |
pst.dec(++lightning) | |
2..40: | |
pst.str(string(13,"Storm distance is ")) | |
pst.dec(distance) | |
pst.str(string(" km, Lightning #")) | |
pst.dec(++lightning) | |
63: pst.str(string(13,"Detected out of range: Lightning #")) | |
pst.dec(++lightning) | |
65: pst.str(string(13,"Noise level too high")) | |
66: pst.str(string(13,"Detected Disturber #")) | |
pst.dec(++disturber) | |
distance := 0 | |
dat | |
org | |
init andn dira,IRQ ' inputs | |
andn dira,MISO ' | |
or outa,CS ' preset CS high | |
or dira,CS ' output | |
andn outa,SCLK ' preset low | |
or dira,SCLK ' output | |
andn outa,MOSI ' preset low | |
or dira,MOSI ' output | |
mov t2,cal_rco ' perform tuning of RCO | |
call #write_data | |
mov t2,tune_cap ' set up tuning caps to tune the antenna to 500kHz resonance. | |
call #write_data | |
' Next 2 instructions may be uncommented to mask disturber; also, 1st instruction may be swapped for | |
' raise_NTH | raise_NTH_WDTH | raise_WDTH | NTH_WDTH_def -- explanations of each in definitions below | |
'mov t2,MASK_DIST ' ignore disturber | |
'call #write_data | |
:loop waitpne null,IRQ ' wait for IRQ high | |
mov time,cnt ' must wait 2ms before reading IRQ register | |
add time,IRQ_delay | |
waitcnt time,IRQ_delay | |
mov t2,read_IRQ ' register 3 [3..0] | |
call #read_data ' t2 contains read data | |
and t2,#%1111 ' only need bits[3..0] | |
' interrupt value of zero means distance changed due to purging | |
test t2,#1 wz ' noise | |
if_nz wrlong INT_NH,par ' 65 represents too much noise | |
test t2,#4 wz ' disturber - if using MASK_DIST above comment out these 2 inst. | |
if_nz wrlong INT_D,par ' 66 represents disturber detected | |
test t2,#8 wz ' %1000 means lightning detected | |
if_z jmp #:loop ' no lightning detected | |
mov t2,read_reg7 ' lightning detected, read register 7 [5..0] for distance of storm in km | |
call #read_data | |
and t2,#%11_1111 ' only need bits[5..0] | |
wrlong t2,par ' write distance of storm to par | |
jmp #:loop | |
write_data andn outa,CS ' CS low | |
mov t1,#16 ' 16 bits | |
:w_next rol t2,#1 wc ' next bit | |
muxc outa,MOSI ' shift bit out to as3935 | |
call #delay ' clock data | |
or outa,SCLK | |
call #delay | |
andn outa,SCLK | |
djnz t1,#:w_next | |
or outa,CS ' end write | |
write_data_ret ret | |
read_data andn outa,CS ' CS low | |
mov t1,#8 ' 8 bits send | |
:send_req rol t2,#1 wc ' next bit | |
muxc outa,MOSI ' shift bit out to as3935 | |
call #delay ' clock data | |
or outa,SCLK | |
call #delay | |
andn outa,SCLK | |
djnz t1,#:send_req | |
call #delay ' need delay between last low SCLK and upcoming high | |
mov t1,#8 ' 8 bits rec | |
xor t2,t2 | |
:rec_bit or outa,SCLK ' clock data | |
call #delay | |
andn outa,SCLK | |
call #delay | |
test MISO,ina wc | |
rcl t2,#1 ' shift bit in to prop | |
djnz t1,#:rec_bit | |
or outa,CS ' end read | |
call #delay | |
andn outa,CS | |
call #delay | |
or outa,CS | |
call #delay | |
read_data_ret ret | |
delay mov time,cnt | |
add time,clk_delay | |
waitcnt time,clk_delay | |
delay_ret ret | |
INT_NH long 65 ' noise interrupt | |
INT_D long 66 ' disturber interrupt | |
IRQ_delay long 160_000 ' delay 2ms before reading interrupt register | |
read_IRQ long %01_000011 << 24 ' register 3 | |
read_reg7 long %01_000111 << 24 ' distance of storm in km | |
CS long 1 << 1 ' propeller pin connections | |
IRQ long 1 << 2 ' | |
SCLK long 1 << 3 ' | |
MISO long 1 << 4 ' | |
MOSI long 1 << 5 ' | |
clk_delay long 40 ' 2 MHz | |
null long 0 ' | |
cal_rco long %00_111101_1001_0110 << 16 ' perform RCO calibration | |
tune_cap long %00_001000_0000_0100 << 16 ' IRQ pin function for tuning [7..4] tuning caps in 120pf increments [3..0] | |
'MASK_DIST long %00_000011_0010_0000 << 16 ' ignore the disturber | |
'raise_WDTH long %00_000001_0010_0011 << 16 ' raise watchdog threshold only[3..0] (disturber tolerance)//////////////////// | |
'raise_NTH long %00_000001_0101_0001 << 16 ' raise noise threshold only[6..4] | |
'raise_NTH_WDTH long %00_000001_0101_0101 << 16 ' raise noise threshold[6..4] and watchdog threshold[3..0] | |
'NTH_WDTH_def long %00_000001_0010_0001 << 16 ' reset NTH and WDTH to default values | |
'show_res_freq long %00_001000_1000_0100 << 16 ' show resonance frequency via IRQ with tuned caps | |
'preset_default long %00_111100_1001_0110 << 16 ' set registers to default values | |
time res 1 ' for delay | |
t1 res 1 ' temp variables | |
t2 res 1 ' | |
fit | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment