diff --git a/main.h b/global.h similarity index 99% rename from main.h rename to global.h index a3d44ab..297b35a 100644 --- a/main.h +++ b/global.h @@ -24,3 +24,4 @@ /* default output (i.e. for printf) */ int _write(int file, char *ptr, int len); + diff --git a/lib/vfd.c b/lib/vfd.c index f1e66f8..d1816c5 100644 --- a/lib/vfd.c +++ b/lib/vfd.c @@ -24,9 +24,11 @@ #include // real-time control clock library #include // general purpose input output library #include // SPI library +#include // timer library #include // interrupt handler -#include "vfd.h" // VFD definitions +#include "global.h" // global definitions +#include "vfd.h" // VFD library API /* get the length of an array */ #define LENGTH(x) (sizeof(x) / sizeof((x)[0])) @@ -60,6 +62,21 @@ /* data input, where the data is shifted to * drive using SPI MOSI (PA7) */ #define VFD_DIN GPIO_SPI1_MOSI +/* timer for automatic refresh */ +#define VFD_TIMER TIM2 +#if (VFD_TIMER==TIM2) +#define VFD_TIMER_RCC RCC_TIM2 +#define VFD_TIMER_IRQ NVIC_TIM2_IRQ +#elif (VFD_TIMER==TIM3) +#define VFD_TIMER_RCC RCC_TIM3 +#define VFD_TIMER_IRQ NVIC_TIM3_IRQ +#elif (VFD_TIMER==TIM4) +#define VFD_TIMER_RCC RCC_TIM4 +#define VFD_TIMER_IRQ NVIC_TIM4_IRQ +#elif (VFD_TIMER==TIM5) +#define VFD_TIMER_RCC RCC_TIM5 +#define VFD_TIMER_IRQ NVIC_TIM5_IRQ +#endif /* ASCII characters encoded for 7 segments display * starts with space @@ -463,6 +480,7 @@ void vfd_off(void) /* setup VFD */ void vfd_setup(void) { + /* setup GPIO to control the VFD */ rcc_periph_clock_enable(VFD_PORT_RCC); // enable clock for VFD GPIO gpio_set_mode(VFD_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, VFD_STR); // set VFD pin to output push-pull gpio_set_mode(VFD_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, VFD_NLE); // set VFD pin to output push-pull @@ -470,10 +488,11 @@ void vfd_setup(void) gpio_set(VFD_PORT, VFD_STR); // disable HV output gpio_clear(VFD_PORT, VFD_NLE); // do not output latched data + /* setup SPI to transmit data */ rcc_periph_clock_enable(VFD_SPI_RCC); // enable SPI clock gpio_set_mode(VFD_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, VFD_CLK); // set VFD pin to alternative function push-pull gpio_set_mode(VFD_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, VFD_DIN); // set VFD pin to alternative function push-pull - + spi_reset(VFD_SPI); // clear SPI values /* set SPI: * - use VFD_SPI port @@ -497,7 +516,18 @@ void vfd_setup(void) spi_enable_tx_buffer_empty_interrupt(VFD_SPI); // enable TX empty interrupt nvic_enable_irq(VFD_SPI_IRQ); // enable SPI interrupt - + + /* setup timer to refresh display */ + 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, 0xfffff); + timer_enable_irq(VFD_TIMER, TIM_DIER_UIE); // enable interrupt for timer + nvic_enable_irq(VFD_TIMER_IRQ); // allow interrupt for timer + timer_enable_counter(VFD_TIMER); // enable timer + vfd_clear(); // initialize values } @@ -516,3 +546,20 @@ void spi2_isr(void) } } } + +#if (VFD_TIMER==TIM2) +void tim2_isr(void) +#elif (VFD_TIMER==TIM3) +void tim3_isr(void) +#elif (VFD_TIMER==TIM4) +void tim4_isr(void) +#elif (VFD_TIMER==TIM5) +void tim5_isr(void) +#endif +{ + if (timer_get_flag(VFD_TIMER, TIM_SR_UIF)) { // overflow even happened + timer_clear_flag(VFD_TIMER, TIM_SR_UIF); // clear flag + gpio_toggle(LED_PORT, LED_PIN); // toggle LED + } + //gpio_toggle(LED_PORT, LED_PIN); // toggle LED +} diff --git a/main.c b/main.c index 9394e5a..8e7b13e 100644 --- a/main.c +++ b/main.c @@ -31,7 +31,7 @@ #include /* own libraries */ -#include "main.h" // board definitions +#include "global.h" // board definitions #include "usart.h" // USART utilities #include "usb_cdcacm.h" // USB CDC ACM utilities #include "vfd.h" // VFD driver @@ -65,31 +65,6 @@ 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 @@ -99,7 +74,6 @@ 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