[资源分享] 7801 PWM模块单一通道实现双边沿捕获

[复制链接]
 楼主| GrootBrain 发表于 2021-1-28 20:13 | 显示全部楼层 |阅读模式
本帖最后由 GrootBrain 于 2021-1-29 09:31 编辑

某些场合下,需要用单一一个PWM通道,捕获输入口的上跳变时刻或下跳变时刻。
从7801的参考手册来看,PWM单通道是可以实现双边沿捕获的。
1611883822(1).png
根据参考手册做相应配置,代码如下:
  1. /**
  2. * Capture_init
  3. *
  4. * @param[in] void
  5. * [url=home.php?mod=space&uid=266161]@return[/url] void
  6. *
  7. * [url=home.php?mod=space&uid=247401]@brief[/url] 捕获初始化
  8. *
  9. */
  10. void Capture_init(void)
  11. {
  12. GPIO_SetFunc(GPIOB, GPIO_PIN3, GPIO_FUN1);


  13. CKGEN_Enable(CLK_PWM0, ENABLE);
  14. CKGEN_SoftReset(SRST_PWM0, ENABLE);

  15. /* Set period value */
  16. PWM_SetClockPrescaler(PWM0, 23);
  17. PWM_SetCounterInitValue((PWM0), 0);
  18. PWM_SetCounter((PWM0), 0);
  19. PWM_SetMaxCountValue((PWM0), 65535);

  20. PWM_SetCountMode(PWM0, PWM_UP_COUNT);

  21. PWM_SetClockSource(PWM0, PWM_CLK_SOURCE_APB);

  22. PWM_SetCallback(PWM0, CaptureIntCallback);
  23. NVIC_EnableIRQ(PWM0_IRQn);


  24. PWM_SetChannelMSR(PWM0, PWM_CH_6, 0U);
  25. PWM_SetChannelELSR(PWM0, PWM_CH_6, PWM_BOTH_EDGE_DETECT);
  26. PWM_SetCaptureEventPrescaler(PWM0, PWM_CH_6, PWM_EVENT_PSC_1);
  27. PWM_SetChannelInterrupt(PWM0, PWM_CH_6, DISABLE);
  28. }
可以设置开始捕获/关闭捕获

  1. /*!
  2. * [url=home.php?mod=space&uid=247401]@brief[/url] capture start
  3. *
  4. * @param[in]
  5. * modemIndex - modem index (0, 1)
  6. * channelIndex - channel index
  7. * [url=home.php?mod=space&uid=266161]@return[/url]
  8. * 0 - fail
  9. * 1 - success
  10. *
  11. */
  12. uint8_t CapTime_StartCapture(PWM_Type* ptrCaptureModem, uint8_t channelIndex)
  13. {
  14. memset(g_capture_buf, 0, sizeof(g_capture_buf));
  15. g_capEdgeCounter = 0;
  16. PWM_SetChannelInterrupt(ptrCaptureModem, (PWM_ChannelType)channelIndex, ENABLE);
  17. PWM_SetChannelELSR(ptrCaptureModem, (PWM_ChannelType)channelIndex, 0x03);

  18. return 1;
  19. }


  20. /*!
  21. * @brief capture stop
  22. *
  23. * @param[in]
  24. * modemIndex - modem index (0, 1)
  25. * channelIndex - channel index
  26. * @return
  27. * 0 - fail
  28. * 1 - success
  29. *
  30. */
  31. uint8_t CapTime_StopCapture(PWM_Type* ptrCaptureModem, uint8_t channelIndex)
  32. {
  33. PWM_SetChannelELSR(ptrCaptureModem, (PWM_ChannelType)channelIndex, 0x00);
  34. PWM_SetChannelInterrupt(ptrCaptureModem, (PWM_ChannelType)channelIndex, DISABLE);

  35. return 1;
  36. }
最后分享一下测试工程 pwm_capture.rar (27.36 KB, 下载次数: 16)

您需要登录后才可以回帖 登录 | 注册

本版积分规则

17

主题

93

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部