You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DFRobot_AS393DFRobot_AS39355/DFRobot_AS3935_I2C.cpp

370 lines
10 KiB

6 years ago
#include "DFRobot_AS3935_I2c.h"
6 years ago
DFRobot_AS3935_I2C::DFRobot_AS3935_I2C(uint8_t irqx, uint8_t devAddx)
6 years ago
{
6 years ago
devAdd = devAddx;
irq = irqx;
6 years ago
6 years ago
// initalize the IRQ pins
6 years ago
pinMode(irq, INPUT);
6 years ago
}
6 years ago
void DFRobot_AS3935_I2C::AS3935SetI2CAddress(uint8_t devAddx)
6 years ago
{
6 years ago
if (devAddx == AS3935_ADD1)
6 years ago
{
6 years ago
devAdd = devAddx;
6 years ago
}
6 years ago
else if (devAddx == AS3935_ADD2)
6 years ago
{
6 years ago
devAdd = devAddx;
6 years ago
}
else
{
6 years ago
devAdd = AS3935_ADD3;
6 years ago
}
}
6 years ago
uint8_t DFRobot_AS3935_I2C::singRegRead(uint8_t regAdd)
6 years ago
{
// I2C address Register address num bytes
6 years ago
I2c.read((uint8_t)devAdd, (uint8_t)regAdd, (uint8_t)0x01); // Use I2C library to read register
uint8_t regData = I2c.receive(); // receive the I2C data
return regData;
6 years ago
}
6 years ago
void DFRobot_AS3935_I2C::singRegWrite(uint8_t regAdd, uint8_t dataMask, uint8_t regData)
6 years ago
{
// start by reading original register data (only modifying what we need to)
6 years ago
uint8_t origRegData = singRegRead(regAdd);
6 years ago
// calculate new register data... 'delete' old targeted data, replace with new data
// note: 'DataMask' must be bits targeted for replacement
// add'l note: this function does NOT shift values into the proper place... they need to be there already
6 years ago
uint8_t newRegData = ((origRegData & ~dataMask) | (regData & dataMask));
6 years ago
// finally, write the data to the register
6 years ago
I2c.write(devAdd, regAdd, newRegData);
6 years ago
Serial.print("wrt: ");
6 years ago
Serial.print(newRegData,HEX);
6 years ago
Serial.print(" Act: ");
6 years ago
Serial.println(singRegRead(regAdd),HEX);
6 years ago
}
6 years ago
void DFRobot_AS3935_I2C::AS3935DefInit()
6 years ago
{
6 years ago
AS3935Reset(); // reset registers to default
6 years ago
}
6 years ago
void DFRobot_AS3935_I2C::AS3935Reset()
6 years ago
{
// run PRESET_DEFAULT Direct Command to set all registers in default state
6 years ago
I2c.write(devAdd, (uint8_t)0x3C, (uint8_t)0x96);
6 years ago
delay(2); // wait 2ms to complete
}
6 years ago
void DFRobot_AS3935_I2C::calRCO()
6 years ago
{
// run ALIB_RCO Direct Command to cal internal RCO
6 years ago
I2c.write(devAdd, (uint8_t)0x3D, (uint8_t)0x96);
6 years ago
delay(2); // wait 2ms to complete
}
6 years ago
void DFRobot_AS3935_I2C::AS3935PowerUp(void)
6 years ago
{
// power-up sequence based on datasheet, pg 23/27
// register 0x00, PWD bit: 0 (clears PWD)
6 years ago
singRegWrite(0x00, 0x01, 0x00);
calRCO(); // run RCO cal cmd
singRegWrite(0x08, 0x20, 0x20); // set DISP_SRCO to 1
6 years ago
delay(2);
6 years ago
singRegWrite(0x08, 0x20, 0x00); // set DISP_SRCO to 0
6 years ago
}
6 years ago
void DFRobot_AS3935_I2C::AS3935PowerDown(void)
6 years ago
{
// register 0x00, PWD bit: 0 (sets PWD)
6 years ago
singRegWrite(0x00, 0x01, 0x01);
6 years ago
Serial.println("AS3935 powered down");
}
6 years ago
void DFRobot_AS3935_I2C::AS3935DisturberEn(void)
6 years ago
{
// register 0x03, PWD bit: 5 (sets MASK_DIST)
6 years ago
singRegWrite(0x03, 0x20, 0x00);
6 years ago
Serial.println("disturber detection enabled");
}
6 years ago
void DFRobot_AS3935_I2C::AS3935DisturberDis(void)
6 years ago
{
// register 0x03, PWD bit: 5 (sets MASK_DIST)
6 years ago
singRegWrite(0x03, 0x20, 0x20);
6 years ago
}
6 years ago
void DFRobot_AS3935_I2C::AS3935SetIRQOutputSource(uint8_t irqSelect)
6 years ago
{
// set interrupt source - what to display on IRQ pin
// reg 0x08, bits 5 (TRCO), 6 (SRCO), 7 (LCO)
// only one should be set at once, I think
// 0 = NONE, 1 = TRCO, 2 = SRCO, 3 = LCO
6 years ago
if(1 == irqSelect)
6 years ago
{
6 years ago
singRegWrite(0x08, 0xE0, 0x20); // set only TRCO bit
6 years ago
}
6 years ago
else if(2 == irqSelect)
6 years ago
{
6 years ago
singRegWrite(0x08, 0xE0, 0x40); // set only SRCO bit
6 years ago
}
6 years ago
else if(3 == irqSelect)
6 years ago
{
6 years ago
singRegWrite(0x08, 0xE0, 0x80); // set only LCO bit
6 years ago
}
else // assume 0
{
6 years ago
singRegWrite(0x08, 0xE0, 0x00); // clear IRQ pin display bits
6 years ago
}
}
6 years ago
void DFRobot_AS3935_I2C::AS3935SetTuningCaps(uint8_t capVal)
6 years ago
{
// Assume only numbers divisible by 8 (because that's all the chip supports)
6 years ago
if(120 < capVal) // cap_value out of range, assume highest capacitance
6 years ago
{
6 years ago
singRegWrite(0x08, 0x0F, 0x0F); // set capacitance bits to maximum
6 years ago
}
else
{
6 years ago
singRegWrite(0x08, 0x0F, (capVal >> 3)); // set capacitance bits
6 years ago
}
Serial.print("capacitance set to 8x");
6 years ago
Serial.println((singRegRead(0x08) & 0x0F));
6 years ago
}
6 years ago
uint8_t DFRobot_AS3935_I2C::AS3935GetInterruptSrc(void)
6 years ago
{
// definition of interrupt data on table 18 of datasheet
// for this function:
// 0 = unknown src, 1 = lightning detected, 2 = disturber, 3 = Noise level too high
delay(10); // wait 3ms before reading (min 2ms per pg 22 of datasheet)
6 years ago
uint8_t intSrc = (singRegRead(0x03) & 0x0F); // read register, get rid of non-interrupt data
if(0x08 == intSrc)
6 years ago
{
return 1; // lightning caused interrupt
}
6 years ago
else if(0x04 == intSrc)
6 years ago
{
return 2; // disturber detected
}
6 years ago
else if(0x01 == intSrc)
6 years ago
{
return 3; // Noise level too high
}
else{return 0;} // interrupt result not expected
}
6 years ago
uint8_t DFRobot_AS3935_I2C::AS3935GetLightningDistKm(void)
6 years ago
{
6 years ago
uint8_t strikeDist = (singRegRead(0x07) & 0x3F); // read register, get rid of non-distance data
return strikeDist;
6 years ago
}
6 years ago
uint32_t DFRobot_AS3935_I2C::AS3935GetStrikeEnergyRaw(void)
6 years ago
{
6 years ago
uint32_t nrgyRaw = ((singRegRead(0x06) & 0x1F) << 8); // MMSB, shift 8 bits left, make room for MSB
nrgyRaw |= singRegRead(0x05); // read MSB
nrgyRaw <<= 8; // shift 8 bits left, make room for LSB
nrgyRaw |= singRegRead(0x04); // read LSB, add to others
6 years ago
6 years ago
return nrgyRaw/16777;
6 years ago
}
6 years ago
uint8_t DFRobot_AS3935_I2C::AS3935SetMinStrikes(uint8_t minStrk)
6 years ago
{
// This function sets min strikes to the closest available number, rounding to the floor,
// where necessary, then returns the physical value that was set. Options are 1, 5, 9 or 16 strikes.
// see pg 22 of the datasheet for more info (#strikes in 17 min)
6 years ago
if(5 > minStrk)
6 years ago
{
6 years ago
singRegWrite(0x02, 0x30, 0x00);
6 years ago
return 1;
}
6 years ago
else if(9 > minStrk)
6 years ago
{
6 years ago
singRegWrite(0x02, 0x30, 0x10);
6 years ago
return 5;
}
6 years ago
else if(16 > minStrk)
6 years ago
{
6 years ago
singRegWrite(0x02, 0x30, 0x20);
6 years ago
return 9;
}
else
{
6 years ago
singRegWrite(0x02, 0x30, 0x30);
6 years ago
return 16;
}
}
6 years ago
void DFRobot_AS3935_I2C::AS3935SetIndoors(void)
6 years ago
{
// AFE settings addres 0x00, bits 5:1 (10010, based on datasheet, pg 19, table 15)
// this is the default setting at power-up (AS3935 datasheet, table 9)
6 years ago
singRegWrite(0x00, 0x3E, 0x24);
6 years ago
Serial.println("set up for indoor operation");
}
6 years ago
void DFRobot_AS3935_I2C::AS3935SetOutdoors(void)
6 years ago
{
// AFE settings addres 0x00, bits 5:1 (01110, based on datasheet, pg 19, table 15)
6 years ago
singRegWrite(0x00, 0x3E, 0x1C);
6 years ago
Serial.println("set up for outdoor operation");
}
6 years ago
void DFRobot_AS3935_I2C::AS3935ClearStatistics(void)
6 years ago
{
// clear is accomplished by toggling CL_STAT bit 'high-low-high' (then set low to move on)
6 years ago
singRegWrite(0x02, 0x40, 0x40); // high
singRegWrite(0x02, 0x40, 0x00); // low
singRegWrite(0x02, 0x40, 0x40); // high
6 years ago
}
6 years ago
uint8_t DFRobot_AS3935_I2C::AS3935GetNoiseFloorLvl(void)
6 years ago
{
// NF settings addres 0x01, bits 6:4
// default setting of 010 at startup (datasheet, table 9)
6 years ago
uint8_t regRaw = singRegRead(0x01); // read register 0x01
return ((regRaw & 0x70) >> 4); // should return value from 0-7, see table 16 for info
6 years ago
}
6 years ago
void DFRobot_AS3935_I2C::AS3935SetNoiseFloorLvl(uint8_t nfSel)
6 years ago
{
// NF settings addres 0x01, bits 6:4
// default setting of 010 at startup (datasheet, table 9)
6 years ago
if(7 >= nfSel) // nf_sel within expected range
6 years ago
{
6 years ago
singRegWrite(0x01, 0x70, ((nfSel & 0x07) << 4));
6 years ago
}
else
{ // out of range, set to default (power-up value 010)
6 years ago
singRegWrite(0x01, 0x70, 0x20);
6 years ago
}
}
6 years ago
uint8_t DFRobot_AS3935_I2C::AS3935GetWatchdogThreshold(void)
6 years ago
{
// This function is used to read WDTH. It is used to increase robustness to disturbers,
// though will make detection less efficient (see page 19, Fig 20 of datasheet)
// WDTH register: add 0x01, bits 3:0
// default value of 0001
// values should only be between 0x00 and 0x0F (0 and 7)
6 years ago
uint8_t regRaw = singRegRead(0x01);
return (regRaw & 0x0F);
6 years ago
}
6 years ago
void DFRobot_AS3935_I2C::AS3935SetWatchdogThreshold(uint8_t wdth)
6 years ago
{
// This function is used to modify WDTH. It is used to increase robustness to disturbers,
// though will make detection less efficient (see page 19, Fig 20 of datasheet)
// WDTH register: add 0x01, bits 3:0
// default value of 0001
// values should only be between 0x00 and 0x0F (0 and 7)
6 years ago
singRegWrite(0x01, 0x0F, (wdth & 0x0F));
6 years ago
}
6 years ago
uint8_t DFRobot_AS3935_I2C::AS3935GetSpikeRejection(void)
6 years ago
{
// This function is used to read SREJ (spike rejection). Similar to the Watchdog threshold,
// it is used to make the system more robust to disturbers, though will make general detection
// less efficient (see page 20-21, especially Fig 21 of datasheet)
// SREJ register: add 0x02, bits 3:0
// default value of 0010
// values should only be between 0x00 and 0x0F (0 and 7)
6 years ago
uint8_t regRaw = singRegRead(0x02);
return (regRaw & 0x0F);
6 years ago
}
6 years ago
void DFRobot_AS3935_I2C::AS3935SetSpikeRejection(uint8_t srej)
6 years ago
{
// This function is used to modify SREJ (spike rejection). Similar to the Watchdog threshold,
// it is used to make the system more robust to disturbers, though will make general detection
// less efficient (see page 20-21, especially Fig 21 of datasheet)
// WDTH register: add 0x02, bits 3:0
// default value of 0010
// values should only be between 0x00 and 0x0F (0 and 7)
6 years ago
singRegWrite(0x02, 0x0F, (srej & 0x0F));
6 years ago
}
6 years ago
void DFRobot_AS3935_I2C::AS3935SetLcoFdiv(uint8_t fdiv)
6 years ago
{
// This function sets LCO_FDIV register. This is useful in the tuning of the antenna
// LCO_FDIV register: add 0x03, bits 7:6
// 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.
6 years ago
singRegWrite(0x03, 0xC0, ((fdiv & 0x03) << 6));
6 years ago
}
6 years ago
void DFRobot_AS3935_I2C::AS3935PrintAllRegs(void)
6 years ago
{
Serial.print("Reg 0x00: ");
6 years ago
Serial.println(singRegRead(0x00));
6 years ago
Serial.print("Reg 0x01: ");
6 years ago
Serial.println(singRegRead(0x01));
6 years ago
Serial.print("Reg 0x02: ");
6 years ago
Serial.println(singRegRead(0x02));
6 years ago
Serial.print("Reg 0x03: ");
6 years ago
Serial.println(singRegRead(0x03));
6 years ago
Serial.print("Reg 0x04: ");
6 years ago
Serial.println(singRegRead(0x04));
6 years ago
Serial.print("Reg 0x05: ");
6 years ago
Serial.println(singRegRead(0x05));
6 years ago
Serial.print("Reg 0x06: ");
6 years ago
Serial.println(singRegRead(0x06));
6 years ago
Serial.print("Reg 0x07: ");
6 years ago
Serial.println(singRegRead(0x07));
6 years ago
Serial.print("Reg 0x08: ");
6 years ago
Serial.println(singRegRead(0x08));
uint32_t nrgyVal = AS3935GetStrikeEnergyRaw();
Serial.println(nrgyVal);
6 years ago
}
6 years ago
void DFRobot_AS3935_I2C::AS3935ManualCal(uint8_t capacitance, uint8_t location, uint8_t disturber)
6 years ago
{
// start by powering up
6 years ago
AS3935PowerUp();
6 years ago
// indoors/outdoors next...
if(1 == location) // set outdoors if 1
{
6 years ago
AS3935SetOutdoors();
6 years ago
}
else // set indoors if anything but 1
{
6 years ago
AS3935SetIndoors();
6 years ago
}
// disturber cal
if(0 == disturber) // disabled if 0
{
6 years ago
AS3935DisturberDis();
6 years ago
}
else // enabled if anything but 0
{
6 years ago
AS3935DisturberEn();
6 years ago
}
6 years ago
AS3935SetIRQOutputSource(0);
6 years ago
delay(500);
// capacitance first... directly write value here
6 years ago
AS3935SetTuningCaps(capacitance);
6 years ago
Serial.println("AS3935 manual cal complete");
}
// a nice function would be to read the last 'x' strike data values....