AS3935 Lightning sensor library

Dependents:   zeus

Fork of AS3935 by valentin spanu

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);
     }
    
 
 }
Committer:
ftagius
Date:
Tue Jun 16 14:21:19 2015 +0000
Revision:
6:c9b9e7d3bced
Parent:
5:28311803e23d
Child:
8:60e1b6b39e1c
add routine to get gain setting;  change value for indoor setting

Who changed what in which revision?

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