中断服务程序对编译器来说首要需要考虑的就是现场寄存器的入栈保护。也就是说,一段在主程序内正在运行的代码,向R1内写了0xFF做为中间变量。而此时,突然来了中断,那么为了保护原有数据不被破坏,就必须将原R1压入堆栈。而PUSH动作是要花费指令周期去执行的,为了提高代码执行速度,若一开始就规划好主程序使用第0和第1组寄存器,而中断服务程序则指定为第2组。那么,编译器在编译的时候就可以做优化,不必再执行原PUSH动作(应为两组寄存器的物理地址不同)。
所以,上述使用分组寄存器的办法是一种代码优化手段。
下附KEIL的HELP文档,作为参考。
//-----------------------------------------
Register Banks
In all members of the 8051 family, the first 32 bytes of DA
Register banks are useful when processing interrupts or when using a real-time operating system because the MCU can switch to a different register bank for a task or interrupt rather than saving all 8 registers on the stack. The MCU can then restore switch back to the original register bank before returning.
The using function attribute specifies the register bank a function uses. For example:
void rb_function (void) using 3
{
.
.
.
}
The argument for the using attribute is an integer constant from 0-3. Expressions with operators are not allowed. The using attribute is not allowed in function prototypes. The using attribute affects the object co
The currently selected register bank is saved on the stack at function entry.
The specified register bank is set.
The former register bank is restored before the function is exited.
The following example shows how to specify the using function attribute and what the generated assembly co
stmt level source
1
2 extern bit alarm;
3 int alarm_count;
4 extern void alfunc (bit b0);
5
6 void falarm (void) using 3 {
7 1 alarm_count++;
8 1 alfunc (alarm = 1);
9 1 }
ASSEMBLY LISTING OF GENERATED OBJECT CO
; FUNCTION falarm (BEGIN)
0000 C0D0 PUSH PSW
0002 75D018 MOV PSW,#018H
; SOURCE LINE # 6
; SOURCE LINE # 7
0005 0500 R INC alarm_count+01H
0007 E500 R MOV A,alarm_count+01H
0009 7002 JNZ ?C0002
000B 0500 R INC alarm_count
000D ?C0002:
; SOURCE LINE # 8
000D D3 SETB C
000E 9200 E MOV alarm,C
0010 9200 E MOV ?alfunc?BIT,C
0012 120000 E LCALL alfunc
; SOURCE LINE # 9
0015 D0D0 POP PSW
0017 22 RET
; FUNCTION falarm (END)
In the previous example, the co
Note
The using attribute may not be used in functions that return a value in registers. You must exercise extreme care to ensure that register bank switches are performed on
The using attribute is most useful in interrupt functions. Usually a different register bank is specified for each interrupt priority level. Therefore, you could assign on
Copyright © Keil, An ARM Company. All rights reserved.
假定你跟你老板共用一张办公桌,每次你老板来了你就得让开桌子给他用,等他用完,桌上原来的摆放已经乱七八糟了.
高人出了个主意,桌子仍共用,但桌面上的板子可整块拆走,老板来了,换另一块桌板就好了,等他走了,把你的板搬回来.
51这张桌子上一共配了4张桌板,使用using n来拆换编号为n的桌板.
上一篇:74hc164驱动数码管程序
下一篇:51 温度PID经典算法
推荐阅读最新更新时间:2024-03-16 15:02