×
嵌入式 > 嵌入式开发 > 详情

51中断 用using 0

发布时间:2020-08-27 发布时间:
|
就是告诉编译器,该段代码使用哪组寄存器。寄存器就是8051自带RAM的0x00-0x1F的8x4组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的桌板.



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

热门文章 更多
五大标准轻松搞定4K超高清电视选购