1 |
/* PCRD */ |
1 |
/* PCRD */ |
2 |
|
2 |
|
3 |
/* |
3 |
/* |
4 |
* SD card attached to SPI bus as follows: |
4 |
* SD card attached to SPI bus as follows: |
5 |
** SDcard01B/CMD MOSI - pin PB3 |
5 |
** SDcard01B/CMD MOSI - pin PB3 |
6 |
** SDcard01B/DATA0 MISO - pin PB4 |
6 |
** SDcard01B/DATA0 MISO - pin PB4 |
7 |
** SDcard01B/CLK CLK - pin PB5 |
7 |
** SDcard01B/CLK CLK - pin PB5 |
8 |
** SDcard01B/CD/DATA3 CS - pin PD4 |
8 |
** SDcard01B/CD/DATA3 CS - pin PD4 |
9 |
|
9 |
|
10 |
** ADCmonoSPI/CONV - pin PD6 |
10 |
** ADCmonoSPI/CONV - pin PD6 |
11 |
** ADCmonoSPI/SDO - pin PD5 |
11 |
** ADCmonoSPI/SDO - pin PD5 |
12 |
** ADCmonoSPI/SCK - pin PD7 shared with LED4 |
12 |
** ADCmonoSPI/SCK - pin PD7 shared with LED4 |
13 |
*/ |
13 |
*/ |
14 |
|
14 |
|
15 |
#include <SD.h> |
15 |
#include <SD.h> |
16 |
//#include <SPI.h> |
- |
|
17 |
|
- |
|
18 |
|
16 |
|
19 |
const String filename = "log.csv "; // filename for logfile |
17 |
const String filename = "log.csv "; // filename for logfile |
20 |
|
18 |
|
21 |
const int detector=3; // PD3 |
19 |
const int detector=3; // PD3 |
22 |
const int eint=2; // PD2 |
20 |
const int eint=2; // PD2 |
23 |
const int LED1=8; // PB0 |
21 |
const int LED1=8; // PB0 |
24 |
const int LED2=9; // PB1 |
22 |
const int LED2=9; // PB1 |
25 |
const int LED3=10; // PB2 |
23 |
const int LED3=10; // PB2 |
26 |
const int LED4=7; // PD7 |
24 |
const int LED4=7; // PD7 |
27 |
const int chipSelect = 4; // CS is PD4 |
25 |
const int chipSelect = 4; // CS is PD4 |
28 |
const int CONV = 6; // CONV is PD6 |
26 |
const int CONV = 6; // CONV is PD6 |
29 |
const int SDO = 5; // SDO is PD5 |
27 |
const int SDO = 5; // SDO is PD5 |
30 |
const int ADSCK = 7; // SCK is PD7 |
28 |
const int ADSCK = 7; // SCK is PD7 |
31 |
|
29 |
|
32 |
const int CHANNELS=32; // Number of channels |
30 |
const int CHANNELS=32; // Number of channels |
33 |
|
31 |
|
34 |
unsigned int channelT[CHANNELS]; // recordig buffer |
32 |
unsigned int channelT[CHANNELS]; // recordig buffer |
35 |
unsigned int channelA[CHANNELS]; // recordig buffer |
33 |
unsigned int channelA[CHANNELS]; // recordig buffer |
36 |
int interval=0; // seconds counter |
34 |
int interval=0; // seconds counter |
37 |
boolean rise=false; // flag fo recording time |
35 |
boolean rise=false; // flag fo recording time |
38 |
char inChar; // input character from GPS |
36 |
char inChar; // input character from GPS |
39 |
String dataString = ""; // concantenated string with NMEA messages and measured values |
37 |
String dataString = ""; // concantenated string with NMEA messages and measured values |
40 |
int coll = 0; // collons counter in NMEA messages |
38 |
int coll = 0; // collons counter in NMEA messages |
41 |
unsigned int i = 0; // measurements counter |
39 |
unsigned int i = 0; // measurements counter |
42 |
|
40 |
|
43 |
|
41 |
|
44 |
// function for reading $GPRMC NMEA message |
42 |
// function for reading $GPRMC NMEA message |
45 |
void ReadGPRMC() |
43 |
void ReadGPRMC() |
46 |
{ |
44 |
{ |
47 |
// $GPRMC,091451.00,A,4915.64143,N,01441.50397,E,0.053,,090215,,,A*74 |
45 |
// $GPRMC,091451.00,A,4915.64143,N,01441.50397,E,0.053,,090215,,,A*74 |
48 |
coll = 0; |
46 |
coll = 0; |
49 |
while(1) // wait for $GPRMC |
47 |
while(1) // wait for $GPRMC |
50 |
{ |
48 |
{ |
51 |
// get incoming byte: |
49 |
// get incoming byte: |
52 |
while (!Serial.available()); |
50 |
while (!Serial.available()); |
53 |
if (Serial.read() != '$') continue; |
51 |
if (Serial.read() != '$') continue; |
54 |
while (!Serial.available()); |
52 |
while (!Serial.available()); |
55 |
if (Serial.read() != 'G') continue; |
53 |
if (Serial.read() != 'G') continue; |
56 |
while (!Serial.available()); |
54 |
while (!Serial.available()); |
57 |
if (Serial.read() != 'P') continue; |
55 |
if (Serial.read() != 'P') continue; |
58 |
while (!Serial.available()); |
56 |
while (!Serial.available()); |
59 |
if (Serial.read() != 'R') continue; |
57 |
if (Serial.read() != 'R') continue; |
60 |
while (!Serial.available()); |
58 |
while (!Serial.available()); |
61 |
if (Serial.read() != 'M') continue; |
59 |
if (Serial.read() != 'M') continue; |
62 |
while (!Serial.available()); |
60 |
while (!Serial.available()); |
63 |
if (Serial.read() != 'C') continue; |
61 |
if (Serial.read() != 'C') continue; |
64 |
while (!Serial.available()); |
62 |
while (!Serial.available()); |
65 |
if (Serial.read() != ',') continue; |
63 |
if (Serial.read() != ',') continue; |
66 |
break; |
64 |
break; |
67 |
} |
65 |
} |
68 |
do |
66 |
do |
69 |
{ |
67 |
{ |
70 |
while (!Serial.available()); |
68 |
while (!Serial.available()); |
71 |
inChar = (char)Serial.read(); |
69 |
inChar = (char)Serial.read(); |
72 |
if (inChar == ',') coll++; |
70 |
if (inChar == ',') coll++; |
73 |
dataString += inChar; |
71 |
dataString += inChar; |
74 |
} |
72 |
} |
75 |
while (coll < 9); // read only 9 coma separated values |
73 |
while (coll < 9); // read only 9 coma separated values |
76 |
} |
74 |
} |
77 |
|
75 |
|
78 |
// function for reading $GPGGA NMEA message |
76 |
// function for reading $GPGGA NMEA message |
79 |
void ReadGPGGA() |
77 |
void ReadGPGGA() |
80 |
{ |
78 |
{ |
81 |
// $GPGGA,091451.00,4915.64143,N,01441.50397,E,1,09,0.90,443.2,M,44.0,M,,*50 |
79 |
// $GPGGA,091451.00,4915.64143,N,01441.50397,E,1,09,0.90,443.2,M,44.0,M,,*50 |
82 |
coll = 0; |
80 |
coll = 0; |
83 |
while(1) // wait for $GPGGA |
81 |
while(1) // wait for $GPGGA |
84 |
{ |
82 |
{ |
85 |
while (!Serial.available()); |
83 |
while (!Serial.available()); |
86 |
if (Serial.read() != '$') continue; |
84 |
if (Serial.read() != '$') continue; |
87 |
while (!Serial.available()); |
85 |
while (!Serial.available()); |
88 |
if (Serial.read() != 'G') continue; |
86 |
if (Serial.read() != 'G') continue; |
89 |
while (!Serial.available()); |
87 |
while (!Serial.available()); |
90 |
if (Serial.read() != 'P') continue; |
88 |
if (Serial.read() != 'P') continue; |
91 |
while (!Serial.available()); |
89 |
while (!Serial.available()); |
92 |
if (Serial.read() != 'G') continue; |
90 |
if (Serial.read() != 'G') continue; |
93 |
while (!Serial.available()); |
91 |
while (!Serial.available()); |
94 |
if (Serial.read() != 'G') continue; |
92 |
if (Serial.read() != 'G') continue; |
95 |
while (!Serial.available()); |
93 |
while (!Serial.available()); |
96 |
if (Serial.read() != 'A') continue; |
94 |
if (Serial.read() != 'A') continue; |
97 |
while (!Serial.available()); |
95 |
while (!Serial.available()); |
98 |
if (Serial.read() != ',') continue; |
96 |
if (Serial.read() != ',') continue; |
99 |
break; |
97 |
break; |
100 |
} |
98 |
} |
101 |
do |
99 |
do |
102 |
{ |
100 |
{ |
103 |
while (!Serial.available()); |
101 |
while (!Serial.available()); |
104 |
inChar = (char)Serial.read(); |
102 |
inChar = (char)Serial.read(); |
105 |
if (inChar == ',') coll++; |
103 |
if (inChar == ',') coll++; |
106 |
if (coll > 4) dataString += inChar; // skip first 5 coma separated values |
104 |
if (coll > 4) dataString += inChar; // skip first 5 coma separated values |
107 |
} |
105 |
} |
108 |
while (coll < 12); // read only 7 coma separated values |
106 |
while (coll < 12); // read only 7 coma separated values |
109 |
} |
107 |
} |
110 |
|
108 |
|
111 |
void isr() // interrupt service routine driven from 1PPS from GPS |
109 |
void isr() // interrupt service routine driven from 1PPS from GPS |
112 |
{ |
110 |
{ |
113 |
if (++interval == 10) // 10 seconds |
111 |
if (++interval == 10) // 10 seconds |
114 |
{ |
112 |
{ |
115 |
rise=true; |
113 |
rise=true; |
116 |
interval = 0; |
114 |
interval = 0; |
117 |
} |
115 |
} |
118 |
|
116 |
|
119 |
} |
117 |
} |
120 |
|
118 |
|
121 |
void record() |
119 |
void record() |
122 |
{ |
120 |
{ |
123 |
for (int c=16; c<CHANNELS; c++) |
121 |
for (int c=16; c<CHANNELS; c++) |
124 |
{ |
122 |
{ |
125 |
if (channelT[c]>0) |
123 |
if (channelT[c]>0) |
126 |
{ |
124 |
{ |
127 |
digitalWrite(LED4, HIGH); // LED 16-64 |
125 |
digitalWrite(LED4, HIGH); // LED 16-64 |
128 |
break; |
126 |
break; |
129 |
} |
127 |
} |
130 |
} |
128 |
} |
131 |
|
129 |
|
132 |
for (int c=9; c<17; c++) |
130 |
for (int c=9; c<17; c++) |
133 |
{ |
131 |
{ |
134 |
if (channelT[c]>0) |
132 |
if (channelT[c]>0) |
135 |
{ |
133 |
{ |
136 |
digitalWrite(LED3, HIGH); // LED 9-16 |
134 |
digitalWrite(LED3, HIGH); // LED 9-16 |
137 |
break; |
135 |
break; |
138 |
} |
136 |
} |
139 |
} |
137 |
} |
140 |
|
138 |
|
141 |
for (int c=5; c<9; c++) |
139 |
for (int c=5; c<9; c++) |
142 |
{ |
140 |
{ |
143 |
if (channelT[c]>0) |
141 |
if (channelT[c]>0) |
144 |
{ |
142 |
{ |
145 |
digitalWrite(LED2, HIGH); // LED 5-8 |
143 |
digitalWrite(LED2, HIGH); // LED 5-8 |
146 |
break; |
144 |
break; |
147 |
} |
145 |
} |
148 |
} |
146 |
} |
149 |
|
147 |
|
150 |
for (int c=0; c<5; c++) |
148 |
for (int c=0; c<5; c++) |
151 |
{ |
149 |
{ |
152 |
if (channelT[c]>0) |
150 |
if (channelT[c]>0) |
153 |
{ |
151 |
{ |
154 |
digitalWrite(LED1, HIGH); // LED 0-4 |
152 |
digitalWrite(LED1, HIGH); // LED 0-4 |
155 |
break; |
153 |
break; |
156 |
} |
154 |
} |
157 |
} |
155 |
} |
158 |
|
156 |
|
159 |
dataString = ""; // make a string for assembling the data to log |
157 |
dataString = ""; // make a string for assembling the data to log |
160 |
ReadGPRMC(); // read NMEA sentences from GPS |
158 |
ReadGPRMC(); // read NMEA sentences from GPS |
161 |
ReadGPGGA(); |
159 |
ReadGPGGA(); |
162 |
// make a string for assembling the data to log: |
160 |
// make a string for assembling the data to log: |
163 |
dataString += String(i++); |
161 |
dataString += String(i++); |
164 |
//dataString += ","; |
162 |
//dataString += ","; |
165 |
//Serial.print(dataString); |
163 |
//Serial.print(dataString); |
166 |
|
164 |
|
167 |
for (int n=0; n<CHANNELS; n++) |
165 |
for (int n=0; n<CHANNELS; n++) |
168 |
{ |
166 |
{ |
169 |
dataString += ","; |
167 |
dataString += ","; |
170 |
dataString += String(channelT[n]); |
168 |
dataString += String(channelT[n]); |
171 |
dataString += ","; |
169 |
dataString += ","; |
172 |
dataString += String(channelA[n]); |
170 |
dataString += String(channelA[n]); |
173 |
//Serial.print(channel[n]); |
171 |
//Serial.print(channel[n]); |
174 |
//Serial.print(','); |
172 |
//Serial.print(','); |
175 |
} |
173 |
} |
176 |
//Serial.println(); |
174 |
//Serial.println(); |
177 |
|
175 |
|
178 |
// open the file. note that only one file can be open at a time, |
176 |
// open the file. note that only one file can be open at a time, |
179 |
// so you have to close this one before opening another. |
177 |
// so you have to close this one before opening another. |
180 |
digitalWrite(chipSelect, HIGH); |
178 |
digitalWrite(chipSelect, HIGH); |
181 |
char fileNameCharArray[filename.length()]; |
179 |
char fileNameCharArray[filename.length()]; |
182 |
filename.toCharArray(fileNameCharArray, filename.length()); |
180 |
filename.toCharArray(fileNameCharArray, filename.length()); |
183 |
File dataFile = SD.open(fileNameCharArray, FILE_WRITE); |
181 |
File dataFile = SD.open(fileNameCharArray, FILE_WRITE); |
184 |
|
182 |
|
185 |
// if the file is available, write to it: |
183 |
// if the file is available, write to it: |
186 |
if (dataFile) |
184 |
if (dataFile) |
187 |
{ |
185 |
{ |
188 |
dataFile.println(dataString); |
186 |
dataFile.println(dataString); |
189 |
dataFile.close(); |
187 |
dataFile.close(); |
190 |
// print to the serial port too: |
188 |
// print to the serial port too: |
191 |
Serial.println(dataString); |
189 |
Serial.println(dataString); |
192 |
} |
190 |
} |
193 |
// if the file isn't open, pop up an error: |
191 |
// if the file isn't open, pop up an error: |
194 |
else { |
192 |
else { |
195 |
Serial.println("error opening datalog.CSV"); |
193 |
Serial.println("error opening datalog.CSV"); |
196 |
} |
194 |
} |
197 |
digitalWrite(chipSelect, LOW); |
195 |
digitalWrite(chipSelect, LOW); |
198 |
|
196 |
|
199 |
|
197 |
|
200 |
for (int n=0; n<CHANNELS; n++) // clear recording buffer |
198 |
for (int n=0; n<CHANNELS; n++) // clear recording buffer |
201 |
{ |
199 |
{ |
202 |
channelT[n]=0; |
200 |
channelT[n]=0; |
203 |
channelA[n]=0; |
201 |
channelA[n]=0; |
204 |
} |
202 |
} |
205 |
|
203 |
|
206 |
digitalWrite(LED1, LOW); // LED OFF |
204 |
digitalWrite(LED1, LOW); // LED OFF |
207 |
digitalWrite(LED2, LOW); // LED OFF |
205 |
digitalWrite(LED2, LOW); // LED OFF |
208 |
digitalWrite(LED3, LOW); // LED OFF |
206 |
digitalWrite(LED3, LOW); // LED OFF |
209 |
digitalWrite(LED4, LOW); // LED OFF |
207 |
digitalWrite(LED4, LOW); // LED OFF |
210 |
|
208 |
|
211 |
} |
209 |
} |
212 |
|
210 |
|
213 |
void setup() |
211 |
void setup() |
214 |
{ |
212 |
{ |
215 |
// Open serial communications and wait for port to open: |
213 |
// Open serial communications and wait for port to open: |
216 |
Serial.begin(9600); |
214 |
Serial.begin(9600); |
217 |
while (!Serial) {;} |
215 |
while (!Serial) {;} |
218 |
Serial.println("#cvak"); |
216 |
Serial.println("#cvak"); |
219 |
|
217 |
|
220 |
pinMode(detector, INPUT); |
218 |
pinMode(detector, INPUT); |
221 |
pinMode(eint, INPUT); |
219 |
pinMode(eint, INPUT); |
222 |
pinMode(SDO, INPUT); |
220 |
pinMode(SDO, INPUT); |
223 |
pinMode(LED1, OUTPUT); |
221 |
pinMode(LED1, OUTPUT); |
224 |
pinMode(LED2, OUTPUT); |
222 |
pinMode(LED2, OUTPUT); |
225 |
pinMode(LED3, OUTPUT); |
223 |
pinMode(LED3, OUTPUT); |
226 |
pinMode(LED4, OUTPUT); |
224 |
pinMode(LED4, OUTPUT); |
227 |
pinMode(CONV, OUTPUT); |
225 |
pinMode(CONV, OUTPUT); |
228 |
//pinMode(SCK, OUTPUT); |
226 |
//pinMode(SCK, OUTPUT); |
229 |
|
227 |
|
230 |
Serial.print("#Initializing SD card..."); // inserting a SD Card always reset the processor and call setup |
228 |
Serial.print("#Initializing SD card..."); // inserting a SD Card always reset the processor and call setup |
231 |
// make sure that the default chip select pin is set to |
229 |
// make sure that the default chip select pin is set to |
232 |
// output, even if you don't use it: |
230 |
// output, even if you don't use it: |
233 |
//pinMode(10, OUTPUT); // PB2 |
- |
|
234 |
//pinMode(LED, OUTPUT); |
- |
|
235 |
|
231 |
|
236 |
// see if the card is present and can be initialized: |
232 |
// see if the card is present and can be initialized: |
237 |
if (!SD.begin(chipSelect)) |
233 |
if (!SD.begin(chipSelect)) |
238 |
{ |
234 |
{ |
239 |
Serial.println("Card failed, or not present"); |
235 |
Serial.println("Card failed, or not present"); |
240 |
// don't do anything more: |
236 |
// don't do anything more: |
241 |
return; |
237 |
return; |
242 |
} |
238 |
} |
243 |
Serial.println("card initialized."); |
239 |
Serial.println("card initialized."); |
244 |
|
240 |
|
245 |
noInterrupts(); // disable all interrupts |
241 |
noInterrupts(); // disable all interrupts |
246 |
attachInterrupt(0, isr, RISING); // initialise interrupt from rising edge of 1PPS |
242 |
attachInterrupt(0, isr, RISING); // initialise interrupt from rising edge of 1PPS |
247 |
|
243 |
|
248 |
for (int n=0; n<CHANNELS; n++) // clear recoding buffer |
244 |
for (int n=0; n<CHANNELS; n++) // clear recoding buffer |
249 |
{ |
245 |
{ |
250 |
channelT[n]=0; |
246 |
channelT[n]=0; |
251 |
channelA[n]=0; |
247 |
channelA[n]=0; |
252 |
} |
248 |
} |
253 |
|
249 |
|
254 |
interrupts(); // enable all interrupts |
250 |
interrupts(); // enable all interrupts |
255 |
|
251 |
|
256 |
Serial.println("#Hmmm"); |
252 |
Serial.println("#Hmmm"); |
257 |
} |
253 |
} |
258 |
|
254 |
|
259 |
void loop() |
255 |
void loop() |
260 |
{ |
256 |
{ |
261 |
//byte msb=0,lsb=0; |
257 |
//byte msb=0,lsb=0; |
262 |
unsigned int val; |
258 |
unsigned int val; |
263 |
|
259 |
|
264 |
while (true) |
260 |
while (true) |
265 |
{ |
261 |
{ |
266 |
unsigned int duration=0; // pulse duration counter |
262 |
unsigned int duration=0; // pulse duration counter |
267 |
|
263 |
|
268 |
while (!digitalRead(detector)) // waiting for pulse |
264 |
while (!digitalRead(detector)) // waiting for pulse |
269 |
{ |
265 |
{ |
270 |
if (rise) break; |
266 |
if (rise) break; |
271 |
//digitalWrite(CONV, LOW); |
- |
|
272 |
digitalWrite(CONV, HIGH); // start AD conversion |
267 |
digitalWrite(CONV, HIGH); // start AD conversion |
273 |
} |
268 |
} |
274 |
while (digitalRead(detector)) |
269 |
while (digitalRead(detector)) |
275 |
{ |
270 |
{ |
276 |
if (rise) break; |
271 |
if (rise) break; |
277 |
if (duration < (CHANNELS-1)) duration++; |
272 |
if (duration < (CHANNELS-1)) duration++; |
278 |
} |
273 |
} |
279 |
|
274 |
|
- |
|
275 |
digitalWrite(ADSCK, HIGH); |
280 |
digitalWrite(CONV, LOW); // start SPI |
276 |
digitalWrite(CONV, LOW); // start SPI |
281 |
val=0; |
277 |
val=0; |
282 |
for (int p=0;p<8;p++) |
278 |
for (int p=0;p<8;p++) |
283 |
{ |
279 |
{ |
284 |
digitalWrite(ADSCK, LOW); // 1 CLK |
280 |
digitalWrite(ADSCK, LOW); // 1 CLK |
285 |
digitalWrite(ADSCK, HIGH); |
281 |
digitalWrite(ADSCK, HIGH); |
286 |
val= (val<<1)|digitalRead(SDO); |
282 |
val= (val<<1)|digitalRead(SDO); |
287 |
} |
283 |
} |
288 |
//val = 140 - val; |
- |
|
289 |
//msb = SPI.transfer(0x00); // read ADC |
- |
|
290 |
//lsb = SPI.transfer(0x00); |
- |
|
291 |
//digitalWrite(CONV, HIGH); // stop SPI |
284 |
digitalWrite(ADSCK, LOW); // 1 CLK |
292 |
//val = (msb<<8) + lsb; |
- |
|
293 |
//Serial.println(val); |
- |
|
294 |
|
285 |
|
295 |
if (rise) // recording time is now |
286 |
if (rise) // recording time is now |
296 |
{ |
287 |
{ |
297 |
record(); // make record |
288 |
record(); // make record |
298 |
rise = false; |
289 |
rise = false; |
299 |
continue; // skip this interrupted impuls |
290 |
continue; // skip this interrupted impuls |
300 |
} |
291 |
} |
301 |
|
292 |
|
302 |
if (channelT[duration] < 65535) channelT[duration]++; /// record duration in apropriate channel |
293 |
if (channelT[duration] < 65535) channelT[duration]++; /// record duration in apropriate channel |
303 |
if (channelA[duration] < (65535-val)) channelA[duration]+=val; /// record amplitude |
294 |
if (channelA[duration] < (65535-val)) channelA[duration]+=val; /// record amplitude |
304 |
} |
295 |
} |
305 |
} |
296 |
} |