Mercurial > templog
comparison main.c @ 14:426fb44ece3f
Lots of it works now.
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 20 May 2012 00:36:52 +0800 |
parents | 4838bfcb3504 |
children | 54b0fda9cba7 |
comparison
equal
deleted
inserted
replaced
13:4838bfcb3504 | 14:426fb44ece3f |
---|---|
27 // - bluetooth params | 27 // - bluetooth params |
28 // - number of sensors (and range?) | 28 // - number of sensors (and range?) |
29 | 29 |
30 // 1 second. we have 1024 prescaler, 32768 crystal. | 30 // 1 second. we have 1024 prescaler, 32768 crystal. |
31 #define SLEEP_COMPARE 32 | 31 #define SLEEP_COMPARE 32 |
32 #define MEASURE_WAKE 60 | 32 #define MEASURE_WAKE 10 |
33 | 33 |
34 #define VALUE_NOSENSOR -9000 | 34 #define VALUE_NOSENSOR -9000 |
35 #define VALUE_BROKEN -8000 | 35 #define VALUE_BROKEN -8000 |
36 | 36 |
37 #define COMMS_WAKE 3600 | 37 #define COMMS_WAKE 3600 |
45 | 45 |
46 #define NUM_MEASUREMENTS 100 | 46 #define NUM_MEASUREMENTS 100 |
47 #define MAX_SENSORS 5 | 47 #define MAX_SENSORS 5 |
48 | 48 |
49 int uart_putchar(char c, FILE *stream); | 49 int uart_putchar(char c, FILE *stream); |
50 static void long_delay(int ms); | |
51 | |
50 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, | 52 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
51 _FDEV_SETUP_WRITE); | 53 _FDEV_SETUP_WRITE); |
52 | 54 |
53 static uint16_t n_measurements = 0; | 55 static uint16_t n_measurements = 0; |
54 // stored as decidegrees | 56 // stored as decidegrees |
55 static int16_t measurements[MAX_SENSORS][NUM_MEASUREMENTS]; | 57 static int16_t measurements[NUM_MEASUREMENTS][MAX_SENSORS]; |
56 | 58 |
57 // boolean flags | 59 // boolean flags |
58 static uint8_t need_measurement = 0; | 60 static uint8_t need_measurement = 0; |
59 static uint8_t need_comms = 0; | 61 static uint8_t need_comms = 0; |
60 static uint8_t comms_done = 0; | 62 static uint8_t comms_done = 0; |
130 | 132 |
131 static void | 133 static void |
132 cmd_fetch() | 134 cmd_fetch() |
133 { | 135 { |
134 uint16_t crc = 0; | 136 uint16_t crc = 0; |
135 uint16_t sens; | 137 uint8_t sens; |
136 eeprom_read(sens, n_sensors); | 138 eeprom_read(sens, n_sensors); |
137 | 139 |
138 printf_P(PSTR("%d measurements\n"), n_measurements); | 140 printf_P(PSTR("%d measurements\n"), n_measurements); |
139 for (uint16_t i = 0; i < n_measurements; i++) | 141 for (uint16_t n = 0; n < n_measurements; n++) |
140 { | 142 { |
141 printf_P(PSTR("%3d :"), i); | 143 printf_P(PSTR("%3d :"), n); |
142 for (uint8_t s = 0; s < sens; s++) | 144 for (uint8_t s = 0; s < sens; s++) |
143 { | 145 { |
144 printf_P(PSTR(" %6d"), measurements[s][i]); | 146 printf_P(PSTR(" %6d"), measurements[n][s]); |
145 crc = _crc_ccitt_update(crc, measurements[s][i]); | 147 crc = _crc_ccitt_update(crc, measurements[n][s]); |
146 } | 148 } |
147 putchar('\n'); | 149 putchar('\n'); |
148 } | 150 } |
149 printf_P(PSTR("CRC : %d\n"), crc); | 151 printf_P(PSTR("CRC : %d\n"), crc); |
150 } | 152 } |
173 | 175 |
174 static void | 176 static void |
175 cmd_sensors() | 177 cmd_sensors() |
176 { | 178 { |
177 uint8_t ret = simple_ds18b20_start_meas(NULL); | 179 uint8_t ret = simple_ds18b20_start_meas(NULL); |
178 printf_P(("All sensors, ret %d, waiting...\n"), ret); | 180 printf_P(PSTR("All sensors, ret %d, waiting...\n"), ret); |
179 _delay_ms(DS18B20_TCONV_12BIT); | 181 long_delay(DS18B20_TCONV_12BIT); |
180 simple_ds18b20_read_all(); | 182 simple_ds18b20_read_all(); |
181 } | 183 } |
182 | 184 |
183 // 0 on success | 185 // 0 on success |
184 static uint8_t | 186 static uint8_t |
258 | 260 |
259 static void | 261 static void |
260 cmd_add_all() | 262 cmd_add_all() |
261 { | 263 { |
262 uint8_t id[OW_ROMCODE_SIZE]; | 264 uint8_t id[OW_ROMCODE_SIZE]; |
263 uint8_t sp[DS18X20_SP_SIZE]; | |
264 printf_P("Adding all\n"); | 265 printf_P("Adding all\n"); |
265 ow_reset(); | 266 ow_reset(); |
266 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) | 267 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) |
267 { | 268 { |
268 diff = ow_rom_search( diff, &id[0] ); | 269 diff = ow_rom_search( diff, &id[0] ); |
311 if (magic != EXPECT_MAGIC) | 312 if (magic != EXPECT_MAGIC) |
312 { | 313 { |
313 printf_P(PSTR("First boot, looking for sensors...\n")); | 314 printf_P(PSTR("First boot, looking for sensors...\n")); |
314 cmd_init(); | 315 cmd_init(); |
315 cmd_add_all(); | 316 cmd_add_all(); |
317 cli(); | |
318 magic = EXPECT_MAGIC; | |
319 eeprom_write(magic, magic); | |
320 sei(); | |
316 } | 321 } |
317 } | 322 } |
318 | 323 |
319 static void | 324 static void |
320 read_handler() | 325 read_handler() |
378 } | 383 } |
379 } | 384 } |
380 | 385 |
381 ISR(TIMER2_COMPA_vect) | 386 ISR(TIMER2_COMPA_vect) |
382 { | 387 { |
388 TCNT2 = 0; | |
383 measure_count ++; | 389 measure_count ++; |
384 comms_count ++; | 390 comms_count ++; |
385 printf("measure_count %d\n", measure_count); | 391 printf("measure_count %d\n", measure_count); |
386 if (measure_count == MEASURE_WAKE) | 392 if (measure_count >= MEASURE_WAKE) |
387 { | 393 { |
388 measure_count = 0; | 394 measure_count = 0; |
389 printf("need_measurement = 1\n"); | 395 printf("need_measurement = 1\n"); |
390 need_measurement = 1; | 396 need_measurement = 1; |
391 } | 397 } |
418 { | 424 { |
419 set_sleep_mode(SLEEP_MODE_IDLE); | 425 set_sleep_mode(SLEEP_MODE_IDLE); |
420 sleep_mode(); | 426 sleep_mode(); |
421 } | 427 } |
422 | 428 |
429 #if 0 | |
430 // untested | |
423 static void | 431 static void |
424 do_adc_335() | 432 do_adc_335() |
425 { | 433 { |
426 //PRR &= ~_BV(PRADC); | 434 //PRR &= ~_BV(PRADC); |
427 | 435 |
472 n_measurements, temp, internal_temp, f_11); | 480 n_measurements, temp, internal_temp, f_11); |
473 | 481 |
474 n_measurements++; | 482 n_measurements++; |
475 //PRR |= _BV(PRADC); | 483 //PRR |= _BV(PRADC); |
476 } | 484 } |
485 #endif | |
477 | 486 |
478 static void | 487 static void |
479 do_measurement() | 488 do_measurement() |
480 { | 489 { |
481 uint8_t n_sensors; | 490 uint8_t n_sensors; |
491 printf("do_measurement\n"); | |
482 eeprom_read(n_sensors, n_sensors); | 492 eeprom_read(n_sensors, n_sensors); |
493 printf("do_measurement sensors %d\n", n_sensors); | |
483 | 494 |
484 uint8_t ret = simple_ds18b20_start_meas(NULL); | 495 uint8_t ret = simple_ds18b20_start_meas(NULL); |
485 printf_P(("Read all sensors, ret %d, waiting...\n"), ret); | 496 printf_P(PSTR("Read all sensors, ret %d, waiting...\n"), ret); |
486 _delay_ms(DS18B20_TCONV_12BIT); | 497 _delay_ms(DS18B20_TCONV_12BIT); |
487 | 498 |
488 if (n_measurements == NUM_MEASUREMENTS) | 499 if (n_measurements == NUM_MEASUREMENTS) |
489 { | 500 { |
490 printf_P(PSTR("Measurements overflow\n")); | 501 printf_P(PSTR("Measurements .overflow\n")); |
491 n_measurements = 0; | 502 n_measurements = 0; |
492 } | 503 } |
493 | 504 |
494 for (uint8_t n = 0; n < MAX_SENSORS; n++) | 505 for (uint8_t s = 0; s < MAX_SENSORS; s++) |
495 { | 506 { |
496 int16_t decicelsius; | 507 int16_t decicelsius; |
497 if (n >= n_sensors) | 508 if (s >= n_sensors) |
498 { | 509 { |
499 decicelsius = VALUE_NOSENSOR; | 510 decicelsius = VALUE_NOSENSOR; |
500 } | 511 } |
501 else | 512 else |
502 { | 513 { |
503 uint8_t id[8]; | 514 uint8_t id[8]; |
504 eeprom_read_to(id, sensor_id[n], 8); | 515 eeprom_read_to(id, sensor_id[s], 8); |
505 | 516 |
506 uint8_t ret = simple_ds18b20_read_decicelsius(id, &decicelsius); | 517 uint8_t ret = simple_ds18b20_read_decicelsius(id, &decicelsius); |
507 if (ret != DS18X20_OK) | 518 if (ret != DS18X20_OK) |
508 { | 519 { |
509 decicelsius = VALUE_BROKEN; | 520 decicelsius = VALUE_BROKEN; |
510 } | 521 } |
511 } | 522 } |
512 measurements[n_measurements][n] = decicelsius; | 523 measurements[n_measurements][s] = decicelsius; |
513 } | 524 } |
525 n_measurements++; | |
514 //do_adc_335(); | 526 //do_adc_335(); |
515 } | 527 } |
516 | 528 |
517 static void | 529 static void |
518 do_comms() | 530 do_comms() |
534 break; | 546 break; |
535 } | 547 } |
536 | 548 |
537 if (need_measurement) | 549 if (need_measurement) |
538 { | 550 { |
551 need_measurement = 0; | |
552 printf("measure from do_comms\n"); | |
539 do_measurement(); | 553 do_measurement(); |
540 } | 554 } |
541 | 555 |
542 idle_sleep(); | 556 idle_sleep(); |
543 } | 557 } |
553 PORT_LED &= ~_BV(PIN_LED); | 567 PORT_LED &= ~_BV(PIN_LED); |
554 _delay_ms(1); | 568 _delay_ms(1); |
555 PORT_LED |= _BV(PIN_LED); | 569 PORT_LED |= _BV(PIN_LED); |
556 } | 570 } |
557 | 571 |
558 #if 0 | |
559 static void | 572 static void |
560 long_delay(int ms) | 573 long_delay(int ms) |
561 { | 574 { |
562 int iter = ms / 100; | 575 int iter = ms / 100; |
563 | 576 |
564 for (int i = 0; i < iter; i++) | 577 for (int i = 0; i < iter; i++) |
565 { | 578 { |
566 _delay_ms(100); | 579 _delay_ms(100); |
567 } | 580 } |
568 } | 581 } |
569 #endif | |
570 | 582 |
571 ISR(BADISR_vect) | 583 ISR(BADISR_vect) |
572 { | 584 { |
573 uart_on(); | 585 //uart_on(); |
574 printf_P(PSTR("Bad interrupt\n")); | 586 printf_P(PSTR("Bad interrupt\n")); |
575 } | 587 } |
576 | 588 |
577 static void | 589 static void |
578 set_2mhz() | 590 set_2mhz() |
609 sei(); | 621 sei(); |
610 | 622 |
611 // set up counter2. | 623 // set up counter2. |
612 // COM21 COM20 Set OC2 on Compare Match (p116) | 624 // COM21 COM20 Set OC2 on Compare Match (p116) |
613 // WGM21 Clear counter on compare | 625 // WGM21 Clear counter on compare |
614 TCCR2A = _BV(COM2A1) | _BV(COM2A0);// | _BV(WGM21); | 626 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); |
627 // toggle on match | |
628 TCCR2A = _BV(COM2A0); | |
615 // CS22 CS21 CS20 clk/1024 | 629 // CS22 CS21 CS20 clk/1024 |
616 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); | 630 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); |
617 // set async mode | 631 // set async mode |
618 ASSR |= _BV(AS2); | 632 ASSR |= _BV(AS2); |
633 TCNT2 = 0; | |
634 OCR2A = SLEEP_COMPARE; | |
619 // interrupt | 635 // interrupt |
620 TIMSK2 = _BV(OCIE2A); | 636 TIMSK2 = _BV(OCIE2A); |
621 | 637 |
638 #if 0 | |
622 for (;;) | 639 for (;;) |
623 { | 640 { |
624 do_comms(); | 641 do_comms(); |
625 } | 642 } |
643 #endif | |
626 | 644 |
627 for(;;){ | 645 for(;;){ |
628 /* insert your main loop code here */ | 646 /* insert your main loop code here */ |
629 if (need_measurement) | 647 if (need_measurement) |
630 { | 648 { |
649 need_measurement = 0; | |
631 do_measurement(); | 650 do_measurement(); |
651 // testing | |
652 cmd_fetch(); | |
632 continue; | 653 continue; |
633 } | 654 } |
634 | 655 |
635 if (need_comms) | 656 if (need_comms) |
636 { | 657 { |