Mercurial > templog
comparison main.c @ 55:8e897a682208
untested code to log voltage and internal temperature
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 24 Jun 2012 23:47:56 +0800 |
parents | 0d3d14af55c2 |
children | 5100e0bdadad |
comparison
equal
deleted
inserted
replaced
54:0d3d14af55c2 | 55:8e897a682208 |
---|---|
45 #define PORT_SHDN PORTD | 45 #define PORT_SHDN PORTD |
46 #define DDR_SHDN DDRD | 46 #define DDR_SHDN DDRD |
47 #define PIN_SHDN PD7 | 47 #define PIN_SHDN PD7 |
48 | 48 |
49 // limited to uint16_t | 49 // limited to uint16_t |
50 // XXX - increasing this to 300 causes strange failures, | |
51 // not sure why | |
50 #define NUM_MEASUREMENTS 280 | 52 #define NUM_MEASUREMENTS 280 |
51 // limited to uint8_t | 53 // limited to uint8_t |
52 #define MAX_SENSORS 3 | 54 #define MAX_SENSORS 3 |
53 | 55 |
54 // fixed at 8, have a shorter name | 56 // fixed at 8, have a shorter name |
57 // #define HAVE_UART_ECHO | 59 // #define HAVE_UART_ECHO |
58 | 60 |
59 int uart_putchar(char c, FILE *stream); | 61 int uart_putchar(char c, FILE *stream); |
60 static void long_delay(int ms); | 62 static void long_delay(int ms); |
61 static void blink(); | 63 static void blink(); |
64 static void adc_internal(uint16_t *millivolt_vcc, uint16_t *int_temp); | |
62 | 65 |
63 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, | 66 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
64 _FDEV_SETUP_WRITE); | 67 _FDEV_SETUP_WRITE); |
65 | 68 |
66 uint16_t crc_out; | 69 uint16_t crc_out; |
251 { | 254 { |
252 crc_out = 0; | 255 crc_out = 0; |
253 uint8_t n_sensors; | 256 uint8_t n_sensors; |
254 eeprom_read(n_sensors, n_sensors); | 257 eeprom_read(n_sensors, n_sensors); |
255 | 258 |
259 uint16_t millivolt_vcc, int_temp; | |
260 | |
261 adc_internal(&millivolt_vcc, &int_temp); | |
262 | |
256 fprintf_P(crc_stdout, PSTR("START\n")); | 263 fprintf_P(crc_stdout, PSTR("START\n")); |
257 fprintf_P(crc_stdout, PSTR("now=%lu\n" | 264 fprintf_P(crc_stdout, PSTR("now=%lu\n" |
258 "time_step=%hu\n" | 265 "time_step=%hu\n" |
259 "first_time=%lu\n" | 266 "first_time=%lu\n" |
260 "last_time=%lu\n"), | 267 "last_time=%lu\n" |
268 "voltage=%hu\n" | |
269 "avrtemp=%hu\n"), | |
261 clock_epoch, | 270 clock_epoch, |
262 (uint16_t)MEASURE_WAKE, | 271 (uint16_t)MEASURE_WAKE, |
263 first_measurement_clock, | 272 first_measurement_clock, |
264 last_measurement_clock); | 273 last_measurement_clock, |
274 millivolt_vcc, | |
275 int_temp | |
276 ); | |
265 fprintf_P(crc_stdout, PSTR("sensors=%u\n"), n_sensors); | 277 fprintf_P(crc_stdout, PSTR("sensors=%u\n"), n_sensors); |
266 for (uint8_t s = 0; s < n_sensors; s++) | 278 for (uint8_t s = 0; s < n_sensors; s++) |
267 { | 279 { |
268 uint8_t id[ID_LEN]; | 280 uint8_t id[ID_LEN]; |
269 fprintf_P(crc_stdout, PSTR("sensor_id%u="), s); | 281 fprintf_P(crc_stdout, PSTR("sensor_id%u="), s); |
593 { | 605 { |
594 set_sleep_mode(SLEEP_MODE_IDLE); | 606 set_sleep_mode(SLEEP_MODE_IDLE); |
595 sleep_mode(); | 607 sleep_mode(); |
596 } | 608 } |
597 | 609 |
610 static void | |
611 adc_internal(uint16_t *millivolt_vcc, uint16_t *int_temp) | |
612 { | |
613 PRR &= ~_BV(PRADC); | |
614 | |
615 // left adjust | |
616 ADMUX = _BV(ADLAR); | |
617 | |
618 // ADPS2 = /16 prescaler, 62khz at 1mhz clock | |
619 ADCSRA = _BV(ADEN) | _BV(ADPS2); | |
620 | |
621 // set to measure 1.1 reference | |
622 ADMUX = _BV(ADLAR) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); | |
623 ADCSRA |= _BV(ADSC); | |
624 loop_until_bit_is_clear(ADCSRA, ADSC); | |
625 uint8_t low_11 = ADCL; | |
626 uint8_t high_11 = ADCH; | |
627 uint16_t f_11 = low_11 + (high_11 << 8); | |
628 | |
629 float res_volts = 1.1 * 1024 / f_11; | |
630 *millivolt_vcc = 1000 * res_volts; | |
631 | |
632 // measure AVR internal temperature against 1.1 ref. | |
633 ADMUX = _BV(ADLAR) | _BV(MUX3) | _BV(REFS1) | _BV(REFS0); | |
634 ADCSRA |= _BV(ADSC); | |
635 loop_until_bit_is_clear(ADCSRA, ADSC); | |
636 uint16_t res_internal = ADCL; | |
637 res_internal |= ADCH << 8; | |
638 float internal_volts = res_internal * (1.1 / 1024.0); | |
639 // decidegrees | |
640 *int_temp = (internal_volts - 2.73) * 1000; | |
641 | |
642 PRR |= _BV(PRADC); | |
643 ADCSRA = 0; | |
644 } | |
645 | |
598 #if 0 | 646 #if 0 |
599 // untested | 647 // untested |
600 static void | 648 static void |
601 do_adc_335() | 649 do_adc_335() |
602 { | 650 { |
655 | 703 |
656 static void | 704 static void |
657 do_measurement() | 705 do_measurement() |
658 { | 706 { |
659 uint8_t n_sensors; | 707 uint8_t n_sensors; |
660 printf("do_measurement\n"); | |
661 eeprom_read(n_sensors, n_sensors); | 708 eeprom_read(n_sensors, n_sensors); |
662 printf("do_measurement sensors %d\n", n_sensors); | 709 |
663 | 710 simple_ds18b20_start_meas(NULL); |
664 uint8_t ret = simple_ds18b20_start_meas(NULL); | |
665 printf_P(PSTR("Read all sensors, ret %d, waiting...\n"), ret); | |
666 _delay_ms(DS18B20_TCONV_12BIT); | 711 _delay_ms(DS18B20_TCONV_12BIT); |
667 | 712 |
668 if (n_measurements == NUM_MEASUREMENTS) | 713 if (n_measurements == NUM_MEASUREMENTS) |
669 { | 714 { |
670 printf_P(PSTR("Measurements overflow\n")); | |
671 n_measurements = 0; | 715 n_measurements = 0; |
672 } | 716 } |
673 | 717 |
674 for (uint8_t s = 0; s < MAX_SENSORS; s++) | 718 for (uint8_t s = 0; s < MAX_SENSORS; s++) |
675 { | 719 { |