0

我正在尝试让 PWM 与计时器 3 一起工作,我已按照参考手册进行操作,并相信我一切都正确,但我在输出上一无所获。

我的代码在下面,我一定遗漏了一些东西

rcc_periph_clock_enable(RCC_GPIOB);
  gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
                  GPIO0 | GPIO4 | GPIO5);
  gpio_set_output_options(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,
                          GPIO0 | GPIO4 | GPIO5);
  gpio_set_af(GPIOB, GPIO_AF2, GPIO0 | GPIO4 | GPIO5);

  rcc_periph_clock_enable(RCC_TIM3);
  timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
  timer_set_prescaler(TIM3, 50000000 / 125000 - 1);

  timer_enable_preload(TIM3);
  timer_enable_oc_preload(TIM3, TIM_OC1);

  timer_set_period(TIM3, 0xffff);

  timer_enable_oc_output(TIM3, TIM_OC1);
  timer_set_oc_mode(TIM3, TIM_OC1, TIM_OCM_PWM2);
  // timer_set_oc_mode(TIM3, TIM_OC2, TIM_OCM_PWM2);
  // timer_set_oc_mode(TIM3, TIM_OC3, TIM_OCM_PWM2);

  timer_set_oc_value(TIM3, TIM_OC1, 0x7fff);
  timer_enable_update_event(TIM3);
  timer_generate_event(TIM3, TIM_EGR_UG);
  timer_enable_counter(TIM3);
4

0 回答 0