use 4096 Hz square wave

This commit is contained in:
King Kévin 2016-03-25 20:24:46 +01:00
parent d674488e2a
commit eb8094f2fb
1 changed files with 29 additions and 12 deletions

41
main.c
View File

@ -50,7 +50,7 @@ volatile bool button_flag = false; /**< flag set if board user button has been p
/** @} */
#define TICKS_PER_SECOND 256 /**< the number of ticks in one second */
#define SQUARE_WAVE_FREQUENCY 1 /**< square wave output frequency from the RTC IC */
#define SQUARE_WAVE_FREQUENCY 4096 /**< square wave output frequency from the RTC IC */
/** @defgroup main_ticks ticks per time units
* @note these are derived from TICKS_PER_SECOND
* @note I have to use type variables because defines would be stored in signed integers, leading to an overflow it later calculations
@ -223,10 +223,22 @@ static void leds_set(void)
}
/** @brief set the time on the LEDs
* @details this will have an animation where time is incremented until it reaches the provided time
* @param[in] time time to set
*/
static void clock_set_time(uint32_t time)
{
led_off();
clock_show_time(time); // set time
leds_set(); // set the colors of all LEDs
ws2812b_transmit(); // transmit set color
led_on();
}
/** @brief incrementally set the time on the LEDs
* @details this will have an animation where time is incremented until it reaches the provided time
* @param[in] time time to set
*/
static void clock_animate_time(uint32_t time)
{
static uint32_t display_time = 0; // the time to display
while (display_time<time) {
@ -239,9 +251,7 @@ static void clock_set_time(uint32_t time)
} else { // finally set time
display_time = time;
}
clock_show_time(display_time); // set time (progress)
leds_set(); // set the colors of all LEDs
ws2812b_transmit(); // transmit set color
clock_set_time(display_time); // set time (progress)
// delay some time for the animation
for (uint32_t i=0; i<400000; i++) {
__asm__("nop");
@ -297,10 +307,11 @@ int main(void)
printf("/!\\ RTC oscillator is disabled\n");
}
// get date
// get date and time
uint16_t* rtc_time = rtc_read_time(); // get RTC time/date
uint32_t current_time = rtc_time[2]*ticks_hour+rtc_time[1]*ticks_minute+rtc_time[0]*ticks_second; // the current time
printf("current date: %04d-%02d-%02d %02d:%02d:%02d\n", rtc_time[6], rtc_time[5], rtc_time[4], rtc_time[2], rtc_time[1], rtc_time[0]);
uint32_t current_time = rtc_time[2]*ticks_hour+rtc_time[1]*ticks_minute+rtc_time[0]*ticks_second; // the current time
clock_animate_time(current_time); // set time with animation
//rtc_write_time(0,52,9,4,23,3,2016);
//rtc_oscillator_enable();
@ -310,6 +321,7 @@ int main(void)
button_flag = false; // reset button flag
char c = ' '; // to store received character
bool char_flag = false; // a new character has been received
uint16_t square_wave_prescale = 0; // prescale the square wave output to generate a tick
while (true) { // infinite loop
while (usart_received) { // data received over UART
action = true; // action has been performed
@ -336,11 +348,16 @@ int main(void)
while (square_wave_flag) { // time passed
square_wave_flag = false; // reset flag
action = true; // action has been performed
led_toggle();
rtc_time = rtc_read_time(); // get RTC time/date
current_time = rtc_time[2]*ticks_hour+rtc_time[1]*ticks_minute+rtc_time[0]*ticks_second; // calculate current time
clock_set_time(current_time); // set time
printf("it is now %02lu:%02lu:%02lu\n", current_time/ticks_hour, (current_time%ticks_hour)/ticks_minute, (current_time%ticks_minute)/ticks_second); // display time
square_wave_prescale = (square_wave_prescale+1)%(SQUARE_WAVE_FREQUENCY/TICKS_PER_SECOND); // increment pre-scale counter
if (square_wave_prescale==0) { // pre-scale completed
current_time++; // increment time
if ((current_time%ticks_second)==0) { // sync on seconds
//rtc_time = rtc_read_time(); // get RTC time/date
//current_time = rtc_time[2]*ticks_hour+rtc_time[1]*ticks_minute+rtc_time[0]*ticks_second; // calculate current time
printf("it is now %02lu:%02lu:%02lu\n", current_time/ticks_hour, (current_time%ticks_hour)/ticks_minute, (current_time%ticks_minute)/ticks_second); // display time
}
clock_set_time(current_time); // set time
}
}
if (action) { // go to sleep if nothing had to be done, else recheck for activity
action = false;