完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
嗨,在ARDUNO中有一个名为MICROSH()的函数,它从程序开始运行起,计算微秒的时间。我一直在尝试使用PIC18F45来使用time0,我使用了一个64 MHz的晶体,1:2的预分频器,8位的模式,和定义的TMR0=248,每次中断被调用。根据我的数学,这会给我1个,直到溢出。由于某种原因,我的程序一直被困在中断中。我创建了ANIF(计数-lSTyTime≫10)-&;在我的while循环中切换PORTB,但是使用.COF文件在proteus ISIS中模拟它,我发现我的程序没有运行切换部分,它一遍又一遍地调用中断。你知道为什么吗?另外,还有别的办法吗?我需要运行一系列指令每22个美国(44100赫兹音频采样),所以我真的需要一个解决方案,以微秒的时间计数,即使这意味着使用另一个PIC设备!提前感谢!
以上来自于百度翻译 以下为原文 Hi. There's a function named micros() in Arduino that counts time in microseconds since The program started running. I've been trying to do that using timer0 with pic18f4550, I've used a 64 MHz Crystal, 1:2 prescaler, 8 bit mode, and defined TMR0 = 248 every time the interrupt is called. Acording to my math, that would give me 1 us until The overflow. For some reason, my program is stuck in The interrupt The whole time. I've created an If(count - last_time > 10) -> toggle PORTB in my while loop, but simulating it in proteus Isis using The .cof file, I've found out that my program is not running The toggle part, it's calling The interrupt over and over again. Do you have any Idea why? Also, is there another way to do that? I Need to run a series of instructions every 22 us ( 44100 Hz audio sampling ), so I really Need a solution to count time in microseconds, even If that means using another pic device! Thanks in advance! |
|
相关推荐
7个回答
|
|
如果将定时器设置为248,它将在7个计数(14个指令与1:2预分频器)中溢出。如果在C中编程,这只是编译器进入或退出中断所需的时间的一小部分。所以,即使你正确地清理IF标志,下一个中断也会发生在你甚至中断之前。不仅没有时间进行主循环,而且恐怕你得到的ISR呼叫要比你想要的少很多。
以上来自于百度翻译 以下为原文 If you set timer to 248, it'll overflow in 7 counts (14 instructions with 1:2 prescaler). If you program in C, this is only a fraction of time which the compiler takes to even enter or leave the interrupt. So, even if you properly clean the IF flag, the next interrupt happens before you even leave the interrupt. Not only there's no time for the main loop, but I'm afraid you're getting much less ISR calls than you want. |
|
|
|
使用TMR2做这样的事情比较容易,因为它有一个PR2寄存器,它可以自动为你重新加载。用一个64 MHz的时钟,你只在16MHz执行指令,这是每微秒16个指令。这不足以进入ISR例程,做上下文保存,做一些W。ORK,做一个上下文恢复。这就是为什么你永远不会中断。我建议你都改变TMR2(使ISR更简单),并减少你的时间分辨率有点,因为你仍然会花费大部分的CPU周期内的中断服务。
以上来自于百度翻译 以下为原文 It's easier to do this sort of thing with TMR2, as it has a PR2 register that does the reloading for you automatically. With a 64MHz clock, you're only executing instructions at 16MHz, that's 16 instructions per microsecond. That's not long enough to enter an ISR routine, do a context save, do some work, and do a context restore. That is why you are never getting out of the interrupt. I would advise that you both change to TMR2 (to make the ISR simpler), and reduce your timing resolution a bit, as you will still be spending the majority of your CPU cycles inside the interrupt service. |
|
|
|
另一个选择是设置一个预调用器,使计时器在微秒内计数。然后,在翻转中,您将添加256到32位计数器(或者您可能需要设置标志并在main中执行)。当需要时,你将把这两个词组合成一个32位的单词。不要假定所有的ARDUIO特性都是简单的,或者简单地在其他CPU上实现。
以上来自于百度翻译 以下为原文 Another option is to set a the prescaller so the timer so it counts in micro seconds. Then then in the rollover you would add 256 to your 32bit counter( or you may need to set a flag and do it in main). You would combine the two to make a 32 bit word when it was needed. Do not assume the all Arduino features are simple, or simple to implement on other CPUs. |
|
|
|
每22微秒就需要一次中断。不是每一个,所以TMR0= 80可以满足你的需要。
以上来自于百度翻译 以下为原文 you need an interrupt every 22 microsec. not every 1. so TMR0=80 might meet your needs |
|
|
|
|
|
|
|
同意。我没有读最后一段,因为第一部分提出的建议太愚蠢了。
以上来自于百度翻译 以下为原文 Agreed. I didn't read the last paragraph, as what was proposed in the first section was so silly. |
|
|
|
谢谢大家的回复。我想把我的时间分辨率降低到22,然后使用Time2来代替。这样做之后,我会带来一些更新。
以上来自于百度翻译 以下为原文 Thank You guys for The responses. Gonna be trying to reduce my time resolution to 22 us and use timer2 instead. After doing that, I'll bring some updates |
|
|
|
只有小组成员才能发言,加入小组>>
5204 浏览 9 评论
2016 浏览 8 评论
1942 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3188 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2244 浏览 5 评论
754浏览 1评论
641浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
549浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
653浏览 0评论
553浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-5 08:21 , Processed in 1.383334 second(s), Total 90, Slave 74 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号