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);
     }
    
 
 }
Revision:
7:0d2586164d5b
Parent:
5:28311803e23d
Child:
8:60e1b6b39e1c
diff -r 28311803e23d -r 0d2586164d5b AS3935.cpp
--- a/AS3935.cpp	Mon Jun 15 20:38:15 2015 +0000
+++ b/AS3935.cpp	Wed Jun 17 14:32:24 2015 +0000
@@ -21,6 +21,9 @@
 #include "AS3935.h"
 #include "pinmap.h"
 
+unsigned long sgIntrPulseCount = 0; 
+
+
 AS3935::AS3935(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, int hz) :  m_Spi(mosi, miso, sclk), m_Cs(cs, 1),  m_FREQ(hz)
 {
  
@@ -137,6 +140,7 @@
     #endif
 }
 
+#if 0 
 // replication of the acurite init sequence
 void AS3935::acurite()
 {
@@ -240,7 +244,7 @@
     
     
 }
-
+#endif 
 int AS3935::interruptSource()
 {
     return registerRead(AS3935_INT);
@@ -374,3 +378,61 @@
 
     return rc;
 }
+
+
+void intrPulseCntr(void)
+{
+    sgIntrPulseCount++; 
+} 
+
+unsigned long AS3935::tuneAntenna(InterruptIn &intrIn)
+{
+#define ANTENA_RES_FREQ     (unsigned long)500000
+Timer pulseTimer;
+int timeNow; 
+unsigned long measFreq; 
+unsigned long measFreqBest = 0; 
+unsigned char tunCapCnt    = 0; 
+unsigned char tunCapBest   = 0;     
+unsigned long minError     = ANTENA_RES_FREQ; 
+unsigned long error; 
+    
+    intrIn.rise(intrPulseCntr); 
+    
+    for (tunCapCnt = 0; tunCapCnt < 16; ++tunCapCnt)    // loop for all possible values of the tuning capacitor
+    {
+      _SPITransfer2(3, 0x80);
+      _SPITransfer2(8, 0x80+tunCapCnt);                 // set the tuning cap and have the frequency output to the IRQ line 
+      wait_ms(20);                                      // wait for the chip to output the frequency, ususally ~2 ms 
+
+      pulseTimer.reset(); 
+      pulseTimer.start(); 
+      sgIntrPulseCount = 0;                             // reset the interrupt counter which serves as our frequency\pulse counter     
+    
+      timeNow = 0; 
+      while (timeNow < 500)                             // wait for 0.5 seconds
+      {
+        timeNow = pulseTimer.read_ms(); 
+      }
+      _SPITransfer2(8, 0x00);                           // stop the output of the frequncy on IRQ line 
+      
+      measFreq = sgIntrPulseCount << 7;                 // calulate the measure frequency based upon period of capture and freq scaler
+      
+      if (measFreq < ANTENA_RES_FREQ)                   // calculate error between actual and desired frequency 
+        error = ANTENA_RES_FREQ - measFreq; 
+      else 
+        error = measFreq - ANTENA_RES_FREQ; 
+        
+      if (error < minError)                             // update the best capacitor tuning so far 
+      {
+        tunCapBest = tunCapCnt; 
+        minError = error; 
+        measFreqBest = measFreq; 
+      }
+      
+      printf("sgIntrCount[%ld] measFreq[%ld] timeNow[%ld] tunCapBest[%d]\n\r", sgIntrPulseCount, measFreq, timeNow, tunCapBest);
+    }
+    setTuneCap(tunCapBest); //  500kHz);                // set the best capacitor tuning that was found 
+    return measFreqBest; 
+}
+