compatibility esp8266

pull/3/head
tangjie133 3 years ago
parent 38f851e63d
commit 065a4674d2
  1. 77
      DFRobot_AS3935_I2C.cpp
  2. 41
      DFRobot_AS3935_I2C.h
  3. 728
      Lib_I2C.cpp
  4. 76
      Lib_I2C.h
  5. 40
      examples/DFRobot_AS3935_lightning_sensor_detailed/DFRobot_AS3935_lightning_sensor_detailed.ino
  6. 34
      examples/DFRobot_AS3935_lightning_sensor_ordinary/DFRobot_AS3935_lightning_sensor_ordinary.ino
  7. 1
      readme.md

@ -12,7 +12,10 @@ DFRobot_AS3935_I2C::DFRobot_AS3935_I2C(uint8_t irqx)
{
irq = irqx;
// initalize the IRQ pins
// pinMode(irq, OUTPUT);
// digitalWrite(irq,1);
pinMode(irq, INPUT);
}
void DFRobot_AS3935_I2C::setI2CAddress(uint8_t devAddx)
@ -34,11 +37,14 @@ void DFRobot_AS3935_I2C::setI2CAddress(uint8_t devAddx)
uint8_t DFRobot_AS3935_I2C::singRegRead(uint8_t regAdd)
{
// I2C address Register address num bytes
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;
//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
uint8_t buf[1];
readReg(regAdd, buf, 1);
return buf[0];
}
void DFRobot_AS3935_I2C::singRegWrite(uint8_t regAdd, uint8_t dataMask, uint8_t regData)
{
// start by reading original register data (only modifying what we need to)
@ -48,9 +54,12 @@ void DFRobot_AS3935_I2C::singRegWrite(uint8_t regAdd, uint8_t dataMask, uint8_t
// 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
uint8_t newRegData = ((origRegData & ~dataMask) | (regData & dataMask));
uint8_t buf[1];
buf[0] = newRegData;
// finally, write the data to the register
I2c.write(devAdd, regAdd, newRegData);
//I2c.write(devAdd, regAdd, newRegData);
writeReg(regAdd, buf, 1);
//Serial.print("wrt: ");
//Serial.print(newRegData,HEX);
//Serial.print(" Act: ");
@ -65,15 +74,21 @@ int DFRobot_AS3935_I2C::defInit()
int DFRobot_AS3935_I2C::reset()
{
// run PRESET_DEFAULT Direct Command to set all registers in default state
int error = I2c.write(devAdd, (uint8_t)0x3C, (uint8_t)0x96);
delay(2); // wait 2ms to complete
return error;
//int error = I2c.write(devAdd, (uint8_t)0x3C, (uint8_t)0x96);
uint8_t buf[1];
buf[0] = 0x96;
writeReg(0x3c, buf, 1);
return 0;
}
void DFRobot_AS3935_I2C::calRCO()
{
// run ALIB_RCO Direct Command to cal internal RCO
I2c.write(devAdd, (uint8_t)0x3D, (uint8_t)0x96);
//I2c.write(devAdd, (uint8_t)0x3D, (uint8_t)0x96);
uint8_t buf[1];
buf[0] = 0x96;
writeReg(0x3D, buf, 1);
delay(2); // wait 2ms to complete
}
@ -369,3 +384,49 @@ void DFRobot_AS3935_I2C::manualCal(uint8_t capacitance, uint8_t location, uint8_
}
// a nice function would be to read the last 'x' strike data values....
uint8_t DFRobot_AS3935_I2C::begin(void)
{
uint8_t buf[2];
Wire.begin();
Wire.setClock(400000);
DBG("i2c init");
if(readReg(0, buf, 2) == 2){
DBG("return");
return 0;
}
return 1;
}
void DFRobot_AS3935_I2C::writeReg(uint8_t reg, void *pBuf, size_t size)
{
if(pBuf == NULL){
DBG("pBuf ERROR!! :null pointer");
}
uint8_t *_pBuf = (uint8_t*)pBuf;
Wire.beginTransmission(devAdd);
Wire.write(reg);
for(size_t i = 0; i < size; i++){
Wire.write(_pBuf[i]);
}
Wire.endTransmission();
DBG("i2c write");
}
size_t DFRobot_AS3935_I2C::readReg(uint8_t reg, void *pBuf, size_t size)
{
if(pBuf == NULL){
DBG("pBuf ERROR!!:null pointer");
return 0;
}
uint8_t *_pBuf = (uint8_t*)pBuf;
Wire.beginTransmission(devAdd);
Wire.write(reg);
Wire.endTransmission(false);
Wire.requestFrom(devAdd, size);
for(size_t i = 0; i < size; i++){
_pBuf[i] = Wire.read();
DBG(_pBuf[i], HEX);
}
return size;
}

@ -2,21 +2,30 @@
#define DFRobot_AS3935_I2C_h
#include "Arduino.h"
#include "avr/pgmspace.h"
#include "util/delay.h"
#include "stdlib.h"
#include "Lib_I2C.h"
#include "Wire.h"
// 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
//#define ENABLE_DBG
#ifdef ENABLE_DBG
#define DBG(...){Serial.print("[");Serial.print(__FUNCTION__);\
Serial.print("():");Serial.print(__LINE__);\
Serial.print("]");Serial.print(__VA_ARGS__); Serial.println("");}
#else
#define DBG(...)
#endif
class DFRobot_AS3935_I2C
{
public:
DFRobot_AS3935_I2C(uint8_t irqx, uint8_t devAddx);
DFRobot_AS3935_I2C(uint8_t irqx);
uint8_t begin(void);
/*! Set i2c address */
void setI2CAddress(uint8_t devAddx);
/*! Manual calibration */
@ -50,11 +59,33 @@ class DFRobot_AS3935_I2C
private:
uint8_t irq, devAdd;
uint8_t singRegRead(uint8_t regAdd);
void singRegWrite(uint8_t regAdd, uint8_t dataMask, uint8_t regData);
uint8_t singRegRead(uint8_t regAdd);//原始I2C数据读取
void singRegWrite(uint8_t regAdd, uint8_t dataMask, uint8_t regData);//原始数据发送
int reset(void);
void powerDown(void);
void calRCO(void);
/**
* @brief Write register value through IIC bus
*
* @param reg Register address 8bits
* @param pBuf Storage cache to write data in
* @param size The length of data to be written
*/
void writeReg(uint8_t reg, void *pBuf, size_t size);
//void writeRegNoStop(uint8_t reg, void *pBuf, size_t size)
/**
* @brief Read register value through IIC bus
*
* @param reg Register address 8bits
* @param pBuf Read data storage cache
* @param size Read the length of data
* @return Return the read length
*/
size_t readReg(uint8_t reg, void *pBuf, size_t size);
};
#endif

@ -1,728 +0,0 @@
/*
I2C.cpp - I2C library
Copyright (c) 2011-2012 Wayne Truchsess. All right reserved.
Rev 5.0 - January 24th, 2012
- Removed the use of interrupts completely from the library
so TWI state changes are now polled.
- Added calls to lockup() function in most functions
to combat arbitration problems
- Fixed scan() procedure which left timeouts enabled
and set to 80msec after exiting procedure
- Changed scan() address range back to 0 - 0x7F
- Removed all Wire legacy functions from library
- A big thanks to Richard Baldwin for all the testing
and feedback with debugging bus lockups!
Rev 4.0 - January 14th, 2012
- Updated to make compatible with 8MHz clock frequency
Rev 3.0 - January 9th, 2012
- Modified library to be compatible with Arduino 1.0
- Changed argument type from boolean to uint8_t in pullUp(),
setSpeed() and receiveByte() functions for 1.0 compatability
- Modified return values for timeout feature to report
back where in the transmission the timeout occured.
- added function scan() to perform a bus scan to find devices
attached to the I2C bus. Similar to work done by Todbot
and Nick Gammon
Rev 2.0 - September 19th, 2011
- Added support for timeout function to prevent
and recover from bus lockup (thanks to PaulS
and CrossRoads on the Arduino forum)
- Changed return type for stop() from void to
uint8_t to handle timeOut function
Rev 1.0 - August 8th, 2011
This is a modified version of the Arduino Wire/TWI
library. Functions were rewritten to provide more functionality
and also the use of Repeated Start. Some I2C devices will not
function correctly without the use of a Repeated Start. The
initial version of this library only supports the Master.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if(ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <inttypes.h>
#include "Lib_I2C.h"
uint8_t I2C::bytesAvailable = 0;
uint8_t I2C::bufferIndex = 0;
uint8_t I2C::totalBytes = 0;
uint16_t I2C::timeOutDelay = 0;
I2C::I2C()
{
}
////////////// Public Methods ////////////////////////////////////////
void I2C::begin()
{
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
// activate internal pull-ups for twi
// as per note from atmega8 manual pg167
sbi(PORTC, 4);
sbi(PORTC, 5);
#else
// activate internal pull-ups for twi
// as per note from atmega128 manual pg204
sbi(PORTD, 0);
sbi(PORTD, 1);
#endif
// initialize twi prescaler and bit rate
cbi(TWSR, TWPS0);
cbi(TWSR, TWPS1);
TWBR = ((F_CPU / 100000) - 16) / 2;
// enable twi module and acks
TWCR = _BV(TWEN) | _BV(TWEA);
}
void I2C::end()
{
TWCR = 0;
}
void I2C::timeOut(uint16_t _timeOut)
{
timeOutDelay = _timeOut;
}
void I2C::setSpeed(uint8_t _fast)
{
if(!_fast)
{
TWBR = ((F_CPU / 100000) - 16) / 2;
}
else
{
TWBR = ((F_CPU / 400000) - 16) / 2;
}
}
void I2C::pullup(uint8_t activate)
{
if(activate)
{
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
// activate internal pull-ups for twi
// as per note from atmega8 manual pg167
sbi(PORTC, 4);
sbi(PORTC, 5);
#else
// activate internal pull-ups for twi
// as per note from atmega128 manual pg204
sbi(PORTD, 0);
sbi(PORTD, 1);
#endif
}
else
{
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
// deactivate internal pull-ups for twi
// as per note from atmega8 manual pg167
cbi(PORTC, 4);
cbi(PORTC, 5);
#else
// deactivate internal pull-ups for twi
// as per note from atmega128 manual pg204
cbi(PORTD, 0);
cbi(PORTD, 1);
#endif
}
}
void I2C::scan()
{
uint16_t tempTime = timeOutDelay;
timeOut(80);
uint8_t totalDevicesFound = 0;
Serial.println(F("Scanning for devices...please wait"));
Serial.println();
for(uint8_t s = 0; s <= 0x7F; s++)
{
returnStatus = 0;
returnStatus = start();
if(!returnStatus)
{
returnStatus = sendAddress(SLA_W(s));
}
if(returnStatus)
{
if(returnStatus == 1)
{
Serial.println(F("There is a problem with the bus, could not complete scan"));
timeOutDelay = tempTime;
return;
}
}
else
{
Serial.print(F("Found device at address - "));
Serial.print(F(" 0x"));
Serial.println(s,HEX);
totalDevicesFound++;
}
stop();
}
if(!totalDevicesFound){Serial.println(F("No devices found"));}
timeOutDelay = tempTime;
}
uint8_t I2C::available()
{
return(bytesAvailable);
}
uint8_t I2C::receive()
{
bufferIndex = totalBytes - bytesAvailable;
if(!bytesAvailable)
{
bufferIndex = 0;
return(0);
}
bytesAvailable--;
return(data[bufferIndex]);
}
/*return values for new functions that use the timeOut feature
will now return at what point in the transmission the timeout
occurred. Looking at a full communication sequence between a
master and slave (transmit data and then readback data) there
a total of 7 points in the sequence where a timeout can occur.
These are listed below and correspond to the returned value:
1 - Waiting for successful completion of a Start bit
2 - Waiting for ACK/NACK while addressing slave in transmit mode (MT)
3 - Waiting for ACK/NACK while sending data to the slave
4 - Waiting for successful completion of a Repeated Start
5 - Waiting for ACK/NACK while addressing slave in receiver mode (MR)
6 - Waiting for ACK/NACK while receiving data from the slave
7 - Waiting for successful completion of the Stop bit
All possible return values:
0 Function executed with no errors
1 - 7 Timeout occurred, see above list
8 - 0xFF See datasheet for exact meaning */
/////////////////////////////////////////////////////
uint8_t I2C::write(uint8_t address, uint8_t registerAddress)
{
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_W(address));
if(returnStatus)
{
if(returnStatus == 1){return(2);}
return(returnStatus);
}
returnStatus = sendByte(registerAddress);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
uint8_t I2C::write(int address, int registerAddress)
{
return(write((uint8_t) address, (uint8_t) registerAddress));
}
uint8_t I2C::write(uint8_t address, uint8_t registerAddress, uint8_t data)
{
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_W(address));
if(returnStatus)
{
if(returnStatus == 1){return(2);}
return(returnStatus);
}
returnStatus = sendByte(registerAddress);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
returnStatus = sendByte(data);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
uint8_t I2C::write(int address, int registerAddress, int data)
{
return(write((uint8_t) address, (uint8_t) registerAddress, (uint8_t) data));
}
uint8_t I2C::write(uint8_t address, uint8_t registerAddress, char *data)
{
uint8_t bufferLength = strlen(data);
returnStatus = 0;
returnStatus = write(address, registerAddress, (uint8_t*)data, bufferLength);
return(returnStatus);
}
uint8_t I2C::write(uint8_t address, uint8_t registerAddress, uint8_t *data, uint8_t numberBytes)
{
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_W(address));
if(returnStatus)
{
if(returnStatus == 1){return(2);}
return(returnStatus);
}
returnStatus = sendByte(registerAddress);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
for (uint8_t i = 0; i < numberBytes; i++)
{
returnStatus = sendByte(data[i]);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
uint8_t I2C::read(int address, int numberBytes)
{
return(read((uint8_t) address, (uint8_t) numberBytes));
}
uint8_t I2C::read(uint8_t address, uint8_t numberBytes)
{
bytesAvailable = 0;
bufferIndex = 0;
if(numberBytes == 0){numberBytes++;}
nack = numberBytes - 1;
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_R(address));
if(returnStatus)
{
if(returnStatus == 1){return(5);}
return(returnStatus);
}
for(uint8_t i = 0; i < numberBytes; i++)
{
if( i == nack )
{
returnStatus = receiveByte(0);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_NACK){return(returnStatus);}
}
else
{
returnStatus = receiveByte(1);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_ACK){return(returnStatus);}
}
data[i] = TWDR;
bytesAvailable = i+1;
totalBytes = i+1;
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
uint8_t I2C::read(int address, int registerAddress, int numberBytes)
{
return(read((uint8_t) address, (uint8_t) registerAddress, (uint8_t) numberBytes));
}
uint8_t I2C::read(uint8_t address, uint8_t registerAddress, uint8_t numberBytes)
{
bytesAvailable = 0;
bufferIndex = 0;
if(numberBytes == 0){numberBytes++;}
nack = numberBytes - 1;
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_W(address));
if(returnStatus)
{
if(returnStatus == 1){return(2);}
return(returnStatus);
}
returnStatus = sendByte(registerAddress);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
returnStatus = start();
if(returnStatus)
{
if(returnStatus == 1){return(4);}
return(returnStatus);
}
returnStatus = sendAddress(SLA_R(address));
if(returnStatus)
{
if(returnStatus == 1){return(5);}
return(returnStatus);
}
for(uint8_t i = 0; i < numberBytes; i++)
{
if( i == nack )
{
returnStatus = receiveByte(0);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_NACK){return(returnStatus);}
}
else
{
returnStatus = receiveByte(1);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_ACK){return(returnStatus);}
}
data[i] = TWDR;
bytesAvailable = i+1;
totalBytes = i+1;
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
uint8_t I2C::read(uint8_t address, uint8_t numberBytes, uint8_t *dataBuffer)
{
bytesAvailable = 0;
bufferIndex = 0;
if(numberBytes == 0){numberBytes++;}
nack = numberBytes - 1;
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_R(address));
if(returnStatus)
{
if(returnStatus == 1){return(5);}
return(returnStatus);
}
for(uint8_t i = 0; i < numberBytes; i++)
{
if( i == nack )
{
returnStatus = receiveByte(0);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_NACK){return(returnStatus);}
}
else
{
returnStatus = receiveByte(1);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_ACK){return(returnStatus);}
}
dataBuffer[i] = TWDR;
bytesAvailable = i+1;
totalBytes = i+1;
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
uint8_t I2C::read(uint8_t address, uint8_t registerAddress, uint8_t numberBytes, uint8_t *dataBuffer)
{
bytesAvailable = 0;
bufferIndex = 0;
if(numberBytes == 0){numberBytes++;}
nack = numberBytes - 1;
returnStatus = 0;
returnStatus = start();
if(returnStatus){return(returnStatus);}
returnStatus = sendAddress(SLA_W(address));
if(returnStatus)
{
if(returnStatus == 1){return(2);}
return(returnStatus);
}
returnStatus = sendByte(registerAddress);
if(returnStatus)
{
if(returnStatus == 1){return(3);}
return(returnStatus);
}
returnStatus = start();
if(returnStatus)
{
if(returnStatus == 1){return(4);}
return(returnStatus);
}
returnStatus = sendAddress(SLA_R(address));
if(returnStatus)
{
if(returnStatus == 1){return(5);}
return(returnStatus);
}
for(uint8_t i = 0; i < numberBytes; i++)
{
if( i == nack )
{
returnStatus = receiveByte(0);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_NACK){return(returnStatus);}
}
else
{
returnStatus = receiveByte(1);
if(returnStatus == 1){return(6);}
if(returnStatus != MR_DATA_ACK){return(returnStatus);}
}
dataBuffer[i] = TWDR;
bytesAvailable = i+1;
totalBytes = i+1;
}
returnStatus = stop();
if(returnStatus)
{
if(returnStatus == 1){return(7);}
return(returnStatus);
}
return(returnStatus);
}
/////////////// Private Methods ////////////////////////////////////////
uint8_t I2C::start()
{
unsigned long startingTime = millis();
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)))
{
if(!timeOutDelay){continue;}
if((millis() - startingTime) >= timeOutDelay)
{
lockUp();
return(1);
}
}
if ((TWI_STATUS == START) || (TWI_STATUS == REPEATED_START))
{
return(0);
}
if (TWI_STATUS == LOST_ARBTRTN)
{
uint8_t bufferedStatus = TWI_STATUS;
lockUp();
return(bufferedStatus);
}
return(TWI_STATUS);
}
uint8_t I2C::sendAddress(uint8_t i2cAddress)
{
TWDR = i2cAddress;
unsigned long startingTime = millis();
TWCR = (1 << TWINT) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)))
{
if(!timeOutDelay){continue;}
if((millis() - startingTime) >= timeOutDelay)
{
lockUp();
return(1);
}
}
if ((TWI_STATUS == MT_SLA_ACK) || (TWI_STATUS == MR_SLA_ACK))
{
return(0);
}
uint8_t bufferedStatus = TWI_STATUS;
if ((TWI_STATUS == MT_SLA_NACK) || (TWI_STATUS == MR_SLA_NACK))
{
stop();
return(bufferedStatus);
}
else
{
lockUp();
return(bufferedStatus);
}
}
uint8_t I2C::sendByte(uint8_t i2cData)
{
TWDR = i2cData;
unsigned long startingTime = millis();
TWCR = (1 << TWINT) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)))
{
if(!timeOutDelay){continue;}
if((millis() - startingTime) >= timeOutDelay)
{
lockUp();
return(1);
}
}
if (TWI_STATUS == MT_DATA_ACK)
{
return(0);
}
uint8_t bufferedStatus = TWI_STATUS;
if (TWI_STATUS == MT_DATA_NACK)
{
stop();
return(bufferedStatus);
}
else
{
lockUp();
return(bufferedStatus);
}
}
uint8_t I2C::receiveByte(uint8_t ack)
{
unsigned long startingTime = millis();
if(ack)
{
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
}
else
{
TWCR = (1 << TWINT) | (1 << TWEN);
}
while (!(TWCR & (1 << TWINT)))
{
if(!timeOutDelay){continue;}
if((millis() - startingTime) >= timeOutDelay)
{
lockUp();
return(1);
}
}
if (TWI_STATUS == LOST_ARBTRTN)
{
uint8_t bufferedStatus = TWI_STATUS;
lockUp();
return(bufferedStatus);
}
return(TWI_STATUS);
}
uint8_t I2C::receiveByte(uint8_t ack, uint8_t *target)
{
uint8_t stat = I2C::receiveByte(ack);
if (stat == 1)
{
return(6);
}
if (ack)
{
if(stat != MR_DATA_ACK)
{
*target = 0x0;
return(stat);
}
}
else
{
if(stat != MR_DATA_NACK)
{
*target = 0x0;
return(stat);
}
}
*target = TWDR;
// I suppose that if we get this far we're ok
return 0;
}
uint8_t I2C::stop()
{
unsigned long startingTime = millis();
TWCR = (1 << TWINT)|(1 << TWEN)| (1 << TWSTO);
while ((TWCR & (1 << TWSTO)))
{
if(!timeOutDelay){continue;}
if((millis() - startingTime) >= timeOutDelay)
{
lockUp();
return(1);
}
}
return(0);
}
void I2C::lockUp()
{
TWCR = 0; //releases SDA and SCL lines to high impedance
TWCR = _BV(TWEN) | _BV(TWEA); //reinitialize TWI
}
I2C I2c = I2C();

@ -1,76 +0,0 @@
#if(ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <inttypes.h>
#ifndef I2C_h
#define I2C_h
#define START 0x08
#define REPEATED_START 0x10
#define MT_SLA_ACK 0x18
#define MT_SLA_NACK 0x20
#define MT_DATA_ACK 0x28
#define MT_DATA_NACK 0x30
#define MR_SLA_ACK 0x40
#define MR_SLA_NACK 0x48
#define MR_DATA_ACK 0x50
#define MR_DATA_NACK 0x58
#define LOST_ARBTRTN 0x38
#define TWI_STATUS (TWSR & 0xF8)
#define SLA_W(address) (address << 1)
#define SLA_R(address) ((address << 1) + 0x01)
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#define MAX_BUFFER_SIZE 32
class I2C
{
public:
I2C();
void begin();
void end();
void timeOut(uint16_t);
void setSpeed(uint8_t);
void pullup(uint8_t);
void scan();
uint8_t available();
uint8_t receive();
uint8_t write(uint8_t, uint8_t);
uint8_t write(int, int);
uint8_t write(uint8_t, uint8_t, uint8_t);
uint8_t write(int, int, int);
uint8_t write(uint8_t, uint8_t, char*);
uint8_t write(uint8_t, uint8_t, uint8_t*, uint8_t);
uint8_t read(uint8_t, uint8_t);
uint8_t read(int, int);
uint8_t read(uint8_t, uint8_t, uint8_t);
uint8_t read(int, int, int);
uint8_t read(uint8_t, uint8_t, uint8_t*);
uint8_t read(uint8_t, uint8_t, uint8_t, uint8_t*);
uint8_t start();
uint8_t sendAddress(uint8_t);
uint8_t sendByte(uint8_t);
uint8_t receiveByte(uint8_t);
uint8_t receiveByte(uint8_t, uint8_t *target);
uint8_t stop();
private:
void lockUp();
uint8_t returnStatus;
uint8_t nack;
uint8_t data[MAX_BUFFER_SIZE];
static uint8_t bytesAvailable;
static uint8_t bufferIndex;
static uint8_t totalBytes;
static uint16_t timeOutDelay;
};
extern I2C I2c;
#endif

@ -13,16 +13,22 @@
Copyright [DFRobot](http://www.dfrobot.com), 2018
Copyright GNU Lesser General Public License
version V1.0
date 2018-11-28
version V1.1
date 2021-08-24
*/
#include "Lib_I2C.h"
#include "DFRobot_AS3935_I2C.h"
volatile int8_t AS3935IsrTrig = 0;
#if defined(ESP32) || defined(ESP8266)
#define IRQ_PIN 0
#else
#define IRQ_PIN 2
#endif
// Antenna tuning capcitance (must be integer multiple of 8, 8 - 120 pf)
#define AS3935_CAPACITANCE 96
@ -40,18 +46,14 @@ 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
if(lightning0.defInit() != 0){
Serial.println("I2C init fail");
while(1){}
while (lightning0.begin() != 0)
{
Serial.print(".");
}
lightning0.defInit();
// Configure sensor
lightning0.powerUp();
@ -64,6 +66,12 @@ void setup()
//lightning0.disturberDis();
lightning0.setIRQOutputSource(0);
#if defined(ESP32) || defined(ESP8266)
attachInterrupt(digitalPinToInterrupt(IRQ_PIN),AS3935_ISR,RISING);
#else
attachInterrupt(/*Interrupt No*/0,AS3935_ISR,RISING);
#endif
delay(500);
//set capacitance
lightning0.setTuningCaps(AS3935_CAPACITANCE);
@ -88,15 +96,12 @@ void setup()
//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) {}
while (AS3935IsrTrig == 0) {delay(1);}
delay(5);
// Reset interrupt flag
@ -137,4 +142,3 @@ void AS3935_ISR()
{
AS3935IsrTrig = 1;
}

@ -12,16 +12,19 @@
Copyright [DFRobot](http://www.dfrobot.com), 2018
Copyright GNU Lesser General Public License
version V1.0
date 2018-11-28
version V1.1
date 2021-08-24
*/
#include "Lib_I2C.h"
#include "DFRobot_AS3935_I2C.h"
volatile int8_t AS3935IsrTrig = 0;
#if defined(ESP32) || defined(ESP8266)
#define IRQ_PIN 0
#else
#define IRQ_PIN 2
#endif
// Antenna tuning capcitance (must be integer multiple of 8, 8 - 120 pf)
#define AS3935_CAPACITANCE 96
@ -49,17 +52,18 @@ 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
if(lightning0.defInit() != 0){
Serial.println("I2C init fail");
while(1){}
while (lightning0.begin() != 0)
{
Serial.print(".");
}
lightning0.defInit();
#if defined(ESP32) || defined(ESP8266)
attachInterrupt(digitalPinToInterrupt(IRQ_PIN),AS3935_ISR,RISING);
#else
attachInterrupt(/*Interrupt No*/0,AS3935_ISR,RISING);
#endif
// Configure sensor
lightning0.manualCal(AS3935_CAPACITANCE, AS3935_MODE, AS3935_DIST);
// Enable interrupt (connect IRQ pin IRQ_PIN: 2, default)
@ -71,14 +75,12 @@ void setup()
// lightning0.setLcoFdiv(0);
// lightning0.setIRQOutputSource(3);
attachInterrupt(0, AS3935_ISR, RISING);
}
void loop()
{
// It does nothing until an interrupt is detected on the IRQ pin.
while (AS3935IsrTrig == 0) {}
while (AS3935IsrTrig == 0) {delay(1);}
delay(5);
// Reset interrupt flag

@ -194,6 +194,7 @@ uint8_t getSpikeRejection(void);
MCU | Work Well | Work Wrong | Untested | Remarks
------------------ | :----------: | :----------: | :---------: | -----
Arduino uno | √ | | |
esp8266 | √ | | |
## Credits

Loading…
Cancel
Save