Rev 4487 Rev 4488
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=256; // 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 int 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 //!!!ReadGPRMC(); // read NMEA sentences from GPS 180 ReadGPRMC(); // read NMEA sentences from GPS
180 //!!!ReadGPGGA(); 181 ReadGPGGA();
-   182 //*/
181 // make a string for assembling the data to log: 183 // make a string for assembling the data to log:
182 dataString += String(num++); 184 dataString += String(num++);
183 //dataString += ","; 185 //dataString += ",";
184 //Serial.print(dataString); 186 Serial.print(dataString);
185 187
186 // 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,
187 // so you have to close this one before opening another. 189 // so you have to close this one before opening another.
188 digitalWrite(chipSelect, HIGH); 190 digitalWrite(chipSelect, HIGH);
189 char fileNameCharArray[filename.length()]; 191 char fileNameCharArray[filename.length()];
190 filename.toCharArray(fileNameCharArray, filename.length()); 192 filename.toCharArray(fileNameCharArray, filename.length());
191 193
192 File dataFile; 194 File dataFile;
193   195  
194 dataFile = SD.open(fileNameCharArray, FILE_WRITE); 196 dataFile = SD.open(fileNameCharArray, FILE_WRITE);
195 if (dataFile) 197 if (dataFile)
196 { 198 {
197 dataFile.print(dataString); 199 dataFile.print(dataString);
198 dataFile.close(); 200 dataFile.close();
199 } 201 }
200 else 202 else
201 { 203 {
202 errorLED(); 204 errorLED();
203 } 205 }
204   206  
205 for (int i=0; i<(256/32); i++) 207 for (int i=0; i<(CHANNELS/32); i++)
206 { 208 {
207 dataString = ""; 209 dataString = "";
208 for (int n=0; n<32; n++) 210 for (int n=0; n<32; n++)
209 { 211 {
210 dataString += ","; 212 dataString += ",";
211 dataString += String(channelA[(i*32)+n]); 213 dataString += String(channelA[(i*32)+n]);
212 }; 214 };
213 215
214 dataFile = SD.open(fileNameCharArray, FILE_WRITE); 216 dataFile = SD.open(fileNameCharArray, FILE_WRITE);
215 if (dataFile) 217 if (dataFile)
216 { 218 {
217 dataFile.print(dataString); 219 dataFile.print(dataString);
218 dataFile.close(); 220 dataFile.close();
219 } 221 }
220 else 222 else
221 { 223 {
222 errorLED(); 224 errorLED();
223 } 225 }
224 } 226 }
225   227  
226 dataString = ","; 228 dataString = ",";
227 dataString += String(count); 229 dataString += String(count);
228 dataFile = SD.open(fileNameCharArray, FILE_WRITE); 230 dataFile = SD.open(fileNameCharArray, FILE_WRITE);
229 if (dataFile) 231 if (dataFile)
230 { 232 {
231 dataFile.println(dataString); 233 dataFile.println(dataString);
232 dataFile.close(); 234 dataFile.close();
233 } 235 }
234 else 236 else
235 { 237 {
236 errorLED(); 238 errorLED();
237 } 239 }
238   240  
239 241
240 digitalWrite(chipSelect, LOW); 242 digitalWrite(chipSelect, LOW);
241   243  
242 //!!! control print 244 //*!!! control print
243 //TODO print to I2C display 245 //TODO print to I2C display
-   246 Serial.print(":");
244 Serial.print(count); 247 Serial.print(count);
245 Serial.print("*"); 248 Serial.print("*");
246 for(int j=0;j<256;j++) {Serial.print(channelA[j]); Serial.print(' ');} 249 for(int j=0;j<36;j++) {Serial.print(channelA[j]); Serial.print(' ');}
247 Serial.println(); 250 Serial.println();
248   251 //*/
249   252  
250 for (int n=0; n<CHANNELS; n++) // clear recording buffer 253 for (int n=0; n<CHANNELS; n++) // clear recording buffer
251 { 254 {
252 channelA[n]=0; 255 channelA[n]=0;
253 } 256 }
254   257  
255 digitalWrite(LED1, LOW); // LED OFF 258 digitalWrite(LED1, LOW); // LED OFF
256 digitalWrite(LED2, LOW); // LED OFF 259 digitalWrite(LED2, LOW); // LED OFF
257 digitalWrite(LED3, LOW); // LED OFF 260 digitalWrite(LED3, LOW); // LED OFF
258 digitalWrite(LED4, LOW); // LED OFF 261 digitalWrite(LED4, LOW); // LED OFF
259   262  
260 } 263 }
261   264  
262 void setup() 265 void setup()
263 { 266 {
264 // Open serial communications and wait for port to open: 267 // Open serial communications and wait for port to open:
265 Serial.begin(9600); 268 Serial.begin(9600);
266 while (!Serial) {;} 269 while (!Serial) {;}
267 //Serial.println("#cvak"); 270 //Serial.println("#cvak");
268   271  
269 pinMode(ADreset, OUTPUT); 272 pinMode(ADreset, OUTPUT);
270 pinMode(eint, INPUT); 273 pinMode(eint, INPUT);
271 pinMode(SDO, INPUT); 274 pinMode(SDO, INPUT);
272 pinMode(LED1, OUTPUT); 275 pinMode(LED1, OUTPUT);
273 pinMode(LED2, OUTPUT); 276 pinMode(LED2, OUTPUT);
274 pinMode(LED3, OUTPUT); 277 pinMode(LED3, OUTPUT);
275 pinMode(LED4, OUTPUT); 278 pinMode(LED4, OUTPUT);
276 pinMode(CONV, OUTPUT); 279 pinMode(CONV, OUTPUT);
277 //pinMode(SCK, OUTPUT); 280 //pinMode(SCK, OUTPUT);
278   281  
279 setupGPS(); 282 setupGPS();
280   283  
281 //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
282 // make sure that the default chip select pin is set to 285 // make sure that the default chip select pin is set to
283 // output, even if you don't use it: 286 // output, even if you don't use it:
284   287  
285 // see if the card is present and can be initialized: 288 // see if the card is present and can be initialized:
286 if (!SD.begin(chipSelect)) 289 if (!SD.begin(chipSelect))
287 { 290 {
288 //Serial.println("Card failed, or not present"); 291 //Serial.println("Card failed, or not present");
289 // don't do anything more: 292 // don't do anything more:
290 errorLED(); 293 errorLED();
291 return; 294 return;
292 } 295 }
293 //Serial.println("card initialized."); 296 //Serial.println("card initialized.");
294   297  
295 noInterrupts(); // disable all interrupts 298 noInterrupts(); // disable all interrupts
296 attachInterrupt(0, isr, RISING); // initialise interrupt from rising edge of 1PPS 299 attachInterrupt(0, isr, RISING); // initialise interrupt from rising edge of 1PPS
297   300  
298 for (int n=0; n<CHANNELS; n++) // clear recoding buffer 301 for (int n=0; n<CHANNELS; n++) // clear recoding buffer
299 { 302 {
300 channelA[n]=0; 303 channelA[n]=0;
301 } 304 }
302   305  
303 interrupts(); // enable all interrupts 306 interrupts(); // enable all interrupts
304 307
305 //Serial.println("#Hmmm"); 308 //Serial.println("#Hmmm");
306 } 309 }
307   310  
308 void loop() 311 void loop()
309 { 312 {
310 //byte msb=0,lsb=0; -  
311 unsigned int val; 313 unsigned int val;
312 unsigned int treshold = 1; 314 unsigned int treshold = 1;
313 315
314 count = 0; 316 count = 0;
315 while (true) 317 while (true)
316 { 318 {
317 //count++; 319 count++;
318 digitalWrite(ADSCK, HIGH); 320 digitalWrite(ADSCK, HIGH);
319 digitalWrite(CONV, HIGH); // start AD conversion 321 digitalWrite(CONV, HIGH); // start AD conversion
320 digitalWrite(ADreset, HIGH); // reset Peack Detector 322 digitalWrite(ADreset, HIGH); // reset Peack Detector
321 digitalWrite(CONV, LOW); // start SPI 323 digitalWrite(CONV, LOW); // start SPI
322 digitalWrite(ADreset, LOW); // start Peack Detector 324 digitalWrite(ADreset, LOW); // start Peack Detector
323 val=0; 325 val=0;
324 for (int p=0;p<16;p++) 326 for (int p=0;p<9;p++)
325 { 327 {
326 digitalWrite(ADSCK, LOW); // 1 CLK 328 digitalWrite(ADSCK, LOW); // 1 CLK
327 digitalWrite(ADSCK, HIGH); 329 digitalWrite(ADSCK, HIGH);
328 val= (val<<1)|digitalRead(SDO); 330 val= (val<<1)|digitalRead(SDO);
329 } 331 }
330 digitalWrite(ADSCK, LOW); // 1 CLK 332 digitalWrite(ADSCK, LOW); // 1 CLK
331   333
332 if ((val > treshold) && (count < (CHANNELS-1))) channelA[count++] = val; 334 if (channelA[val] < 255) channelA[val]++;
333   335  
334 if (rise) // recording time is now 336 if (rise) // recording time is now
335 { 337 {
336 record(); // make record 338 record(); // make record
337 if ((count == 255) && (treshold < 0x8000)) treshold <<= 1; -  
338 if ((count == 0) && (treshold > 1)) treshold >>= 1; -  
339 Serial.println(count); -  
340 Serial.println(treshold); -  
341 digitalWrite(ADreset, HIGH); // reset Peack Detector 339 digitalWrite(ADreset, HIGH); // reset Peack Detector
342 rise = false; 340 rise = false;
343 count = 0; 341 count = 0;
344 digitalWrite(ADreset, LOW); // start Peack Detector 342 digitalWrite(ADreset, LOW); // start Peack Detector
345 continue; // skip this interrupted impuls 343 continue; // skip this interrupted impuls
346 } 344 }
347 } 345 }
348 } 346 }