Programming External Interrupt

2010-06-22
2013-03-12
  • Manish Verma
    Manish Verma
    2010-06-22

    Hi

    I am trying external interrupt thru sdcc but it's not working. Can anyone help me?
    This is my code:

    unsigned char test = 0xAA;

    void EXT0_isr (void) __interrupt(2)
    {
    test = ~test;
    }

    void ext_intr(void) {
       EX1 = 1;
       EA = 1;
    }

    void main(void)
    {
       SP = 0x40;
       ext_intr();
    while(1)
    {
    P0 = test;
    }
    }

     
  • Erik Petrich
    Erik Petrich
    2010-06-22

    Any variables that an interrupt handler modifies and are read outside of the interrupt handler should be declared with the volatile keyword:

    volatile unsigned char test = 0xAA;

    Otherwise, the compiler sees that nothing is changing the variable inside the loop and may decide to load the variable into a register before the loop begins and never reload it. (so any changes by the interrupt handler are unseen).

    Not necessarily an error, but you'll be less likely to get into trouble later if you remove the explicit stack point initialization and let the start-up code set the stack. If you need to control the memory layout, use the linker related command-line options. Otherwise, you might accidentally put the stack on top a variable and have no warning, other than the program behaves unexpectedly.

     
  • Manish Verma
    Manish Verma
    2010-06-22

    What happening is that, pin EX1 and EA is also not getting set. I tried with "volatile" also.

     
  • Manish Verma
    Manish Verma
    2010-06-22

    Also, How come I can do LJMP to main at starting in 'c' code?

    Thanks!

     
  • Which chip are you using? I'm also curious about your comments about the EA and EX1 pin, as the code doesn't manipulate them. The code changes the EA and EX1 bits in the interrupt enable SFR in the chip. The pin called EA has an unrelated function and should be left alone. How are you generating at interrupt edge on the EXT1 pin?

     
  • Manish Verma
    Manish Verma
    2010-06-23

    I have written this program for 8051 which has EA as interrupt enable/disable pin and EX1 for enabling the external interrupt. Now it's working fine, i fixed it.
    I have just one question that How come I can do LJMP __main at 0000H address from 'C' code.

    Thanks!