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相关推荐

  1. 51单片机的工作寄存器R0~R7位于内部RAM什么位置

    转自:https://zhidao.baidu.com/question/1495509806096353459.html 51单片机的工作寄存器一共有32个,为RAM中的00H--1FH单元,分为4 ...

  2. 解决keil出现cpu is not halted cannot read register XX while cpu is running的另类方法

    最近在做keil硬件仿真的时候发现了很奇怪的问题,程序应该没有问题,可以下载到板子上,但是仿真就不行,点击仿真按钮的同时可以看到jlink的灯熄灭了好一阵才亮起来,然后debug页面如图1所示,我使用 ...

  3. Cortex-A7 MPCore 架构详细介绍(九种运行模式、内核寄存器组R0~R15,有特定的名字和功能)

    目录 0.ARM架构的历史简介 1.Cortex-A7 MPCore(即多核) 简介 2.Cortex-A 处理器九种运行模式 3.Cortex-A 寄存器组(内核寄存器) 3.1通用寄存器 3.1. ...

  4. Cortex-M3 R0~R15寄存器组

    [R0~R12通用寄存器] R0~R12都是32位通用寄存器,用于数据操作.其中: R0~R7为低组寄存器,所有的指令都可以访问. R8~R12为高组寄存器,只有32位Thumb2指令和很少的16位T ...

  5. u-boot移植第二弹——移植2012.10u-boot到RealARM210 cortex-A8开发板

    本次移植的目的: 1.u-boot能够跑起来 2.能够进入控制台打印出如下信息 本次移植是基于官方的u-boot版本是u-boot-2012.10,温馨提示,如果是新手可以完全按照这个步骤走就行.好, ...

  6. u-boot2013.01.01 for s5pv210: u-boot启动流程

    转载请注明地址:http://blog.csdn.net/zsy2020314/article/details/9824035 1.关于启动流程 1.1 启动阶段分为3个,bl0,bl1,bl2.下面 ...

  7. keil c语言中断怎么写,如何利用keil C实现单片机中断功能

    直接访问寄存器和端口 定义 sfr P0 0x80 sfr P1 0x81 sfr ADCON; 0xDE sbit EA  0x9F 操作 ADCON = 0x08; P1 = 0xFF; io_s ...

  8. interrupt using

    interrupt 表示中断优先级,using表示所用工作寄存器组. interrupt      x      using      y         跟在interrupt      后面的xx ...

  9. 在2410上移植uboot1.3.2

    转贴地址:http://blog.chinaunix.net/u2/74524/showart_1105803.html 目前官方网站上最新版本的u-boot 是1.3.3,本来想打算移植1.3.3的 ...

  10. 在2410上移植uboot1.3.2【转】

    转贴地址:http://blog.chinaunix.net/u2/74524/showart_1105803.html 目前官方网站上最新版本的u-boot 是1.3.3,本来想打算移植1.3.3的 ...

最新文章

  1. .net软件工程师笔试题目
  2. puppet集群之 Nginx and Passenger
  3. 【TypeScript】字符串转义符序列
  4. python3l下载_lunix 安装python3
  5. php绕过漏洞的函数,PHP中有漏洞的函数总结
  6. 房间计费系统改造E-R图纸设计
  7. 互联网+商业 济宁苏宁生活广场打造智慧城市范本
  8. 使用 JavaScript 生成二维码 —— QRCode.js
  9. 线程的sleep()方法和yield()方法有什么区别?
  10. proteus元件图片_proteus元件库
  11. 超级终端连接华为交换机_小编解决win8系统使用超级终端连接华为交换机的设置步骤...
  12. EPSON ME office 700FW打印机废墨收集垫已到使用寿命解决办法
  13. 安装黑苹果系统前请看:macOS Mojave 的硬件兼容性列表
  14. SpringBoot集成Beetl
  15. 微信客服介绍和使用指引(4.19)
  16. 猎头解密互联网公司offer行情,网易游戏研发高达40W--IT薪资待遇
  17. Unity3D Editor 编辑器扩展3 Editor脚本
  18. 谷歌(chrome)浏览器,页面翻译设置
  19. 哭了,谁还会心疼?累了,谁让我依靠?
  20. MATLAB颜色图中,小于某个值的所有点设为白色

热门文章

  1. java 获取网络接口信息,看这一篇就行了 | Java工具类
  2. 向榜样学习是我们每个人都应该做的
  3. ARP***原理和解决方法
  4. 密码技术挑战赛-多选题
  5. Java编程思想,读书笔记八(第10章 内部类)
  6. linux服务器使用jupyter notebook时提示磁盘已满“Unexpected error while saving file:ipynb database or disk is full”
  7. Datawhale集成学习学习笔记:偏差与方差理论
  8. 泛微e-cology9 browser.jsp SQL注入漏洞
  9. ArchLinux搭建高效便捷的平铺式桌面
  10. 南山区服务器维修,深圳南山区的云服务器