remove battery voltage reading
This commit is contained in:
parent
60608dcaa5
commit
a82d3dc9ef
66
main.c
66
main.c
@ -69,14 +69,6 @@ const uint32_t ticks_hour = 60*60*TICKS_PER_SECOND;
|
||||
const uint32_t ticks_midday = 12*60*60*TICKS_PER_SECOND;
|
||||
/** @} */
|
||||
|
||||
/** @defgroup battery_adc ADC used to measure battery voltage
|
||||
* @{
|
||||
*/
|
||||
#define BATTERY_ADC_CHANNEL ADC_CHANNEL1 /**< ADC channel */
|
||||
#define BATTERY_PORT GPIOA /**< port on which the battery is connected */
|
||||
#define BATTERY_PORT_RCC RCC_GPIOA /**< timer port peripheral clock */
|
||||
#define BATTERY_PIN GPIO1 /**< pin of the port on which the battery is connected */
|
||||
/** @} */
|
||||
/** @defgroup photoresistor_adc ADC used to ambient luminosity
|
||||
* @{
|
||||
*/
|
||||
@ -396,9 +388,7 @@ int main(void)
|
||||
clock_leds_set(); // set the colors of all LEDs
|
||||
ws2812b_transmit(); // transmit set color
|
||||
|
||||
// setup ADC to read battery voltage
|
||||
rcc_periph_clock_enable(BATTERY_PORT_RCC); // enable clock for battery GPIO peripheral
|
||||
gpio_set_mode(BATTERY_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, BATTERY_PIN); // set battery GPIO as analogue input for the ADC
|
||||
// setup ADC to photo-resistor voltage
|
||||
rcc_periph_clock_enable(PHOTORESISTOR_PORT_RCC); // enable clock for photo-resistor GPIO peripheral
|
||||
gpio_set_mode(PHOTORESISTOR_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, PHOTORESISTOR_PIN); // set photo-resistor GPIO as analogue input for the ADC
|
||||
rcc_periph_clock_enable(RCC_ADC1); // enable clock for ADC peripheral
|
||||
@ -416,6 +406,19 @@ int main(void)
|
||||
adc_reset_calibration(ADC1); // remove previous non-calibration
|
||||
adc_calibration(ADC1); // calibrate ADC for less accuracy errors
|
||||
|
||||
// read internal reference 1.2V
|
||||
uint8_t channels[] = {ADC_CHANNEL17}; // voltages to convert
|
||||
adc_set_regular_sequence(ADC1, LENGTH(channels), channels); // set channels to convert
|
||||
adc_start_conversion_regular(ADC1); // start conversion to get first voltage of this group
|
||||
while (!adc_eoc(ADC1)); // wait until conversion finished
|
||||
uint16_t ref_value = adc_read_regular(ADC1); // read internal reference 1.2V voltage value
|
||||
|
||||
// now use interrupts to only measure ambient luminosity
|
||||
channels[0] = PHOTORESISTOR_ADC_CHANNEL; // only measure ambient luminosity
|
||||
adc_set_regular_sequence(ADC1, 1, channels); // set now group
|
||||
adc_enable_eoc_interrupt(ADC1); // enable interrupt for end of conversion
|
||||
nvic_enable_irq(NVIC_ADC1_2_IRQ); // enable ADC interrupts
|
||||
|
||||
printf("welcome to the CuVoodoo LED clock\n"); // print welcome message
|
||||
led_on(); // switch on LED to indicate setup completed
|
||||
|
||||
@ -424,47 +427,6 @@ int main(void)
|
||||
printf("/!\\ RTC oscillator is disabled: the battery may be empty\n");
|
||||
}
|
||||
|
||||
// read internal reference 1.2V and RTC battery voltages
|
||||
uint8_t channels[] = {ADC_CHANNEL17, BATTERY_ADC_CHANNEL}; // voltages to convert
|
||||
adc_set_regular_sequence(ADC1, LENGTH(channels), channels); // set channels to convert
|
||||
adc_start_conversion_regular(ADC1); // start conversion to get first voltage of this group
|
||||
while (!adc_eoc(ADC1)); // wait until conversion finished
|
||||
uint16_t ref_value = adc_read_regular(ADC1); // read internal reference 1.2V voltage value
|
||||
adc_start_conversion_regular(ADC1); // start conversion to get second voltage of this group
|
||||
while (!adc_eoc(ADC1)); // wait until conversion finished
|
||||
uint16_t battery_value = adc_read_regular(ADC1); // read converted battery voltage
|
||||
float battery_voltage = battery_value*1.2/ref_value; // calculate battery voltage
|
||||
if (battery_voltage<1.0) {
|
||||
printf("no battery detected\n");
|
||||
} else {
|
||||
if (battery_voltage<2.4) { // low battery voltage
|
||||
printf("/!\\ low ");
|
||||
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
|
||||
}
|
||||
}
|
||||
printf("battery voltage: %.2fV\n", battery_voltage);
|
||||
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
|
||||
adc_set_regular_sequence(ADC1, 1, channels); // set now group
|
||||
adc_enable_eoc_interrupt(ADC1); // enable interrupt for end of convertion
|
||||
nvic_enable_irq(NVIC_ADC1_2_IRQ); // enable ADC interrupts
|
||||
|
||||
// get date and time
|
||||
printf("current time: %02lu:%02lu:%02lu\n", rtc_get_counter_val()/ticks_hour, (rtc_get_counter_val()%ticks_hour)/ticks_minute, (rtc_get_counter_val()%ticks_minute)/ticks_second); // display time
|
||||
clock_animate_time(rtc_get_counter_val()); // set time with animation
|
||||
|
Loading…
Reference in New Issue
Block a user