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); } }
AS3935.cpp
- Committer:
- ftagius
- Date:
- 2015-06-16
- Revision:
- 6:c9b9e7d3bced
- Parent:
- 5:28311803e23d
- Child:
- 8:60e1b6b39e1c
File content as of revision 6:c9b9e7d3bced:
/* AS3935.cpp - AS3935 Franklin Lightning Sensor™ IC by AMS library Copyright (c) 2012 Raivis Rengelis (raivis [at] rrkb.lv). All rights reserved. Porté sur MBED par Valentin, version I2C This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "AS3935.h" #include "pinmap.h" 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 pin_mode(miso, PullUp); //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) { m_Cs = 0; m_Spi.write(high); char regval = m_Spi.write(low); m_Cs = 1; return regval; } char AS3935::_rawRegisterRead(char reg) { return _SPITransfer2((reg & 0x3F) | 0x40, 0); } char AS3935::_ffsz(char mask) { char i = 0; while(!(mask & 1)) { mask >>= 1; i++; } return i; } void AS3935::registerWrite(char reg, char mask, char data) { char regval; regval = _rawRegisterRead(reg); regval &= ~(mask); regval |= (data << (_ffsz(mask))); m_Cs = 0; m_Spi.write(reg); m_Spi.write(regval); //printf("raw transfer for reg %x is 0x%02x\r\n", reg, 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; regval >>= (_ffsz(mask)); return regval; } void AS3935::reset() { m_Cs = 0; m_Spi.write(0x3D); m_Spi.write(0x96); m_Cs = 1; 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); } void AS3935::powerUp() { #if 0 registerWrite(AS3935_PWD,0); //printf("wrote power up \r\n"); 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); #else m_Cs = 0; m_Spi.write(0x00); m_Spi.write(0x1c); m_Cs = 1; wait_ms(2); #endif } // replication of the acurite init sequence void AS3935::acurite() { m_Cs = 0; m_Spi.write(0x00); m_Spi.write(0x1d); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x3c); m_Spi.write(0x96); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x01); m_Spi.write(0x24); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x03); m_Spi.write(0x41); m_Cs = 1; wait_ms(2); int i; for (i=0;i<8; i++) { m_Cs = 0; m_Spi.write(0x08); m_Spi.write(0x80+i); m_Cs = 1; wait_ms(2); } m_Cs = 0; m_Spi.write(0x08); m_Spi.write(0x08); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x00); m_Spi.write(0x1c); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x3d); m_Spi.write(0x96); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x08); m_Spi.write(0x48); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x08); m_Spi.write(0x8); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x08); m_Spi.write(0x28); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x08); m_Spi.write(0x48); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x08); m_Spi.write(0x8); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x02); m_Spi.write(0x82); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x01); m_Spi.write(0x24); m_Cs = 1; wait_ms(2); m_Cs = 0; m_Spi.write(0x02); m_Spi.write(0xc2); m_Cs = 1; wait_ms(2); } int AS3935::interruptSource() { return registerRead(AS3935_INT); } void AS3935::disableDisturbers() { registerWrite(AS3935_MASK_DIST,1); } void AS3935::enableDisturbers() { registerWrite(AS3935_MASK_DIST,0); } int AS3935::getMinimumLightnings() { return registerRead(AS3935_MIN_NUM_LIGH); } int AS3935::setMinimumLightnings(int minlightning) { registerWrite(AS3935_MIN_NUM_LIGH,minlightning); return getMinimumLightnings(); } int AS3935::lightningDistanceKm() { return registerRead(AS3935_DISTANCE); } void AS3935::setIndoors() { registerWrite(AS3935_AFE_GB,AS3935_AFE_INDOOR); } int AS3935::getGain() { return registerRead(AS3935_AFE_GB); } void AS3935::setOutdoors() { registerWrite(AS3935_AFE_GB,AS3935_AFE_OUTDOOR); } int AS3935::getNoiseFloor() { return registerRead(AS3935_NF_LEV); } int AS3935::setNoiseFloor(int noisefloor) { registerWrite(AS3935_NF_LEV,noisefloor); return getNoiseFloor(); } int AS3935::getSpikeRejection() { return registerRead(AS3935_SREJ); } int AS3935::setSpikeRejection(int srej) { registerWrite(AS3935_SREJ, srej); return getSpikeRejection(); } int AS3935::getWatchdogThreshold() { return registerRead(AS3935_WDTH); } int AS3935::setWatchdogThreshold(int wdth) { registerWrite(AS3935_WDTH,wdth); return getWatchdogThreshold(); } int AS3935::getTuneCap() { return registerRead(AS3935_TUN_CAP); } int AS3935::setTuneCap(int cap) { registerWrite(AS3935_TUN_CAP,cap); return getTuneCap(); } void AS3935::clearStats() { registerWrite(AS3935_CL_STAT,1); registerWrite(AS3935_CL_STAT,0); registerWrite(AS3935_CL_STAT,1); } 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(AS3935_DISP_SRCO, 0x40); // ERRATA: after power down mode wait_ms(2); // wait 1ms registerWrite(AS3935_DISP_SRCO, 0x00); // ERRATA: after power down mode wait_ms(1); // cal sequence from acurite follows registerWrite(AS3935_DISP_SRCO, 0x48); wait_ms(1); registerWrite(AS3935_DISP_LCO, 0x8); wait_ms(1); registerWrite(AS3935_DISP_TRCO, 0x28); wait_ms(1); registerWrite(AS3935_DISP_SRCO, 0x48); wait_ms(1); registerWrite(AS3935_DISP_LCO, 0x8); wait_ms(1); 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; }