本文主要是介绍C8051 Register Banks R0-R7,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Register Banks
http://www.keil.com/support/man/docs/c51/c51_le_regbankspec.htm
主要讲的register banks 切换,实际使用过程中不建议使用using x。
KEIL编译器在编译函数的局部变量时,可能不是直接调用R0-7,很有可能直接寻址,如下图; 如果中断ISR 使用了using 1描述符,并调用了该函数,进入中断后register banks为1,如果直接从0x05地址取数据会导致错误,正确的应该从R5(实际地址为0x08《Bank 1起始地址》+0x05)取数据。
其实利用好register banks 切换可以加快执行中断的速率(因为不需要将R0-R7压栈),但编译器如果处理不好,或是代码没有考虑到切换,最好不要使用。
如果想用Register Banks 切换,可以考虑在ISR中所调用的函数添加下面的宏:
1.Method 1
#pragma NOAREGS // 告知编译器下段代码对R0-R7不使用绝对地址
void function(uint8_t data)
{
}
#pragma AREGS
2.Method 2
#pragma SAVE // Rember current registerbank
#pragma REGISTERBANK(1) // Tel C51 base address of current registerbank.
void function(uint8_t data)
{
}
#pragma RESTORE // Restore register bank
Home » Language Extensions » Function Declarations » Register Banks
In all members of the 8051 family, the first 32 bytes of DATA memory (0x00-0x1F) is grouped into 4 banks of 8 registers each. Programs access these registers as R0-R7. The register bank is selected by two bits of the program status word, PSW.
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 code of the function as follows:
- 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 code for the function entry and exit looks like.
stmt level source12 extern bit alarm;3 int alarm_count;4 extern void alfunc (bit b0);56 void falarm (void) using 3 {7 1 alarm_count++;8 1 alfunc (alarm = 1);9 1 }ASSEMBLY LISTING OF GENERATED OBJECT CODE; 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 code starting at offset 0000h saves the initial PSW on the stack and sets the new register bank. The code starting at offset 0015h restores the original register bank by popping the original PSWfrom the stack.
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 only in carefully controlled areas. Failure to do so may yield incorrect function results. Even when you use the same register bank, functions declared with the using attribute cannot return a bit value.
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 one register bank for all non-interrupt code, a second register bank for the high-level interrupt, and a third register bank for the low-level interrupt.
Related Knowledgebase Articles
- C51: XC800: 'Use multiple DPTR registers' may cause runtime errors
- C51: ABSOLUTE REGISTERS AND USING DIRECTIVE
- C51: ACCESSING REGISTER BANKS IN C
这篇关于C8051 Register Banks R0-R7的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!