完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我有一个围绕STM8S105构建的嵌入式系统,除其他外,它必须测量脉冲输入的频率。脉冲在引脚29(PD4 / tiM2 CH1)上进入,每秒0到大约1200个脉冲。以下是我对定时器和中断功能的初始化。我知道中断是以输入速率发生的,因为我用示波器和中断功能中的测试输出进行了检查。但是当我在中断函数中读取计数器寄存器时,它总是不同的,几乎是随机的。看起来计数器永远不会重置,但我不知道为什么。我知道它正在计数输入脉冲,因为当我停止输入时,中断停止。请看看我的init和中断函数,并告诉我我做错了什么。
谢谢 我的初始化函数: 我的中断功能: #tim2#stm8 #capture 以上来自于谷歌翻译 以下为原文 I have an embedded system built around a STM8S105 which, among other things, must measure the frequency of a pulsed input. The pulses are coming in on pin 29 (PD4/TIM2 CH1) anywhere from 0 to about 1200 pulses per second. What follows is my initialization of the timer and the interrupt function. I know the interrupts are happening at the input rate because I checked with an oscilloscope and a test output in the interrupt function. But when I read the counter register in the interrupt function, it is always different, almost random. It looks like the counter never resets but I don't know why. I know it is counting input pulses because when I stop the input, the interrupts stop. Please take a look at my init and interrupt functions and tell me what I am doing wrong. Thanks My initialize function:
My interrupt function:
#tim2 #stm8 #capture |
|
相关推荐
7个回答
|
|
我认为最好在中断开始时禁用定时器,重置其值,然后再次启用它。我不确定计时器的计数器值是否正确写入。
或者,您可以在从模式复位中使用Timer1触发控制器,以便它自动重启。 以上来自于谷歌翻译 以下为原文 I think it is better to disable the timer at the beginning of the interrupt, resetting its value, and then enabling it again. I'm not sure if the timer's counter value is correctly written otherwise. Alternatively, you could use Timer1 trigger controller in slave mode reset, so that it restarts automatically. |
|
|
|
madroadbiker,
我想你应该阅读TIM2-> CCR1如下: 如果使用u16变量,编译器可能会使用16位宽的读取,这可能会产生意外的副作用。 问候, EtaPhi 以上来自于谷歌翻译 以下为原文 madroadbiker, I think you should read TIM2->CCR1 as follows:
If you use a u16 variable, the compiler may use a 16 bit wide read which may have unexpected side effects. Regards, EtaPhi |
|
|
|
感谢您的回复。禁用然后重新启用将导致输入计数丢失。这个项目上的Timer1都用完了。
以上来自于谷歌翻译 以下为原文 Thanks for your reply. Disabling then re-enabling will cause a loss of counts on the input. Timer1 on this project is all used up. |
|
|
|
感谢您的回复。你有一个有效的点约16比8读取。所以我把中断函数改为:
然而它仍然看起来很奇怪。现在,它不是随机结果,而是倒计时然后重新计算。真奇怪。我最后读取低字节的原因是,根据RM0016参考手册,寄存器SR1的CC1位由CCR1L的软件读取清零。但我尝试交换字节读取顺序,但结果没有变化。 也许有人可以在他们的系统上实现这个算法并尝试一下? 以上来自于谷歌翻译 以下为原文 Thanks for your reply. You have a valid point about 16 vs 8 bit reads. So I changed the interrupt function to:
However it still looks odd. Now instead of random results, it looks like it counts down then back up. Really weird. And the reason I read the low byte last is because, according to the RM0016 reference manual, the CC1 bit of register SR1 is cleared by a software read of CCR1L. But I tried swapping the byte read order anyway with no change in results. Maybe someone can implement this algorithm on their system and try it out? |
|
|
|
计数丢失取决于计数器的速度和测量脉冲的速度。
你说1200 Hz因此定时器频率为12kHz或120 kHz,对于大多数应用来说都是足够的。然后,如果你的MCU是16 MHz,那么两个指令就是花生。 你真的尝试过吗?即使您不想这样做,看看它是否会改变问题也很有帮助。 以上来自于谷歌翻译 以下为原文 Loss of counts depends on the speed of your counter and the speed of the measured pulses. You said 1200 Hz so having the timer frequency at 12kHz or 120 kHz will be plenty for most applications. Then, if your MCU is at 16 MHz, two instructions are peanuts. Have you actually tried it? Even if you don't want to do it that way, it is helpful to see if it changes the problem. |
|
|
|
点好了。所以我尝试了它仍然没有改变。
以上来自于谷歌翻译 以下为原文 Point well taken. So I tried it and still no change. |
|
|
|
对所有人。我终于意识到我的愚蠢错误。我正在设置ARR寄存器并将PSCR寄存器保留为默认值。我应该设置PSCR并单独留下ARR。我倒退了。现在我的init看起来像这样,它工作正常。
感谢大家的投入。 以上来自于谷歌翻译 以下为原文 To all. I finally realized my dumb mistake. I was setting the ARR register and leaving the PSCR register at its default. I should have been setting PSCR and leaving ARR alone. I had it backwards. Now my init looks like this and it works fine. Thanks to all for your input.
|
|
|
|
只有小组成员才能发言,加入小组>>
请教:在使用UDE STK时,单片机使用SPC560D30L1,在配置文件怎么设置或选择?里面只有SPC560D40的选项
2258 浏览 1 评论
3037 浏览 1 评论
请问是否有通过UART连接的两个微处理器之间实现双向值交换的方法?
1613 浏览 1 评论
3427 浏览 6 评论
5763 浏览 21 评论
773浏览 4评论
1140浏览 4评论
在Linux上安装Atollic TRUEStudio的步骤有哪些呢?
409浏览 3评论
使用DMA激活某些外设会以导致外设无法工作的方式生成代码是怎么回事
1119浏览 3评论
1172浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-8-16 22:19 , Processed in 1.174656 second(s), Total 88, Slave 72 queries .
Powered by 电子发烧友网
© 2015 www.ws-dc.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号