comparison main.c @ 46:9ccd965d938a

Use the PRR etc, set value to proper ones
author Matt Johnston <matt@ucc.asn.au>
date Sun, 24 Jun 2012 00:44:28 +0800
parents 96c336896201
children c3f5e02c1c42
comparison
equal deleted inserted replaced
45:425af63d584d 46:9ccd965d938a
22 // - number of sensors (and range?) 22 // - number of sensors (and range?)
23 23
24 // 1 second. we have 1024 prescaler, 32768 crystal. 24 // 1 second. we have 1024 prescaler, 32768 crystal.
25 #define SLEEP_COMPARE 32 25 #define SLEEP_COMPARE 32
26 // limited to uint16_t 26 // limited to uint16_t
27 #define MEASURE_WAKE 5 // testing 27 #define MEASURE_WAKE 60
28 28
29 #define VALUE_NOSENSOR -9000 29 #define VALUE_NOSENSOR -9000
30 #define VALUE_BROKEN -8000 30 #define VALUE_BROKEN -8000
31 31
32 // limited to uint16_t 32 // limited to uint16_t
33 #define COMMS_WAKE 40 // XXX testing 33 #define COMMS_WAKE 3600 // XXX testing
34 // limited to uint8_t 34 // limited to uint8_t
35 #define WAKE_SECS 30 // XXX testing 35 #define WAKE_SECS 60 // XXX testing
36 36
37 #define BAUD 19200 37 #define BAUD 19200
38 #define UBRR ((F_CPU)/8/(BAUD)-1) 38 #define UBRR ((F_CPU)/8/(BAUD)-1)
39 39
40 #define PORT_LED PORTC 40 #define PORT_LED PORTC
44 #define PORT_SHDN PORTD 44 #define PORT_SHDN PORTD
45 #define DDR_SHDN DDRD 45 #define DDR_SHDN DDRD
46 #define PIN_SHDN PD7 46 #define PIN_SHDN PD7
47 47
48 // limited to uint16_t 48 // limited to uint16_t
49 #define NUM_MEASUREMENTS 100 49 #define NUM_MEASUREMENTS 280
50 // limited to uint8_t 50 // limited to uint8_t
51 #define MAX_SENSORS 5 51 #define MAX_SENSORS 3
52 52
53 // fixed at 8, have a shorter name 53 // fixed at 8, have a shorter name
54 #define ID_LEN OW_ROMCODE_SIZE 54 #define ID_LEN OW_ROMCODE_SIZE
55 55
56 // #define HAVE_UART_ECHO 56 // #define HAVE_UART_ECHO
124 124
125 // 3.3v power for bluetooth and SD 125 // 3.3v power for bluetooth and SD
126 DDR_LED |= _BV(PIN_LED); 126 DDR_LED |= _BV(PIN_LED);
127 DDR_SHDN |= _BV(PIN_SHDN); 127 DDR_SHDN |= _BV(PIN_SHDN);
128 128
129 // set pullup
130 PORTD |= _BV(PD2);
129 // INT0 setup 131 // INT0 setup
130 EIMSK = _BV(INT0); 132 EIMSK = _BV(INT0);
131 // set pullup
132 PORTD |= _BV(PD2);
133 } 133 }
134 134
135 static void 135 static void
136 set_aux_power(uint8_t on) 136 set_aux_power(uint8_t on)
137 { 137 {
166 166
167 static void 167 static void
168 uart_on() 168 uart_on()
169 { 169 {
170 // Power reduction register 170 // Power reduction register
171 //PRR &= ~_BV(PRUSART0); 171 PRR &= ~_BV(PRUSART0);
172 172
173 // All of this needs to be done each time after turning off the PRR 173 // All of this needs to be done each time after turning off the PRR
174 // baud rate 174 // baud rate
175 UBRR0H = (unsigned char)(UBRR >> 8); 175 UBRR0H = (unsigned char)(UBRR >> 8);
176 UBRR0L = (unsigned char)UBRR; 176 UBRR0L = (unsigned char)UBRR;
188 // Turn of interrupts and disable tx/rx 188 // Turn of interrupts and disable tx/rx
189 UCSR0B = 0; 189 UCSR0B = 0;
190 uart_enabled = 0; 190 uart_enabled = 0;
191 191
192 // Power reduction register 192 // Power reduction register
193 //PRR |= _BV(PRUSART0); 193 PRR |= _BV(PRUSART0);
194 } 194 }
195 195
196 int 196 int
197 uart_putchar(char c, FILE *stream) 197 uart_putchar(char c, FILE *stream)
198 { 198 {
250 fputc('\n', crc_stdout); 250 fputc('\n', crc_stdout);
251 } 251 }
252 fprintf_P(crc_stdout, PSTR("measurements=%hu\n"), n_measurements); 252 fprintf_P(crc_stdout, PSTR("measurements=%hu\n"), n_measurements);
253 for (uint16_t n = 0; n < n_measurements; n++) 253 for (uint16_t n = 0; n < n_measurements; n++)
254 { 254 {
255 fprintf_P(crc_stdout, PSTR("meas%u="), n); 255 fprintf_P(crc_stdout, PSTR("meas%hu="), n);
256 for (uint8_t s = 0; s < n_sensors; s++) 256 for (uint8_t s = 0; s < n_sensors; s++)
257 { 257 {
258 fprintf_P(crc_stdout, PSTR(" %hu"), measurements[n][s]); 258 fprintf_P(crc_stdout, PSTR(" %hu"), measurements[n][s]);
259 } 259 }
260 fputc('\n', crc_stdout); 260 fputc('\n', crc_stdout);
271 } 271 }
272 272
273 static void 273 static void
274 cmd_btoff() 274 cmd_btoff()
275 { 275 {
276 uint16_t next_wake = COMMS_WAKE - comms_count; 276 comms_count = 0;
277 printf_P(PSTR("off:%d\n"), next_wake); 277 printf_P(PSTR("off:%hu\n"), COMMS_WAKE);
278 _delay_ms(50); 278 _delay_ms(100);
279 comms_timeout = 0; 279 comms_timeout = 0;
280 } 280 }
281 281
282 static void 282 static void
283 cmd_measure() 283 cmd_measure()
617 printf_P(PSTR("Read all sensors, ret %d, waiting...\n"), ret); 617 printf_P(PSTR("Read all sensors, ret %d, waiting...\n"), ret);
618 _delay_ms(DS18B20_TCONV_12BIT); 618 _delay_ms(DS18B20_TCONV_12BIT);
619 619
620 if (n_measurements == NUM_MEASUREMENTS) 620 if (n_measurements == NUM_MEASUREMENTS)
621 { 621 {
622 printf_P(PSTR("Measurements .overflow\n")); 622 printf_P(PSTR("Measurements overflow\n"));
623 n_measurements = 0; 623 n_measurements = 0;
624 } 624 }
625 625
626 for (uint8_t s = 0; s < MAX_SENSORS; s++) 626 for (uint8_t s = 0; s < MAX_SENSORS; s++)
627 { 627 {
682 // wait for commands from the master 682 // wait for commands from the master
683 idle_sleep(); 683 idle_sleep();
684 } 684 }
685 685
686 uart_off(); 686 uart_off();
687 // in case bluetooth takes time to flush
688 _delay_ms(100);
687 set_aux_power(0); 689 set_aux_power(0);
688 } 690 }
689 691
690 static void 692 static void
691 blink() 693 blink()
728 check_first_startup(); 730 check_first_startup();
729 731
730 uart_off(); 732 uart_off();
731 733
732 // turn off everything except timer2 734 // turn off everything except timer2
733 //PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); 735 PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC);
734 736
735 // for testing 737 // for testing
736 uart_on(); 738 uart_on();
737 739
738 setup_tick_counter(); 740 setup_tick_counter();
757 do_comms(); 759 do_comms();
758 continue; 760 continue;
759 } 761 }
760 762
761 deep_sleep(); 763 deep_sleep();
762 blink(); 764 if (clock_epoch % 60 == 0)
765 {
766 blink();
767 }
763 } 768 }
764 769
765 return 0; /* never reached */ 770 return 0; /* never reached */
766 } 771 }