您的位置 首页 传感器

51中止 用using 0

就是告诉编译器,该段代码使用哪组寄存器。寄存器就是8051自带RAM的0x00-0x1F的8×4组R0-R7寄存器。中断服务程序对编译器来说首要需要考虑的…

便是告知编译器,该段代码运用哪组寄存器。寄存器便是8051自带RAM的0x00-0x1F的8×4组R0-R7寄存器。

中止服务程序对编译器来说首要需求考虑的便是现场寄存器的入栈维护。也便是说,一段在主程序内正在运转的代码,向R1内写了0xFF做为中心变量。而此刻,忽然来了中止,那么为了维护原有数据不被损坏,就必须将原R1压入仓库。而PUSH动作是要花费指令周期去履行的,为了进步代码履行速度,若一开始就规划好主程序运用第0和第1组寄存器,而中止服务程序则指定为第2组。那么,编译器在编译的时分就可以做优化,不用再履行原PUSH动作(应为两组寄存器的物理地址不同)。
所以,上述运用分组寄存器的方法是一种代码优化手法。
下附KEIL的HELP文档,作为参阅。
//—————————————–

RegisterBanks
Inallmembersofthe8051family,thefirst32bytesofDATAmemory(0x00-0x1F)isgroupedinto4banksof8registerseach.ProgramsaccesstheseregistersasR0-R7.Theregisterbankisselectedbytwobitsoftheprogramstatusword,PSW.

Registerbanksareusefulwhenprocessinginterruptsorwhenusingareal-timeoperatingsystembecausetheMCUcanswitchtoadifferentregisterbankforataskorinterruptratherthansavingall8registersonthestack.TheMCUcanthenrestoreswitchbacktotheoriginalregisterbankbeforereturning.

Theusingfunctionattributespecifiestheregisterbankafunctionuses.Forexample:

voidrb_function(void)using3
{
.
.
.
}

Theargumentfortheusingattributeisanintegerconstantfrom0-3.Expressionswithoperatorsarenotallowed.Theusingattributeisnotallowedinfunctionprototypes.Theusingattributeaffectstheobjectcodeofthefunctionasfollows:

Thecurrentlyselectedregisterbankissavedonthestackatfunctionentry.
Thespecifiedregisterbankisset.
Theformerregisterbankisrestoredbeforethefunctionisexited.
Thefollowingexampleshowshowtospecifytheusingfunctionattributeandwhatthegeneratedassemblycodeforthefunctionentryandexitlookslike.

stmtlevelsource

1
2externbitalarm;
3intalarm_count;
4externvoidalfunc(bitb0);
5
6voidfalarm(void)using3{
71alarm_count++;
81alfunc(alarm=1);
91}

ASSEMBLYLISTINGOFGENERATEDOBJECTCODE

;FUNCTIONfalarm(BEGIN)
0000C0D0PUSHPSW
000275D018MOVPSW,#018H
;SOURCELINE#6
;SOURCELINE#7
00050500RINCalarm_count+01H
0007E500RMOVA,alarm_count+01H
00097002JNZ?C0002
000B0500RINCalarm_count
000D?C0002:
;SOURCELINE#8
000DD3SETBC
000E9200EMOValarm,C
00109200EMOV?alfunc?BIT,C
0012120000ELCALLalfunc
;SOURCELINE#9
0015D0D0POPPSW
001722RET
;FUNCTIONfalarm(END)

Inthepreviousexample,thecodestartingatoffset0000hsavestheinitialPSWonthestackandsetsthenewregisterbank.Thecodestartingatoffset0015hrestorestheoriginalregisterbankbypoppingtheoriginalPSWfromthestack.

Note

Theusingattributemaynotbeusedinfunctionsthatreturnavalueinregisters.Youmustexerciseextremecaretoensurethatregisterbankswitchesareperformedonlyincarefullycontrolledareas.Failuretodosomayyieldincorrectfunctionresults.Evenwhenyouusethesameregisterbank,functionsdeclaredwiththeusingattributecannotreturnabitvalue.
Theusingattributeismostusefulininterruptfunctions.Usuallyadifferentregisterbankisspecifiedforeachinterruptprioritylevel.Therefore,youcouldassignoneregisterbankforallnon-interruptcode,asecondregisterbankforthehigh-levelinterrupt,andathirdregisterbankforthelow-levelinterrupt.

CopyrightKeil,AnARMCompany.Allrightsreserved.

假定你跟你老板共用一张办公桌,每次你老板来了你就得让开桌子给他用,等他用完,桌上本来的摆放现已杂乱无章了.

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

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

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/chuanganqi/275325.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部