static void FA_NORETURN( reboot ) ( void ) { cli() ; // Disable interrupts USBCON = _B0(USBE) | _B1(FRZCLK) ; // Kill USB for ( ;; ) // Wait for watchdog to bite (.5s) ; }
// returns the battery voltage in 10mV units // for instance: get_battery_voltage() returning 278 equals a voltage of 2.78V uint16_t get_battery_voltage(void) { power_adc_enable(); ADMUX = _B0(REFS1) | _B1(REFS0) // AVCC with external capacitor at AREF pin #ifndef PREC_BATT_VOLTAGE | _BV(ADLAR) // left adjust ADC - drops the two LSBs #endif | 0b11110; // measure 1.1v internal reference ADCSRA = _BV(ADEN) // enable ADC | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0) // prescaler 128 | _BV(ADSC); // start conversion // wait for the conversion to finish loop_until_bit_is_set(ADCSRA, ADIF); // remember the result #ifdef PREC_BATT_VOLTAGE uint16_t adc_result = ADC; #else uint8_t adc_result = ADCH; #endif // clear the ADIF bit by writing one SetBit(ADCSRA, ADIF); ADCSRA = 0; // disable ADC power_adc_disable(); // ADC power off #ifdef PREC_BATT_VOLTAGE return 112640 / adc_result; #else return 28050 / adc_result; #endif }