[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-libc-dev] Disable/Restore all Interrupts

From: Sam de Jongh Hepworth
Subject: [avr-libc-dev] Disable/Restore all Interrupts
Date: Thu, 4 Sep 2003 00:18:57 +0200


avr-libc is great! I have writte two macros that help disable and restore all 
interrupts. I think the cli(); and sei(); macros are a little dangerous to use, 
please consider this;

Before this code is executed all interrupts has been disabled. Now the 
following code is executed;

-- BEGIN --------------------------------
// do some work with all interrupts disabled...
-- END --------------------------------

After executing the code all interrupts will be enabled! But they where 
disabled before the code was executed :-(

My solution to this problem is;

-- BEGIN --------------------------------
#define DISABLE_INTERRUPTS() {ubyte _save_int = SREG & flag(7); cli();
#define RESTORE_INTERRUPTS() (SREG |= _save_int);}

// do some work with all interrupts disabled...
-- END --------------------------------

The DISABLE_INTERRUPTS macro save the current state of SREG bit 7 and then 
disabled interrupts. The RESTORE_INTERRUPTS macro restore SREG bit 7. Nice!

Best regards,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]