Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void timer2_setup(void)
- {
- // gpio_set(GPIOD, GPIO12);
- /*Enable AF clock*/
- rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
- rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPBEN);
- /*
- * Set TIM3 channel output pins to
- * 'output alternate function push-pull'.
- */
- gpio_mode_setup(GPIOA, GPIO_MODE_AF,
- GPIO_PUPD_NONE, GPIO5);
- /*TIM3 CH3, CH4 as alternate functions*/
- gpio_set_af(GPIOA, GPIO_AF1, GPIO5);
- gpio_mode_setup(GPIOB, GPIO_MODE_AF,
- GPIO_PUPD_NONE, GPIO3|GPIO10|GPIO11);
- /*TIM3 CH3, CH4 as alternate functions*/
- gpio_set_af(GPIOB, GPIO_AF1, GPIO3|GPIO10|GPIO11);
- /* Enable TIM3 clock. */
- rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM2EN);
- setup_periodic_timer(TIM2, 15000);
- // timer_enable_counter(TIM3);
- // /* Reset TIM1 peripheral. */
- timer_reset(TIM2);
- // /* Timer global mode:
- // * - No divider
- // * - Alignment, center with mode 3* see manual for details
- // * - Direction up
- // */
- timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1, TIM_CR1_DIR_UP);
- // /* Set prescaler value for TIM1. */
- // timer_set_prescaler(TIM3, prescale);
- timer_set_oc_mode(TIM2, TIM_OC1, TIM_OCM_PWM1);
- timer_set_oc_mode(TIM2, TIM_OC2, TIM_OCM_PWM1);
- timer_set_oc_mode(TIM2, TIM_OC3, TIM_OCM_PWM1);
- timer_set_oc_mode(TIM2, TIM_OC4, TIM_OCM_PWM1);
- // /* Reenable outputs. */
- timer_enable_oc_output(TIM2, TIM_OC1);
- timer_enable_oc_output(TIM2, TIM_OC2);
- timer_enable_oc_output(TIM2, TIM_OC3);
- timer_enable_oc_output(TIM2, TIM_OC4);
- // timer_enable_break_main_output(TIM3);
- // /* ARR reload enable. */
- // timer_enable_preload(TIM3);
- // /* Pnce the configuration is done and the lines are enabled again, */
- // /* we reload the TIM3 */
- // /* Set the initial capture compare value for OC1. */
- // //this will be changed later to: initial_duty_cycle*pwm_period_ARR
- timer_set_oc_value(TIM2, TIM_OC1, 65535);
- timer_set_oc_value(TIM2, TIM_OC2, 65535);
- timer_set_oc_value(TIM2, TIM_OC3, 65535);
- timer_set_oc_value(TIM2, TIM_OC4, 65535);
- // /* Sets the Period for TIM1 PWM, named ARR. Set the defines in motordrive.c */
- // timer_set_period(TIM3,pwm_period_ARR);
- // /* Sets TIM1 Continuous mode. */
- // // timer_continuous_mode(TIM3);
- timer_enable_counter(TIM2);
- /* Enable capture/compare interrupt. */
- timer_enable_irq(TIM2, TIM_SR_UIF);
- //DO NOT ENABLE 2 timer IRQs!
- // timer_enable_irq(TIM3, TIM_DIER_CC4IE);
- nvic_enable_irq(NVIC_TIM2_IRQ);
- // nvic_set_priority(NVIC_TIM3_IRQ, 1);
- }
- void tim2_isr (void)//TIM3 CC interruption
- {
- timer_clear_flag(TIM2, TIM_SR_UIF);
- printf("Blah\r\n");
- }
- void setup_periodic_timer(uint32_t timer, unsigned int freq_in_hz)
- {
- unsigned int prescaler = 1;
- while (system_freq / prescaler / freq_in_hz > 0xffff)
- prescaler *= 2;
- timer_reset(timer);
- timer_set_prescaler(timer, prescaler-1);
- // printf("%u\r\n", system_freq / prescaler / freq_in_hz);
- timer_set_period(timer, system_freq / prescaler / freq_in_hz);
- timer_direction_down(timer);
- timer_enable_preload(timer);
- }
Advertisement
Add Comment
Please, Sign In to add comment