完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
亲爱的朋友们,我有一个情况,主程序和中断服务程序共享两个变量。中断服务例程实际上更新了这个变量。为了防止竞争条件,我停止主程序中的中断例程,读取常用值的新值,然后再次启动中断服务程序。它运行良好,但是……我想知道你们是否知道一个干净的。这样做的方法是什么?非常尊敬的发明家
以上来自于百度翻译 以下为原文 Dear folks, I have a situation whereby two variables are shared by the main program and the interrupt service routine. The interrupt service routine actually updates this variable. To prevent race conditions, I stop the interrupt routine in the main program, read the new value of the commonly used value and then I start the interrupt service routine again. It works fine, but.....I am wondering if you guys know a cleaner way of doing this? Very respectfully, inventor |
|
相关推荐
5个回答
|
|
你没有提到这是一个8位变量,或者更大,或者你正在做什么样的改变。
以上来自于百度翻译 以下为原文 You don't mention if this is an 8 bit variable, or larger, or what sort of changes you are doing. |
|
|
|
亲爱的qub,谢谢你这么快就回到我身边。实际上,有两个变量被彼此正确地更新。它们是在16F1836上运行的8位变量。代码是用汇编语言编写的,通过SSP中断,PIC正在接收这两个中断。我写在同一ISR在两个其他变量。在主程序中,我复制了这两个更新,并在主循环中使用它们。它运行良好,但时不时地……有一个故障:-因为还有其他中断,我立即停止全局中断标志来拾取这两个更新,然后重新启动它。我应该停止这个中断吗?但是解决这个问题必须有一些优雅的方式。我在想,也许是推动和弹出堆栈?但是,你只是用一个旧的值重写一个更新的值?请告知发明人
以上来自于百度翻译 以下为原文 Dear qub, Thank you for getting back to me so fast. There are actually two variables that are updated right after one another. They are 8 bit variables running on a 16F1836. The code is in assembly language. Via the SSP interrupt the pic is receiving these two interrupts. I write them within the same ISR in two other variables. In the main program I then copy these two updates and use them in the main loop. It works fine but every so now and then..........there is a glitch :-) Because there are other interrupts going on as well and I stop the global interrupt flag shortly to pick up the two updates and then I restart it. Perhaps I should just stop this interrupt? But there must be some elegant manner in resolving this. I was thinking of perhaps pushing and popping on the stack? But then you just overwrite an updated value with an old value? Please advise, inventor |
|
|
|
有很多方法来处理它。最好的方法归结于你的特定需求。如果你只中断中断足够长的时间来复制两个8位变量,那么这只是一对指令。只要你不介意一些指令现在中断服务上的额外延迟就可以了。HEN,这不重要。是的,只禁用SSP中断,而不是GIE,如果您同时启用其他中断,将更干净。
以上来自于百度翻译 以下为原文 There's many ways to handle it. The best way comes down to your specific requirements. If you're only disabling interrupts long enough to copy two 8 bit variables, then that is only a couple of instructions. So long as you don't mind a few instructions extra latency on your interrupt service now and then, it shouldn't matter. Yes, it would be cleaner to only disable the SSP interrupt, rather than GIE, if you have other interrupts enabled at the same time. |
|
|
|
正如Qub所说,有很多方法取决于你的要求。如果禁用中断不是一种选择,那么一种方式是在ISR DO TISISR中:在主循环中更新VARS SET FLAG并做这个循环:清除标志复制VAR,如果标志被设置,转到循环
以上来自于百度翻译 以下为原文 As Qub said, there are many ways depending on your requirements. If disabling interrupts is not an option, then one way is in the ISR do this isr: update vars set flag and in the main loop do this loop: clear flag copy vars if flag is set, go to loop |
|
|
|
如果你不想中断中断,一个方法是读取第一个字节,读取第二个字节,再次读取第一个字节,看看它是否仍然是相同的。如果不再试一次。
以上来自于百度翻译 以下为原文 If you do not want to disable interrupts, a method is read the first byte, read the second byte, read the first byte again and see if it is still the same. If not try again. |
|
|
|
只有小组成员才能发言,加入小组>>
5250 浏览 9 评论
2037 浏览 8 评论
1958 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3218 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2266 浏览 5 评论
788浏览 1评论
680浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
609浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
685浏览 0评论
582浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-29 01:57 , Processed in 1.213351 second(s), Total 84, Slave 68 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号