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); } }
Revision 3:c536a9aa2a1c, committed 2015-06-11
- Comitter:
- ftagius
- Date:
- Thu Jun 11 19:45:11 2015 +0000
- Parent:
- 2:3b3b435ca8d9
- Child:
- 4:ab1f1b65468b
- Commit message:
- fix spi bus frequency init
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 15:53:51 2015 +0000 +++ b/AS3935.cpp Thu Jun 11 19:45:11 2015 +0000 @@ -21,7 +21,7 @@ #include "AS3935.h" #include "pinmap.h" -AS3935::AS3935(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd, int hz) : m_Spi(mosi, miso, sclk), m_Cs(cs, 1), m_Cd(cd), m_FREQ(hz) +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) { //Enable the internal pull-up resistor on MISO @@ -29,6 +29,9 @@ //Configure the SPI bus m_Spi.format(8, 1); + printf("spi bus frequency set to %d hz\r\n",hz); + m_Spi.frequency(hz); + } char AS3935::_SPITransfer2(char high, char low) @@ -201,4 +204,33 @@ registerWrite(AS3935_CL_STAT,1); registerWrite(AS3935_CL_STAT,0); registerWrite(AS3935_CL_STAT,1); -} \ No newline at end of file +} + +int AS3935::calibrateRCOs (void) +{ + int rc; + uint8_t trco; + uint8_t srco; + m_Cs = 0; + m_Spi.write(0x3D); // Direct Command Calibrate RCO + m_Spi.write(0x96); + m_Cs = 1; + wait_ms(1); // wait 1ms + registerWrite(0x08, 0x40, 0x40); // ERRATA: after power down mode + wait_ms(2); // wait 1ms + registerWrite(0x08, 0x40, 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) + { + rc = 1; + printf("cal is done\r\n"); + } + else + { + printf("cal is not done\r\n"); + rc = 0; + } + + return rc; +}
--- a/AS3935.h Thu Jun 11 15:53:51 2015 +0000 +++ b/AS3935.h Thu Jun 11 19:45:11 2015 +0000 @@ -50,26 +50,19 @@ class AS3935 { public: - /* - * Initializes i2c interface and IRQ pin - */ - // AS3935(PinName sda, PinName scl, int adresse, int hz = 1000000); - - + /** Create a virtual file system for accessing SD/MMC cards via SPI * - * @param mosi The SPI data out pin. + * @param mosi The SPI data out pin. * @param miso The SPI data in pin. * @param sclk The SPI clock pin. * @param cs The SPI chip select pin. - * @param name The name used to access the virtual filesystem. - * @param cd The card detect pin. - * @param cdtype The type of card detect switch. + * @param name The name used to access the spi bus. * @param hz The SPI bus frequency (defaults to 1MHz). - */ + */ - AS3935(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd = NC, int hz = 10000); - + AS3935(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, int hz = 1000000); + //destruction //~AS3935(); @@ -145,6 +138,9 @@ //clear internal accumulated lightning statistics void clearStats(); + + int calibrateRCOs (void); + /** Attach a function, lightning interrupt * @param fptr pointer to a void function, or 0 to set as none @@ -166,7 +162,7 @@ //DigitalOut _irq; SPI m_Spi; DigitalOut m_Cs; - InterruptIn m_Cd; + //InterruptIn m_Cd; int m_CdAssert; const int m_FREQ;