From: Martin Mares Date: Tue, 30 Jul 2019 21:23:47 +0000 (+0200) Subject: Timer 1 & 2 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=bd0869a2d37beebab7a4633b0e5b2687509ad69e;p=home-hw.git Timer 1 & 2 --- diff --git a/protab/timer1-clock/Makefile b/protab/timer1-clock/Makefile new file mode 100644 index 0000000..9f34630 --- /dev/null +++ b/protab/timer1-clock/Makefile @@ -0,0 +1,6 @@ +ROOT=../.. +BINARY=timer +OBJS=timer.o +LIB_OBJS= + +include $(ROOT)/mk/bluepill.mk diff --git a/protab/timer1-clock/timer.c b/protab/timer1-clock/timer.c new file mode 100644 index 0000000..1042e46 --- /dev/null +++ b/protab/timer1-clock/timer.c @@ -0,0 +1,34 @@ +#include +#include +#include + +static void delay_ms(unsigned int ms) +{ + timer_set_period(TIM3, 2*ms); + timer_enable_counter(TIM3); + while (TIM_CR1(TIM3) & TIM_CR1_CEN) + ; +} + +int main(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + rcc_periph_clock_enable(RCC_GPIOC); + rcc_periph_clock_enable(RCC_TIM3); + + // PC13 = BluePill LED + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); + + timer_set_prescaler(TIM3, 35999); // 72 MHz / 36000 = 2 kHz + timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_DOWN); + timer_one_shot_mode(TIM3); + + for (;;) { + gpio_clear(GPIOC, GPIO13); + delay_ms(100); + gpio_set(GPIOC, GPIO13); + delay_ms(500); + } + + return 0; +} diff --git a/protab/timer2-irq/Makefile b/protab/timer2-irq/Makefile new file mode 100644 index 0000000..9f34630 --- /dev/null +++ b/protab/timer2-irq/Makefile @@ -0,0 +1,6 @@ +ROOT=../.. +BINARY=timer +OBJS=timer.o +LIB_OBJS= + +include $(ROOT)/mk/bluepill.mk diff --git a/protab/timer2-irq/timer.c b/protab/timer2-irq/timer.c new file mode 100644 index 0000000..86ad521 --- /dev/null +++ b/protab/timer2-irq/timer.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +void tim3_isr(void) +{ + if (TIM_SR(TIM3) & TIM_SR_UIF) { + TIM_SR(TIM3) &= ~TIM_SR_UIF; + gpio_toggle(GPIOC, GPIO13); + } +} + +int main(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + rcc_periph_clock_enable(RCC_GPIOC); + rcc_periph_clock_enable(RCC_TIM3); + + // PC13 = BluePill LED + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); + + timer_set_prescaler(TIM3, 35999); // 72 MHz / 36000 = 2 kHz + timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); + timer_set_period(TIM3, 199); // 200 ticks = 100 ms + timer_update_on_overflow(TIM3); + timer_enable_irq(TIM3, TIM_DIER_UIE); + nvic_enable_irq(NVIC_TIM3_IRQ); + timer_enable_counter(TIM3); + + for (;;) { + asm volatile ("wfi"); + } + + return 0; +}