-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAS3935.cpp
More file actions
340 lines (301 loc) · 7.68 KB
/
AS3935.cpp
File metadata and controls
340 lines (301 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
Copyright (c) 2017, Embedded Adventures
All rights reserved.
Contact us at source [at] embeddedadventures.com
www.embeddedadventures.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of Embedded Adventures nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
// AS3935 MOD-1016 Lightning Sensor Arduino library
// Written originally by Embedded Adventures
#include "Arduino.h"
#include "Wire.h"
#include "SPI.h"
#include "AS3935.h"
volatile bool displayingFrequency;
volatile sgn32 pulse = 0;
sgn32 cap_frequencies[16];
void pulseDetected() {
if (displayingFrequency)
pulse++;
}
void autoTuneCaps(uns8 irq) {
int fdiv = mod1016.getDivisionRatio();
Serial.println("Measuring frequency. Please wait...\n");
freqPerTuneCaps(fdiv, irq);
recommendTuning();
mod1016.calibrateRCO();
delay(1000);
}
sgn32 getFrequency(uns8 irq) {
mod1016.writeRegister(DISP_LCO, 0x80);
displayingFrequency = true;
pulse = 0;
attachInterrupt(digitalPinToInterrupt(irq), pulseDetected, RISING);
delay(200);
displayingFrequency = false;
detachInterrupt(irq);
mod1016.writeRegister(DISP_LCO, 0x00);
return (pulse * 5);
}
void freqPerTuneCaps(uns16 fdiv, uns8 irq) {
for (int i = 0; i < 16; i++) {
mod1016.setTuneCaps(i);
delay(2);
/*Measure frequency 5x. Get average.
If you want to use a slower aproach that measures frequency over a longer period of time, uncomment this section,
and comment out the other sgn32 freq line.
sgn32 freq = 0;
for (int i = 0; i < 5; i++) {
freq += getFrequency(irq);
}
freq = ((freq / 5) * fdiv) - 500000;
/*Measure frequency in 1/5th of a second, multiply by 500000*/
sgn32 freq = (getFrequency(irq) * fdiv) - 500000;
cap_frequencies[i] = (freq < 0) ? -freq : freq;
//Uncomment to print out frequnecy calculations
/*
Serial.print("TUNE CAPS = ");
Serial.print(i);
Serial.print("\tFrequency - 500k = ");
Serial.println(cap_frequencies[i]);
Serial.println();*/
}
}
void recommendTuning() {
int best = 0, next, current;
for (int i = 1; i < 16; i++) {
current = best;
next = i;
best = (cap_frequencies[next] < cap_frequencies[current]) ? next : best;
}
//Serial.print("Best value for TUNE_CAPS - ");
//Serial.println(best);
//Serial.println("Setting TUNE_CAPS...");
mod1016.setTuneCaps(best);
}
/*--------------------------------------------------------*/
void AS3935Class::init(uns8 irqPin) {
_usingI2C = true;
calibrateRCO();
pinMode(irqPin, INPUT);
}
void AS3935Class::init(uns8 irqPin, uns8 csPin) {
_usingI2C = false;
_irq = irqPin;
_cs = csPin;
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
calibrateRCO();
pinMode(_irq, INPUT);
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE2));
}
void AS3935Class::init(uns8 irqPin, uns8 clkPin, uns8 mosiPin, uns8 misoPin, uns8 csPin) {
_usingI2C = false;
_irq = irqPin;
_clk = clkPin;
_mosi = mosiPin;
_miso = misoPin;
_cs = csPin;
pinMode(_clk, OUTPUT);
pinMode(_mosi, OUTPUT);
pinMode(_miso, INPUT);
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
calibrateRCO();
pinMode(_irq, INPUT);
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE2));
}
void AS3935Class::calibrateRCO() {
if (_usingI2C) {
Wire.beginTransmission(AS3935_ADDR);
Wire.write(0x3D);
Wire.write(0x96);
Wire.endTransmission();
}
else {
digitalWrite(_cs, LOW);
SPI.transfer(AS3935_SPI_WRITE & 0x3D);
SPI.transfer(0x96);
digitalWrite(_cs, HIGH);
}
writeRegister(DISP_TRCO, (0x01 << 5));
delay(2);
writeRegister(DISP_TRCO, (0x00 << 5));
}
uns8 AS3935Class::readRegisterRaw(uns8 reg) {
uns8 result;
if (_usingI2C) {
Wire.beginTransmission(AS3935_ADDR);
Wire.write(reg);
Wire.endTransmission(false);
Wire.requestFrom(AS3935_ADDR, 1);
if (Wire.available()) {
result = Wire.read();
}
}
else {
reg &= 0x3F;
reg |= AS3935_SPI_READ;
digitalWrite(_cs, LOW);
SPI.transfer(reg);
result = SPI.transfer(0x00);
digitalWrite(_cs, HIGH);
}
return result;
}
uns8 AS3935Class::readRegister(uns8 reg, uns8 mask) {
uns8 data = readRegisterRaw(reg);
return (data & mask);
}
void AS3935Class::writeRegister(uns8 reg, uns8 mask, uns8 data) {
uns8 currentReg = readRegisterRaw(reg);
currentReg = currentReg & (~mask);
data |= currentReg;
if (_usingI2C) {
Wire.beginTransmission(AS3935_ADDR);
Wire.write(reg);
Wire.write(data);
Wire.endTransmission();
}
else {
reg &= AS3935_SPI_WRITE;
digitalWrite(_cs, LOW);
SPI.transfer(reg);
SPI.transfer(data);
digitalWrite(_cs, HIGH);
}
}
void AS3935Class::setIndoors() {
writeRegister(AFE_GB, INDOORS);
}
void AS3935Class::setOutdoors() {
writeRegister(AFE_GB, OUTDOORS);
}
void AS3935Class::setNoiseFloor(uns8 noise) {
writeRegister(NOISE_FLOOR, (noise << 4));
}
void AS3935Class::setTuneCaps(uns8 tune) {
writeRegister(TUNE_CAPS, tune);
delay(2);
}
void AS3935Class::enableDisturbers() {
writeRegister(MASK_DST, (0 << 5));
delay(2);
}
void AS3935Class::disableDisturbers() {
writeRegister(MASK_DST, (1 << 5));
delay(2);
}
uns8 AS3935Class::getNoiseFloor() {
return (readRegister(NOISE_FLOOR) >> 4);
}
uns8 AS3935Class::getAFE() {
return (readRegister(AFE_GB) >> 1);
}
uns8 AS3935Class::getTuneCaps() {
return readRegister(TUNE_CAPS);
}
uns8 AS3935Class::getIRQ() {
delay(2);
return readRegister(IRQ_TBL);
}
uns8 AS3935Class::getLightDistance() {
return readRegister(LGHT_DIST);
}
sgn16 AS3935Class::calculateDistance() {
uns8 dist = getLightDistance();
sgn16 km;
switch (dist) {
case 0x3F:
km = -1;
break;
case 0x28:
km = 40;
break;
case 0x25:
km = 37;
break;
case 0x22:
km = 34;
break;
case 0x1F:
km = 31;
break;
case 0x1B:
km = 27;
break;
case 0x18:
km = 24;
break;
case 0x14:
km = 20;
break;
case 0x11:
km = 17;
break;
case 0x0E:
km = 14;
break;
case 0x0C:
km = 12;
break;
case 0x0A:
km = 10;
break;
case 0x08:
km = 8;
break;
case 0x06:
km = 6;
break;
case 0x05:
km = 5;
break;
case 0x01:
km = 0;
break;
default:
km = 1;
}
return km;
}
sgn16 AS3935Class::getDivisionRatio() {
uns8 fdiv = (readRegister(LCO_FDIV) >> 6);
if (fdiv == 0)
return 16;
else if (fdiv == 1)
return 32;
else if (fdiv == 2)
return 64;
else
return 128;
}
uns16 AS3935Class::getIntensity() {
uns8 lsb, msb, mmsb;
lsb = readRegisterRaw(0x04);
msb = readRegisterRaw(0x05);
mmsb = readRegisterRaw(0x06) & 0x1F;
uns16 result = (uns16)lsb | ((uns16)msb << 8) | ((uns16)mmsb << 16);
return result;
}
AS3935Class mod1016;