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