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);
     }
    
 
 }

Files at this revision

API Documentation at this revision

Comitter:
ftagius
Date:
Mon Jun 15 14:27:34 2015 +0000
Parent:
3:c536a9aa2a1c
Child:
5:28311803e23d
Commit message:
Add functions from the as3935 demo source code

Changed in this revision

AS3935.cpp Show annotated file Show diff for this revision Revisions of this file
AS3935.h Show annotated file Show diff for this revision Revisions of this file
--- a/AS3935.cpp	Thu Jun 11 19:45:11 2015 +0000
+++ b/AS3935.cpp	Mon Jun 15 14:27:34 2015 +0000
@@ -95,6 +95,21 @@
     wait_ms(2);
 } 
 
+void AS3935::presetDefault()
+ {
+    m_Cs = 0;
+    m_Spi.write(0x3C);
+    m_Spi.write(0x96);
+    m_Cs = 1;
+    wait_ms(2);
+}
+
+void AS3935::init()
+ {
+    presetDefault();
+    registerWrite(AS3935_WDTH, 0x04);  // set WDTH level to 4 
+}
+ 
 void AS3935::powerDown()
 {
     registerWrite(AS3935_PWD,1);
@@ -216,9 +231,9 @@
     m_Spi.write(0x96);
     m_Cs = 1;
     wait_ms(1);                          // wait 1ms
-    registerWrite(0x08, 0x40, 0x40);    // ERRATA: after power down mode
+    registerWrite(AS3935_DISP_SRCO, 0x40);    // ERRATA: after power down mode
     wait_ms(2);                                   // wait 1ms
-    registerWrite(0x08, 0x40, 0x00);    // ERRATA: after power down mode
+    registerWrite(AS3935_DISP_SRCO, 0x00);    // ERRATA: after power down mode
     trco=registerRead(0x3A, 0x80);        // Read out Calibration of TRCO done
     srco=registerRead(0x3B, 0x80);        // Readout Calibration of SRCO done
     if(trco != 0x00 && srco != 0x00)
--- a/AS3935.h	Thu Jun 11 19:45:11 2015 +0000
+++ b/AS3935.h	Mon Jun 15 14:27:34 2015 +0000
@@ -70,76 +70,82 @@
     //the rest of the register remains intact
     void registerWrite(char reg, char mask, char data);
         
-        //read specified register using specified bitmask and return value aligned     
-        //to lsb, i.e. if value to be read is in a middle of register, function     
-        //reads register and then aligns lsb of value to lsb of byte
-        char registerRead(char reg, char mask);
+    //read specified register using specified bitmask and return value aligned     
+    //to lsb, i.e. if value to be read is in a middle of register, function     
+    //reads register and then aligns lsb of value to lsb of byte
+    char registerRead(char reg, char mask);
         
-        //reset all the registers on chip to default values
-        void reset();
+    //reset all the registers on chip to default values
+    void reset();
         
-        //put chip into power down mode
-        void powerDown();
-        
-        //bring chip out of power down mode and perform RCO calibration
-        void powerUp();
+    //set preset defaults
+    void presetDefault();
         
-        //return interrupt source, bitmask, 0b0001 - noise, 0b0100 - disturber,     
-        //0b1000 - lightning
-        int interruptSource();
-        
-        //disable indication of disturbers
-        void disableDisturbers();
-        
-        //enable indication of distrubers
-        void enableDisturbers();
-        
-        //return number of lightnings that need to be detected in 17 minute period     
-        //before interrupt is issued
-        int getMinimumLightnings();
-        
-        //set number of lightnings that need to be detected in 17 minute period     
-        //before interrupt is issued
-        int setMinimumLightnings(int minlightning);
+    //initialization
+    void init();
+    
+    //put chip into power down mode
+    void powerDown();
+    
+    //bring chip out of power down mode and perform RCO calibration
+    void powerUp();
+    
+    //return interrupt source, bitmask, 0b0001 - noise, 0b0100 - disturber,     
+    //0b1000 - lightning
+    int interruptSource();
         
-        //return distance to lightning in kilometers, 1 means storm is overhead,     
-        //63 means it is too far to reliably calculate distance
-        int lightningDistanceKm();
-        
-        // load gain preset to operate indoors
-        void setIndoors();
+    //disable indication of disturbers
+    void disableDisturbers();
+    
+    //enable indication of distrubers
+    void enableDisturbers();
+    
+    //return number of lightnings that need to be detected in 17 minute period     
+    //before interrupt is issued
+    int getMinimumLightnings();
+    
+    //set number of lightnings that need to be detected in 17 minute period     
+    //before interrupt is issued
+    int setMinimumLightnings(int minlightning);
+    
+    //return distance to lightning in kilometers, 1 means storm is overhead,     
+    //63 means it is too far to reliably calculate distance
+    int lightningDistanceKm();
         
-        //load gain preset to operate outdoors
-        void setOutdoors();
-        
-        //return noise floor setting - refer to datasheet for meaning and range
-        int getNoiseFloor();
+    // load gain preset to operate indoors
+    void setIndoors();
         
-        //set noise floor setting
-        int setNoiseFloor(int noisefloor);
-        
-        //return spike rejection value - refer to datasheet for meaning and range
-        int getSpikeRejection();
+    //load gain preset to operate outdoors
+    void setOutdoors();
         
-        //set spike rejection value
-        int setSpikeRejection(int srej);
+    //return noise floor setting - refer to datasheet for meaning and range
+    int getNoiseFloor();
+        
+    //set noise floor setting
+    int setNoiseFloor(int noisefloor);
         
-        //return watchdog threshold value - refer to datasheet for meaning and range
-        int getWatchdogThreshold();
+    //return spike rejection value - refer to datasheet for meaning and range
+    int getSpikeRejection();
         
-        //set watchdog threshold value
-        int setWatchdogThreshold(int wdth);
+    //set spike rejection value
+    int setSpikeRejection(int srej);
         
-        //return tune Capacity value 
-        int getTuneCap();
+    //return watchdog threshold value - refer to datasheet for meaning and range
+    int getWatchdogThreshold();
+        
+    //set watchdog threshold value
+    int setWatchdogThreshold(int wdth);
         
-        //set tune Capacity value
-        int setTuneCap(int cap);
+    //return tune Capacity value 
+    int getTuneCap();
         
-        //clear internal accumulated lightning statistics
-        void clearStats();
+    //set tune Capacity value
+    int setTuneCap(int cap);
+        
+    //clear internal accumulated lightning statistics
+    void clearStats();
 
-        int calibrateRCOs (void);
+    int calibrateRCOs (void);
 
         
     /** Attach a function, lightning interrupt