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