1
完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
亲爱的大家,
我正在尝试测量使用tiM1捕获功能连接到PD4的1 kHz至2 kHz信号的频率(和占空比)。据我所知,PD4连接到TIM1.IC2,这是默认路由。我已按如下方式设置TIM1: / *初始定时器1 * / CLK_PeripheralClockConfig(CLK_Peripheral_TIM1,ENABLE); //配置TIM1通道1以捕获PWM信号 TIM1_PWMIConfig(TIM1_Channel_2,TIM1_ICPolarity_Rising,TIM1_ICSelection_DirectTI, TIM1_ICPSC_DIV8,ICFilter); //选择TIM1输入触发:TI1FP1 TIM1_SelectInputTrigger(TIM1_TRGSelection_TI1FP1); TIM1_SelectSlaveMode(TIM1_SlaveMode_Reset); //启用CC1中断请求 TIM1_ITConfig(TIM1_IT_CC2,ENABLE); //启用TIM1 TIM1_Cmd(ENABLE); enableInterrupts(); 中断处理程序执行以下代码: INTERRUPT_HANDLER(TIM1_CC_IRQHandler,24) { / *通过读取CCR1寄存器获取输入捕获值* / / * CCR1寄存器包含信号频率值* / IC1Value = TIM1_GetCapture2(); if(IC1Value!= 0) { / *通过读取CCR2寄存器来获取输入捕获值* / / * CCR2寄存器包含信号保持高电平的时间* / IC2Value = TIM1_GetCapture2(); / *占空比计算* / SignalDutyCycle =((uint32_t)IC2Value * 100)/ IC1Value; / *频率计算* / SignalFrequency =(uint32_t)(CLK_GetClockFreq()/ IC1Value); } 其他 { SignalDutyCycle = 0; SignalFrequency = 0; } / *清除TIM1捕获比较中断挂起位* / TIM1_ClearITPendingBit(TIM1_IT_CC2); TIM1_ClearFlag(TIM1_FLAG_CC1); } 当输入信号施加到PD4时,TIM1-> CCR1L,TIM1-> CCR1H,TIM1-> CCR2L和TIM1-> CCR2H中的值一直在变化,就像计数器翻转或未重置一样。有人可以帮助我更好地理解TIM1捕获功能的实现,并告诉我上面的实现有什么问题。 我还想将不同的端口路由到捕获输入,但是在使用任何值加载寄存器RI-> IC2CS时都没有成功? 感谢您的任何帮助 Wynand 以上来自于谷歌翻译 以下为原文 Dear All, I am trying to measure the frequency (and duty cycle) of a 1 kHz to 2 kHz signal which is connected to PD4 using TIM1 capture capability. As far as I can see from the documentation PD4 is connected to TIM1.IC2 which is the default routing. I have setup TIM1 as follows: /* Init TIMER 1 */ CLK_PeripheralClockConfig(CLK_Peripheral_TIM1, ENABLE); // configure TIM1 channel 1 to capture a PWM signal TIM1_PWMIConfig(TIM1_Channel_2, TIM1_ICPolarity_Rising, TIM1_ICSelection_DirectTI, TIM1_ICPSC_DIV8, ICFilter); // Select the TIM1 Input Trigger: TI1FP1 TIM1_SelectInputTrigger(TIM1_TRGSelection_TI1FP1); TIM1_SelectSlaveMode(TIM1_SlaveMode_Reset); // Enable CC1 interrupt request TIM1_ITConfig(TIM1_IT_CC2, ENABLE); // Enable TIM1 TIM1_Cmd(ENABLE); enableInterrupts(); The interrupt handler executes the following code: INTERRUPT_HANDLER(TIM1_CC_IRQHandler,24) { /* Get the Input Capture value by reading CCR1 register */ /* CCR1 regsiter contains signal frequency value */ IC1Value = TIM1_GetCapture2(); if (IC1Value != 0) { /* Get the Input Capture value by reading CCR2 register */ /* CCR2 regsiter contains how much time the signal remained at high level */ IC2Value = TIM1_GetCapture2(); /* Duty cycle computation */ SignalDutyCycle = ((uint32_t) IC2Value * 100) / IC1Value; /* Frequency computation */ SignalFrequency = (uint32_t) (CLK_GetClockFreq() / IC1Value); } else { SignalDutyCycle = 0; SignalFrequency = 0; } /* Clear TIM1 Capture compare interrupt pending bit */ TIM1_ClearITPendingBit(TIM1_IT_CC2);TIM1_ClearFlag(TIM1_FLAG_CC1); } When the input signal is applied to PD4 the values in TIM1->CCR1L, TIM1->CCR1H, TIM1->CCR2L and TIM1->CCR2H changes all the time as if the counter is rolling over or does not get reset. Could someone please help me understand the implementation of the TIM1 capture function better and also tell me what is wrong with the above implementation. I would also like to route different ports to the capture input, but was not successful in loading register RI->IC2CS with any values? Thank you for any help Wynand |
|
相关推荐
6个回答
|
|
嗨Wynand,
我不知道这是否会有所帮助,但我使用TIM2测量频率,但我不使用库调用。我已编码自己的。但是,在您的代码中,我没有看到您将计数器归零的位置。计数器不会归零。所以在我的中断函数中我做了 TIM2-> CNTRH = 0 TIM2-> CNTRL = 0 也许你需要为TIM1做类似的事情? 我希望有所帮助。 麦克风 以上来自于谷歌翻译 以下为原文 Hi Wynand, I don't know if this will help but I have used TIM2 for measuring frequency but I don't use the library calls. I have coded my own. However, in your code I don't see where you are zeroing the counters. The counters do not zero themselves. So in my interrupt function I do TIM2->CNTRH = 0 TIM2->CNTRL = 0 Perhaps you need to do something similar for TIM1? I hope that helps. Mike |
|
|
|
谢谢Mike,将计数器归零确实有所帮助。我的印象是,如果TIM1_SMCR.SMS设置为100,计数器将在触发器的上升沿重置。但它不会发生你100%正确。
感谢您的帮助。 问候 Wynand 以上来自于谷歌翻译 以下为原文 Thank you Mike, zeroing the counters did help. I was under the impression that if TIM1_SMCR.SMS is set to 100 the counter will reset on rising edge of the trigger. But it does not happen you are 100% correct. Thank you for the help. Regards Wynand |
|
|
|
很高兴我能帮忙。现在,如果有人只能帮我解决我的CAN问题。
以上来自于谷歌翻译 以下为原文 Cool glad I could help. Now if someone could only help me with my CAN problem. |
|
|
|
|
|
|
|
嗨Wynand,
我是拉马纳。我正在尝试测量PWM输入信号测量。 我打算用你的例子。你说计数器归零后你的程序正在运行。你能把你的工作代码寄给我吗?这可能对我有帮助 问候 拉玛纳 以上来自于谷歌翻译 以下为原文 Hi Wynand, I am Ramana. I am trying to measure PWM input signal measurement. I am planning to use your example. You said your program is working after zeroing the counters. Can you send me your working code. It may be helpful for me Regards Ramana |
|
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
请教:在使用UDE STK时,单片机使用SPC560D30L1,在配置文件怎么设置或选择?里面只有SPC560D40的选项
2725 浏览 1 评论
3237 浏览 1 评论
请问是否有通过UART连接的两个微处理器之间实现双向值交换的方法?
1807 浏览 1 评论
3646 浏览 6 评论
6034 浏览 21 评论
1337浏览 4评论
197浏览 3评论
对H747I-DISCO写程序时将CN2的st-link复用为usart1,再次烧录时无法检测到stlink怎么解决?
350浏览 2评论
STM32G474RE芯片只是串口发个数据就发烫严重是怎么回事?
442浏览 2评论
STM32处理增量式编码器Z信号如何判断中断是正转的还是反向转的?
273浏览 2评论
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 21:06 , Processed in 1.148112 second(s), Total 86, Slave 70 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号