+ -
当前位置:首页 → 问答吧 → C program - interrupt问题

C program - interrupt问题

时间:2014-05-13

来源:互联网

*非Timer interrupt

我的情况是用类似8051的micro-controller
其中一个port是连接去infra-red sensor
我想要的效果是:如果infra-red sensor有感应 (ie. P2^2 = 1)
原本在运行的主程式就会立即被interrupt, 运行一个更优先的程式 (因为sensor感应到东西)

除了每一句指令之间都加if, 例如
(...)
if (interrupt == 1)
{...............}
(...)
if (interrupt == 1)
{...............}
(...)
if (interrupt == 1)
{...............}
还有别的方法吗?

[ 本帖最后由 Dragon619 於 2014-4-26 02:22 AM 编辑 ]

作者: Dragon619   发布时间: 2014-05-13

引用:原帖由 Dragon619 於 2014-4-26 02:21 AM 发表
*非Timer interrupt

我的情况是用类似8051的micro-controller
其中一个port是连接去infra-red sensor
我想要的效果是:如果infra-red sensor有感应 (ie. P2^2 = 1)
原本在运行的主程式就会立即被interrupt, 运 ...
估计会有特别的写法。
如: http://www.keil.com/support/man/docs/c51/c51_le_interruptfuncs.htm

作者: xianrenb   发布时间: 2014-05-13

引用:原帖由 xianrenb 於 2014-4-26 08:17 AM 发表


估计会有特别的写法。
如: http://www.keil.com/support/man/docs/c51/c51_le_interruptfuncs.htm
看不懂

作者: Dragon619   发布时间: 2014-05-13

引用:原帖由 Dragon619 於 2014-4-27 02:13 AM 发表

看不懂
无留意到你采用的 pin 应该不是 interrupt pin 。

基本上这些果西我无什么印象了。
不过看 http://www.keil.com/dd/docs/datashts/atmel/at89s51_ds.pdf ,见到 interrupt pin 是 p3.2 及 p3.3 的。
用法亦好像是要 0 或 falling edge 。

花时间阅读 datasheet 与 user guide 一类资料是必须的。

如果是普通 input ,就算不上是 interrupt 。

作者: xianrenb   发布时间: 2014-05-13

引用:原帖由 xianrenb 於 2014-4-27 08:14 AM 发表


无留意到你采用的 pin 应该不是 interrupt pin 。

基本上这些果西我无什么印象了。
不过看 http://www.keil.com/dd/docs/datashts/atmel/at89s51_ds.pdf ,见到 interrupt pin 是 p3.2 及 p3.3 的。
用法亦 ...
可能我表达方式有点问题...
其实我想知道的是C有没有方法做到interrupt的效果 (我的做法好像怪怪的XD?

作者: Dragon619   发布时间: 2014-05-13

I think 8051 like hardware has no interrupt vector table/register?

If there is not such thing, then "interrupt" is just one of the way to "wake-up" the suspending/sleeping hardware.

As seen from your code, you're already doing the right thing.......

Could you explain what can't you do currently? Or what is the problem? (apart from perfectionalism)
引用:原帖由 Dragon619 於 2014-4-28 00:28 发表

可能我表达方式有点问题...
其实我想知道的是C有没有方法做到interrupt的效果 (我的做法好像怪怪的XD?

作者: a8d7e8   发布时间: 2014-05-13

引用:原帖由 a8d7e8 於 2014-4-28 11:52 AM 发表
I think 8051 like hardware has no interrupt vector table/register?

If there is not such thing, then "interrupt" is just one of the way to "wake-up" the suspending/sleeping hardware.

As seen from ...
My problem is already solved, but actually I didn't get answer from my question.
My program is not really complicated, it does every action with a delay loop.
So what I have to do is add a condition (by using "if") in the delay loop. (which is equal to adding "if" between all actions)

And actually I am not really familiar with 8051, what I did was just using 2 sensors + 2 motors to design 2 functions, which are avoiding obstacles and detecting fire
The "interrupt" I want to use is "STOP" when fire is detected even though the some actions are being done.
If I just design the program with "avoiding" and "stop when fire is detected" separately, my program wiil not execute "stop" until "avoiding" is finished.

作者: Dragon619   发布时间: 2014-05-13

solved? but didn't get answer?

"If I just design the program with "avoiding" and "stop when fire is detected" separately, my program wiil not execute "stop" until "avoiding" is finished." ?
So it seems that there are 2 problems:

1. The already solved problem about termination on fire;
2. The instant termination on fire - no matter if any subroutine is being processed in a timely manner (eg. it may needs 1s to finish the avoiding logic).

Is it possible to connect the fire sensor signal pin(if it is just a pin) to a reset pin(if there is any)?

Such that after POR if the fire pin is still true, u know there is fire so just do nothing.
引用:原帖由 Dragon619 於 2014-4-28 21:03 发表

My problem is already solved, but actually I didn't get answer from my question.
My program is not really complicated, it does every action with a delay loop.
So what I have to do is add a conditi ...

作者: a8d7e8   发布时间: 2014-05-13

Oh but as seen from comment #2, Cx51 has an interrupt vector table. (u may use those "external int")

So to summarize:

1. Go check the exact mpu model you're using, whether it has the interrupt vector table that can be hooked with the fire sensor(IR sensor in your case).
2. Declare an Interrupt Service Routine for that interrupt as in the example in the link of comment #2.
3. Verify that the ISR take control when the IR sensor change its signal (eg. change from 0 to 1)
4. Wo-ha your device stop instantly when an extremely high amount of IR is detected!

作者: a8d7e8   发布时间: 2014-05-13