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@11:ee2e7a573227, 2015-06-24 (annotated)
- Committer:
- cmkachur
- Date:
- Wed Jun 24 16:36:47 2015 +0000
- Revision:
- 11:ee2e7a573227
- Parent:
- 10:bf33e2946bab
- Child:
- 12:ac6ba62a31c6
Store all configuration register values and firmware version to config_data.csv file at startup. Delete obsolete code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
casper | 0:346c723cac97 | 1 | /* |
casper | 0:346c723cac97 | 2 | AS3935.cpp - AS3935 Franklin Lightning Sensor™ IC by AMS library |
casper | 0:346c723cac97 | 3 | Copyright (c) 2012 Raivis Rengelis (raivis [at] rrkb.lv). All rights reserved. |
casper | 0:346c723cac97 | 4 | Porté sur MBED par Valentin, version I2C |
casper | 0:346c723cac97 | 5 | |
casper | 0:346c723cac97 | 6 | This library is free software; you can redistribute it and/or |
casper | 0:346c723cac97 | 7 | modify it under the terms of the GNU Lesser General Public |
casper | 0:346c723cac97 | 8 | License as published by the Free Software Foundation; either |
casper | 0:346c723cac97 | 9 | version 3 of the License, or (at your option) any later version. |
casper | 0:346c723cac97 | 10 | |
casper | 0:346c723cac97 | 11 | This library is distributed in the hope that it will be useful, |
casper | 0:346c723cac97 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
casper | 0:346c723cac97 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
casper | 0:346c723cac97 | 14 | Lesser General Public License for more details. |
casper | 0:346c723cac97 | 15 | |
casper | 0:346c723cac97 | 16 | You should have received a copy of the GNU Lesser General Public |
casper | 0:346c723cac97 | 17 | License along with this library; if not, write to the Free Software |
casper | 0:346c723cac97 | 18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
casper | 0:346c723cac97 | 19 | */ |
casper | 0:346c723cac97 | 20 | |
ftagius | 1:f2d9ed33c276 | 21 | #include "AS3935.h" |
ftagius | 1:f2d9ed33c276 | 22 | #include "pinmap.h" |
casper | 0:346c723cac97 | 23 | |
cmkachur | 7:0d2586164d5b | 24 | unsigned long sgIntrPulseCount = 0; |
cmkachur | 7:0d2586164d5b | 25 | |
cmkachur | 7:0d2586164d5b | 26 | |
ftagius | 3:c536a9aa2a1c | 27 | 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) |
ftagius | 1:f2d9ed33c276 | 28 | { |
ftagius | 1:f2d9ed33c276 | 29 | |
ftagius | 1:f2d9ed33c276 | 30 | //Enable the internal pull-up resistor on MISO |
ftagius | 1:f2d9ed33c276 | 31 | pin_mode(miso, PullUp); |
ftagius | 1:f2d9ed33c276 | 32 | |
ftagius | 1:f2d9ed33c276 | 33 | //Configure the SPI bus |
ftagius | 1:f2d9ed33c276 | 34 | m_Spi.format(8, 1); |
ftagius | 3:c536a9aa2a1c | 35 | printf("spi bus frequency set to %d hz\r\n",hz); |
ftagius | 3:c536a9aa2a1c | 36 | m_Spi.frequency(hz); |
ftagius | 3:c536a9aa2a1c | 37 | |
ftagius | 1:f2d9ed33c276 | 38 | } |
ftagius | 1:f2d9ed33c276 | 39 | |
ftagius | 1:f2d9ed33c276 | 40 | char AS3935::_SPITransfer2(char high, char low) |
ftagius | 1:f2d9ed33c276 | 41 | { |
ftagius | 1:f2d9ed33c276 | 42 | m_Cs = 0; |
ftagius | 1:f2d9ed33c276 | 43 | m_Spi.write(high); |
ftagius | 1:f2d9ed33c276 | 44 | char regval = m_Spi.write(low); |
ftagius | 1:f2d9ed33c276 | 45 | m_Cs = 1; |
ftagius | 1:f2d9ed33c276 | 46 | return regval; |
ftagius | 1:f2d9ed33c276 | 47 | } |
ftagius | 1:f2d9ed33c276 | 48 | |
casper | 0:346c723cac97 | 49 | char AS3935::_rawRegisterRead(char reg) |
casper | 0:346c723cac97 | 50 | { |
ftagius | 1:f2d9ed33c276 | 51 | return _SPITransfer2((reg & 0x3F) | 0x40, 0); |
casper | 0:346c723cac97 | 52 | } |
casper | 0:346c723cac97 | 53 | |
ftagius | 1:f2d9ed33c276 | 54 | |
casper | 0:346c723cac97 | 55 | char AS3935::_ffsz(char mask) |
casper | 0:346c723cac97 | 56 | { |
casper | 0:346c723cac97 | 57 | char i = 0; |
ftagius | 2:3b3b435ca8d9 | 58 | |
casper | 0:346c723cac97 | 59 | while(!(mask & 1)) { |
ftagius | 2:3b3b435ca8d9 | 60 | mask >>= 1; |
casper | 0:346c723cac97 | 61 | i++; |
casper | 0:346c723cac97 | 62 | } |
ftagius | 2:3b3b435ca8d9 | 63 | |
casper | 0:346c723cac97 | 64 | return i; |
casper | 0:346c723cac97 | 65 | } |
casper | 0:346c723cac97 | 66 | |
casper | 0:346c723cac97 | 67 | void AS3935::registerWrite(char reg, char mask, char data) |
casper | 0:346c723cac97 | 68 | { |
ftagius | 1:f2d9ed33c276 | 69 | |
casper | 0:346c723cac97 | 70 | char regval; |
casper | 0:346c723cac97 | 71 | regval = _rawRegisterRead(reg); |
casper | 0:346c723cac97 | 72 | regval &= ~(mask); |
ftagius | 2:3b3b435ca8d9 | 73 | regval |= (data << (_ffsz(mask))); |
ftagius | 1:f2d9ed33c276 | 74 | m_Cs = 0; |
ftagius | 1:f2d9ed33c276 | 75 | m_Spi.write(reg); |
ftagius | 1:f2d9ed33c276 | 76 | m_Spi.write(regval); |
ftagius | 5:28311803e23d | 77 | //printf("raw transfer for reg %x is 0x%02x\r\n", reg, regval); |
ftagius | 1:f2d9ed33c276 | 78 | m_Cs = 1; |
ftagius | 1:f2d9ed33c276 | 79 | wait_ms(2); |
casper | 0:346c723cac97 | 80 | } |
casper | 0:346c723cac97 | 81 | |
casper | 0:346c723cac97 | 82 | char AS3935::registerRead(char reg, char mask) |
ftagius | 1:f2d9ed33c276 | 83 | { |
casper | 0:346c723cac97 | 84 | char regval; |
casper | 0:346c723cac97 | 85 | regval = _rawRegisterRead(reg); |
ftagius | 1:f2d9ed33c276 | 86 | // printf("raw regval is 0x%02x\r\n", regval); |
casper | 0:346c723cac97 | 87 | regval = regval & mask; |
ftagius | 1:f2d9ed33c276 | 88 | regval >>= (_ffsz(mask)); |
casper | 0:346c723cac97 | 89 | return regval; |
casper | 0:346c723cac97 | 90 | } |
cmkachur | 11:ee2e7a573227 | 91 | |
ftagius | 4:ab1f1b65468b | 92 | void AS3935::presetDefault() |
ftagius | 4:ab1f1b65468b | 93 | { |
ftagius | 4:ab1f1b65468b | 94 | m_Cs = 0; |
ftagius | 4:ab1f1b65468b | 95 | m_Spi.write(0x3C); |
ftagius | 4:ab1f1b65468b | 96 | m_Spi.write(0x96); |
ftagius | 4:ab1f1b65468b | 97 | m_Cs = 1; |
ftagius | 4:ab1f1b65468b | 98 | wait_ms(2); |
ftagius | 4:ab1f1b65468b | 99 | } |
ftagius | 4:ab1f1b65468b | 100 | |
ftagius | 4:ab1f1b65468b | 101 | void AS3935::init() |
ftagius | 4:ab1f1b65468b | 102 | { |
ftagius | 4:ab1f1b65468b | 103 | presetDefault(); |
ftagius | 4:ab1f1b65468b | 104 | registerWrite(AS3935_WDTH, 0x04); // set WDTH level to 4 |
ftagius | 4:ab1f1b65468b | 105 | } |
ftagius | 4:ab1f1b65468b | 106 | |
casper | 0:346c723cac97 | 107 | void AS3935::powerDown() |
ftagius | 1:f2d9ed33c276 | 108 | { |
casper | 0:346c723cac97 | 109 | registerWrite(AS3935_PWD,1); |
ftagius | 1:f2d9ed33c276 | 110 | } |
casper | 0:346c723cac97 | 111 | |
ftagius | 5:28311803e23d | 112 | |
cmkachur | 11:ee2e7a573227 | 113 | |
casper | 0:346c723cac97 | 114 | int AS3935::interruptSource() |
ftagius | 2:3b3b435ca8d9 | 115 | { |
casper | 0:346c723cac97 | 116 | return registerRead(AS3935_INT); |
ftagius | 2:3b3b435ca8d9 | 117 | } |
casper | 0:346c723cac97 | 118 | |
casper | 0:346c723cac97 | 119 | void AS3935::disableDisturbers() |
ftagius | 2:3b3b435ca8d9 | 120 | { |
casper | 0:346c723cac97 | 121 | registerWrite(AS3935_MASK_DIST,1); |
ftagius | 2:3b3b435ca8d9 | 122 | } |
casper | 0:346c723cac97 | 123 | |
casper | 0:346c723cac97 | 124 | void AS3935::enableDisturbers() |
ftagius | 2:3b3b435ca8d9 | 125 | { |
casper | 0:346c723cac97 | 126 | registerWrite(AS3935_MASK_DIST,0); |
ftagius | 2:3b3b435ca8d9 | 127 | } |
casper | 0:346c723cac97 | 128 | |
casper | 0:346c723cac97 | 129 | int AS3935::getMinimumLightnings() |
ftagius | 2:3b3b435ca8d9 | 130 | { |
casper | 0:346c723cac97 | 131 | return registerRead(AS3935_MIN_NUM_LIGH); |
ftagius | 2:3b3b435ca8d9 | 132 | } |
casper | 0:346c723cac97 | 133 | |
casper | 0:346c723cac97 | 134 | int AS3935::setMinimumLightnings(int minlightning) |
ftagius | 2:3b3b435ca8d9 | 135 | { |
casper | 0:346c723cac97 | 136 | registerWrite(AS3935_MIN_NUM_LIGH,minlightning); |
casper | 0:346c723cac97 | 137 | return getMinimumLightnings(); |
ftagius | 2:3b3b435ca8d9 | 138 | } |
casper | 0:346c723cac97 | 139 | |
casper | 0:346c723cac97 | 140 | int AS3935::lightningDistanceKm() |
ftagius | 2:3b3b435ca8d9 | 141 | { |
casper | 0:346c723cac97 | 142 | return registerRead(AS3935_DISTANCE); |
ftagius | 2:3b3b435ca8d9 | 143 | } |
casper | 0:346c723cac97 | 144 | |
casper | 0:346c723cac97 | 145 | void AS3935::setIndoors() |
ftagius | 2:3b3b435ca8d9 | 146 | { |
casper | 0:346c723cac97 | 147 | registerWrite(AS3935_AFE_GB,AS3935_AFE_INDOOR); |
ftagius | 2:3b3b435ca8d9 | 148 | } |
casper | 0:346c723cac97 | 149 | |
ftagius | 6:c9b9e7d3bced | 150 | int AS3935::getGain() |
ftagius | 6:c9b9e7d3bced | 151 | { |
ftagius | 6:c9b9e7d3bced | 152 | return registerRead(AS3935_AFE_GB); |
ftagius | 6:c9b9e7d3bced | 153 | } |
ftagius | 6:c9b9e7d3bced | 154 | |
casper | 0:346c723cac97 | 155 | void AS3935::setOutdoors() |
ftagius | 2:3b3b435ca8d9 | 156 | { |
casper | 0:346c723cac97 | 157 | registerWrite(AS3935_AFE_GB,AS3935_AFE_OUTDOOR); |
ftagius | 2:3b3b435ca8d9 | 158 | } |
casper | 0:346c723cac97 | 159 | |
casper | 0:346c723cac97 | 160 | int AS3935::getNoiseFloor() |
ftagius | 2:3b3b435ca8d9 | 161 | { |
casper | 0:346c723cac97 | 162 | return registerRead(AS3935_NF_LEV); |
ftagius | 2:3b3b435ca8d9 | 163 | } |
casper | 0:346c723cac97 | 164 | |
casper | 0:346c723cac97 | 165 | int AS3935::setNoiseFloor(int noisefloor) |
ftagius | 2:3b3b435ca8d9 | 166 | { |
casper | 0:346c723cac97 | 167 | registerWrite(AS3935_NF_LEV,noisefloor); |
casper | 0:346c723cac97 | 168 | return getNoiseFloor(); |
ftagius | 2:3b3b435ca8d9 | 169 | } |
casper | 0:346c723cac97 | 170 | |
casper | 0:346c723cac97 | 171 | int AS3935::getSpikeRejection() |
ftagius | 2:3b3b435ca8d9 | 172 | { |
casper | 0:346c723cac97 | 173 | return registerRead(AS3935_SREJ); |
ftagius | 2:3b3b435ca8d9 | 174 | } |
casper | 0:346c723cac97 | 175 | |
casper | 0:346c723cac97 | 176 | int AS3935::setSpikeRejection(int srej) |
ftagius | 2:3b3b435ca8d9 | 177 | { |
casper | 0:346c723cac97 | 178 | registerWrite(AS3935_SREJ, srej); |
casper | 0:346c723cac97 | 179 | return getSpikeRejection(); |
ftagius | 2:3b3b435ca8d9 | 180 | } |
casper | 0:346c723cac97 | 181 | |
casper | 0:346c723cac97 | 182 | int AS3935::getWatchdogThreshold() |
ftagius | 2:3b3b435ca8d9 | 183 | { |
casper | 0:346c723cac97 | 184 | return registerRead(AS3935_WDTH); |
ftagius | 2:3b3b435ca8d9 | 185 | } |
casper | 0:346c723cac97 | 186 | |
casper | 0:346c723cac97 | 187 | int AS3935::setWatchdogThreshold(int wdth) |
ftagius | 2:3b3b435ca8d9 | 188 | { |
casper | 0:346c723cac97 | 189 | registerWrite(AS3935_WDTH,wdth); |
casper | 0:346c723cac97 | 190 | return getWatchdogThreshold(); |
ftagius | 2:3b3b435ca8d9 | 191 | } |
casper | 0:346c723cac97 | 192 | |
casper | 0:346c723cac97 | 193 | int AS3935::getTuneCap() |
ftagius | 2:3b3b435ca8d9 | 194 | { |
casper | 0:346c723cac97 | 195 | return registerRead(AS3935_TUN_CAP); |
ftagius | 2:3b3b435ca8d9 | 196 | } |
casper | 0:346c723cac97 | 197 | |
casper | 0:346c723cac97 | 198 | int AS3935::setTuneCap(int cap) |
ftagius | 2:3b3b435ca8d9 | 199 | { |
casper | 0:346c723cac97 | 200 | registerWrite(AS3935_TUN_CAP,cap); |
ftagius | 2:3b3b435ca8d9 | 201 | return getTuneCap(); |
ftagius | 2:3b3b435ca8d9 | 202 | } |
casper | 0:346c723cac97 | 203 | |
casper | 0:346c723cac97 | 204 | void AS3935::clearStats() |
ftagius | 2:3b3b435ca8d9 | 205 | { |
casper | 0:346c723cac97 | 206 | registerWrite(AS3935_CL_STAT,1); |
casper | 0:346c723cac97 | 207 | registerWrite(AS3935_CL_STAT,0); |
casper | 0:346c723cac97 | 208 | registerWrite(AS3935_CL_STAT,1); |
ftagius | 3:c536a9aa2a1c | 209 | } |
cmkachur | 9:19a323b1c508 | 210 | |
cmkachur | 9:19a323b1c508 | 211 | static void intrPulseCntr(void) {sgIntrPulseCount++;} |
ftagius | 3:c536a9aa2a1c | 212 | |
cmkachur | 9:19a323b1c508 | 213 | int AS3935::calibrateRCOs (InterruptIn &intrIn) |
ftagius | 3:c536a9aa2a1c | 214 | { |
ftagius | 3:c536a9aa2a1c | 215 | int rc; |
ftagius | 3:c536a9aa2a1c | 216 | uint8_t trco; |
ftagius | 3:c536a9aa2a1c | 217 | uint8_t srco; |
cmkachur | 9:19a323b1c508 | 218 | Timer pulseTimer; |
cmkachur | 9:19a323b1c508 | 219 | int timeNow; |
cmkachur | 9:19a323b1c508 | 220 | unsigned long measFreq; |
cmkachur | 9:19a323b1c508 | 221 | |
cmkachur | 9:19a323b1c508 | 222 | intrIn.rise(intrPulseCntr); |
cmkachur | 9:19a323b1c508 | 223 | |
cmkachur | 9:19a323b1c508 | 224 | _SPITransfer2(0x3D, 0x96); // send command to calibrate the internal RC oscillators |
cmkachur | 9:19a323b1c508 | 225 | _SPITransfer2(8, 0x20); // put TRCO on the IRQ line for measurement |
cmkachur | 9:19a323b1c508 | 226 | wait_ms(20); // wait for the chip to output the frequency, ususally ~2 ms |
ftagius | 5:28311803e23d | 227 | |
cmkachur | 9:19a323b1c508 | 228 | pulseTimer.reset(); |
cmkachur | 9:19a323b1c508 | 229 | pulseTimer.start(); |
cmkachur | 9:19a323b1c508 | 230 | sgIntrPulseCount = 0; // reset the interrupt counter which serves as our frequency\pulse counter |
cmkachur | 9:19a323b1c508 | 231 | |
cmkachur | 9:19a323b1c508 | 232 | timeNow = 0; |
cmkachur | 9:19a323b1c508 | 233 | while (timeNow < 500) // wait for 0.5 seconds |
cmkachur | 9:19a323b1c508 | 234 | { |
cmkachur | 9:19a323b1c508 | 235 | timeNow = pulseTimer.read_ms(); |
cmkachur | 9:19a323b1c508 | 236 | } |
cmkachur | 9:19a323b1c508 | 237 | _SPITransfer2(8, 0); // stop the output of the frequncy on IRQ line |
cmkachur | 9:19a323b1c508 | 238 | measFreq = sgIntrPulseCount << 1; // calculate the measure frequency based upon period of capture and freq scaler |
cmkachur | 9:19a323b1c508 | 239 | |
cmkachur | 9:19a323b1c508 | 240 | printf("timer RCO: %ld Hz\n\r", measFreq); |
cmkachur | 9:19a323b1c508 | 241 | |
cmkachur | 9:19a323b1c508 | 242 | trco=registerRead(0x3A, 0x80); // Read out Calibration of TRCO done |
cmkachur | 9:19a323b1c508 | 243 | srco=registerRead(0x3B, 0x80); // Readout Calibration of SRCO done |
ftagius | 3:c536a9aa2a1c | 244 | if(trco != 0x00 && srco != 0x00) |
ftagius | 3:c536a9aa2a1c | 245 | { |
ftagius | 3:c536a9aa2a1c | 246 | rc = 1; |
ftagius | 3:c536a9aa2a1c | 247 | printf("cal is done\r\n"); |
ftagius | 3:c536a9aa2a1c | 248 | } |
ftagius | 3:c536a9aa2a1c | 249 | else |
ftagius | 3:c536a9aa2a1c | 250 | { |
ftagius | 3:c536a9aa2a1c | 251 | printf("cal is not done\r\n"); |
ftagius | 3:c536a9aa2a1c | 252 | rc = 0; |
ftagius | 3:c536a9aa2a1c | 253 | } |
ftagius | 3:c536a9aa2a1c | 254 | |
ftagius | 3:c536a9aa2a1c | 255 | return rc; |
ftagius | 3:c536a9aa2a1c | 256 | } |
cmkachur | 7:0d2586164d5b | 257 | |
cmkachur | 7:0d2586164d5b | 258 | |
cmkachur | 7:0d2586164d5b | 259 | unsigned long AS3935::tuneAntenna(InterruptIn &intrIn) |
cmkachur | 7:0d2586164d5b | 260 | { |
cmkachur | 7:0d2586164d5b | 261 | #define ANTENA_RES_FREQ (unsigned long)500000 |
cmkachur | 7:0d2586164d5b | 262 | Timer pulseTimer; |
cmkachur | 7:0d2586164d5b | 263 | int timeNow; |
cmkachur | 7:0d2586164d5b | 264 | unsigned long measFreq; |
cmkachur | 7:0d2586164d5b | 265 | unsigned long measFreqBest = 0; |
cmkachur | 7:0d2586164d5b | 266 | unsigned char tunCapCnt = 0; |
cmkachur | 7:0d2586164d5b | 267 | unsigned char tunCapBest = 0; |
cmkachur | 7:0d2586164d5b | 268 | unsigned long minError = ANTENA_RES_FREQ; |
cmkachur | 7:0d2586164d5b | 269 | unsigned long error; |
cmkachur | 7:0d2586164d5b | 270 | |
cmkachur | 7:0d2586164d5b | 271 | intrIn.rise(intrPulseCntr); |
cmkachur | 9:19a323b1c508 | 272 | _SPITransfer2(3, 0x80); // set frequency division to 64 |
cmkachur | 7:0d2586164d5b | 273 | |
cmkachur | 7:0d2586164d5b | 274 | for (tunCapCnt = 0; tunCapCnt < 16; ++tunCapCnt) // loop for all possible values of the tuning capacitor |
cmkachur | 7:0d2586164d5b | 275 | { |
cmkachur | 7:0d2586164d5b | 276 | _SPITransfer2(8, 0x80+tunCapCnt); // set the tuning cap and have the frequency output to the IRQ line |
cmkachur | 7:0d2586164d5b | 277 | wait_ms(20); // wait for the chip to output the frequency, ususally ~2 ms |
cmkachur | 7:0d2586164d5b | 278 | |
cmkachur | 7:0d2586164d5b | 279 | pulseTimer.reset(); |
cmkachur | 7:0d2586164d5b | 280 | pulseTimer.start(); |
cmkachur | 7:0d2586164d5b | 281 | sgIntrPulseCount = 0; // reset the interrupt counter which serves as our frequency\pulse counter |
cmkachur | 7:0d2586164d5b | 282 | |
cmkachur | 7:0d2586164d5b | 283 | timeNow = 0; |
cmkachur | 7:0d2586164d5b | 284 | while (timeNow < 500) // wait for 0.5 seconds |
cmkachur | 7:0d2586164d5b | 285 | { |
cmkachur | 7:0d2586164d5b | 286 | timeNow = pulseTimer.read_ms(); |
cmkachur | 7:0d2586164d5b | 287 | } |
cmkachur | 7:0d2586164d5b | 288 | _SPITransfer2(8, 0x00); // stop the output of the frequncy on IRQ line |
cmkachur | 7:0d2586164d5b | 289 | |
cmkachur | 7:0d2586164d5b | 290 | measFreq = sgIntrPulseCount << 7; // calulate the measure frequency based upon period of capture and freq scaler |
cmkachur | 7:0d2586164d5b | 291 | |
cmkachur | 7:0d2586164d5b | 292 | if (measFreq < ANTENA_RES_FREQ) // calculate error between actual and desired frequency |
cmkachur | 7:0d2586164d5b | 293 | error = ANTENA_RES_FREQ - measFreq; |
cmkachur | 7:0d2586164d5b | 294 | else |
cmkachur | 7:0d2586164d5b | 295 | error = measFreq - ANTENA_RES_FREQ; |
cmkachur | 7:0d2586164d5b | 296 | |
cmkachur | 7:0d2586164d5b | 297 | if (error < minError) // update the best capacitor tuning so far |
cmkachur | 7:0d2586164d5b | 298 | { |
cmkachur | 7:0d2586164d5b | 299 | tunCapBest = tunCapCnt; |
cmkachur | 7:0d2586164d5b | 300 | minError = error; |
cmkachur | 7:0d2586164d5b | 301 | measFreqBest = measFreq; |
cmkachur | 7:0d2586164d5b | 302 | } |
cmkachur | 7:0d2586164d5b | 303 | |
cmkachur | 7:0d2586164d5b | 304 | printf("sgIntrCount[%ld] measFreq[%ld] timeNow[%ld] tunCapBest[%d]\n\r", sgIntrPulseCount, measFreq, timeNow, tunCapBest); |
cmkachur | 7:0d2586164d5b | 305 | } |
cmkachur | 7:0d2586164d5b | 306 | setTuneCap(tunCapBest); // 500kHz); // set the best capacitor tuning that was found |
cmkachur | 7:0d2586164d5b | 307 | return measFreqBest; |
cmkachur | 7:0d2586164d5b | 308 | } |
cmkachur | 7:0d2586164d5b | 309 | |
cmkachur | 10:bf33e2946bab | 310 | unsigned long AS3935::getEnergy(void) |
cmkachur | 10:bf33e2946bab | 311 | { |
cmkachur | 10:bf33e2946bab | 312 | unsigned long retVal; |
cmkachur | 10:bf33e2946bab | 313 | unsigned char rxBuff[3]; |
cmkachur | 10:bf33e2946bab | 314 | |
cmkachur | 10:bf33e2946bab | 315 | rxBuff[0] = registerRead(AS3935_S_LIG_L); |
cmkachur | 10:bf33e2946bab | 316 | rxBuff[1] = registerRead(AS3935_S_LIG_M); |
cmkachur | 10:bf33e2946bab | 317 | rxBuff[2] = registerRead(AS3935_S_LIG_MM); |
cmkachur | 10:bf33e2946bab | 318 | |
cmkachur | 10:bf33e2946bab | 319 | retVal = ((unsigned long)rxBuff[2] << 16) | ((unsigned long)rxBuff[1] << 8) | (unsigned long)rxBuff[0]; |
cmkachur | 10:bf33e2946bab | 320 | return retVal; |
cmkachur | 10:bf33e2946bab | 321 | } |
cmkachur | 11:ee2e7a573227 | 322 | |
cmkachur | 11:ee2e7a573227 | 323 | bool AS3935::getConfigRegisters(unsigned char *pBuff, unsigned char buffLen) |
cmkachur | 11:ee2e7a573227 | 324 | { |
cmkachur | 11:ee2e7a573227 | 325 | unsigned char cnt = 0; |
cmkachur | 11:ee2e7a573227 | 326 | |
cmkachur | 11:ee2e7a573227 | 327 | if (NULL == pBuff) |
cmkachur | 11:ee2e7a573227 | 328 | return false; |
cmkachur | 11:ee2e7a573227 | 329 | |
cmkachur | 11:ee2e7a573227 | 330 | for (cnt = 0; cnt < buffLen && cnt < MAX_CONFIG_REGS; ++cnt) |
cmkachur | 11:ee2e7a573227 | 331 | { |
cmkachur | 11:ee2e7a573227 | 332 | pBuff[cnt] = _rawRegisterRead(cnt); |
cmkachur | 11:ee2e7a573227 | 333 | } |
cmkachur | 11:ee2e7a573227 | 334 | return true; |
cmkachur | 11:ee2e7a573227 | 335 | } |
cmkachur | 11:ee2e7a573227 | 336 | |
cmkachur | 11:ee2e7a573227 | 337 | |
cmkachur | 11:ee2e7a573227 | 338 |