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