From a82d3dc9ef1dc015887ee75ec742ddfc6d33d095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sat, 30 Apr 2016 12:18:32 +0200 Subject: [PATCH] remove battery voltage reading --- main.c | 66 +++++++++++++--------------------------------------------- 1 file changed, 14 insertions(+), 52 deletions(-) diff --git a/main.c b/main.c index c92e8db..50e4863 100644 --- a/main.c +++ b/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