完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我在使用一个PIC24FJ128GA202的定制板上使用MCC,我不得不说我对目前的结果印象深刻。我用MCC添加了TMR4,并选中了“启用定时器中断”框;它每秒触发16次,我打算用它来做家务,比如轮询键盘。生成的代码如下,tmr4.c还包括一个回调函数。我的问题是,无论是在主ISR例程中还是在回调函数中添加代码,都有什么区别?
以上来自于百度翻译 以下为原文 I'm using MCC on a custom board that uses a PIC24FJ128GA202, I have to say that I'm VERY impressed with the results so far. I've added TMR4 using MCC and checked the 'Enable timer Interrupt' box; It triggers 16 times per second and I'm planning on using it to do house keeping stuff like polling a keypad. The code that's generated is as follows void __attribute__ ( ( interrupt, no_auto_psv ) ) _T4Interrupt ( ) { /* Check if the Timer Interrupt/Status is set */ //***User Area Begin // ticker function call; // ticker is 1 -> Callback function gets called everytime this ISR executes TMR4_CallBack(); //***User Area End tmr4_obj.count++; tmr4_obj.timerElapsed = true; IFS1bits.T4IF = false; } And tmr4.c also includes a callback function void __attribute__ ((weak)) TMR4_CallBack(void) { // Add your custom callback code here } My question is what difference does it make whether I add my code in the main ISR routine or in the callback function? |
|
相关推荐
7个回答
|
|
我称之为“代码干净”,可能还有可管理性、可移植性。如果再调用这个函数,将会占用一些时间(和内存),但我认为您不会注意到。
以上来自于百度翻译 以下为原文 I'd call it "clean-ness of code" and possibly manageability, portability. Calling that further funcion will steal some time (and memory) but I don't think you'll notice. |
|
|
|
如果调用回调,那么与直接在ISR中编写代码相比,至少有4个周期开销,这种情况下可能没有任何区别。请确保使用低优先级ISR,否则它会阻塞在做家务时其他中断。嗯,最适合做家务的地方是主环路。
以上来自于百度翻译 以下为原文 If you call the callback there's at least 4 cycle overhead compared to writing code directly in the ISR, which probably doesn't make any difference in this case. Make sure you use low priority ISR or it'll block other interrupts while doing your housekeeping. IMHO, the best place for housekeeping is the main loop. |
|
|
|
是的,正如西蒙所说的!这是“回调”的常用用法。
以上来自于百度翻译 以下为原文 Yep, as Simon says! It's the usual use for a "callback". |
|
|
|
考虑到上市成本,这是主要好处。责任分离。假设你有一个应用程序想要每秒旋转一个5度的马达。将TMR4设置为每秒触发一次,并调用RoTaTy5Duffes()方法作为回调。此外,您还像这样测试它,并且为此花费了大量金钱。现在,如果您想将TMR4用于其他用途,并将电机切换为使用TMR1,那么您更改的代码仅限于计时器模块。您的电机代码不需要重新测试,因为它没有被修改。只要TMR1可以显示出来履行与TMR4相同的合同,并且您在应用程序的其余部分都遵循了模块化原则,那么您对出现的新bug的关注就可以消除。因此,现在我要完全重新测试,我随时修改它。这对于您的应用程序可能是可以接受的,甚至是必要的,但是大多数应用程序将从抽象结构中受益,比次要的但有时需要的速度优化方法更多。
以上来自于百度翻译 以下为原文 This is the primary benefit, given the listed costs. Separation of responsibility. Let's say you have an application that wants to rotate a motor 5 degrees every second. You set your TMR4 to trigger once per second and call the Rotate_5degrees() method as your callback. Furthermore, you test it like this and spend a lot of money doing so. Now if you want to use TMR4 for something else and switch the motor to use TMR1, the code you've changed is limited to the timer module. Your motor code doesn't need to be retested because it hasn't been modified. As long as TMR1 can be shown to fulfill the same contract as TMR4 and you have followed this principle of modularity throughout the rest of your application, your concerns about new bugs arising can be eliminated. If I place all my code directly in the ISR, it will of course be faster - but it is also now subject to being fully retested anytime I make a modification to it. This might be acceptable or even necessary for your application, but the majority of applications will benefit from the abstracted structure more than the minor-but-sometimes-required speed-optimized method. |
|
|
|
回调的一个重要特征。但是它在小型嵌入式CPU中的使用可能有限。
以上来自于百度翻译 以下为原文 An important feature of the callback. But it maybe of limited use in a small embedded CPU. |
|
|
|
取决于编译器如何处理弱函数,它可能等同于直接调用。假设这样,就可以获得代码的回调,而无需开销。那将是一个巧妙的编译技巧。如果评论指出的话,那就太好了。
以上来自于百度翻译 以下为原文 Depending on how the compiler handles the weak function, it may be the equivalent of a direct call. Assuming so you get the code seperation of a callback without the overhead. That would then be a neat compiler trick. and it would be nice if the comments noted it. |
|
|
|
一个不错的“诀窍”是如果代码被分离,但是,当编译时,它将直接进入中断(就像内联函数那样),从而消除函数调用开销。我认为C不能做这件事。
以上来自于百度翻译 以下为原文 A nice "trick" would be if the code was separated, but, when compiled, it would go straight into the interrupt (as an inline function would) thus eliminating the function call overhead. I don't think C can do it. |
|
|
|
只有小组成员才能发言,加入小组>>
5228 浏览 9 评论
2026 浏览 8 评论
1950 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3199 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2253 浏览 5 评论
770浏览 1评论
658浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
585浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
668浏览 0评论
570浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-18 15:03 , Processed in 1.505426 second(s), Total 89, Slave 72 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191