comparison main.c @ 58:5100e0bdadad

fix voltage
author Matt Johnston <matt@ucc.asn.au>
date Mon, 25 Jun 2012 01:23:34 +0800
parents 8e897a682208
children d5b269352ba0 62112fc2af21
comparison
equal deleted inserted replaced
57:cec925666c27 58:5100e0bdadad
617 617
618 // ADPS2 = /16 prescaler, 62khz at 1mhz clock 618 // ADPS2 = /16 prescaler, 62khz at 1mhz clock
619 ADCSRA = _BV(ADEN) | _BV(ADPS2); 619 ADCSRA = _BV(ADEN) | _BV(ADPS2);
620 620
621 // set to measure 1.1 reference 621 // set to measure 1.1 reference
622 ADMUX = _BV(ADLAR) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 622 ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
623 ADCSRA |= _BV(ADSC); 623 ADCSRA |= _BV(ADSC);
624 loop_until_bit_is_clear(ADCSRA, ADSC); 624 loop_until_bit_is_clear(ADCSRA, ADSC);
625 uint8_t low_11 = ADCL; 625 uint8_t low_11 = ADCL;
626 uint8_t high_11 = ADCH; 626 uint8_t high_11 = ADCH;
627 uint16_t f_11 = low_11 + (high_11 << 8); 627 uint16_t f_11 = low_11 + (high_11 << 8);
628 628
629 float res_volts = 1.1 * 1024 / f_11; 629 float res_volts = 1.1 * 1024 / f_11;
630 *millivolt_vcc = 1000 * res_volts; 630 *millivolt_vcc = 1000 * res_volts;
631 631
632 // measure AVR internal temperature against 1.1 ref. 632 // measure AVR internal temperature against 1.1 ref.
633 ADMUX = _BV(ADLAR) | _BV(MUX3) | _BV(REFS1) | _BV(REFS0); 633 ADMUX = _BV(MUX3) | _BV(REFS1) | _BV(REFS0);
634 ADCSRA |= _BV(ADSC); 634 ADCSRA |= _BV(ADSC);
635 loop_until_bit_is_clear(ADCSRA, ADSC); 635 loop_until_bit_is_clear(ADCSRA, ADSC);
636 uint16_t res_internal = ADCL; 636 uint8_t low_temp = ADCL;
637 res_internal |= ADCH << 8; 637 uint8_t high_temp = ADCH;
638 uint16_t res_internal = low_temp + (high_temp << 8);
638 float internal_volts = res_internal * (1.1 / 1024.0); 639 float internal_volts = res_internal * (1.1 / 1024.0);
639 // decidegrees 640 // millivolts
640 *int_temp = (internal_volts - 2.73) * 1000; 641 *int_temp = internal_volts * 1000;
641 642
642 PRR |= _BV(PRADC); 643 PRR |= _BV(PRADC);
643 ADCSRA = 0; 644 ADCSRA = 0;
644 } 645 }
645 646