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