本帖最后由 GrootBrain 于 2021-1-29 09:31 编辑
某些场合下,需要用单一一个PWM通道,捕获输入口的上跳变时刻或下跳变时刻。
从7801的参考手册来看,PWM单通道是可以实现双边沿捕获的。
根据参考手册做相应配置,代码如下:
- /**
- * Capture_init
- *
- * @param[in] void
- * [url=home.php?mod=space&uid=266161]@return[/url] void
- *
- * [url=home.php?mod=space&uid=247401]@brief[/url] 捕获初始化
- *
- */
- void Capture_init(void)
- {
- GPIO_SetFunc(GPIOB, GPIO_PIN3, GPIO_FUN1);
- CKGEN_Enable(CLK_PWM0, ENABLE);
- CKGEN_SoftReset(SRST_PWM0, ENABLE);
- /* Set period value */
- PWM_SetClockPrescaler(PWM0, 23);
- PWM_SetCounterInitValue((PWM0), 0);
- PWM_SetCounter((PWM0), 0);
- PWM_SetMaxCountValue((PWM0), 65535);
- PWM_SetCountMode(PWM0, PWM_UP_COUNT);
- PWM_SetClockSource(PWM0, PWM_CLK_SOURCE_APB);
- PWM_SetCallback(PWM0, CaptureIntCallback);
- NVIC_EnableIRQ(PWM0_IRQn);
- PWM_SetChannelMSR(PWM0, PWM_CH_6, 0U);
- PWM_SetChannelELSR(PWM0, PWM_CH_6, PWM_BOTH_EDGE_DETECT);
- PWM_SetCaptureEventPrescaler(PWM0, PWM_CH_6, PWM_EVENT_PSC_1);
- PWM_SetChannelInterrupt(PWM0, PWM_CH_6, DISABLE);
- }
可以设置开始捕获/关闭捕获
- /*!
- * [url=home.php?mod=space&uid=247401]@brief[/url] capture start
- *
- * @param[in]
- * modemIndex - modem index (0, 1)
- * channelIndex - channel index
- * [url=home.php?mod=space&uid=266161]@return[/url]
- * 0 - fail
- * 1 - success
- *
- */
- uint8_t CapTime_StartCapture(PWM_Type* ptrCaptureModem, uint8_t channelIndex)
- {
- memset(g_capture_buf, 0, sizeof(g_capture_buf));
- g_capEdgeCounter = 0;
- PWM_SetChannelInterrupt(ptrCaptureModem, (PWM_ChannelType)channelIndex, ENABLE);
- PWM_SetChannelELSR(ptrCaptureModem, (PWM_ChannelType)channelIndex, 0x03);
- return 1;
- }
- /*!
- * @brief capture stop
- *
- * @param[in]
- * modemIndex - modem index (0, 1)
- * channelIndex - channel index
- * @return
- * 0 - fail
- * 1 - success
- *
- */
- uint8_t CapTime_StopCapture(PWM_Type* ptrCaptureModem, uint8_t channelIndex)
- {
- PWM_SetChannelELSR(ptrCaptureModem, (PWM_ChannelType)channelIndex, 0x00);
- PWM_SetChannelInterrupt(ptrCaptureModem, (PWM_ChannelType)channelIndex, DISABLE);
- return 1;
- }
最后分享一下测试工程
pwm_capture.rar
(27.36 KB, 下载次数: 16)
|