Merge pull request #131 from sparkman1/master
Example sketch for PWF Lightning Sensor
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
// This sketch is to integrate the Playing With Fusion AXS3935 Lightning Sensor Breakout Board
|
||||
// with the MySensors environment and is based on a sketch provided by Playing With Fusion
|
||||
// http://playingwithfusion.com/productview.php?pdid=22&catid=1001
|
||||
//
|
||||
#include "MySensor.h"
|
||||
// the lightning sensor can communicate via SPI or I2C. This sketch uses the SPI interface
|
||||
#include "SPI.h"
|
||||
// include Playing With Fusion AXS3935 libraries
|
||||
#include "PWFusion_AS3935.h"
|
||||
|
||||
// setup CS pins used for the connection with the lightning sensor
|
||||
// other connections are controlled by the SPI library)
|
||||
int8_t CS_PIN = 8;
|
||||
int8_t SI_PIN = 7;
|
||||
int8_t IRQ_PIN = 3;
|
||||
volatile int8_t AS3935_ISR_Trig = 0;
|
||||
|
||||
// #defines
|
||||
#define AS3935_INDOORS 1
|
||||
#define AS3935_OUTDOORS 0
|
||||
#define AS3935_DIST_DIS 0
|
||||
#define AS3935_DIST_EN 1
|
||||
#define AS3935_CAPACITANCE 96 // <-- SET THIS VALUE TO THE NUMBER LISTED ON YOUR BOARD
|
||||
// prototypes
|
||||
void AS3935_ISR();
|
||||
|
||||
PWF_AS3935 lightning0(CS_PIN, IRQ_PIN, SI_PIN);
|
||||
|
||||
#define CHILD_ID_DISTANCE 1
|
||||
#define CHILD_ID_INTENSITY 2
|
||||
MySensor gw;
|
||||
MyMessage msgDist(CHILD_ID_DISTANCE, V_DISTANCE);
|
||||
MyMessage msgInt(CHILD_ID_INTENSITY, V_VAR1);
|
||||
|
||||
void setup()
|
||||
{
|
||||
gw.begin();
|
||||
|
||||
// Send the sketch version information to the gateway and Controller
|
||||
gw.sendSketchInfo("Lightning Sensor", "1.1");
|
||||
|
||||
// Register all sensors to gw (they will be created as child devices)
|
||||
gw.present(CHILD_ID_DISTANCE, S_DISTANCE);
|
||||
gw.present(CHILD_ID_INTENSITY, S_CUSTOM);
|
||||
boolean metric = gw.getConfig().isMetric;
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.println("Playing With Fusion: AS3935 Lightning Sensor, SEN-39001");
|
||||
Serial.println("beginning boot procedure....");
|
||||
|
||||
// setup for the the SPI library:
|
||||
SPI.begin(); // begin SPI
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV4); // SPI speed to SPI_CLOCK_DIV16/1MHz (max 2MHz, NEVER 500kHz!)
|
||||
SPI.setDataMode(SPI_MODE1); // MAX31855 is a Mode 1 device
|
||||
// --> clock starts low, read on rising edge
|
||||
SPI.setBitOrder(MSBFIRST); // data sent to chip MSb first
|
||||
|
||||
lightning0.AS3935_DefInit(); // set registers to default
|
||||
// now update sensor cal for your application and power up chip
|
||||
lightning0.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_OUTDOORS, AS3935_DIST_EN);
|
||||
// AS3935_ManualCal Parameters:
|
||||
// --> capacitance, in pF (marked on package)
|
||||
// --> indoors/outdoors (AS3935_INDOORS:0 / AS3935_OUTDOORS:1)
|
||||
// --> disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:2)
|
||||
// function also powers up the chip
|
||||
|
||||
// enable interrupt (hook IRQ pin to Arduino Uno/Mega interrupt input: 1 -> pin 3 )
|
||||
attachInterrupt(1, AS3935_ISR, RISING);
|
||||
// dump the registry data to the serial port for troubleshooting purposes
|
||||
lightning0.AS3935_PrintAllRegs();
|
||||
|
||||
// delay execution to allow chip to stabilize.
|
||||
delay(1000);
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
// This program only handles an AS3935 lightning sensor. It does nothing until
|
||||
// an interrupt is detected on the IRQ pin.
|
||||
while(0 == AS3935_ISR_Trig){}
|
||||
|
||||
// reset interrupt flag
|
||||
AS3935_ISR_Trig = 0;
|
||||
|
||||
// now get interrupt source
|
||||
uint8_t int_src = lightning0.AS3935_GetInterruptSrc();
|
||||
if(0 == int_src)
|
||||
{
|
||||
Serial.println("Unknown interrupt source");
|
||||
}
|
||||
else if(1 == int_src)
|
||||
{
|
||||
uint8_t lightning_dist_km = lightning0.AS3935_GetLightningDistKm();
|
||||
uint32_t lightning_intensity = lightning0.AS3935_GetStrikeEnergyRaw();
|
||||
|
||||
Serial.print("Lightning detected! Distance to strike: ");
|
||||
Serial.print(lightning_dist_km);
|
||||
Serial.println(" kilometers");
|
||||
Serial.print("Lightning detected! Lightning Intensity: ");
|
||||
Serial.println(lightning_intensity);
|
||||
gw.send(msgDist.set(lightning_dist_km));
|
||||
gw.send(msgInt.set(lightning_intensity));
|
||||
}
|
||||
else if(2 == int_src)
|
||||
{
|
||||
Serial.println("Disturber detected");
|
||||
}
|
||||
else if(3 == int_src)
|
||||
{
|
||||
Serial.println("Noise level too high");
|
||||
}
|
||||
}
|
||||
|
||||
// this is irq handler for AS3935 interrupts, has to return void and take no arguments
|
||||
// always make code in interrupt handlers fast and short
|
||||
void AS3935_ISR()
|
||||
{
|
||||
AS3935_ISR_Trig = 1;
|
||||
}␍
|
||||
@@ -0,0 +1,10 @@
|
||||
Notes from AS3935 datasheet:
|
||||
Register Map: Page 11, detailed version on Page 12
|
||||
|
||||
Max clock freq for SPI = 2MHz
|
||||
|
||||
SPI command Structure: Page14
|
||||
To activate this SPI, the CS has to be set to low. A SPI command consists of two bytes serial command and the data are sampled on the falling
|
||||
edge of SCLK (CPHA=1). The next table shows command structure, from the MSB (B15) to LSB (B0). The command stream has to be sent to
|
||||
the SPI from the MSB (B15) to the LSB (B0).
|
||||
|
||||
@@ -0,0 +1,421 @@
|
||||
/***************************************************************************
|
||||
* File Name: PWFusion_AS3935.h
|
||||
* Processor/Platform: Arduino Uno R3 (tested)
|
||||
* Development Environment: Arduino 1.0.5
|
||||
*
|
||||
* Designed for use with with Playing With Fusion AS3935 Lightning Sensor
|
||||
* Breakout: SEN-39001.
|
||||
*
|
||||
* SEN-39001 (universal applications)
|
||||
* ---> http://www.playingwithfusion.com/productview.php?pdid=22
|
||||
*
|
||||
* Copyright © 2014 Playing With Fusion, Inc.
|
||||
* SOFTWARE LICENSE AGREEMENT: This code is released under the MIT License.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
* **************************************************************************
|
||||
* REVISION HISTORY:
|
||||
* Author Date Comments
|
||||
* J. Steinlage 2014Aug20 Original version
|
||||
*
|
||||
* Playing With Fusion, Inc. invests time and resources developing open-source
|
||||
* code. Please support Playing With Fusion and continued open-source
|
||||
* development by buying products from Playing With Fusion!
|
||||
*
|
||||
* **************************************************************************
|
||||
* ADDITIONAL NOTES:
|
||||
* This file contains functions to interface with the AS3935 Franklin
|
||||
* Lightning Sensor manufactured by AMS. Originally designed for application
|
||||
* on the Arduino Uno platform.
|
||||
**************************************************************************/
|
||||
#include "PWFusion_AS3935.h"
|
||||
|
||||
PWF_AS3935::PWF_AS3935(int8_t CSx, int8_t IRQx, int8_t SIx)
|
||||
{
|
||||
_cs = CSx;
|
||||
_si = SIx;
|
||||
_irq = IRQx;
|
||||
|
||||
// initalize the chip select pins
|
||||
pinMode(_cs, OUTPUT);
|
||||
pinMode(_si, OUTPUT);
|
||||
pinMode(_irq, INPUT);
|
||||
|
||||
digitalWrite(_cs, HIGH); // set pin high to initialize/clear SPI bus
|
||||
digitalWrite(_si, LOW); // set pin low for SPI mode
|
||||
|
||||
}
|
||||
|
||||
uint8_t PWF_AS3935::_sing_reg_read(uint8_t RegAdd)
|
||||
{
|
||||
digitalWrite(_cs, LOW); // set pin low to start talking to IC
|
||||
// next pack command byte, send AS3935.... structure is shown below
|
||||
// MODE Register Address/Direct Cmd
|
||||
// B15-14 B13------------------8
|
||||
// 15 is always 0; B14, 0: Write/Direct Command, 1: Read
|
||||
SPI.transfer((RegAdd & 0x3F) | 0x40); // simple write, nothing to read back
|
||||
uint8_t RegData = SPI.transfer(0x00); // read register data from IC
|
||||
digitalWrite(_cs, HIGH); // set pin high to end SPI session
|
||||
|
||||
return RegData;
|
||||
}
|
||||
|
||||
void PWF_AS3935::_sing_reg_write(uint8_t RegAdd, uint8_t DataMask, uint8_t RegData)
|
||||
{
|
||||
// start by reading original register data (only modifying what we need to)
|
||||
uint8_t OrigRegData = _sing_reg_read(RegAdd);
|
||||
|
||||
// calculate new register data... 'delete' old targeted data, replace with new data
|
||||
// note: 'DataMask' must be bits targeted for replacement
|
||||
// add'l note: this function does NOT shift values into the proper place... they need to be there already
|
||||
uint8_t NewRegData = ((OrigRegData & ~DataMask) | (RegData & DataMask));
|
||||
|
||||
// now configure and write the updated register value
|
||||
digitalWrite(_cs, LOW); // set pin low to start talking to IC
|
||||
// next pack command byte, send AS3935.... structure is shown below
|
||||
// MODE Register Address/Direct Cmd
|
||||
// B15-14 B13------------------8
|
||||
// 15 is always 0; B14, 0: Write/Direct Command, 1: Read
|
||||
SPI.transfer((RegAdd & 0x3F)); // simple write, nothing to read back
|
||||
SPI.transfer(RegData); // write register data to IC
|
||||
digitalWrite(_cs, HIGH); // set pin high to end SPI session
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_DefInit()
|
||||
{
|
||||
_AS3935_Reset(); // reset registers to default
|
||||
}
|
||||
|
||||
void PWF_AS3935::_AS3935_Reset()
|
||||
{
|
||||
// run PRESET_DEFAULT Direct Command to set all registers in default state
|
||||
digitalWrite(_cs, LOW); // begin transfer
|
||||
SPI.transfer(0x3C); // send first byte (write, PRESET_DEFAULT register)
|
||||
SPI.transfer(0x96); // send second byte (direct command)
|
||||
digitalWrite(_cs, HIGH); // end transfer
|
||||
delay(2); // wait 2ms to complete
|
||||
}
|
||||
|
||||
void PWF_AS3935::_CalRCO()
|
||||
{
|
||||
// run ALIB_RCO Direct Command to cal internal RCO
|
||||
digitalWrite(_cs, LOW); // begin transfer
|
||||
SPI.transfer(0x3D); // send first byte (write, CALIB_RCO register)
|
||||
SPI.transfer(0x96); // send second byte (direct command)
|
||||
digitalWrite(_cs, HIGH); // end transfer
|
||||
delay(3); // wait 3ms to complete
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_PowerUp(void)
|
||||
{
|
||||
// power-up sequence based on datasheet, pg 23/27
|
||||
// register 0x00, PWD bit: 0 (clears PWD)
|
||||
_sing_reg_write(0x00, 0x01, 0x00);
|
||||
_CalRCO(); // run RCO cal cmd
|
||||
_sing_reg_write(0x08, 0x20, 0x20); // set DISP_SRCO to 1
|
||||
delay(2);
|
||||
_sing_reg_write(0x08, 0x20, 0x00); // set DISP_SRCO to 0
|
||||
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_PowerDown(void)
|
||||
{
|
||||
// register 0x00, PWD bit: 0 (sets PWD)
|
||||
_sing_reg_write(0x00, 0x01, 0x01);
|
||||
Serial.println("AS3935 powered down");
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_DisturberEn(void)
|
||||
{
|
||||
// register 0x03, PWD bit: 5 (sets MASK_DIST)
|
||||
_sing_reg_write(0x03, 0x20, 0x00);
|
||||
Serial.println("disturber detection enabled");
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_DisturberDis(void)
|
||||
{
|
||||
// register 0x03, PWD bit: 5 (sets MASK_DIST)
|
||||
_sing_reg_write(0x03, 0x20, 0x20);
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_SetIRQ_Output_Source(uint8_t irq_select)
|
||||
{
|
||||
// set interrupt source - what to displlay on IRQ pin
|
||||
// reg 0x08, bits 5 (TRCO), 6 (SRCO), 7 (LCO)
|
||||
// only one should be set at once, I think
|
||||
// 0 = NONE, 1 = TRCO, 2 = SRCO, 3 = LCO
|
||||
|
||||
if(1 == irq_select)
|
||||
{
|
||||
_sing_reg_write(0x08, 0xE0, 0x20); // set only TRCO bit
|
||||
}
|
||||
else if(2 == irq_select)
|
||||
{
|
||||
_sing_reg_write(0x08, 0xE0, 0x40); // set only SRCO bit
|
||||
}
|
||||
else if(3 == irq_select)
|
||||
{
|
||||
_sing_reg_write(0x08, 0xE0, 0x80); // set only LCO bit
|
||||
}
|
||||
else // assume 0
|
||||
{
|
||||
_sing_reg_write(0x08, 0xE0, 0x00); // clear IRQ pin display bits
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_SetTuningCaps(uint8_t cap_val)
|
||||
{
|
||||
// Assume only numbers divisible by 8 (because that's all the chip supports)
|
||||
if(120 < cap_val) // cap_value out of range, assume highest capacitance
|
||||
{
|
||||
_sing_reg_write(0x08, 0x0F, 0x0F); // set capacitance bits to maximum
|
||||
}
|
||||
else
|
||||
{
|
||||
_sing_reg_write(0x08, 0x0F, (cap_val>>3)); // set capacitance bits
|
||||
}
|
||||
Serial.print("capacitance set to 8x");
|
||||
Serial.println((_sing_reg_read(0x08) & 0x0F));
|
||||
}
|
||||
|
||||
uint8_t PWF_AS3935::AS3935_GetInterruptSrc(void)
|
||||
{
|
||||
// definition of interrupt data on table 18 of datasheet
|
||||
// for this function:
|
||||
// 0 = unknown src, 1 = lightning detected, 2 = disturber, 3 = Noise level too high
|
||||
delay(10); // wait 10ms before reading (2ms per pg 22 of datasheet)
|
||||
uint8_t int_src = (_sing_reg_read(0x03) & 0x0F); // read register, get rid of non-interrupt data
|
||||
if(0x08 == int_src)
|
||||
{
|
||||
return 1; // lightning caused interrupt
|
||||
}
|
||||
else if(0x04 == int_src)
|
||||
{
|
||||
return 2; // disturber detected
|
||||
}
|
||||
else if(0x01 == int_src)
|
||||
{
|
||||
return 3; // Noise level too high
|
||||
}
|
||||
else{return 0;} // interrupt result not expected
|
||||
|
||||
}
|
||||
|
||||
uint8_t PWF_AS3935::AS3935_GetLightningDistKm(void)
|
||||
{
|
||||
uint8_t strike_dist = (_sing_reg_read(0x07) & 0x3F); // read register, get rid of non-distance data
|
||||
return strike_dist;
|
||||
}
|
||||
|
||||
uint32_t PWF_AS3935::AS3935_GetStrikeEnergyRaw(void)
|
||||
{
|
||||
uint32_t nrgy_raw = ((_sing_reg_read(0x06) & 0x1F) << 8); // MMSB, shift 8 bits left, make room for MSB
|
||||
nrgy_raw |= _sing_reg_read(0x05); // read MSB
|
||||
nrgy_raw <= 8; // shift 8 bits left, make room for LSB
|
||||
nrgy_raw |= _sing_reg_read(0x04); // read LSB, add to others
|
||||
|
||||
return nrgy_raw;
|
||||
}
|
||||
|
||||
uint8_t PWF_AS3935::AS3935_SetMinStrikes(uint8_t min_strk)
|
||||
{
|
||||
// This function sets min strikes to the closest available number, rounding to the floor,
|
||||
// where necessary, then returns the physical value that was set. Options are 1, 5, 9 or 16 strikes.
|
||||
// see pg 22 of the datasheet for more info (#strikes in 17 min)
|
||||
if(5 > min_strk)
|
||||
{
|
||||
_sing_reg_write(0x02, 0x30, 0x00);
|
||||
return 1;
|
||||
}
|
||||
else if(9 > min_strk)
|
||||
{
|
||||
_sing_reg_write(0x02, 0x30, 0x10);
|
||||
return 5;
|
||||
}
|
||||
else if(16 > min_strk)
|
||||
{
|
||||
_sing_reg_write(0x02, 0x30, 0x20);
|
||||
return 9;
|
||||
}
|
||||
else
|
||||
{
|
||||
_sing_reg_write(0x02, 0x30, 0x30);
|
||||
return 16;
|
||||
}
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_SetIndoors(void)
|
||||
{
|
||||
// AFE settings addres 0x00, bits 5:1 (10010, based on datasheet, pg 19, table 15)
|
||||
// this is the default setting at power-up (AS3935 datasheet, table 9)
|
||||
_sing_reg_write(0x00, 0x3E, 0x24);
|
||||
Serial.println("set up for indoor operation");
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_SetOutdoors(void)
|
||||
{
|
||||
// AFE settings addres 0x00, bits 5:1 (01110, based on datasheet, pg 19, table 15)
|
||||
_sing_reg_write(0x00, 0x3E, 0x1C);
|
||||
Serial.println("set up for outdoor operation");
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_ClearStatistics(void)
|
||||
{
|
||||
// clear is accomplished by toggling CL_STAT bit 'high-low-high' (then set low to move on)
|
||||
_sing_reg_write(0x02, 0x40, 0x40); // high
|
||||
_sing_reg_write(0x02, 0x40, 0x00); // low
|
||||
_sing_reg_write(0x02, 0x40, 0x40); // high
|
||||
}
|
||||
|
||||
uint8_t PWF_AS3935::AS3935_GetNoiseFloorLvl(void)
|
||||
{
|
||||
// NF settings addres 0x01, bits 6:4
|
||||
// default setting of 010 at startup (datasheet, table 9)
|
||||
uint8_t reg_raw = _sing_reg_read(0x01); // read register 0x01
|
||||
return ((reg_raw & 0x70)>>4); // should return value from 0-7, see table 16 for info
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_SetNoiseFloorLvl(uint8_t nf_sel)
|
||||
{
|
||||
// NF settings addres 0x01, bits 6:4
|
||||
// default setting of 010 at startup (datasheet, table 9)
|
||||
if(7 >= nf_sel) // nf_sel within expected range
|
||||
{
|
||||
_sing_reg_write(0x01, 0x70, ((nf_sel & 0x07)<<4));
|
||||
}
|
||||
else
|
||||
{ // out of range, set to default (power-up value 010)
|
||||
_sing_reg_write(0x01, 0x70, 0x20);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t PWF_AS3935::AS3935_GetWatchdogThreshold(void)
|
||||
{
|
||||
// This function is used to read WDTH. It is used to increase robustness to disturbers,
|
||||
// though will make detection less efficient (see page 19, Fig 20 of datasheet)
|
||||
// WDTH register: add 0x01, bits 3:0
|
||||
// default value of 0001
|
||||
// values should only be between 0x00 and 0x0F (0 and 7)
|
||||
uint8_t reg_raw = _sing_reg_read(0x01);
|
||||
return (reg_raw & 0x0F);
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_SetWatchdogThreshold(uint8_t wdth)
|
||||
{
|
||||
// This function is used to modify WDTH. It is used to increase robustness to disturbers,
|
||||
// though will make detection less efficient (see page 19, Fig 20 of datasheet)
|
||||
// WDTH register: add 0x01, bits 3:0
|
||||
// default value of 0001
|
||||
// values should only be between 0x00 and 0x0F (0 and 7)
|
||||
_sing_reg_write(0x01, 0x0F, (wdth & 0x0F));
|
||||
}
|
||||
|
||||
uint8_t PWF_AS3935::AS3935_GetSpikeRejection(void)
|
||||
{
|
||||
// This function is used to read SREJ (spike rejection). Similar to the Watchdog threshold,
|
||||
// it is used to make the system more robust to disturbers, though will make general detection
|
||||
// less efficient (see page 20-21, especially Fig 21 of datasheet)
|
||||
// SREJ register: add 0x02, bits 3:0
|
||||
// default value of 0010
|
||||
// values should only be between 0x00 and 0x0F (0 and 7)
|
||||
uint8_t reg_raw = _sing_reg_read(0x02);
|
||||
return (reg_raw & 0x0F);
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_SetSpikeRejection(uint8_t srej)
|
||||
{
|
||||
// This function is used to modify SREJ (spike rejection). Similar to the Watchdog threshold,
|
||||
// it is used to make the system more robust to disturbers, though will make general detection
|
||||
// less efficient (see page 20-21, especially Fig 21 of datasheet)
|
||||
// WDTH register: add 0x02, bits 3:0
|
||||
// default value of 0010
|
||||
// values should only be between 0x00 and 0x0F (0 and 7)
|
||||
_sing_reg_write(0x02, 0x0F, (srej & 0x0F));
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_SetLCO_FDIV(uint8_t fdiv)
|
||||
{
|
||||
// This function sets LCO_FDIV register. This is useful in the tuning of the antenna
|
||||
// LCO_FDIV register: add 0x03, bits 7:6
|
||||
// default value: 00
|
||||
// set 0, 1, 2 or 3 for ratios of 16, 32, 64 and 128, respectively.
|
||||
// See pg 23, Table 20 for more info.
|
||||
_sing_reg_write(0x03, 0xC0, ((fdiv & 0x03) << 5));
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_PrintAllRegs(void)
|
||||
{
|
||||
Serial.print("Reg 0x00: ");
|
||||
Serial.println(_sing_reg_read(0x00));
|
||||
Serial.print("Reg 0x01: ");
|
||||
Serial.println(_sing_reg_read(0x01));
|
||||
Serial.print("Reg 0x02: ");
|
||||
Serial.println(_sing_reg_read(0x02));
|
||||
Serial.print("Reg 0x03: ");
|
||||
Serial.println(_sing_reg_read(0x03));
|
||||
Serial.print("Reg 0x04: ");
|
||||
Serial.println(_sing_reg_read(0x04));
|
||||
Serial.print("Reg 0x05: ");
|
||||
Serial.println(_sing_reg_read(0x05));
|
||||
Serial.print("Reg 0x06: ");
|
||||
Serial.println(_sing_reg_read(0x06));
|
||||
Serial.print("Reg 0x07: ");
|
||||
Serial.println(_sing_reg_read(0x07));
|
||||
Serial.print("Reg 0x08: ");
|
||||
Serial.println(_sing_reg_read(0x08));
|
||||
uint32_t nrgy_val = AS3935_GetStrikeEnergyRaw();
|
||||
Serial.println(nrgy_val);
|
||||
}
|
||||
|
||||
void PWF_AS3935::AS3935_ManualCal(uint8_t capacitance, uint8_t location, uint8_t disturber)
|
||||
{
|
||||
// start by powering up
|
||||
AS3935_PowerUp();
|
||||
|
||||
// indoors/outdoors next...
|
||||
if(1 == location) // set outdoors if 1
|
||||
{
|
||||
AS3935_SetOutdoors();
|
||||
}
|
||||
else // set indoors if anything but 1
|
||||
{
|
||||
AS3935_SetIndoors();
|
||||
}
|
||||
|
||||
// disturber cal
|
||||
if(0 == disturber) // disabled if 0
|
||||
{
|
||||
AS3935_DisturberDis();
|
||||
}
|
||||
else // enabled if anything but 0
|
||||
{
|
||||
AS3935_DisturberEn();
|
||||
}
|
||||
|
||||
AS3935_SetIRQ_Output_Source(0);
|
||||
|
||||
delay(500);
|
||||
// capacitance first... directly write value here
|
||||
AS3935_SetTuningCaps(capacitance);
|
||||
|
||||
Serial.println("AS3935 manual cal complete");
|
||||
}
|
||||
// a nice function would be to read the last 'x' strike data values....
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/***************************************************************************
|
||||
* File Name: PWFusion_AS3935.h
|
||||
* Processor/Platform: Arduino Uno R3 (tested)
|
||||
* Development Environment: Arduino 1.0.5
|
||||
*
|
||||
* Designed for use with with Playing With Fusion AS3935 Lightning Sensor
|
||||
* Breakout: SEN-39001.
|
||||
*
|
||||
* SEN-39001 (universal applications)
|
||||
* ---> http://www.playingwithfusion.com/productview.php?pdid=22
|
||||
*
|
||||
* Copyright © 2014 Playing With Fusion, Inc.
|
||||
* SOFTWARE LICENSE AGREEMENT: This code is released under the MIT License.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
* **************************************************************************
|
||||
* REVISION HISTORY:
|
||||
* Author Date Comments
|
||||
* J. Steinlage 2014Aug20 Original version
|
||||
*
|
||||
* Playing With Fusion, Inc. invests time and resources developing open-source
|
||||
* code. Please support Playing With Fusion and continued open-source
|
||||
* development by buying products from Playing With Fusion!
|
||||
*
|
||||
* **************************************************************************
|
||||
* ADDITIONAL NOTES:
|
||||
* This file contains functions to interface with the AS3935 Franklin
|
||||
* Lightning Sensor manufactured by AMS. Originally designed for application
|
||||
* on the Arduino Uno platform.
|
||||
**************************************************************************/
|
||||
#ifndef PWF_AS3935_h
|
||||
#define PWF_AS3935_h
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "avr/pgmspace.h"
|
||||
#include "util/delay.h"
|
||||
#include "stdlib.h"
|
||||
#include "SPI.h"
|
||||
|
||||
class PWF_AS3935
|
||||
{
|
||||
public:
|
||||
PWF_AS3935(int8_t CSx, int8_t IRQx, int8_t SIx);
|
||||
void AS3935_ManualCal(uint8_t capacitance, uint8_t location, uint8_t disturber);
|
||||
void AS3935_DefInit(void);
|
||||
void AS3935_PowerUp(void);
|
||||
void AS3935_PowerDown(void);
|
||||
void AS3935_DisturberEn(void);
|
||||
void AS3935_DisturberDis(void);
|
||||
void AS3935_SetIRQ_Output_Source(uint8_t irq_select);
|
||||
void AS3935_SetTuningCaps(uint8_t cap_val);
|
||||
uint8_t AS3935_GetInterruptSrc(void);
|
||||
uint8_t AS3935_GetLightningDistKm(void);
|
||||
uint32_t AS3935_GetStrikeEnergyRaw(void);
|
||||
uint8_t AS3935_SetMinStrikes(uint8_t min_strk);
|
||||
void AS3935_ClearStatistics(void);
|
||||
void AS3935_SetIndoors(void);
|
||||
void AS3935_SetOutdoors(void);
|
||||
uint8_t AS3935_GetNoiseFloorLvl(void);
|
||||
void AS3935_SetNoiseFloorLvl(uint8_t nf_sel);
|
||||
uint8_t AS3935_GetWatchdogThreshold(void);
|
||||
void AS3935_SetWatchdogThreshold(uint8_t wdth);
|
||||
uint8_t AS3935_GetSpikeRejection(void);
|
||||
void AS3935_SetSpikeRejection(uint8_t srej);
|
||||
void AS3935_SetLCO_FDIV(uint8_t fdiv);
|
||||
void AS3935_PrintAllRegs(void);
|
||||
|
||||
private:
|
||||
int8_t _cs, _irq, _si;
|
||||
uint8_t _sing_reg_read(uint8_t RegAdd);
|
||||
void _sing_reg_write(uint8_t RegAdd, uint8_t DataMask, uint8_t RegData);
|
||||
void _AS3935_Reset(void);
|
||||
void _CalRCO(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user