add battery voltage display on LED

This commit is contained in:
King Kévin 2016-04-04 22:10:13 +02:00
parent 3864026633
commit cb1c160753
1 changed files with 21 additions and 1 deletions

22
main.c
View File

@ -399,7 +399,7 @@ int main(void)
adc_enable_discontinuous_mode_regular(ADC1, 1); // do only one conversion per sequence
adc_enable_external_trigger_regular(ADC1, ADC_CR2_EXTSEL_SWSTART); // use software trigger to start conversion
adc_power_on(ADC1); // switch on ADC
for (uint32_t i = 0; i < 800000; i++) { // wait t_stab for the ADC to stabilize
for (uint32_t i=0; i<800000; i++) { // wait t_stab for the ADC to stabilize
__asm__("nop");
}
adc_reset_calibration(ADC1); // remove previous non-calibration
@ -422,6 +422,26 @@ int main(void)
printf("/!\\ low ");
}
printf("battery voltage: %.2fV\n", battery_voltage);
// show voltage on LEDs
if (battery_voltage<2.4) { // low battery voltage
for (uint16_t led=0; led<2; led++) { // display red on 2 LEDs
clock_leds[led*3+0] = 0xff; // set red
}
} else if (battery_voltage>3.0) { // battery full
for (uint16_t led=0; led<WS2812B_LEDS; led++) { // display green on all LEDs
clock_leds[led*3+1] = 0xff; // set green
}
} else { // intermediate batter voltage
for (uint16_t led=0; led<(uint8_t)(WS2812B_LEDS*(battery_voltage-2.4)/0.6); led++) { // display blue on proportional LEDs
clock_leds[led*3+2] = 0xff; // set blue
}
}
clock_leds_set(); // set the colors of all LEDs
ws2812b_transmit(); // transmit set color
for (uint32_t i=0; i<10000000; i++) { // display for a small while
__asm__("nop");
}
// now use interrupts to only measure ambient luminosity
channels[0] = PHOTORESISTOR_ADC_CHANNEL; // only measure ambient luminosity