Rev Author Line No. Line
4848 kakl 1 /* CANDY DRO */
4847 kakl 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=512; // Number of channels
31  
32 //unsigned int channelT[CHANNELS]; // recordig buffer
33 unsigned char 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 {
142 for (int c=67; c<CHANNELS; c++)
143 {
144 if (channelA[c]>0)
145 {
146 digitalWrite(LED4, HIGH); // LED 16-64
147 break;
148 }
149 }
150  
151 for (int c=33; c<64; c++)
152 {
153 if (channelA[c]>0)
154 {
155 digitalWrite(LED3, HIGH); // LED 9-16
156 break;
157 }
158 }
159  
160 for (int c=17; c<32; c++)
161 {
162 if (channelA[c]>0)
163 {
164 digitalWrite(LED2, HIGH); // LED 5-8
165 break;
166 }
167 }
168  
169 for (int c=0; c<16; c++)
170 {
171 if (channelA[c]>0)
172 {
173 digitalWrite(LED1, HIGH); // LED 0-4
174 break;
175 }
176 }
177  
178 //!!!!!!!!!!!!!! uprava pro DRO
179 for (int n=0; n<CHANNELS; n++) // clear recording buffer
180 {
181 channelA[n]=0;
182 }
183 delay(100000);
184 digitalWrite(LED1, LOW); // LED OFF
185 digitalWrite(LED2, LOW); // LED OFF
186 digitalWrite(LED3, LOW); // LED OFF
187 digitalWrite(LED4, LOW); // LED OFF
188 return;
189  
190 dataString = ""; // make a string for assembling the data to log
191 //*
192 ReadGPRMC(); // read NMEA sentences from GPS
193 ReadGPGGA();
194 //*/
195 // make a string for assembling the data to log:
196 dataString += String(num++);
197 //dataString += ",";
198 //!!! Serial.print(dataString);
199  
200 // open the file. note that only one file can be open at a time,
201 // so you have to close this one before opening another.
202 digitalWrite(chipSelect, HIGH);
203 char fileNameCharArray[filename.length()];
204 filename.toCharArray(fileNameCharArray, filename.length());
205  
206 File dataFile;
207  
208 dataFile = SD.open(fileNameCharArray, FILE_WRITE);
209 if (dataFile)
210 {
211 dataFile.print(dataString);
212 dataFile.close();
213 }
214 else
215 {
216 errorLED();
217 }
218  
219 for (int i=0; i<(CHANNELS/32); i++)
220 {
221 dataString = "";
222 for (int n=0; n<32; n++)
223 {
224 dataString += ",";
225 dataString += String(channelA[(i*32)+n]);
226 };
227  
228 dataFile = SD.open(fileNameCharArray, FILE_WRITE);
229 if (dataFile)
230 {
231 dataFile.print(dataString);
232 dataFile.close();
233 }
234 else
235 {
236 errorLED();
237 }
238 }
239  
240 dataString = ",";
241 dataString += String(count);
242 dataFile = SD.open(fileNameCharArray, FILE_WRITE);
243 if (dataFile)
244 {
245 dataFile.println(dataString);
246 dataFile.close();
247 }
248 else
249 {
250 errorLED();
251 }
252  
253  
254 digitalWrite(chipSelect, LOW);
255  
256 /*!!! control print
257 //TODO print to I2C display
258 Serial.print(":");
259 Serial.print(count);
260 Serial.print("*");
261 for(int j=0;j<36;j++) {Serial.print(channelA[j]); Serial.print(' ');}
262 Serial.println();
263 //*/
264  
265 for (int n=0; n<CHANNELS; n++) // clear recording buffer
266 {
267 channelA[n]=0;
268 }
269  
270 digitalWrite(LED1, LOW); // LED OFF
271 digitalWrite(LED2, LOW); // LED OFF
272 digitalWrite(LED3, LOW); // LED OFF
273 digitalWrite(LED4, LOW); // LED OFF
274  
275 }
276  
277 void setup()
278 {
279 // Open serial communications and wait for port to open:
280 Serial.begin(9600);
281 while (!Serial) {;}
282 //Serial.println("#cvak");
283  
284 pinMode(ADreset, OUTPUT);
285 pinMode(eint, INPUT);
286 pinMode(SDO, INPUT);
287 pinMode(LED1, OUTPUT);
288 pinMode(LED2, OUTPUT);
289 pinMode(LED3, OUTPUT);
290 pinMode(LED4, OUTPUT);
291 pinMode(CONV, OUTPUT);
292 //pinMode(SCK, OUTPUT);
293  
294 setupGPS();
295  
296 //Serial.print("#Initializing SD card..."); // inserting a SD Card always reset the processor and call setup
297 // make sure that the default chip select pin is set to
298 // output, even if you don't use it:
299  
300 // see if the card is present and can be initialized:
301 if (!SD.begin(chipSelect))
302 {
303 //Serial.println("Card failed, or not present");
304 // don't do anything more:
305 errorLED();
306 return;
307 }
308 //Serial.println("card initialized.");
309  
310 noInterrupts(); // disable all interrupts
311 //!!! attachInterrupt(0, isr, RISING); // initialise interrupt from rising edge of 1PPS
312  
313 for (int n=0; n<CHANNELS; n++) // clear recoding buffer
314 {
315 channelA[n]=0;
316 }
317  
318 //!!! interrupts(); // enable all interrupts
319  
320 //Serial.println("#Hmmm");
321 }
322  
323 void loop()
324 {
325 unsigned int val;
326 unsigned int treshold = 1;
327  
328 count = 0;
329 while (true)
330 {
331 count++;
332 digitalWrite(ADSCK, HIGH);
333 digitalWrite(CONV, HIGH); // start AD conversion
334 digitalWrite(ADreset, HIGH); // reset Peack Detector
335 digitalWrite(CONV, LOW); // start SPI
336 digitalWrite(ADreset, LOW); // start Peack Detector
337 val=0;
338 for (int p=0;p<9;p++)
339 {
340 digitalWrite(ADSCK, LOW); // 1 CLK
341 digitalWrite(ADSCK, HIGH);
342 val= (val<<1)|digitalRead(SDO);
343 }
344 digitalWrite(ADSCK, LOW); // 1 CLK
345  
346 if (channelA[val] < 255) channelA[val]++;
347  
348 if (count > 10000) // recording time cca 1s pro ukazku na DRO
349 {
350 record(); // make record
351 digitalWrite(ADreset, HIGH); // reset Peack Detector
352 rise = false;
353 count = 0;
354 digitalWrite(ADreset, LOW); // start Peack Detector
355 continue; // skip this interrupted impuls
356 }
357 }
358 }