diff --git a/DFRobot_AS3935_I2C.cpp b/DFRobot_AS3935_I2C.cpp index 637faa4..a429bdd 100644 --- a/DFRobot_AS3935_I2C.cpp +++ b/DFRobot_AS3935_I2C.cpp @@ -46,18 +46,18 @@ void DFRobot_AS3935_I2C::singRegWrite(uint8_t regAdd, uint8_t dataMask, uint8_t // finally, write the data to the register I2c.write(devAdd, regAdd, newRegData); - Serial.print("wrt: "); - Serial.print(newRegData,HEX); - Serial.print(" Act: "); - Serial.println(singRegRead(regAdd),HEX); + //Serial.print("wrt: "); + //Serial.print(newRegData,HEX); + //Serial.print(" Act: "); + //Serial.println(singRegRead(regAdd),HEX); } void DFRobot_AS3935_I2C::defInit() { - AS3935Reset(); // reset registers to default + reset(); // reset registers to default } -void DFRobot_AS3935_I2C::AS3935Reset() +void DFRobot_AS3935_I2C::reset() { // run PRESET_DEFAULT Direct Command to set all registers in default state I2c.write(devAdd, (uint8_t)0x3C, (uint8_t)0x96); @@ -109,7 +109,7 @@ void DFRobot_AS3935_I2C::setIRQOutputSource(uint8_t irqSelect) // only one should be set at once, I think // 0 = NONE, 1 = TRCO, 2 = SRCO, 3 = LCO -if(1 == irqSelect) + if(1 == irqSelect) { singRegWrite(0x08, 0xE0, 0x20); // set only TRCO bit } @@ -240,7 +240,7 @@ void DFRobot_AS3935_I2C::setNoiseFloorLvl(uint8_t nfSel) { // NF settings addres 0x01, bits 6:4 // default setting of 010 at startup (datasheet, table 9) - if(7 >= nfSel) // nf_sel within expected range + if(7 >= nfSel) // nfSel within expected range { singRegWrite(0x01, 0x70, ((nfSel & 0x07) << 4)); } diff --git a/DFRobot_AS3935_I2C.h b/DFRobot_AS3935_I2C.h index fe030c4..ae45dd6 100644 --- a/DFRobot_AS3935_I2C.h +++ b/DFRobot_AS3935_I2C.h @@ -22,8 +22,6 @@ class DFRobot_AS3935_I2C void manualCal(uint8_t capacitance, uint8_t location, uint8_t disturber); /*! reset registers to default */ void defInit(void); - void powerUp(void); - void powerDown(void); void disturberEn(void); void disturberDis(void); void setIRQOutputSource(uint8_t irqSelect); @@ -52,7 +50,9 @@ class DFRobot_AS3935_I2C uint8_t irq, devAdd; uint8_t singRegRead(uint8_t regAdd); void singRegWrite(uint8_t regAdd, uint8_t dataMask, uint8_t regData); - void AS3935Reset(void); + void reset(void); + void powerUp(void); + void powerDown(void); void calRCO(void); }; diff --git a/examples/DFRobot_AS3935_lightning_sensor_detailed/DFRobot_AS3935_lightning_sensor_detailed.ino b/examples/DFRobot_AS3935_lightning_sensor_detailed/DFRobot_AS3935_lightning_sensor_detailed.ino new file mode 100644 index 0000000..04caee3 --- /dev/null +++ b/examples/DFRobot_AS3935_lightning_sensor_detailed/DFRobot_AS3935_lightning_sensor_detailed.ino @@ -0,0 +1,140 @@ +/*! + file DFRobot_AS3935_lightning_sensor.ino + + SEN0290 Lightning Sensor + This sensor can detect lightning and display the distance and intensity of the lightning within 40 km + It can be set as indoor or outdoor mode. + The module has three I2C, these addresses are: + AS3935_ADD1 0x01 A0 = High A1 = Low + AS3935_ADD3 0x03 A0 = High A1 = High + AS3935_ADD2 0x02 A0 = Low A1 = High + + Copyright [DFRobot](http://www.dfrobot.com), 2018 + Copyright GNU Lesser General Public License + + version V0.4 + date 2018-11-15 +*/ + +#include "Lib_I2C.h" +#include "DFRobot_AS3935_I2C.h" + +volatile int8_t AS3935IsrTrig = 0; + +#define IRQ_PIN 2 + +// Antenna tuning capcitance (must be integer multiple of 8, 8 - 120 pf) +#define AS3935_CAPACITANCE 96 + +// Indoor/outdoor mode selection +#define AS3935_INDOORS 0 +#define AS3935_OUTDOORS 1 +#define AS3935_MODE AS3935_INDOORS + +// Enable/disable disturber detection +#define AS3935_DIST_DIS 0 +#define AS3935_DIST_EN 1 +#define AS3935_DIST AS3935_DIST_EN + +// I2C address +#define AS3935_I2C_ADDR AS3935_ADD2 + +void AS3935_ISR(); + +DFRobot_AS3935_I2C lightning0((uint8_t)IRQ_PIN, (uint8_t)AS3935_I2C_ADDR); + +void setup() +{ + + Serial.begin(115200); + Serial.println("DFRobot AS3935 lightning sensor begin!"); + + // Setup for the the I2C library: (enable pullups, set speed to 400kHz) + I2c.begin(); + I2c.pullup(true); + I2c.setSpeed(1); + delay(2); + + lightning0.setI2CAddress(AS3935_ADD3); + // Set registers to default + lightning0.defInit(); + // Configure sensor + lightning0.manualCal(AS3935_CAPACITANCE, AS3935_MODE, AS3935_DIST); + + //set indoors or outdoors models + lightning0.setIndoors(); + //lightning0.setOutdoors(); + + //disturber detection + lightning0.disturberEn(); + //lightning0.disturberDis(); + + // Enable interrupt (connect IRQ pin IRQ_PIN: 2, default) +// Connect the IRQ and GND pin to the oscilloscope. +// uncomment the following sentences to fine tune the antenna for better performance. +// This will dispaly the antenna's resonance frequency/16 on IRQ pin (The resonance frequency will be divided by 16 on this pin) +// Tuning AS3935_CAPACITANCE to make the frequency within 500/16 kHz ± 3.5% +// lightning0.setLcoFdiv(0); +// lightning0.setIRQOutputSource(3); + +// Set the noise level,use a default value greater than 7 + lightning0.setNoiseFloorLvl(2); + //uint8_t noiseLv = lightning0.getNoiseFloorLvl(); + +//used to modify WDTH,alues should only be between 0x00 and 0x0F (0 and 7) + lightning0.setWatchdogThreshold(0); + //uint8_t wtdgThreshold = lightning0.getWatchdogThreshold(); + +//used to modify SREJ (spike rejection),values should only be between 0x00 and 0x0F (0 and 7) + lightning0.setSpikeRejection(2); + //uint8_t spikeRejection = lightning0.getSpikeRejection(); + + attachInterrupt(0, AS3935_ISR, RISING); + +} + +void loop() +{ + // It does nothing until an interrupt is detected on the IRQ pin. + while (AS3935IsrTrig == 0) {} + delay(5); + + // Reset interrupt flag + AS3935IsrTrig = 0; + + // Get interrupt source + uint8_t intSrc = lightning0.getInterruptSrc(); + if (intSrc == 1) + { + // Get rid of non-distance data + uint8_t lightningDistKm = lightning0.getLightningDistKm(); + Serial.println("Lightning occurs!"); + Serial.print("Distance: "); + Serial.print(lightningDistKm); + Serial.println(" km"); + + // Get lightning energy intensity + uint32_t lightningEnergyVal = lightning0.getStrikeEnergyRaw(); + Serial.print("Intensity: "); + Serial.print(lightningEnergyVal); + Serial.println(""); + } + else if (intSrc == 2) + { + Serial.println("Disturber discovered!"); + } + else if (intSrc == 3) + { + Serial.println("Noise level too high!"); + } + + //View register data + //lightning0.printAllRegs(); +} + +//IRQ handler for AS3935 interrupts +void AS3935_ISR() +{ + AS3935IsrTrig = 1; +} + diff --git a/examples/DFRobot_AS3935_lightning_sensor/DFRobot_AS3935_lightning_sensor.ino b/examples/DFRobot_AS3935_lightning_sensor_ordinary/DFRobot_AS3935_lightning_sensor_ordinary.ino similarity index 100% rename from examples/DFRobot_AS3935_lightning_sensor/DFRobot_AS3935_lightning_sensor.ino rename to examples/DFRobot_AS3935_lightning_sensor_ordinary/DFRobot_AS3935_lightning_sensor_ordinary.ino diff --git a/keywords.txt b/keywords.txt index 287515d..cdaed40 100644 --- a/keywords.txt +++ b/keywords.txt @@ -27,8 +27,6 @@ receive KEYWORD2 setI2CAddress KEYWORD2 manualCal KEYWORD2 defInit KEYWORD2 -powerUp KEYWORD2 -powerDown KEYWORD2 disturberEn KEYWORD2 disturberDis KEYWORD2 setIRQOutputSource KEYWORD2 diff --git a/readme.md b/readme.md index 443222f..44a1f57 100644 --- a/readme.md +++ b/readme.md @@ -134,6 +134,48 @@ void setLcoFdiv(uint8_t fdiv); */ void setIRQOutputSource(uint8_t irqSelect); +/* + * @brief Set the noise level + * + * @param 0~7,More than 7 will use the default value:2 + */ +void setNoiseFloorLvl(uint8_t nfSel); + +/* + * @brief Get the noise level + * + * @return 0~7 + */ +uint8_t getNoiseFloorLvl(void); + +/* + * @brief Set an anti-interference rating + * + * @param 0~7,More than 7 will use the default value:1 + */ +void setWatchdogThreshold(uint8_t wdth); + +/* + * @brief read WDTH + * + * @return 0~7 + */ +uint8_t getWatchdogThreshold(void); + +/* + * @brief Modify SREJ (spike rejection) + * + * @param 0~7,More than 7 will use the default value:2 + */ +void setSpikeRejection(uint8_t srej); + +/* + * @brief read SREJ (spike rejection) + * + * @return 0~7 + */ +uint8_t getSpikeRejection(void); + ```