From 26a9bf394f9a9e18e5392bd51e5f0ecbb316edd8 Mon Sep 17 00:00:00 2001 From: ouki-wang <358023925@qq.com> Date: Wed, 28 Nov 2018 11:37:36 +0800 Subject: [PATCH] arduino V0.5 --- DFRobot_AS3935_I2C.cpp | 20 +++++---- DFRobot_AS3935_I2C.h | 7 ++-- ...Robot_AS3935_lightning_sensor_detailed.ino | 42 +++++++++---------- ...Robot_AS3935_lightning_sensor_ordinary.ino | 17 ++++---- keywords.txt | 1 + readme.md | 11 ++++- 6 files changed, 59 insertions(+), 39 deletions(-) diff --git a/DFRobot_AS3935_I2C.cpp b/DFRobot_AS3935_I2C.cpp index a429bdd..7e23d8a 100644 --- a/DFRobot_AS3935_I2C.cpp +++ b/DFRobot_AS3935_I2C.cpp @@ -4,10 +4,15 @@ DFRobot_AS3935_I2C::DFRobot_AS3935_I2C(uint8_t irqx, uint8_t devAddx) { devAdd = devAddx; irq = irqx; - // initalize the IRQ pins pinMode(irq, INPUT); +} +DFRobot_AS3935_I2C::DFRobot_AS3935_I2C(uint8_t irqx) +{ + irq = irqx; + // initalize the IRQ pins + pinMode(irq, INPUT); } void DFRobot_AS3935_I2C::setI2CAddress(uint8_t devAddx) @@ -52,16 +57,17 @@ void DFRobot_AS3935_I2C::singRegWrite(uint8_t regAdd, uint8_t dataMask, uint8_t //Serial.println(singRegRead(regAdd),HEX); } -void DFRobot_AS3935_I2C::defInit() +int DFRobot_AS3935_I2C::defInit() { - reset(); // reset registers to default + return reset(); // reset registers to default } -void DFRobot_AS3935_I2C::reset() +int 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); + int error = I2c.write(devAdd, (uint8_t)0x3C, (uint8_t)0x96); delay(2); // wait 2ms to complete + return error; } void DFRobot_AS3935_I2C::calRCO() @@ -139,8 +145,8 @@ void DFRobot_AS3935_I2C::setTuningCaps(uint8_t capVal) { singRegWrite(0x08, 0x0F, (capVal >> 3)); // set capacitance bits } - Serial.print("capacitance set to 8x"); - Serial.println((singRegRead(0x08) & 0x0F)); + //Serial.print("capacitance set to 8x"); + //Serial.println((singRegRead(0x08) & 0x0F)); } uint8_t DFRobot_AS3935_I2C::getInterruptSrc(void) diff --git a/DFRobot_AS3935_I2C.h b/DFRobot_AS3935_I2C.h index ae45dd6..4b775c1 100644 --- a/DFRobot_AS3935_I2C.h +++ b/DFRobot_AS3935_I2C.h @@ -16,12 +16,13 @@ class DFRobot_AS3935_I2C { public: DFRobot_AS3935_I2C(uint8_t irqx, uint8_t devAddx); + DFRobot_AS3935_I2C(uint8_t irqx); /*! Set i2c address */ void setI2CAddress(uint8_t devAddx); /*! Manual calibration */ void manualCal(uint8_t capacitance, uint8_t location, uint8_t disturber); /*! reset registers to default */ - void defInit(void); + int defInit(void); void disturberEn(void); void disturberDis(void); void setIRQOutputSource(uint8_t irqSelect); @@ -45,13 +46,13 @@ class DFRobot_AS3935_I2C void setLcoFdiv(uint8_t fdiv); /*! View register data */ void printAllRegs(void); + void powerUp(void); private: uint8_t irq, devAdd; uint8_t singRegRead(uint8_t regAdd); void singRegWrite(uint8_t regAdd, uint8_t dataMask, uint8_t regData); - void reset(void); - void powerUp(void); + int reset(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 index 04caee3..0c3fbb6 100644 --- 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 @@ -5,15 +5,16 @@ 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 + AS3935_ADD1 0x01 A0 = 1 A1 = 0 + AS3935_ADD2 0x02 A0 = 0 A1 = 1 + AS3935_ADD3 0x03 A0 = 1 A1 = 1 + Copyright [DFRobot](http://www.dfrobot.com), 2018 Copyright GNU Lesser General Public License - version V0.4 - date 2018-11-15 + version V0.5 + date 2018-11-28 */ #include "Lib_I2C.h" @@ -26,22 +27,12 @@ volatile int8_t AS3935IsrTrig = 0; // 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 +#define AS3935_I2C_ADDR AS3935_ADD3 void AS3935_ISR(); -DFRobot_AS3935_I2C lightning0((uint8_t)IRQ_PIN, (uint8_t)AS3935_I2C_ADDR); +DFRobot_AS3935_I2C lightning0((uint8_t)IRQ_PIN); void setup() { @@ -57,9 +48,12 @@ void setup() lightning0.setI2CAddress(AS3935_ADD3); // Set registers to default - lightning0.defInit(); + if(lightning0.defInit() != 0){ + Serial.println("I2C init fail"); + while(1){} + } // Configure sensor - lightning0.manualCal(AS3935_CAPACITANCE, AS3935_MODE, AS3935_DIST); + lightning0.powerUp(); //set indoors or outdoors models lightning0.setIndoors(); @@ -68,6 +62,12 @@ void setup() //disturber detection lightning0.disturberEn(); //lightning0.disturberDis(); + + lightning0.setIRQOutputSource(0); + delay(500); + //set capacitance + lightning0.setTuningCaps(AS3935_CAPACITANCE); + Serial.println("AS3935 manual cal complete"); // Enable interrupt (connect IRQ pin IRQ_PIN: 2, default) // Connect the IRQ and GND pin to the oscilloscope. @@ -77,12 +77,12 @@ void setup() // lightning0.setLcoFdiv(0); // lightning0.setIRQOutputSource(3); -// Set the noise level,use a default value greater than 7 +// Set the noise level,more than 7 will use the default value:2 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); + lightning0.setWatchdogThreshold(1); //uint8_t wtdgThreshold = lightning0.getWatchdogThreshold(); //used to modify SREJ (spike rejection),values should only be between 0x00 and 0x0F (0 and 7) diff --git a/examples/DFRobot_AS3935_lightning_sensor_ordinary/DFRobot_AS3935_lightning_sensor_ordinary.ino b/examples/DFRobot_AS3935_lightning_sensor_ordinary/DFRobot_AS3935_lightning_sensor_ordinary.ino index a571777..688c357 100644 --- a/examples/DFRobot_AS3935_lightning_sensor_ordinary/DFRobot_AS3935_lightning_sensor_ordinary.ino +++ b/examples/DFRobot_AS3935_lightning_sensor_ordinary/DFRobot_AS3935_lightning_sensor_ordinary.ino @@ -5,15 +5,15 @@ 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 - + AS3935_ADD1 0x01 A0 = 1 A1 = 0 + AS3935_ADD2 0x02 A0 = 0 A1 = 1 + AS3935_ADD3 0x03 A0 = 1 A1 = 1 + Copyright [DFRobot](http://www.dfrobot.com), 2018 Copyright GNU Lesser General Public License - version V0.4 - date 2018-11-15 + version V0.5 + date 2018-11-28 */ #include "Lib_I2C.h" @@ -56,7 +56,10 @@ void setup() delay(2); // Set registers to default - lightning0.defInit(); + if(lightning0.defInit() != 0){ + Serial.println("I2C init fail"); + while(1){} + } // Configure sensor lightning0.manualCal(AS3935_CAPACITANCE, AS3935_MODE, AS3935_DIST); // Enable interrupt (connect IRQ pin IRQ_PIN: 2, default) diff --git a/keywords.txt b/keywords.txt index cdaed40..be86de4 100644 --- a/keywords.txt +++ b/keywords.txt @@ -46,6 +46,7 @@ getSpikeRejection KEYWORD2 setSpikeRejection KEYWORD2 setLcoFdiv KEYWORD2 printAllRegs KEYWORD2 +powerUp KEYWORD2 ####################################### # Constants (LITERAL1) diff --git a/readme.md b/readme.md index 44a1f57..d2375be 100644 --- a/readme.md +++ b/readme.md @@ -50,10 +50,19 @@ Download the library ZIP file and unzip it to the Arduino folder of the library. */ DFRobot_AS3935_I2C(uint8_t irqx, uint8_t devAddx); +/* + * @brief AS3935 object + * + * @param irqx irq pin + */ +DFRobot_AS3935_I2C(uint8_t irqx); + /* * @brief reset registers to default + * + * @return 0 success */ -void defInit(void); +int defInit(void); /* * @brief set i2c address