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@1:f2d9ed33c276, 2015-06-11 (annotated)
- Committer:
- ftagius
- Date:
- Thu Jun 11 15:47:29 2015 +0000
- Revision:
- 1:f2d9ed33c276
- Parent:
- AS3935.hpp@0:346c723cac97
- Child:
- 3:c536a9aa2a1c
Convert AS3935 library to SPI
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 |
casper | 0:346c723cac97 | 40 | #define AS3935_DISTANCE 0x07, 0x3F |
casper | 0:346c723cac97 | 41 | #define AS3935_DISP_LCO 0x08, 0x80 |
casper | 0:346c723cac97 | 42 | #define AS3935_DISP_SRCO 0x08, 0x40 |
casper | 0:346c723cac97 | 43 | #define AS3935_DISP_TRCO 0x08, 0x20 |
casper | 0:346c723cac97 | 44 | #define AS3935_TUN_CAP 0x08, 0x0F |
casper | 0:346c723cac97 | 45 | |
casper | 0:346c723cac97 | 46 | // other constants |
casper | 0:346c723cac97 | 47 | #define AS3935_AFE_INDOOR 0x12 |
casper | 0:346c723cac97 | 48 | #define AS3935_AFE_OUTDOOR 0x0E |
casper | 0:346c723cac97 | 49 | |
casper | 0:346c723cac97 | 50 | class AS3935 { |
casper | 0:346c723cac97 | 51 | |
casper | 0:346c723cac97 | 52 | public: |
ftagius | 1:f2d9ed33c276 | 53 | /* |
ftagius | 1:f2d9ed33c276 | 54 | * Initializes i2c interface and IRQ pin |
ftagius | 1:f2d9ed33c276 | 55 | */ |
ftagius | 1:f2d9ed33c276 | 56 | // AS3935(PinName sda, PinName scl, int adresse, int hz = 1000000); |
ftagius | 1:f2d9ed33c276 | 57 | |
casper | 0:346c723cac97 | 58 | |
ftagius | 1:f2d9ed33c276 | 59 | /** Create a virtual file system for accessing SD/MMC cards via SPI |
ftagius | 1:f2d9ed33c276 | 60 | * |
ftagius | 1:f2d9ed33c276 | 61 | * @param mosi The SPI data out pin. |
ftagius | 1:f2d9ed33c276 | 62 | * @param miso The SPI data in pin. |
ftagius | 1:f2d9ed33c276 | 63 | * @param sclk The SPI clock pin. |
ftagius | 1:f2d9ed33c276 | 64 | * @param cs The SPI chip select pin. |
ftagius | 1:f2d9ed33c276 | 65 | * @param name The name used to access the virtual filesystem. |
ftagius | 1:f2d9ed33c276 | 66 | * @param cd The card detect pin. |
ftagius | 1:f2d9ed33c276 | 67 | * @param cdtype The type of card detect switch. |
ftagius | 1:f2d9ed33c276 | 68 | * @param hz The SPI bus frequency (defaults to 1MHz). |
ftagius | 1:f2d9ed33c276 | 69 | */ |
ftagius | 1:f2d9ed33c276 | 70 | |
ftagius | 1:f2d9ed33c276 | 71 | AS3935(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd = NC, int hz = 10000); |
casper | 0:346c723cac97 | 72 | |
ftagius | 1:f2d9ed33c276 | 73 | //destruction |
ftagius | 1:f2d9ed33c276 | 74 | //~AS3935(); |
ftagius | 1:f2d9ed33c276 | 75 | |
ftagius | 1:f2d9ed33c276 | 76 | //write to specified register specified data using specified bitmask, |
ftagius | 1:f2d9ed33c276 | 77 | //the rest of the register remains intact |
ftagius | 1:f2d9ed33c276 | 78 | void registerWrite(char reg, char mask, char data); |
casper | 0:346c723cac97 | 79 | |
casper | 0:346c723cac97 | 80 | //read specified register using specified bitmask and return value aligned |
casper | 0:346c723cac97 | 81 | //to lsb, i.e. if value to be read is in a middle of register, function |
casper | 0:346c723cac97 | 82 | //reads register and then aligns lsb of value to lsb of byte |
casper | 0:346c723cac97 | 83 | char registerRead(char reg, char mask); |
casper | 0:346c723cac97 | 84 | |
casper | 0:346c723cac97 | 85 | //reset all the registers on chip to default values |
casper | 0:346c723cac97 | 86 | void reset(); |
casper | 0:346c723cac97 | 87 | |
casper | 0:346c723cac97 | 88 | //put chip into power down mode |
casper | 0:346c723cac97 | 89 | void powerDown(); |
casper | 0:346c723cac97 | 90 | |
casper | 0:346c723cac97 | 91 | //bring chip out of power down mode and perform RCO calibration |
casper | 0:346c723cac97 | 92 | void powerUp(); |
casper | 0:346c723cac97 | 93 | |
casper | 0:346c723cac97 | 94 | //return interrupt source, bitmask, 0b0001 - noise, 0b0100 - disturber, |
casper | 0:346c723cac97 | 95 | //0b1000 - lightning |
casper | 0:346c723cac97 | 96 | int interruptSource(); |
casper | 0:346c723cac97 | 97 | |
casper | 0:346c723cac97 | 98 | //disable indication of disturbers |
casper | 0:346c723cac97 | 99 | void disableDisturbers(); |
casper | 0:346c723cac97 | 100 | |
casper | 0:346c723cac97 | 101 | //enable indication of distrubers |
casper | 0:346c723cac97 | 102 | void enableDisturbers(); |
casper | 0:346c723cac97 | 103 | |
casper | 0:346c723cac97 | 104 | //return number of lightnings that need to be detected in 17 minute period |
casper | 0:346c723cac97 | 105 | //before interrupt is issued |
casper | 0:346c723cac97 | 106 | int getMinimumLightnings(); |
casper | 0:346c723cac97 | 107 | |
casper | 0:346c723cac97 | 108 | //set number of lightnings that need to be detected in 17 minute period |
casper | 0:346c723cac97 | 109 | //before interrupt is issued |
casper | 0:346c723cac97 | 110 | int setMinimumLightnings(int minlightning); |
casper | 0:346c723cac97 | 111 | |
casper | 0:346c723cac97 | 112 | //return distance to lightning in kilometers, 1 means storm is overhead, |
casper | 0:346c723cac97 | 113 | //63 means it is too far to reliably calculate distance |
casper | 0:346c723cac97 | 114 | int lightningDistanceKm(); |
casper | 0:346c723cac97 | 115 | |
casper | 0:346c723cac97 | 116 | // load gain preset to operate indoors |
casper | 0:346c723cac97 | 117 | void setIndoors(); |
casper | 0:346c723cac97 | 118 | |
casper | 0:346c723cac97 | 119 | //load gain preset to operate outdoors |
casper | 0:346c723cac97 | 120 | void setOutdoors(); |
casper | 0:346c723cac97 | 121 | |
casper | 0:346c723cac97 | 122 | //return noise floor setting - refer to datasheet for meaning and range |
casper | 0:346c723cac97 | 123 | int getNoiseFloor(); |
casper | 0:346c723cac97 | 124 | |
casper | 0:346c723cac97 | 125 | //set noise floor setting |
casper | 0:346c723cac97 | 126 | int setNoiseFloor(int noisefloor); |
casper | 0:346c723cac97 | 127 | |
casper | 0:346c723cac97 | 128 | //return spike rejection value - refer to datasheet for meaning and range |
casper | 0:346c723cac97 | 129 | int getSpikeRejection(); |
casper | 0:346c723cac97 | 130 | |
casper | 0:346c723cac97 | 131 | //set spike rejection value |
casper | 0:346c723cac97 | 132 | int setSpikeRejection(int srej); |
casper | 0:346c723cac97 | 133 | |
casper | 0:346c723cac97 | 134 | //return watchdog threshold value - refer to datasheet for meaning and range |
casper | 0:346c723cac97 | 135 | int getWatchdogThreshold(); |
casper | 0:346c723cac97 | 136 | |
casper | 0:346c723cac97 | 137 | //set watchdog threshold value |
casper | 0:346c723cac97 | 138 | int setWatchdogThreshold(int wdth); |
casper | 0:346c723cac97 | 139 | |
casper | 0:346c723cac97 | 140 | //return tune Capacity value |
casper | 0:346c723cac97 | 141 | int getTuneCap(); |
casper | 0:346c723cac97 | 142 | |
casper | 0:346c723cac97 | 143 | //set tune Capacity value |
casper | 0:346c723cac97 | 144 | int setTuneCap(int cap); |
casper | 0:346c723cac97 | 145 | |
casper | 0:346c723cac97 | 146 | //clear internal accumulated lightning statistics |
casper | 0:346c723cac97 | 147 | void clearStats(); |
ftagius | 1:f2d9ed33c276 | 148 | |
ftagius | 1:f2d9ed33c276 | 149 | /** Attach a function, lightning interrupt |
ftagius | 1:f2d9ed33c276 | 150 | * @param fptr pointer to a void function, or 0 to set as none |
ftagius | 1:f2d9ed33c276 | 151 | */ |
ftagius | 1:f2d9ed33c276 | 152 | void attach(void (*fptr)(void)) { |
ftagius | 1:f2d9ed33c276 | 153 | _func.attach(fptr); |
ftagius | 1:f2d9ed33c276 | 154 | } |
ftagius | 1:f2d9ed33c276 | 155 | /** Attach a member function, lightning interrupt |
ftagius | 1:f2d9ed33c276 | 156 | * @param tptr pointer to the object to call the member function on |
ftagius | 1:f2d9ed33c276 | 157 | * @param mptr pointer to the member function to be called |
ftagius | 1:f2d9ed33c276 | 158 | */ |
ftagius | 1:f2d9ed33c276 | 159 | template<typename T> |
ftagius | 1:f2d9ed33c276 | 160 | void attach(T *tptr, void (T::*mptr)(void)) { |
ftagius | 1:f2d9ed33c276 | 161 | _func.attach(tptr, mptr); |
ftagius | 1:f2d9ed33c276 | 162 | } |
ftagius | 1:f2d9ed33c276 | 163 | |
casper | 0:346c723cac97 | 164 | private: |
ftagius | 1:f2d9ed33c276 | 165 | //I2C i2c; |
ftagius | 1:f2d9ed33c276 | 166 | //DigitalOut _irq; |
ftagius | 1:f2d9ed33c276 | 167 | SPI m_Spi; |
ftagius | 1:f2d9ed33c276 | 168 | DigitalOut m_Cs; |
ftagius | 1:f2d9ed33c276 | 169 | InterruptIn m_Cd; |
ftagius | 1:f2d9ed33c276 | 170 | |
ftagius | 1:f2d9ed33c276 | 171 | int m_CdAssert; |
ftagius | 1:f2d9ed33c276 | 172 | const int m_FREQ; |
ftagius | 1:f2d9ed33c276 | 173 | int _adress; |
ftagius | 1:f2d9ed33c276 | 174 | FunctionPointer _func; |
ftagius | 1:f2d9ed33c276 | 175 | char _rawRegisterRead(char reg); |
ftagius | 1:f2d9ed33c276 | 176 | char _SPITransfer2(char high, char low); |
ftagius | 1:f2d9ed33c276 | 177 | char _ffsz(char mask); |
casper | 0:346c723cac97 | 178 | }; |
casper | 0:346c723cac97 | 179 | |
ftagius | 1:f2d9ed33c276 | 180 | /* !_AS3935_H_ */ |
casper | 0:346c723cac97 | 181 | #endif |