Saving Spray in AVR Interrupts

What is the mechanism used to save the sreg status register in the AVR microcontroller? RETI implies that these bits are not on the stack. Is one of the general registers also a sphere or something like that?

+8
assembly avr arduino atmel
source share
2 answers

This is explained in every AVR datasheet. For example, page 8 of the ATtiny2313 datasheet says:

The status register is not automatically saved when the interrupt routine is entered and is restored when the interrupt returns. This must be done using software.

You can achieve this by storing it in a temporary register:

  interrupt: in r16, SREG ; save SREG ... out SREG, r16 ; restore SREG reti 

Also note that if you access registers that are not used exclusively in this interrupt procedure, you also need to save them. In addition, you can push the SREG value onto the stack if you have few registers:

  interrupt: push r16 ; save global registers on stack push r17 push r18 in r16, SREG ; save SREG push r16 ; do this if you want to use r16 in your interrupt routine ... pop r16 ; do this if you pushed SREG above out SREG, r16 ; restore SREG pop r18 ; restore global registers pop r17 pop r16 reti 

See here for more details.

+11
source share

Alternatively

 PUSH Rn LDS Rn, SREG PUSH Rn 

and

 POP Rn STS SREG, Rn POP Rn 

seems valid.

+1
source share

All Articles