comparison main.c @ 10:1bfe28c348dd

reading DS18B20 works add some linker optimisations
author Matt Johnston <matt@ucc.asn.au>
date Sat, 19 May 2012 17:10:13 +0800
parents 7da9a3f23592
children 3c27bfbd7f3a
comparison
equal deleted inserted replaced
9:7da9a3f23592 10:1bfe28c348dd
13 #include <avr/pgmspace.h> 13 #include <avr/pgmspace.h>
14 #include <util/crc16.h> 14 #include <util/crc16.h>
15 15
16 #include "integer.h" 16 #include "integer.h"
17 #include "onewire.h" 17 #include "onewire.h"
18 #include "ds18x20.h"
18 19
19 // configuration params 20 // configuration params
20 // - measurement interval 21 // - measurement interval
21 // - transmit interval 22 // - transmit interval
22 // - bluetooth params 23 // - bluetooth params
26 #define SLEEP_COMPARE 32 27 #define SLEEP_COMPARE 32
27 #define MEASURE_WAKE 60 28 #define MEASURE_WAKE 60
28 29
29 #define COMMS_WAKE 3600 30 #define COMMS_WAKE 3600
30 31
31 #define BAUD 9600 32 #define BAUD 19200
32 #define UBRR ((F_CPU)/8/(BAUD)-1) 33 #define UBRR ((F_CPU)/8/(BAUD)-1)
33 34
34 #define PORT_LED PORTC 35 #define PORT_LED PORTC
35 #define DDR_LED DDRC 36 #define DDR_LED DDRC
36 #define PIN_LED PC4 37 #define PIN_LED PC4
37 38
38 #define NUM_MEASUREMENTS 300 39 #define NUM_MEASUREMENTS 300
39 40
40 static int uart_putchar(char c, FILE *stream); 41 int uart_putchar(char c, FILE *stream);
41 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, 42 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
42 _FDEV_SETUP_WRITE); 43 _FDEV_SETUP_WRITE);
43 44
44 static uint8_t n_measurements = 0; 45 static uint8_t n_measurements = 0;
45 // stored as 1/5 degree above 0 46 // stored as 1/5 degree above 0
85 86
86 // Power reduction register 87 // Power reduction register
87 //PRR |= _BV(PRUSART0); 88 //PRR |= _BV(PRUSART0);
88 } 89 }
89 90
90 static int 91 int
91 uart_putchar(char c, FILE *stream) 92 uart_putchar(char c, FILE *stream)
92 { 93 {
94 // XXX should sleep in the loop for power.
93 if (c == '\n') 95 if (c == '\n')
94 { 96 {
95 uart_putchar('\r', stream); 97 loop_until_bit_is_set(UCSR0A, UDRE0);
96 } 98 UDR0 = '\r';;
97 // XXX should sleep in the loop for power. 99 }
98 loop_until_bit_is_set(UCSR0A, UDRE0); 100 loop_until_bit_is_set(UCSR0A, UDRE0);
99 UDR0 = c; 101 UDR0 = c;
102 if (c == '\r')
103 {
104 loop_until_bit_is_set(UCSR0A, UDRE0);
105 UDR0 = '\n';;
106 }
100 return 0; 107 return 0;
101 } 108 }
102 109
103 static void 110 static void
104 cmd_fetch() 111 cmd_fetch()
151 } 158 }
152 159
153 ISR(USART_RX_vect) 160 ISR(USART_RX_vect)
154 { 161 {
155 char c = UDR0; 162 char c = UDR0;
156 printf_P(PSTR("wake up '%c'\n"), c);
157 if (c == '\n') 163 if (c == '\n')
158 { 164 {
159 readbuf[readpos] = '\0'; 165 readbuf[readpos] = '\0';
160 read_handler(); 166 read_handler();
161 readpos = 0; 167 readpos = 0;
171 } 177 }
172 } 178 }
173 179
174 ISR(TIMER2_COMPA_vect) 180 ISR(TIMER2_COMPA_vect)
175 { 181 {
176 DEBUG("wake up\n");
177 measure_count ++; 182 measure_count ++;
178 comms_count ++; 183 comms_count ++;
179 if (measure_count == MEASURE_WAKE) 184 if (measure_count == MEASURE_WAKE)
180 { 185 {
181 measure_count = 0; 186 measure_count = 0;
195 } 200 }
196 201
197 static void 202 static void
198 deep_sleep() 203 deep_sleep()
199 { 204 {
200 DEBUG("deep sleep\n");
201 // p119 of manual 205 // p119 of manual
202 OCR2A = SLEEP_COMPARE; 206 OCR2A = SLEEP_COMPARE;
203 loop_until_bit_is_clear(ASSR, OCR2AUB); 207 loop_until_bit_is_clear(ASSR, OCR2AUB);
204 208
205 DEBUG("really about to\n");
206
207 set_sleep_mode(SLEEP_MODE_PWR_SAVE); 209 set_sleep_mode(SLEEP_MODE_PWR_SAVE);
208 DEBUG("done.\n");
209 sleep_mode(); 210 sleep_mode();
210 } 211 }
211 212
212 static void 213 static void
213 idle_sleep() 214 idle_sleep()
344 } 345 }
345 346
346 static void 347 static void
347 test1wire() 348 test1wire()
348 { 349 {
349 ow_reset(); 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();
350 } 356 }
351 357
352 int main(void) 358 int main(void)
353 { 359 {
354 set_2mhz(); 360 set_2mhz();
366 //PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); 372 //PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC);
367 373
368 // for testing 374 // for testing
369 uart_on(); 375 uart_on();
370 376
371 DEBUG("power off\n"); 377 //sei();
372 sei(); 378
373 DEBUG("sei done\n"); 379 for (;;)
374 380 {
381 test1wire();
382 long_delay(2000);
383 }
375 384
376 // set up counter2. 385 // set up counter2.
377 // COM21 COM20 Set OC2 on Compare Match (p116) 386 // COM21 COM20 Set OC2 on Compare Match (p116)
378 // WGM21 Clear counter on compare 387 // WGM21 Clear counter on compare
379 TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); 388 TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21);
382 // set async mode 391 // set async mode
383 ASSR |= _BV(AS2); 392 ASSR |= _BV(AS2);
384 // interrupt 393 // interrupt
385 TIMSK2 = _BV(OCIE2A); 394 TIMSK2 = _BV(OCIE2A);
386 395
387 DEBUG("async setup\n");
388
389 #ifdef TEST_MODE 396 #ifdef TEST_MODE
390 for (;;) 397 for (;;)
391 { 398 {
392 do_comms() 399 do_comms()
393 } 400 }
409 continue; 416 continue;
410 } 417 }
411 418
412 deep_sleep(); 419 deep_sleep();
413 blink(); 420 blink();
421 printf(".");
414 } 422 }
415 #endif 423 #endif
416 return 0; /* never reached */ 424 return 0; /* never reached */
417 } 425 }