TheLegace

Untitled

May 11th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. void timer2_setup(void)
  2. {
  3.   // gpio_set(GPIOD, GPIO12);
  4.   /*Enable AF clock*/
  5.   rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
  6.   rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPBEN);
  7.   /*
  8.    * Set TIM3 channel output pins to
  9.    * 'output alternate function push-pull'.
  10.    */
  11.   gpio_mode_setup(GPIOA, GPIO_MODE_AF,
  12.                 GPIO_PUPD_NONE, GPIO5);
  13.   /*TIM3 CH3, CH4 as alternate functions*/
  14.   gpio_set_af(GPIOA, GPIO_AF1, GPIO5);
  15.  
  16.   gpio_mode_setup(GPIOB, GPIO_MODE_AF,
  17.                 GPIO_PUPD_NONE, GPIO3|GPIO10|GPIO11);
  18.   /*TIM3 CH3, CH4 as alternate functions*/
  19.   gpio_set_af(GPIOB, GPIO_AF1, GPIO3|GPIO10|GPIO11);
  20.  
  21.   /* Enable TIM3 clock. */
  22.   rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM2EN);
  23.   setup_periodic_timer(TIM2, 15000);
  24.   // timer_enable_counter(TIM3);
  25.   // /* Reset TIM1 peripheral. */
  26.   timer_reset(TIM2);
  27.   // /* Timer global mode:
  28.   //  * - No divider
  29.   //  * - Alignment, center with mode 3* see manual for details
  30.   //  * - Direction up
  31.   //  */
  32.   timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1, TIM_CR1_DIR_UP);
  33.  
  34.   // /* Set prescaler value for TIM1. */
  35.   // timer_set_prescaler(TIM3, prescale);
  36.  
  37.   timer_set_oc_mode(TIM2, TIM_OC1, TIM_OCM_PWM1);
  38.   timer_set_oc_mode(TIM2, TIM_OC2, TIM_OCM_PWM1);
  39.   timer_set_oc_mode(TIM2, TIM_OC3, TIM_OCM_PWM1);
  40.   timer_set_oc_mode(TIM2, TIM_OC4, TIM_OCM_PWM1);  
  41.   // /* Reenable outputs. */
  42.   timer_enable_oc_output(TIM2, TIM_OC1);
  43.   timer_enable_oc_output(TIM2, TIM_OC2);
  44.   timer_enable_oc_output(TIM2, TIM_OC3);
  45.   timer_enable_oc_output(TIM2, TIM_OC4);  
  46.   // timer_enable_break_main_output(TIM3);
  47.   // /* ARR reload enable. */
  48.   // timer_enable_preload(TIM3);
  49.   // /* Pnce the configuration is done and the lines are enabled again, */
  50.   // /* we reload the TIM3 */
  51.  
  52.   // /* Set the initial capture compare value for OC1. */
  53.   // //this will be changed later to: initial_duty_cycle*pwm_period_ARR
  54.   timer_set_oc_value(TIM2, TIM_OC1, 65535);
  55.   timer_set_oc_value(TIM2, TIM_OC2, 65535);
  56.   timer_set_oc_value(TIM2, TIM_OC3, 65535);
  57.   timer_set_oc_value(TIM2, TIM_OC4, 65535);
  58.   // /* Sets the Period for TIM1 PWM, named ARR. Set the defines in motordrive.c */
  59.   // timer_set_period(TIM3,pwm_period_ARR);
  60.   // /* Sets TIM1 Continuous mode. */
  61.   // // timer_continuous_mode(TIM3);
  62.   timer_enable_counter(TIM2);
  63.  
  64.   /* Enable capture/compare interrupt. */
  65.   timer_enable_irq(TIM2, TIM_SR_UIF);
  66.   //DO NOT ENABLE 2 timer IRQs!
  67.   // timer_enable_irq(TIM3, TIM_DIER_CC4IE);
  68.  
  69.   nvic_enable_irq(NVIC_TIM2_IRQ);
  70.   // nvic_set_priority(NVIC_TIM3_IRQ, 1);
  71.  
  72. }
  73.  
  74. void tim2_isr (void)//TIM3 CC interruption
  75. {
  76.   timer_clear_flag(TIM2, TIM_SR_UIF);
  77.   printf("Blah\r\n");
  78. }
  79.  
  80. void setup_periodic_timer(uint32_t timer, unsigned int freq_in_hz)
  81. {
  82.     unsigned int prescaler = 1;
  83.     while (system_freq / prescaler / freq_in_hz > 0xffff)
  84.         prescaler *= 2;
  85.     timer_reset(timer);
  86.     timer_set_prescaler(timer, prescaler-1);
  87.     // printf("%u\r\n", system_freq / prescaler / freq_in_hz);
  88.     timer_set_period(timer, system_freq / prescaler / freq_in_hz);
  89.     timer_direction_down(timer);
  90.     timer_enable_preload(timer);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment