]> mj.ucw.cz Git - home-hw.git/blob - protab/timer1-clock/timer.c
Merge branch 'master' of ssh://git.ucw.cz/home/mj/GIT/home-hw
[home-hw.git] / protab / timer1-clock / timer.c
1 #include <libopencm3/stm32/rcc.h>
2 #include <libopencm3/stm32/gpio.h>
3 #include <libopencm3/stm32/timer.h>
4
5 static void delay_ms(unsigned int ms)
6 {
7         timer_set_period(TIM3, 2*ms);
8         timer_enable_counter(TIM3);
9         while (TIM_CR1(TIM3) & TIM_CR1_CEN)
10                 ;
11 }
12
13 int main(void)
14 {
15         rcc_clock_setup_in_hse_8mhz_out_72mhz();
16         rcc_periph_clock_enable(RCC_GPIOC);
17         rcc_periph_clock_enable(RCC_TIM3);
18
19         // PC13 = BluePill LED
20         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
21
22         timer_set_prescaler(TIM3, 35999);       // 72 MHz / 36000 = 2 kHz
23         timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_DOWN);
24         timer_one_shot_mode(TIM3);
25
26         for (;;) {
27                 gpio_clear(GPIOC, GPIO13);
28                 delay_ms(100);
29                 gpio_set(GPIOC, GPIO13);
30                 delay_ms(500);
31         }
32
33         return 0;
34 }