打印
[程序源码]

STC8,keil扩展SFR读写指令被优化的问题。

[复制链接]
866|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
在调试STC8扩展串口时,由于扩展的SCON不能位寻址,只能字节寻址。但是写出来的代码被优化掉。
声明SFR的时候加volatile 直接报错。
目前只能分两步进行才不被优化掉。请问这个问题大家是怎么解决的。
分两步不会被优化

直接写会被优化


求教各位

使用特权

评论回复

相关帖子

沙发
oufuqiang|  楼主 | 2021-1-6 22:55 | 只看该作者
sfr
The sfr type defines a special function register (SFR). It is used as follows:

sfr name = address;
Where

name is the name of the SFR.
address is the address of the SFR.

SFRs are declared in the same fashion as other C variables. The only difference is that the type specified is sfr rather than char or int. For example:

sfr P0 = 0x80;    /* Port-0, address 80h */
sfr P1 = 0x90;    /* Port-1, address 90h */
sfr P2 = 0xA0;    /* Port-2, address 0A0h */
sfr P3 = 0xB0;    /* Port-3, address 0B0h */
P0, P1, P2, and P3 are the SFR name declarations. Names for sfr variables are defined just like other C variable declarations. Any symbolic name may be used in an sfr declaration.

The address specification after the equal sign ('=') must be a numeric constant. Expressions with operators are not allowed. Classic 8051 devices support the SFR address range 0x80-0xFF. The NXP 80C51MX provides an additional extended SFR space with the address range 0x180-0x1FF.

Note

sfr variables may not be declared inside a function. They must be declared outside of the function body.
sfr variables are always volatile. The compiler will not optimize accesses to this type of variables.

事实却不像keil说的那样。

.H

//串行口特殊功能寄存器
sfr SCON        =   0x98;
sbit SM0        =   SCON^7;
sbit SM1        =   SCON^6;
sbit SM2        =   SCON^5;
sbit REN        =   SCON^4;
sbit TB8        =   SCON^3;
sbit RB8        =   SCON^2;
sbit TI         =   SCON^1;
sbit RI         =   SCON^0;
sfr SBUF        =   0x99;
sfr S2CON       =   0x9a;
#define S2SM0       0x80
#define S2ST4       0x40
#define S2SM2       0x20
#define S2REN       0x10
#define S2TB8       0x08
#define S2RB8       0x04
#define S2TI        0x02
#define S2RI        0x01
sfr S2BUF       =   0x9b;

使用特权

评论回复
板凳
ayb_ice| | 2021-1-7 08:32 | 只看该作者
优先级问题,加括号 (xxx & xxx) == 0

使用特权

评论回复
地板
coody| | 2021-1-7 10:56 | 只看该作者
寄存器是直接寻址,不会被优化掉的,仔细检查程序。

使用特权

评论回复
5
oufuqiang|  楼主 | 2021-1-7 18:40 | 只看该作者
真是丢脸了,居然踩回初学者的坑里面去了。
印象中一直记得==的优先级很低的。居然就是一前一后。
看来还是要随时记得用括号括起来,毕竟括号免费

使用特权

评论回复
6
Light_David| | 2021-1-7 19:20 | 只看该作者
一般用 if(xxx&yyy)  和  if(!(xxx&yyy)) , 没遇到这个坑

使用特权

评论回复
7
xyz549040622| | 2021-1-9 20:06 | 只看该作者
加括号是一个非常非常好的习惯,我也不知道优先级别,但是我都是加括号的。

使用特权

评论回复
8
sdwys| | 2021-1-10 22:45 | 只看该作者
keil 选择大模式时,在函数中定义个临时变量也被放在外部存储器,效率一下子低了很多很多,如果能优化成临时变量编译器能自动定义在内部直接寻址的128个寄存器就好了。,

使用特权

评论回复
9
qq986433936| | 2021-1-11 23:46 | 只看该作者
从来不写==0,免得与=0混淆,写!x。

使用特权

评论回复
10
oufuqiang|  楼主 | 2021-1-12 08:45 | 只看该作者
对于位变量,我会写!,对于字节,多字节变量,我习惯写==0.虽然很多时候编译出来的代码一样。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

106

主题

1420

帖子

20

粉丝