×
单片机 > 单片机程序设计 > 详情

51中断 用using 0

发布时间:2020-06-20 发布时间:
|
就是告诉编译器,该段代码使用哪组寄存器。寄存器就是8051自带RAM的0x00-0x1F的8x4组R0-R7寄存器。 

中断服务程序对编译器来说首要需要考虑的就是现场寄存器的入栈保护。也就是说,一段在主程序内正在运行的代码,向R1内写了0xFF做为中间变量。而此时,突然来了中断,那么为了保护原有数据不被破坏,就必须将原R1压入堆栈。而PUSH动作是要花费指令周期去执行的,为了提高代码执行速度,若一开始就规划好主程序使用第0和第1组寄存器,而中断服务程序则指定为第2组。那么,编译器在编译的时候就可以做优化,不必再执行原PUSH动作(应为两组寄存器的物理地址不同)。 
所以,上述使用分组寄存器的办法是一种代码优化手段。 
下附KEIL的HELP文档,作为参考。 
//----------------------------------------- 

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  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 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 PSW from 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. 

Copyright © Keil, An ARM Company. All rights reserved.

假定你跟你老板共用一张办公桌,每次你老板来了你就得让开桌子给他用,等他用完,桌上原来的摆放已经乱七八糟了. 

高人出了个主意,桌子仍共用,但桌面上的板子可整块拆走,老板来了,换另一块桌板就好了,等他走了,把你的板搬回来. 

51这张桌子上一共配了4张桌板,使用using n来拆换编号为n的桌板.

关键字:51中断  用using 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
MSP430F5529 上手小例程2