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