external interrupt programming

Discussion in 'Embedded Systems and Microcontrollers' started by bobparihar, Sep 15, 2014.

  1. bobparihar

    Thread Starter Member

    Jul 31, 2014
    i am programming interrupt 0 using 8051
    my task is.. explained in the circuit diagram as follows

    when ever till the push button pressed the led should goes low which is connected at P2.1

    but the problem i am facing is if long press the button the led toggles quickly and repeatedly... but i want that till the button is pressed the led should be off

    here is my code

    Code (Text):
    3. #include<reg51.h>
    4. sbit a=P2^1;                                                            // for led
    5. void ext(void) interrupt 0
    6. {
    7.   a=0;
    9. }
    10. void main()
    11. {
    12.   IE=0x81;                                                      // using external 0 interrupt
    14.   while(1)
    15.   {
    16.   a=1;
    17.   }
    18. }
    Last edited by a moderator: Sep 15, 2014
  2. FroceMaster


    Jan 28, 2012
    Not that mcu i use. But do you not need a pull up on the input ??
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    I would add two resistors to this: first one to limit the current thru the LED. As is, you have an uncontrolled amount of current there. That could get huge and let smoke out.

    Next you need one at the switch input to the + power so the input can change states.

    Last, add bypass caps to all the chips so they work reliably.

    Now for the code: I don't use this device so I am not sure if the interrupt fires for the + or the - input, but I betcha it doesn't fire on the level. Your code needs to be "stuck" in the interrupt section to keep the LED off, I do not believe it does that.
  4. Papabravo


    Feb 24, 2006
    Port 2 has internal weak pullups (40K approx). External interrupts can be either edge triggered or level triggered. Whichever way you do it you must clear the source of the interrupt in the service routine or you will execute the service routine repeatedly