comparison main.c @ 12:3c27bfbd7f3a

Add simple_ds18b20.c etc
author Matt Johnston <matt@ucc.asn.au>
date Sat, 19 May 2012 21:47:09 +0800
parents 1bfe28c348dd
children 4838bfcb3504
comparison
equal deleted inserted replaced
11:06bedbe8540d 12:3c27bfbd7f3a
11 #include <avr/sleep.h> 11 #include <avr/sleep.h>
12 #include <util/delay.h> 12 #include <util/delay.h>
13 #include <avr/pgmspace.h> 13 #include <avr/pgmspace.h>
14 #include <util/crc16.h> 14 #include <util/crc16.h>
15 15
16 // for DWORD of get_fattime()
16 #include "integer.h" 17 #include "integer.h"
17 #include "onewire.h" 18
18 #include "ds18x20.h" 19 #include "simple_ds18b20.h"
19 20
20 // configuration params 21 // configuration params
21 // - measurement interval 22 // - measurement interval
22 // - transmit interval 23 // - transmit interval
23 // - bluetooth params 24 // - bluetooth params
135 _delay_ms(50); 136 _delay_ms(50);
136 comms_done = 1; 137 comms_done = 1;
137 } 138 }
138 139
139 static void 140 static void
141 cmd_measure()
142 {
143 printf_P(PSTR("Measuring\n"));
144 need_measurement = 1;
145 }
146
147 static void
148 cmd_sensors()
149 {
150 uint8_t ret = simple_ds18b20_start_meas(NULL);
151 printf("All sensors, ret %d, waiting...\n", ret);
152 _delay_ms(DS18B20_TCONV_12BIT);
153 simple_ds18b20_read_all();
154 }
155
156 static void
140 read_handler() 157 read_handler()
141 { 158 {
142 if (strcmp_P(readbuf, PSTR("fetch")) == 0) 159 if (strcmp_P(readbuf, PSTR("fetch")) == 0)
143 { 160 {
144 cmd_fetch(); 161 cmd_fetch();
149 } 166 }
150 else if (strcmp_P(readbuf, PSTR("btoff")) == 0) 167 else if (strcmp_P(readbuf, PSTR("btoff")) == 0)
151 { 168 {
152 cmd_btoff(); 169 cmd_btoff();
153 } 170 }
171 else if (strcmp_P(readbuf, PSTR("measure")) == 0)
172 {
173 cmd_measure();
174 }
175 else if (strcmp_P(readbuf, PSTR("sensors")) == 0)
176 {
177 cmd_sensors();
178 }
154 else 179 else
155 { 180 {
156 printf_P(PSTR("Bad command\n")); 181 printf_P(PSTR("Bad command\n"));
157 } 182 }
158 } 183 }
159 184
160 ISR(USART_RX_vect) 185 ISR(USART_RX_vect)
161 { 186 {
162 char c = UDR0; 187 char c = UDR0;
163 if (c == '\n') 188 uart_putchar(c, NULL);
189 if (c == '\r')
164 { 190 {
165 readbuf[readpos] = '\0'; 191 readbuf[readpos] = '\0';
166 read_handler(); 192 read_handler();
167 readpos = 0; 193 readpos = 0;
168 } 194 }
179 205
180 ISR(TIMER2_COMPA_vect) 206 ISR(TIMER2_COMPA_vect)
181 { 207 {
182 measure_count ++; 208 measure_count ++;
183 comms_count ++; 209 comms_count ++;
210 printf("measure_count %d\n", measure_count);
184 if (measure_count == MEASURE_WAKE) 211 if (measure_count == MEASURE_WAKE)
185 { 212 {
186 measure_count = 0; 213 measure_count = 0;
214 printf("need_measurement = 1\n");
187 need_measurement = 1; 215 need_measurement = 1;
188 } 216 }
189 217
190 if (comms_count == COMMS_WAKE) 218 if (comms_count == COMMS_WAKE)
191 { 219 {
271 } 299 }
272 300
273 static void 301 static void
274 do_measurement() 302 do_measurement()
275 { 303 {
304 printf("do_measurement\n");
276 need_measurement = 0; 305 need_measurement = 0;
277 306
278 do_adc_335(); 307 //do_adc_335();
279 } 308 }
280 309
281 static void 310 static void
282 do_comms() 311 do_comms()
283 { 312 {
286 // turn on bluetooth 315 // turn on bluetooth
287 uart_on(); 316 uart_on();
288 317
289 // write sd card here? same 3.3v regulator... 318 // write sd card here? same 3.3v regulator...
290 319
320 printf("ready> \n");
321
291 comms_done = 0; 322 comms_done = 0;
292 for (;;) 323 for (;;)
293 { 324 {
294 if (comms_done) 325 if (comms_done)
295 { 326 {
343 CLKPR = _BV(CLKPS1); 374 CLKPR = _BV(CLKPS1);
344 sei(); 375 sei();
345 } 376 }
346 377
347 static void 378 static void
348 test1wire() 379 dump_ds18x20()
349 { 380 {
350 //ow_reset();
351
352 uint8_t ret = DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL);
353 printf("ret %d\n", ret);
354 _delay_ms(DS18B20_TCONV_12BIT);
355 DS18X20_read_meas_all_verbose();
356 } 381 }
357 382
358 int main(void) 383 int main(void)
359 { 384 {
360 set_2mhz(); 385 set_2mhz();
372 //PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); 397 //PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC);
373 398
374 // for testing 399 // for testing
375 uart_on(); 400 uart_on();
376 401
377 //sei(); 402 sei();
378 403
379 for (;;) 404 #if 0
380 {
381 test1wire();
382 long_delay(2000);
383 }
384
385 // set up counter2. 405 // set up counter2.
386 // COM21 COM20 Set OC2 on Compare Match (p116) 406 // COM21 COM20 Set OC2 on Compare Match (p116)
387 // WGM21 Clear counter on compare 407 // WGM21 Clear counter on compare
388 TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); 408 TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21);
389 // CS22 CS21 CS20 clk/1024 409 // CS22 CS21 CS20 clk/1024
390 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); 410 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);
391 // set async mode 411 // set async mode
392 ASSR |= _BV(AS2); 412 ASSR |= _BV(AS2);
393 // interrupt 413 // interrupt
394 TIMSK2 = _BV(OCIE2A); 414 TIMSK2 = _BV(OCIE2A);
395 415 #endif
396 #ifdef TEST_MODE 416
397 for (;;) 417 for (;;)
398 { 418 {
399 do_comms() 419 do_comms();
400 } 420 }
401 #else 421
402 for(;;){ 422 for(;;){
403
404 test1wire();
405
406 /* insert your main loop code here */ 423 /* insert your main loop code here */
407 if (need_measurement) 424 if (need_measurement)
408 { 425 {
409 do_measurement(); 426 do_measurement();
410 continue; 427 continue;
418 435
419 deep_sleep(); 436 deep_sleep();
420 blink(); 437 blink();
421 printf("."); 438 printf(".");
422 } 439 }
423 #endif 440
424 return 0; /* never reached */ 441 return 0; /* never reached */
425 } 442 }