diff --git a/main.c b/main.c index 14dba91..9394e5a 100644 --- a/main.c +++ b/main.c @@ -27,7 +27,8 @@ #include // vector table definition #include // Cortex M3 utilities -#include // SPI library +#include +#include /* own libraries */ #include "main.h" // board definitions @@ -64,6 +65,31 @@ static void gpio_setup(void) gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); // set LED pin to 'output push-pull' } +static void timer_setup(void) +{ +#define VFD_TIMER TIM2 +#define VFD_TIMER_RCC RCC_TIM2 +#define VFD_TIMER_IRQ NVIC_TIM2_IRQ + + rcc_periph_clock_enable(VFD_TIMER_RCC); // enable clock for timer block + timer_reset(VFD_TIMER); // reset timer state + timer_set_mode(VFD_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock,edge alignment (simple count), and count up + timer_set_prescaler(VFD_TIMER, (SYSTEM_CLOCK_FREQ/(1<<16))-1); // set the prescaler so this 16 bits timer overflows at 1Hz + //timer_set_period(0xffff/(VFD_DIGITS+VFD_MATRIX)/150); // set the refresh frequency + timer_set_period(VFD_TIMER, 0xffff); + timer_enable_irq(VFD_TIMER, TIM_DIER_CC1IE); // enable interrupt for timer + nvic_enable_irq(VFD_TIMER_IRQ); // allow interrupt for timer + timer_enable_counter(VFD_TIMER); // enable timer +} + +void tim2_isr(void) +{ + if (timer_get_flag(VFD_TIMER, TIM_SR_UIF)) { + timer_clear_flag(VFD_TIMER, TIM_SR_UIF); + gpio_toggle(LED_PORT, LED_PIN); + } +} + int main(void) { SCB_VTOR = (uint32_t) 0x08002000; // relocate vector table because of the bootloader @@ -73,6 +99,7 @@ int main(void) usart_setup(); // setup USART (for printing) cdcacm_setup(); // setup USB CDC ACM (for printing) vfd_setup(); // setup VFD + timer_setup(); setbuf(stdout, NULL); // set standard out buffer to NULL to immediately print setbuf(stderr, NULL); // set standard error buffer to NULL to immediately print diff --git a/main.h b/main.h index 6d2e1eb..a3d44ab 100644 --- a/main.h +++ b/main.h @@ -14,6 +14,9 @@ */ /* Copyright (c) 2016 King Kévin */ +/* system clock frequency in Hz */ +#define SYSTEM_CLOCK_FREQ 72000000 + /* LED is on pin 11/PA1 */ #define LED_RCC RCC_GPIOA #define LED_PORT GPIOA