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