diff --git a/main.c b/main.c index 0964380..525dd71 100644 --- a/main.c +++ b/main.c @@ -56,8 +56,8 @@ volatile bool photoresistor_flag = false; /**< flag set when ambient luminosity * @note I have to use type variables because defines would be stored in signed integers, leading to an overflow it later calculations * @{ */ -/** the number of ticks in one second (greater than 256*LED_WS2812B_LEDS/60) */ -#define TICKS_PER_SECOND SQUARE_WAVE_FREQUENCY/SQUARE_WAVE_TICKS +/** the number of ticks in one second (32768 divisor greater than 256*LED_WS2812B_LEDS/60) */ +#define TICKS_PER_SECOND (RTC_DS1307_SQUARE_WAVE_FREQUENCY/RTC_DS1307_SQUARE_WAVE_TICKS) /** number of ticks in one second */ const uint32_t ticks_second = TICKS_PER_SECOND; /** number of ticks in one minute */ @@ -349,15 +349,20 @@ error: int main(void) { rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use 8 MHz high speed external clock to generate 72 MHz internal clock + + // setup LED + rcc_periph_clock_enable(LED_RCC); // enable clock for LED + gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); // set LED pin to 'output push-pull' + led_off(); // switch off LED per default + + // setup USART and USB for user communication usart_setup(); // setup USART (for printing) cdcacm_setup(); // setup USB CDC ACM (for printing) setbuf(stdout, NULL); // set standard out buffer to NULL to immediately print setbuf(stderr, NULL); // set standard error buffer to NULL to immediately print - // setup LED - rcc_periph_clock_enable(LED_RCC); // enable clock for LED - gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); // set LED pin to 'output push-pull' - led_off(); // switch off LED to indicate setup started + // minimal setup ready + printf("welcome to the CuVoodoo LED clock\n"); // print welcome message // setup button #if defined(BUTTON_RCC) && defined(BUTTON_PORT) && defined(BUTTON_PIN) && defined(BUTTON_EXTI) && defined(BUTTON_IRQ) @@ -366,29 +371,30 @@ int main(void) gpio_clear(BUTTON_PORT, BUTTON_PIN); // pull down to be able to detect button push (go high) rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt exti_select_source(BUTTON_EXTI, BUTTON_PORT); // mask external interrupt of this pin only for this port - exti_set_trigger(BUTTON_EXTI, EXTI_TRIGGER_RISING); // trigger on both edge + exti_set_trigger(BUTTON_EXTI, EXTI_TRIGGER_RISING); // trigger when button is pressed exti_enable_request(BUTTON_EXTI); // enable external interrupt nvic_enable_irq(BUTTON_IRQ); // enable interrupt #endif - // setup internal RTC + // setup RTC + printf("setup RTC: "); rtc_auto_awake(RCC_LSE, 32768/ticks_second-1); // ensure RTC is on, uses the 32.678 kHz LSE, and the prescale is set to our tick speed, else update backup registers accordingly (power off the micro-controller for the change to take effect) - - // setup external RTC - rtc_setup(); // setup RTC module - - // generate gamma correction table (with fixed gamma value) - for (uint16_t i=0; i=ticks_midday*2) { // one day passed + if (rtc_ds1307_ticks>=ticks_midday*2) { // one day passed rtc_set_counter_val(rtc_get_counter_val()%ticks_midday); // reset time counter } - clock_set_time(rtc_ticks); // set time + clock_set_time(rtc_ds1307_ticks); // set time } while (photoresistor_flag) { // new photo-resistor value has been measured photoresistor_flag = false; // reset flag @@ -527,7 +529,7 @@ int main(void) } #if defined(BUTTON_ISR) && defined(BUTTON_EXTI) -/** @brief interrupt service routine called when button is pressed of released */ +/** @brief interrupt service routine called when button is pressed */ void BUTTON_ISR(void) { exti_reset_request(BUTTON_EXTI); // reset interrupt