Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
AS3935.cpp
00001 /* 00002 * AMS, Franklin Lightning Sensor "AS3935" Library 00003 * Copyright (c) 2015 Hiroshi Suga 00004 * Released under the MIT License: http://mbed.org/license/mit 00005 */ 00006 // http://ams.com/eng/Products/Lightning-Sensor/Franklin-Lightning-Sensor/AS3935 00007 00008 #include "AS3935.h" 00009 00010 //#define DBG(...) printf("" __VA_ARGS__) 00011 #define DBG(...) 00012 00013 #define AS3935_ADDR 0x00 00014 00015 AS3935::AS3935 (I2C &i2c, PinName irq) : _i2c(i2c), _irq(irq) { 00016 _irq.mode(PullUp); 00017 _mode = 0; 00018 _type = 0; 00019 } 00020 00021 AS3935::AS3935 (PinName sda, PinName scl, PinName irq) : _i2c(sda, scl), _irq(irq) { 00022 _irq.mode(PullUp); 00023 _mode = 0; 00024 _type = 0; 00025 } 00026 00027 void AS3935::init () { 00028 char cmd[2]; 00029 00030 cmd[0] = 0x3c; 00031 cmd[1] = 0x96; // PRESET_DEFAULT 00032 _i2c.write(AS3935_ADDR, cmd, 2); 00033 cmd[0] = 0x3d; 00034 cmd[1] = 0x96; // CALIB_RCO 00035 _i2c.write(AS3935_ADDR, cmd, 2); 00036 cmd[0] = 0x00; 00037 cmd[1] = (0x12<<1); // AFE_GB=12(Indoor) 00038 _i2c.write(AS3935_ADDR, cmd, 2); 00039 cmd[0] = 0x01; 00040 cmd[1] = (0x02<<4)|(0x02<<0); // NF_LEV, WDTH 00041 _i2c.write(AS3935_ADDR, cmd, 2); 00042 cmd[0] = 0x03; 00043 cmd[1] = 0x00; // LCO_FDIV = 1/16 00044 _i2c.write(AS3935_ADDR, cmd, 2); 00045 00046 calib_lco(); 00047 00048 _irq.fall(this, &AS3935::isr_lightning); 00049 } 00050 00051 // Antenna Tuning (500kHz) 00052 void AS3935::calib_lco () { 00053 int i, n, m = 10000, r = 0; 00054 char cmd[2]; 00055 Timer t; 00056 00057 _mode = 0; 00058 _irq.fall(this, &AS3935::isr_freq); 00059 for (i = 0; i < 0x10; i ++) { 00060 cmd[0] = 0x08; 00061 cmd[1] = 0x80 | i; 00062 _i2c.write(AS3935_ADDR, cmd, 2); 00063 wait_ms(10); 00064 t.reset(); 00065 t.start(); 00066 _freq = 0; 00067 _mode = 1; 00068 while (t.read_ms() < 100); 00069 _mode = 0; 00070 n = abs(_freq - 3125); 00071 if (m > n) { 00072 r = i; 00073 } else { 00074 break; 00075 } 00076 m = n; 00077 } 00078 _irq.fall(NULL); 00079 t.stop(); 00080 cmd[0] = 0x08; 00081 cmd[1] = r; 00082 _i2c.write(AS3935_ADDR, cmd, 2); 00083 DBG("- init %d %d\r\n", r, _freq * 16 * 10); 00084 } 00085 00086 void AS3935::read (int &energy, int &distance) { 00087 char cmd[4]; 00088 00089 cmd[0] = 0x04; 00090 _i2c.write(AS3935_ADDR, cmd, 1, true); 00091 _i2c.read(AS3935_ADDR, cmd, 4); 00092 energy = ((cmd[2] & 0x1f) << 16) | (cmd[1] << 8) | cmd[0]; 00093 distance = cmd[3] & 0x3f; 00094 } 00095 00096 void AS3935::isr_freq () { 00097 if (_mode == 1) { 00098 _freq ++; 00099 } 00100 } 00101 00102 void AS3935::isr_lightning () { 00103 char cmd[2]; 00104 00105 cmd[0] = 0x03; 00106 _i2c.write(AS3935_ADDR, cmd, 1, true); 00107 _i2c.read(AS3935_ADDR, cmd, 1); 00108 DBG("- irq %02x\r\n", cmd[0]); 00109 _type = cmd[0] & 0x0f; 00110 switch (_type) { 00111 case 0x01: // Noise level too high 00112 break; 00113 case 0x04: // Disturber detected 00114 break; 00115 case 0x08: // Lightning interrupt 00116 _func.call(); 00117 break; 00118 } 00119 }
Generated on Fri Jul 22 2022 07:09:25 by
1.7.2