AS3935 Lightning sensor library
Fork of AS3935 by
include the mbed library with this snippet
#include "mbed.h" #include "AS3935.h" // frdm-kl25z sd card connections for spi1 // ------------------------------------------------ // Header -- kl25z -- SD/MMC // J2-20 -- PTE1 -- MOSI // J9-13 -- PTE4 -- CS // J2-14 -- GND -- Vss (GND) // J9-9 -- PTE2 -- SCK // J9-11 -- PTE3 -- MISO AS3935 ld(PTE1, PTE3, PTE2, PTE4, "ld", 100000); // MOSI, MISO, SCK, CS, SPI bus freq (hz) InterruptIn IntLightning(PTA12); //IRQ AS3935 DigitalOut led1(LED_RED); Serial pc(USBTX, USBRX); void DetectLightning() { char OriginInt; wait_ms(2); OriginInt = ld.interruptSource(); if (OriginInt == 1) { // pc.printf(" Noise level too high\r\n"); } if (OriginInt == 4) { // pc.printf(" Disturber\r\n"); } if (OriginInt == 8) { // detection // pc.printf(" Lightning detection\r\n"); pc.printf(" Lightning detection, distance=%dkm\r\n", ld.lightningDistanceKm()); ld.clearStats(); } } int main() { pc.baud(9600); pc.printf("\r\nstart lightning detector\r\n"); //initialisations ld.reset(); ld.setTuneCap(5); // 500kHz ld.powerUp(); ld.setIndoors(); ld.setMinimumLightnings(1); //ld.setSpikeRejection(2); ld.setNoiseFloor(2); ld.disableDisturbers(); //ld.enableDisturbers(); ld.setWatchdogThreshold(2); wait_ms(10); IntLightning.rise(&DetectLightning); int MinBlysk = ld.getMinimumLightnings(); int Noise = ld.getNoiseFloor(); int TuneCap = ld.getTuneCap(); int SpikeRej = ld.getSpikeRejection(); int WatchDog = ld.getWatchdogThreshold(); pc.printf(" Min wylad: %i", MinBlysk); pc.printf("\r\n"); pc.printf(" Noise: %i", Noise); pc.printf("\r\n"); pc.printf(" Tune CAP: %i", TuneCap); pc.printf("\r\n"); pc.printf(" Spike rej: %i", SpikeRej); pc.printf("\r\n"); pc.printf(" Watchdog: %i", WatchDog); pc.printf("\r\n"); while(1) { led1 = ! led1; wait(0.2); } }
AS3935.h@10:bf33e2946bab, 2015-06-23 (annotated)
- Committer:
- cmkachur
- Date:
- Tue Jun 23 21:01:27 2015 +0000
- Revision:
- 10:bf33e2946bab
- Parent:
- 9:19a323b1c508
- Child:
- 11:ee2e7a573227
Add method for reading energy of lightning strike. Add energy data to the log file. Remove call to powerUp() since this was only setting outdoor mode which was done elsewhere.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
casper | 0:346c723cac97 | 1 | /* |
casper | 0:346c723cac97 | 2 | AS3935.h - AS3935 Franklin Lightning Sensor™ IC by AMS library |
casper | 0:346c723cac97 | 3 | Copyright (c) 2012 Raivis Rengelis (raivis [at] rrkb.lv). All rights reserved. |
casper | 0:346c723cac97 | 4 | Portée sur MBED par Valentin |
casper | 0:346c723cac97 | 5 | |
casper | 0:346c723cac97 | 6 | This library is free software; you can redistribute it and/or |
casper | 0:346c723cac97 | 7 | modify it under the terms of the GNU Lesser General Public |
casper | 0:346c723cac97 | 8 | License as published by the Free Software Foundation; either |
casper | 0:346c723cac97 | 9 | version 3 of the License, or (at your option) any later version. |
casper | 0:346c723cac97 | 10 | |
casper | 0:346c723cac97 | 11 | This library is distributed in the hope that it will be useful, |
casper | 0:346c723cac97 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
casper | 0:346c723cac97 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
casper | 0:346c723cac97 | 14 | Lesser General Public License for more details. |
casper | 0:346c723cac97 | 15 | |
casper | 0:346c723cac97 | 16 | You should have received a copy of the GNU Lesser General Public |
casper | 0:346c723cac97 | 17 | License along with this library; if not, write to the Free Software |
casper | 0:346c723cac97 | 18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
casper | 0:346c723cac97 | 19 | */ |
casper | 0:346c723cac97 | 20 | |
ftagius | 1:f2d9ed33c276 | 21 | #ifndef _AS3935_H |
ftagius | 1:f2d9ed33c276 | 22 | #define _AS3935_H |
casper | 0:346c723cac97 | 23 | |
casper | 0:346c723cac97 | 24 | #include "mbed.h" |
ftagius | 1:f2d9ed33c276 | 25 | #include <stdint.h> |
ftagius | 1:f2d9ed33c276 | 26 | |
casper | 0:346c723cac97 | 27 | //#include "i2c.hpp" |
casper | 0:346c723cac97 | 28 | |
casper | 0:346c723cac97 | 29 | // register access macros - register address, bitmask |
casper | 0:346c723cac97 | 30 | #define AS3935_AFE_GB 0x00, 0x3E |
casper | 0:346c723cac97 | 31 | #define AS3935_PWD 0x00, 0x01 |
casper | 0:346c723cac97 | 32 | #define AS3935_NF_LEV 0x01, 0x70 |
casper | 0:346c723cac97 | 33 | #define AS3935_WDTH 0x01, 0x0F |
casper | 0:346c723cac97 | 34 | #define AS3935_CL_STAT 0x02, 0x40 |
casper | 0:346c723cac97 | 35 | #define AS3935_MIN_NUM_LIGH 0x02, 0x30 |
casper | 0:346c723cac97 | 36 | #define AS3935_SREJ 0x02, 0x0F |
casper | 0:346c723cac97 | 37 | #define AS3935_LCO_FDIV 0x03, 0xC0 |
casper | 0:346c723cac97 | 38 | #define AS3935_MASK_DIST 0x03, 0x20 |
casper | 0:346c723cac97 | 39 | #define AS3935_INT 0x03, 0x0F |
cmkachur | 10:bf33e2946bab | 40 | #define AS3935_S_LIG_L 0x04, 0xff |
cmkachur | 10:bf33e2946bab | 41 | #define AS3935_S_LIG_M 0x05, 0xff |
cmkachur | 10:bf33e2946bab | 42 | #define AS3935_S_LIG_MM 0x06, 0x1f |
casper | 0:346c723cac97 | 43 | #define AS3935_DISTANCE 0x07, 0x3F |
casper | 0:346c723cac97 | 44 | #define AS3935_DISP_LCO 0x08, 0x80 |
casper | 0:346c723cac97 | 45 | #define AS3935_DISP_SRCO 0x08, 0x40 |
casper | 0:346c723cac97 | 46 | #define AS3935_DISP_TRCO 0x08, 0x20 |
casper | 0:346c723cac97 | 47 | #define AS3935_TUN_CAP 0x08, 0x0F |
casper | 0:346c723cac97 | 48 | |
casper | 0:346c723cac97 | 49 | // other constants |
ftagius | 6:c9b9e7d3bced | 50 | #define AS3935_AFE_INDOOR 0x10 |
casper | 0:346c723cac97 | 51 | #define AS3935_AFE_OUTDOOR 0x0E |
casper | 0:346c723cac97 | 52 | |
casper | 0:346c723cac97 | 53 | class AS3935 { |
casper | 0:346c723cac97 | 54 | |
casper | 0:346c723cac97 | 55 | public: |
ftagius | 3:c536a9aa2a1c | 56 | |
ftagius | 1:f2d9ed33c276 | 57 | /** Create a virtual file system for accessing SD/MMC cards via SPI |
ftagius | 1:f2d9ed33c276 | 58 | * |
ftagius | 3:c536a9aa2a1c | 59 | * @param mosi The SPI data out pin. |
ftagius | 1:f2d9ed33c276 | 60 | * @param miso The SPI data in pin. |
ftagius | 1:f2d9ed33c276 | 61 | * @param sclk The SPI clock pin. |
ftagius | 1:f2d9ed33c276 | 62 | * @param cs The SPI chip select pin. |
ftagius | 3:c536a9aa2a1c | 63 | * @param name The name used to access the spi bus. |
ftagius | 1:f2d9ed33c276 | 64 | * @param hz The SPI bus frequency (defaults to 1MHz). |
ftagius | 3:c536a9aa2a1c | 65 | */ |
ftagius | 1:f2d9ed33c276 | 66 | |
ftagius | 3:c536a9aa2a1c | 67 | AS3935(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, int hz = 1000000); |
ftagius | 3:c536a9aa2a1c | 68 | |
ftagius | 1:f2d9ed33c276 | 69 | //destruction |
ftagius | 1:f2d9ed33c276 | 70 | //~AS3935(); |
ftagius | 1:f2d9ed33c276 | 71 | |
ftagius | 1:f2d9ed33c276 | 72 | //write to specified register specified data using specified bitmask, |
ftagius | 1:f2d9ed33c276 | 73 | //the rest of the register remains intact |
ftagius | 1:f2d9ed33c276 | 74 | void registerWrite(char reg, char mask, char data); |
casper | 0:346c723cac97 | 75 | |
ftagius | 4:ab1f1b65468b | 76 | //read specified register using specified bitmask and return value aligned |
ftagius | 4:ab1f1b65468b | 77 | //to lsb, i.e. if value to be read is in a middle of register, function |
ftagius | 4:ab1f1b65468b | 78 | //reads register and then aligns lsb of value to lsb of byte |
ftagius | 4:ab1f1b65468b | 79 | char registerRead(char reg, char mask); |
casper | 0:346c723cac97 | 80 | |
ftagius | 4:ab1f1b65468b | 81 | //reset all the registers on chip to default values |
ftagius | 4:ab1f1b65468b | 82 | void reset(); |
casper | 0:346c723cac97 | 83 | |
ftagius | 4:ab1f1b65468b | 84 | //set preset defaults |
ftagius | 4:ab1f1b65468b | 85 | void presetDefault(); |
casper | 0:346c723cac97 | 86 | |
ftagius | 4:ab1f1b65468b | 87 | //initialization |
ftagius | 4:ab1f1b65468b | 88 | void init(); |
ftagius | 4:ab1f1b65468b | 89 | |
ftagius | 5:28311803e23d | 90 | // replicate the acurite sequece |
ftagius | 5:28311803e23d | 91 | void acurite(); |
ftagius | 5:28311803e23d | 92 | |
ftagius | 4:ab1f1b65468b | 93 | //put chip into power down mode |
ftagius | 4:ab1f1b65468b | 94 | void powerDown(); |
ftagius | 4:ab1f1b65468b | 95 | |
ftagius | 4:ab1f1b65468b | 96 | //bring chip out of power down mode and perform RCO calibration |
ftagius | 4:ab1f1b65468b | 97 | void powerUp(); |
ftagius | 4:ab1f1b65468b | 98 | |
ftagius | 4:ab1f1b65468b | 99 | //return interrupt source, bitmask, 0b0001 - noise, 0b0100 - disturber, |
ftagius | 4:ab1f1b65468b | 100 | //0b1000 - lightning |
ftagius | 4:ab1f1b65468b | 101 | int interruptSource(); |
casper | 0:346c723cac97 | 102 | |
ftagius | 4:ab1f1b65468b | 103 | //disable indication of disturbers |
ftagius | 4:ab1f1b65468b | 104 | void disableDisturbers(); |
ftagius | 4:ab1f1b65468b | 105 | |
ftagius | 4:ab1f1b65468b | 106 | //enable indication of distrubers |
ftagius | 4:ab1f1b65468b | 107 | void enableDisturbers(); |
ftagius | 4:ab1f1b65468b | 108 | |
ftagius | 4:ab1f1b65468b | 109 | //return number of lightnings that need to be detected in 17 minute period |
ftagius | 4:ab1f1b65468b | 110 | //before interrupt is issued |
ftagius | 4:ab1f1b65468b | 111 | int getMinimumLightnings(); |
ftagius | 4:ab1f1b65468b | 112 | |
ftagius | 4:ab1f1b65468b | 113 | //set number of lightnings that need to be detected in 17 minute period |
ftagius | 4:ab1f1b65468b | 114 | //before interrupt is issued |
ftagius | 4:ab1f1b65468b | 115 | int setMinimumLightnings(int minlightning); |
ftagius | 4:ab1f1b65468b | 116 | |
ftagius | 4:ab1f1b65468b | 117 | //return distance to lightning in kilometers, 1 means storm is overhead, |
ftagius | 4:ab1f1b65468b | 118 | //63 means it is too far to reliably calculate distance |
ftagius | 4:ab1f1b65468b | 119 | int lightningDistanceKm(); |
casper | 0:346c723cac97 | 120 | |
ftagius | 4:ab1f1b65468b | 121 | // load gain preset to operate indoors |
ftagius | 4:ab1f1b65468b | 122 | void setIndoors(); |
casper | 0:346c723cac97 | 123 | |
ftagius | 4:ab1f1b65468b | 124 | //load gain preset to operate outdoors |
ftagius | 4:ab1f1b65468b | 125 | void setOutdoors(); |
ftagius | 6:c9b9e7d3bced | 126 | |
ftagius | 6:c9b9e7d3bced | 127 | //get gain preset |
ftagius | 6:c9b9e7d3bced | 128 | int getGain(); |
casper | 0:346c723cac97 | 129 | |
ftagius | 4:ab1f1b65468b | 130 | //return noise floor setting - refer to datasheet for meaning and range |
ftagius | 4:ab1f1b65468b | 131 | int getNoiseFloor(); |
ftagius | 4:ab1f1b65468b | 132 | |
ftagius | 4:ab1f1b65468b | 133 | //set noise floor setting |
ftagius | 4:ab1f1b65468b | 134 | int setNoiseFloor(int noisefloor); |
casper | 0:346c723cac97 | 135 | |
ftagius | 4:ab1f1b65468b | 136 | //return spike rejection value - refer to datasheet for meaning and range |
ftagius | 4:ab1f1b65468b | 137 | int getSpikeRejection(); |
casper | 0:346c723cac97 | 138 | |
ftagius | 4:ab1f1b65468b | 139 | //set spike rejection value |
ftagius | 4:ab1f1b65468b | 140 | int setSpikeRejection(int srej); |
casper | 0:346c723cac97 | 141 | |
ftagius | 4:ab1f1b65468b | 142 | //return watchdog threshold value - refer to datasheet for meaning and range |
ftagius | 4:ab1f1b65468b | 143 | int getWatchdogThreshold(); |
ftagius | 4:ab1f1b65468b | 144 | |
ftagius | 4:ab1f1b65468b | 145 | //set watchdog threshold value |
ftagius | 4:ab1f1b65468b | 146 | int setWatchdogThreshold(int wdth); |
casper | 0:346c723cac97 | 147 | |
ftagius | 4:ab1f1b65468b | 148 | //return tune Capacity value |
ftagius | 4:ab1f1b65468b | 149 | int getTuneCap(); |
casper | 0:346c723cac97 | 150 | |
ftagius | 4:ab1f1b65468b | 151 | //set tune Capacity value |
ftagius | 4:ab1f1b65468b | 152 | int setTuneCap(int cap); |
ftagius | 4:ab1f1b65468b | 153 | |
ftagius | 4:ab1f1b65468b | 154 | //clear internal accumulated lightning statistics |
ftagius | 4:ab1f1b65468b | 155 | void clearStats(); |
ftagius | 3:c536a9aa2a1c | 156 | |
cmkachur | 9:19a323b1c508 | 157 | int calibrateRCOs (InterruptIn &intrIn); |
ftagius | 3:c536a9aa2a1c | 158 | |
cmkachur | 7:0d2586164d5b | 159 | unsigned long tuneAntenna(InterruptIn &intrIn); |
cmkachur | 10:bf33e2946bab | 160 | unsigned long getEnergy(void); |
cmkachur | 9:19a323b1c508 | 161 | |
ftagius | 1:f2d9ed33c276 | 162 | /** Attach a function, lightning interrupt |
ftagius | 1:f2d9ed33c276 | 163 | * @param fptr pointer to a void function, or 0 to set as none |
ftagius | 1:f2d9ed33c276 | 164 | */ |
ftagius | 1:f2d9ed33c276 | 165 | void attach(void (*fptr)(void)) { |
ftagius | 1:f2d9ed33c276 | 166 | _func.attach(fptr); |
ftagius | 1:f2d9ed33c276 | 167 | } |
ftagius | 1:f2d9ed33c276 | 168 | /** Attach a member function, lightning interrupt |
ftagius | 1:f2d9ed33c276 | 169 | * @param tptr pointer to the object to call the member function on |
ftagius | 1:f2d9ed33c276 | 170 | * @param mptr pointer to the member function to be called |
ftagius | 1:f2d9ed33c276 | 171 | */ |
ftagius | 1:f2d9ed33c276 | 172 | template<typename T> |
ftagius | 1:f2d9ed33c276 | 173 | void attach(T *tptr, void (T::*mptr)(void)) { |
ftagius | 1:f2d9ed33c276 | 174 | _func.attach(tptr, mptr); |
ftagius | 1:f2d9ed33c276 | 175 | } |
cmkachur | 9:19a323b1c508 | 176 | |
cmkachur | 9:19a323b1c508 | 177 | |
casper | 0:346c723cac97 | 178 | private: |
ftagius | 1:f2d9ed33c276 | 179 | //I2C i2c; |
ftagius | 1:f2d9ed33c276 | 180 | //DigitalOut _irq; |
ftagius | 1:f2d9ed33c276 | 181 | SPI m_Spi; |
ftagius | 1:f2d9ed33c276 | 182 | DigitalOut m_Cs; |
ftagius | 3:c536a9aa2a1c | 183 | //InterruptIn m_Cd; |
ftagius | 1:f2d9ed33c276 | 184 | |
ftagius | 1:f2d9ed33c276 | 185 | int m_CdAssert; |
ftagius | 1:f2d9ed33c276 | 186 | const int m_FREQ; |
ftagius | 1:f2d9ed33c276 | 187 | int _adress; |
ftagius | 1:f2d9ed33c276 | 188 | FunctionPointer _func; |
ftagius | 1:f2d9ed33c276 | 189 | char _rawRegisterRead(char reg); |
ftagius | 1:f2d9ed33c276 | 190 | char _SPITransfer2(char high, char low); |
ftagius | 1:f2d9ed33c276 | 191 | char _ffsz(char mask); |
cmkachur | 9:19a323b1c508 | 192 | |
cmkachur | 9:19a323b1c508 | 193 | |
cmkachur | 9:19a323b1c508 | 194 | |
cmkachur | 9:19a323b1c508 | 195 | |
casper | 0:346c723cac97 | 196 | }; |
casper | 0:346c723cac97 | 197 | |
ftagius | 1:f2d9ed33c276 | 198 | /* !_AS3935_H_ */ |
casper | 0:346c723cac97 | 199 | #endif |