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