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); } }
Diff: AS3935.cpp
- Revision:
- 1:f2d9ed33c276
- Parent:
- 0:346c723cac97
- Child:
- 2:3b3b435ca8d9
diff -r 346c723cac97 -r f2d9ed33c276 AS3935.cpp --- a/AS3935.cpp Sat Apr 05 23:24:41 2014 +0000 +++ b/AS3935.cpp Thu Jun 11 15:47:29 2015 +0000 @@ -18,26 +18,49 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "AS3935.hpp" +#include "AS3935.h" +#include "pinmap.h" -AS3935::AS3935(PinName sda, PinName scl, int adresse): i2c(sda, scl), _adress(adresse) { - +#if 0 +AS3935::AS3935(PinName sda, PinName scl, int adress, int hz): i2c(sda, scl), _adress(adress), m_FREQ(hz) { + wait_ms(11); - } - + //Configure the SPI bus + m_Spi.format(8, 1); +} //~AS3935(){ //} - +#endif +#if 1 +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) +{ + + //Enable the internal pull-up resistor on MISO + pin_mode(miso, PullUp); + + //Configure the SPI bus + m_Spi.format(8, 1); +} +#endif + + +char AS3935::_SPITransfer2(char high, char low) +{ + m_Cs = 0; + m_Spi.write(high); + char regval = m_Spi.write(low); + // printf("raw transfer for reg %x is 0x%02x\r\n", high, regval); + m_Cs = 1; + return regval; +} + char AS3935::_rawRegisterRead(char reg) { - char data; - i2c.write( _adress, ®, 1, true); - i2c.read(_adress, &data, 1); - return data; - + return _SPITransfer2((reg & 0x3F) | 0x40, 0); } + char AS3935::_ffsz(char mask) { char i = 0; @@ -59,7 +82,7 @@ void AS3935::registerWrite(char reg, char mask, char data) { - char cmd[2]; + char regval; regval = _rawRegisterRead(reg); regval &= ~(mask); @@ -69,51 +92,52 @@ //else { // regval |= data; // } - cmd[0] = reg; - cmd[1] = regval; - i2c.write( _adress, cmd, 2); - + + m_Cs = 0; + m_Spi.write(reg); + m_Spi.write(regval); + //printf("raw transfer for reg %x is 0x%02x\r\n", high, regval); + m_Cs = 1; + wait_ms(2); } char AS3935::registerRead(char reg, char mask) - { +{ char regval; regval = _rawRegisterRead(reg); + // printf("raw regval is 0x%02x\r\n", regval); regval = regval & mask; - //if (mask){ - regval >>= (_ffsz(mask)); - // } + regval >>= (_ffsz(mask)); return regval; } void AS3935::reset() - { - char cmd[2]; - cmd[0] = 0x3C; - cmd[1] = 0x96; - i2c.write( _adress, cmd, 2); + { + m_Cs = 0; + m_Spi.write(0x3D); + m_Spi.write(0x96); + m_Cs = 1; wait_ms(2); - } - - +} void AS3935::powerDown() - { +{ registerWrite(AS3935_PWD,1); - } +} void AS3935::powerUp() - { - char cmd[2]; - cmd[0] = 0x3D; - cmd[1] = 0x96; +{ + registerWrite(AS3935_PWD,0); - i2c.write( _adress, cmd, 2); + m_Cs = 0; + m_Spi.write(0x3D); + m_Spi.write(0x96); + m_Cs = 1; wait_ms(3); registerWrite(AS3935_DISP_TRCO,1); wait_ms(2); registerWrite(AS3935_DISP_TRCO,0); - } +} int AS3935::interruptSource() {