佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

12
返回列表 发新帖
楼主: Ha_He_Hi_Ho_Hu

latching 和state control 里的 looping 问题

[复制链接]
 楼主| 发表于 14-8-2009 04:12 PM | 显示全部楼层
原帖由 fritlizt 于 14-8-2009 03:31 PM 发表


对。 你现在用interrupt做, 不要用delay.看做到吗。
10ms interrupt一次。
然后check 如果overflow 50次, 就toogle led.

原帖由 本弘 于 14-8-2009 03:56 PM 发表
aiya...hint是:在timer interrupt 那里set tick ...每interrupt 一次就tick++...
在check tick 的condition...if (tick ==50)then continue else perform nothing.
这个是将interrupt 变成timer 和interrupt 的 ...


其实我没有用过interupt的。。。虽然我知道interupt能做到很多事情。

看来我真的要好好研究interupt的应用了。谢谢你们的帮忙哦!
回复

使用道具 举报


ADVERTISEMENT

发表于 14-8-2009 04:22 PM | 显示全部楼层
原帖由 本弘 于 14-8-2009 12:21 PM 发表
我没有写debounce 的。。。原来那个是debounce 的。。。

有一些我做的系统, button debounce , 对我来说是一个很重要的task。
在debounce 的当儿, MCU 还是有做工的。。。

if ( button == 0) {
    while (button == 0);
    counter ++;
}

Button在按下不放的当儿, MCU 只是在等, 没做工。
当然, 对楼主而言, 上面的可能已经足够。
回复

使用道具 举报

发表于 14-8-2009 04:45 PM | 显示全部楼层
原帖由 本弘 于 14-8-2009 02:57 AM 发表
应该ok 了:
#include
#define RUN 1
#define        STOP 0
void Delay2 ();
unsigned int counter=0;
sbit button = P0^0;
sbit led1 = P0^1; //  led turn on when 1 and turn off when 0
sbit led2 = P0^2;

...


code还是有少许问题.
你的code. 最多能闪烁一次, 接下来, 怎样按button就没会反应了。。。。
还有, interrupt里面不建议直接用delay,导致interrupt execution time太长。 虽然没错。
像你的case, 当led闪烁的时候, 你按button的时候, 是没反应的。。。。。
可能要按最多delay2+delay2的时间, 才会有反应。
这就是因为你interrupt execution time太长了。
回复

使用道具 举报

发表于 14-8-2009 04:58 PM | 显示全部楼层
原帖由 本弘 于 14-8-2009 12:21 PM 发表
我没有写debounce 的。。。原来那个是debounce 的。。。


theoretically, 一个button是perfect的。。。。。
没按就是一, 按了就是0。。。。。
不过现实上, 当你按button的时候, 再button short circuit的那一刹那, 会有很多不确定的signal....
也就是。

1111111110101101010101010000000000000000

没按的时候是1, 按的时候是0, 所以要如果只是
while (button == 1);
while(button ==0);
即使你只按一次, 可能counter会increment很多次。

debouncing是一种方法, 让你忽略/消除 noise,拿到不必要的结果
可以有software debounce, hardware debounce etc.
回复

使用道具 举报

发表于 16-8-2009 12:13 PM | 显示全部楼层
原帖由 Ha_He_Hi_Ho_Hu 于 14-8-2009 04:12 PM 发表




其实我没有用过interupt的。。。虽然我知道interupt能做到很多事情。

看来我真的要好好研究interupt的应用了。谢谢你们的帮忙哦!


给你一些hints使用interrupt
  1. #include <16f876a.H>
  2. #fuses HS,NOWDT,NOPROTECT,NOLVP
  3. #use delay(clock=20000000)

  4. void system_init(void);
  5. unsigned int8 TimeOut = FALSE;

  6. void main (void)
  7. {
  8.         system_init();
  9.         while(1);
  10. }

  11. void system_init (void)
  12. {
  13.         setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
  14.         set_timer1(15536);
  15.         enable_interrupts(INT_TIMER1);
  16.         enable_interrupts(GLOBAL);
  17. }

  18. #INT_TIMER1
  19. void timer1_irq (void)
  20. {
  21.         set_timer1(15536);
  22.         TimeOut = TRUE;
  23. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 18-4-2024 12:27 PM , Processed in 0.054181 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表