在 AVR 中断中保留 sreg

发布于 2024-12-27 15:53:04 字数 92 浏览 2 评论 0原文

AVR 微控制器中用于保存状态寄存器 sreg 的机制是什么? RETI 意味着这些位不在堆栈上。通用寄存器之一也是 sreg 或类似的东西吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯 2025-01-03 15:53:04

每个 AVR 数据表中对此都有解释。例如,ATtiny2313 数据表的第 8 页显示:

进入中断例程时状态寄存器不会自动存储,从中断返回时状态寄存器也不会恢复。这必须由软件处理。

您可以通过将其存储在临时寄存器中来实现此目的:

 interrupt:
     in r16, SREG   ; save SREG

     ...

     out SREG, r16  ; restore SREG
     reti

另请注意,如果您正在访问不专门用于此中断例程的寄存器,您也需要保存这些寄存器。此外,如果寄存器不足,您可以将 SREG 的值推送到堆栈:

 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

有关详细信息,请参阅 此处

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

The Status Register is not automatically stored when entering an interrupt routine and restored when returning from an interrupt. This must be handled by 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're accessing registers that are not exclusively used in this interrupt routine, you need to save those, too. Furthermore you can push the value of SREG to the stack if you're low on 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

For more information look here.

葵雨 2025-01-03 15:53:04

或者

PUSH Rn
LDS Rn, SREG
PUSH Rn

POP Rn
STS SREG, Rn
POP Rn

似乎是有效的。

Alternatively

PUSH Rn
LDS Rn, SREG
PUSH Rn

and

POP Rn
STS SREG, Rn
POP Rn

seems to be valid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文