// ************************************************************************************************* // @fn temperature_measurement // @brief Init ADC12. Do single conversion of temperature sensor voltage. Turn off ADC12. // @param none // @return none // ************************************************************************************************* void temperature_measurement(u8 filter) { u16 adc_result; volatile s32 temperature; // Convert internal temperature diode voltage adc_result = adc12_single_conversion(REFVSEL_0, ADC12SHT0_8, ADC12INCH_10); // Convert ADC value to "xx.x °C" // Temperature in Celsius // ((A10/4096*1500mV) - 680mV)*(1/2.25mV) = (A10/4096*667) - 302 // = (A10 - 1855) * (667 / 4096) temperature = (((s32) ((s32) adc_result - 1855)) * 667 * 10) / 4096; // Add temperature offset temperature += sTemp.offset; // Store measured temperature if (filter == FILTER_ON) { // Change temperature in 0.1° steps towards measured value if (temperature > sTemp.degrees) sTemp.degrees += 1; else if (temperature < sTemp.degrees) sTemp.degrees -= 1; } else { // Override filter sTemp.degrees = (s16) temperature; } // New data is available --> do display update display.flag.update_temperature = 1; }
void battery_measurement(void) { /* Convert external battery voltage (ADC12INCH_11=AVCC-AVSS/2) voltage = adc12_single_conversion(REFVSEL_2, ADC12SHT0_10, ADC12SSEL_0, ADC12SREF_1, ADC12INCH_11, ADC12_BATT_CONVERSION_TIME_USEC); */ uint16_t voltage = adc12_single_conversion(REFVSEL_1, ADC12SHT0_10, ADC12INCH_11); /* Convert ADC value to "x.xx V" Ideally we have A11=0->AVCC=0V ... A11=4095(2^12-1)->AVCC=4V --> (A11/4095)*4V=AVCC --> AVCC=(A11*4)/4095 */ voltage = (voltage << 2) / 41; /* Correct measured voltage with calibration value */ voltage += battery_info.offset; /* Discard values that are clearly outside the measurement range */ if (voltage > BATTERY_HIGH_THRESHOLD) voltage = battery_info.voltage; #ifndef CONFIG_BATTERY_DISABLE_FILTER /* Filter battery voltage */ battery_info.voltage = ((voltage << 1) + (battery_info.voltage << 3)) / 10; #else /* Get it raw instead for testing */ battery_info.voltage = voltage; #endif /* Display blinking battery symbol if low */ if (battery_info.voltage < BATTERY_LOW_THRESHOLD) display_symbol(0, LCD_SYMB_BATTERY, SEG_ON | BLINK_ON); }
// ************************************************************************************************* // @fn do_light_measurement // @brief Measures light level // @param none // @return none // ************************************************************************************************* void do_light_measurement(void) { u16 voltage; voltage = adc12_single_conversion(REFVSEL_1, ADC12SHT0_10, ADC12INCH_2); voltage = (voltage * 2 * 2) / 41; light.value = light.value*0.7 + voltage*0.3; }
uint32_t battery_get_voltage(void) { uint32_t voltage; voltage = adc12_single_conversion(REFVSEL_1, ADC12SHT0_10, ADC12INCH_11); /* Ideally we have A11=0->AVCC=0V ... A11=4095(2^12-1)->AVCC=4V * --> (A11/4095)*4V=AVCC --> AVCC=(A11*4)/4095 */ voltage = (voltage * 2 * 2 * 1000) / 4095; return voltage; }
// ************************************************************************************************* // @fn battery_measurement // @brief Init ADC12. Do single conversion of AVCC voltage. Turn off ADC12. // @param none // @return none // ************************************************************************************************* void battery_measurement(void) { u16 voltage; // Convert external battery voltage (ADC12INCH_11=AVCC-AVSS/2) voltage = adc12_single_conversion(REFVSEL_1, ADC12SHT0_10, ADC12INCH_11); // Convert ADC value to "x.xx V" // Ideally we have A11=0->AVCC=0V ... A11=4095(2^12-1)->AVCC=4V // --> (A11/4095)*4V=AVCC --> AVCC=(A11*4)/4095 voltage = (voltage * 2 * 2) / 41; // Correct measured voltage with calibration value voltage += sBatt.offset; // Discard values that are clearly outside the measurement range if (voltage > BATTERY_HIGH_THRESHOLD) { voltage = sBatt.voltage; } // Filter battery voltage sBatt.voltage = ((voltage * 2) + (sBatt.voltage * 8)) / 10; // If battery voltage falls below low battery threshold, set system flag and modify LINE2 // display function pointer if (sBatt.voltage < BATTERY_LOW_THRESHOLD) { sys.flag.low_battery = 1; // Set sticky battery icon display_symbol(LCD_SYMB_BATTERY, SEG_ON); } else { sys.flag.low_battery = 0; // Clear sticky battery icon display_symbol(LCD_SYMB_BATTERY, SEG_OFF); } // Update LINE2 display.flag.line2_full_update = 1; // Indicate to display function that new value is available display.flag.update_battery_voltage = 1; }