From 81571da1ebf94f79ed562735f4f5fb219391b59f Mon Sep 17 00:00:00 2001 From: ouki-wang <358023925@qq.com> Date: Mon, 5 Nov 2018 10:42:38 +0800 Subject: [PATCH] V0.2 --- DFRobot_AS3935_I2C.cpp | 11 +- DFRobot_AS3935_I2C.h | 12 +- I2C.cpp => Lib_I2C.cpp | 2 +- I2C.h => Lib_I2C.h | 0 .../DFRobot_AS3935_lightning_I2c.ino | 111 ----------------- .../DFRobot_AS3935_lightning_sensor.ino | 117 ++++++++++++++++++ 6 files changed, 127 insertions(+), 126 deletions(-) rename I2C.cpp => Lib_I2C.cpp (99%) rename I2C.h => Lib_I2C.h (100%) delete mode 100644 examples/DFRobot_AS3935_lightning_I2c/DFRobot_AS3935_lightning_I2c.ino create mode 100644 examples/DFRobot_AS3935_lightning_sensor/DFRobot_AS3935_lightning_sensor.ino diff --git a/DFRobot_AS3935_I2C.cpp b/DFRobot_AS3935_I2C.cpp index 78e78bd..e07a2eb 100644 --- a/DFRobot_AS3935_I2C.cpp +++ b/DFRobot_AS3935_I2C.cpp @@ -1,17 +1,12 @@ #include "DFRobot_AS3935_I2c.h" -DF_AS3935_I2C::DF_AS3935_I2C(uint8_t IRQx, uint8_t SIx, uint8_t DEVADDx) +DF_AS3935_I2C::DF_AS3935_I2C(uint8_t IRQx, uint8_t DEVADDx) { _devadd = DEVADDx; - _si = SIx; _irq = IRQx; - // initalize the chip select pins - pinMode(_si, OUTPUT); + // initalize the IRQ pins pinMode(_irq, INPUT); - - // set output pin initical condition - digitalWrite(_si, HIGH); // set pin high for I2C mode } @@ -310,7 +305,7 @@ void DF_AS3935_I2C::AS3935_SetLCO_FDIV(uint8_t fdiv) // default value: 00 // set 0, 1, 2 or 3 for ratios of 16, 32, 64 and 128, respectively. // See pg 23, Table 20 for more info. - _sing_reg_write(0x03, 0xC0, ((fdiv & 0x03) << 5)); + _sing_reg_write(0x03, 0xC0, ((fdiv & 0x03) << 6)); } void DF_AS3935_I2C::AS3935_PrintAllRegs(void) diff --git a/DFRobot_AS3935_I2C.h b/DFRobot_AS3935_I2C.h index b4195ec..5c2636d 100644 --- a/DFRobot_AS3935_I2C.h +++ b/DFRobot_AS3935_I2C.h @@ -5,17 +5,17 @@ #include "avr/pgmspace.h" #include "util/delay.h" #include "stdlib.h" -#include "I2C.h" +#include "Lib_I2C.h" -// I2c address -#define AS3935_ADD3 0x03 // x03 - A0->high A1->high -#define AS3935_ADD2 0x02 // x02 - A0->low A1->high -#define AS3935_ADD1 0x01 // x01 - A0->high A1->low +// I2C address +#define AS3935_ADD1 0x01 // A0=high, A1=low +#define AS3935_ADD3 0x03 // A0=high, A1=high +#define AS3935_ADD2 0x02 // A0=low, A1=high class DF_AS3935_I2C { public: - DF_AS3935_I2C(uint8_t IRQx, uint8_t SIx, uint8_t DEVADDx); + DF_AS3935_I2C(uint8_t IRQx, uint8_t DEVADDx); /*! Set i2c address */ void AS3935_SetI2CAddress(uint8_t DEVADDx); /*! Manual calibration */ diff --git a/I2C.cpp b/Lib_I2C.cpp similarity index 99% rename from I2C.cpp rename to Lib_I2C.cpp index 70765da..3465496 100644 --- a/I2C.cpp +++ b/Lib_I2C.cpp @@ -60,7 +60,7 @@ #endif #include -#include "I2C.h" +#include "Lib_I2C.h" uint8_t I2C::bytesAvailable = 0; uint8_t I2C::bufferIndex = 0; diff --git a/I2C.h b/Lib_I2C.h similarity index 100% rename from I2C.h rename to Lib_I2C.h diff --git a/examples/DFRobot_AS3935_lightning_I2c/DFRobot_AS3935_lightning_I2c.ino b/examples/DFRobot_AS3935_lightning_I2c/DFRobot_AS3935_lightning_I2c.ino deleted file mode 100644 index 73f1c68..0000000 --- a/examples/DFRobot_AS3935_lightning_I2c/DFRobot_AS3935_lightning_I2c.ino +++ /dev/null @@ -1,111 +0,0 @@ -/*! - * file DFRobot_AS3935_lightning_I2c.ino - * - * On The Lightning Sensor - * This sensor can detect lightning and display the distance and intensity of the lightning without the - * disturbance of electric arc and noise.It can be set as indoor or outdoor mode. - * The module has three I2C, the addresses are: - * 0x03 A0-High A1-High - * 0x02 A0-Low A1-High - * 0x01 A0-High A1-Low - * - * Copyright [DFRobot](http://www.dfrobot.com), 2018 - * Copyright GNU Lesser General Public License - * - * version V0.1 - * date 2018-9-6 - */ - -#include "I2C.h" -#include "DFRobot_AS3935_I2C.h" - -volatile int8_t AS3935_ISR_Trig = 0; - -#define SI_PIN 9 -#define IRQ_PIN 2 - -#define AS3935_CAPACITANCE 72 - -#define AS3935_INDOORS 0 -#define AS3935_OUTDOORS 1 -#define AS3935_DIST_DIS 0 -#define AS3935_DIST_EN 1 - -void AS3935_ISR(); - -DF_AS3935_I2C lightning0((uint8_t)IRQ_PIN, (uint8_t)SI_PIN, (uint8_t)AS3935_ADD3); - -void setup() -{ - - Serial.begin(115200); - Serial.println("Playing With DFRobot: AS3935 Lightning Sensor"); - Serial.println("beginning boot procedure...."); - - // Setup for the the I2C library: (enable pullups, set speed to 400kHz) - I2c.begin(); - I2c.pullup(true); - I2c.setSpeed(1); - delay(2); - - //lightning0.AS3935_I2CAddress(AS3935_ADD1); // x01 - A0->high A1->low - //lightning0.AS3935_I2CAddress(AS3935_ADD2); // x02 - A0->low A1->high - //lightning0.AS3935_I2CAddress(AS3935_ADD3); // x03 - A0->high A1->high - lightning0.AS3935_SetI2CAddress(AS3935_ADD3); - - // Set registers to default - lightning0.AS3935_DefInit(); - // Now update sensor cal for your application and power up chip - lightning0.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_INDOORS, AS3935_DIST_EN); - // Enable interrupt (hook IRQ pin to Arduino Uno/Mega interrupt input: 0 -> pin 2, 1 -> pin 3 ) - attachInterrupt(0, AS3935_ISR, RISING); - // For debug, view register data - lightning0.AS3935_PrintAllRegs(); - // Clear trigger - AS3935_ISR_Trig = 0; - -} - -void loop() -{ - // It does nothing until an interrupt is detected on the IRQ pin. - while(0 == AS3935_ISR_Trig){} - delay(5); - - // Reset interrupt flag - AS3935_ISR_Trig = 0; - - // Now get interrupt source - uint8_t int_src = lightning0.AS3935_GetInterruptSrc(); - if(1 == int_src) - { - // Get rid of non-distance data - uint8_t lightning_dist_km = lightning0.AS3935_GetLightningDistKm(); - Serial.print("发生闪电啦! 闪电发生距离: "); - Serial.print(lightning_dist_km); - Serial.println(" km"); - // Get lightning energy intensity - uint32_t lightning_energy_val = lightning0.AS3935_GetStrikeEnergyRaw(); - Serial.print("雷电强度:"); - Serial.print(lightning_energy_val); - Serial.println(""); - } - else if(2 == int_src) - { - Serial.println("发现干扰源"); - } - else if(3 == int_src) - { - Serial.println("噪音强度过高"); - } - lightning0.AS3935_PrintAllRegs(); - Serial.println(""); -} - -// This is irq handler for AS3935 interrupts, has to return void and take no arguments -// always make code in interrupt handlers fast and short -void AS3935_ISR() -{ - AS3935_ISR_Trig = 1; -} - diff --git a/examples/DFRobot_AS3935_lightning_sensor/DFRobot_AS3935_lightning_sensor.ino b/examples/DFRobot_AS3935_lightning_sensor/DFRobot_AS3935_lightning_sensor.ino new file mode 100644 index 0000000..99109df --- /dev/null +++ b/examples/DFRobot_AS3935_lightning_sensor/DFRobot_AS3935_lightning_sensor.ino @@ -0,0 +1,117 @@ +/*! + file DFRobot_AS3935_lightning_I2c.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.2 + date 2018-10-08 +*/ + +#include "Lib_I2C.h" +#include "DFRobot_AS3935_I2C.h" + +volatile int8_t AS3935_ISR_Trig = 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_ADD3 + +void AS3935_ISR(); + +DF_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); + + // Set registers to default + lightning0.AS3935_DefInit(); + // Configure sensor + lightning0.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_MODE, AS3935_DIST); + // 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.AS3935_SetLCO_FDIV(0); +// lightning0.AS3935_SetIRQ_Output_Source(3); + + attachInterrupt(0, AS3935_ISR, RISING); + +} + +void loop() +{ + // It does nothing until an interrupt is detected on the IRQ pin. + while (AS3935_ISR_Trig == 0) {} + delay(5); + + // Reset interrupt flag + AS3935_ISR_Trig = 0; + + // Get interrupt source + uint8_t int_src = lightning0.AS3935_GetInterruptSrc(); + if (int_src == 1) + { + // Get rid of non-distance data + uint8_t lightning_dist_km = lightning0.AS3935_GetLightningDistKm(); + Serial.println("Lightning occurs!"); + Serial.print("Distance: "); + Serial.print(lightning_dist_km); + Serial.println(" km"); + + // Get lightning energy intensity + uint32_t lightning_energy_val = lightning0.AS3935_GetStrikeEnergyRaw(); + Serial.print("Intensity: "); + Serial.print(lightning_energy_val); + Serial.println(""); + } + else if (int_src == 2) + { + Serial.println("Disturber discovered!"); + } + else if (int_src == 3) + { + Serial.println("Noise level too high!"); + } + +} + +//IRQ handler for AS3935 interrupts +void AS3935_ISR() +{ + AS3935_ISR_Trig = 1; +} +