新聞中心

51中斷 用using 0

作者: 時(shí)間:2016-11-11 來源:網(wǎng)絡(luò) 收藏
就是告訴編譯器,該段代碼使用哪組寄存器。寄存器就是8051自帶RAM的0x00-0x1F的8x4組R0-R7寄存器。

中斷服務(wù)程序?qū)幾g器來說首要需要考慮的就是現(xiàn)場寄存器的入棧保護(hù)。也就是說,一段在主程序內(nèi)正在運(yùn)行的代碼,向R1內(nèi)寫了0xFF做為中間變量。而此時(shí),突然來了中斷,那么為了保護(hù)原有數(shù)據(jù)不被破壞,就必須將原R1壓入堆棧。而PUSH動作是要花費(fèi)指令周期去執(zhí)行的,為了提高代碼執(zhí)行速度,若一開始就規(guī)劃好主程序使用第0和第1組寄存器,而中斷服務(wù)程序則指定為第2組。那么,編譯器在編譯的時(shí)候就可以做優(yōu)化,不必再執(zhí)行原PUSH動作(應(yīng)為兩組寄存器的物理地址不同)。
所以,上述使用分組寄存器的辦法是一種代碼優(yōu)化手段。
下附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.

本文引用地址:http://www.butianyuan.cn/article/201611/317065.htm

假定你跟你老板共用一張辦公桌,每次你老板來了你就得讓開桌子給他用,等他用完,桌上原來的擺放已經(jīng)亂七八糟了.

高人出了個(gè)主意,桌子仍共用,但桌面上的板子可整塊拆走,老板來了,換另一塊桌板就好了,等他走了,把你的板搬回來.

51這張桌子上一共配了4張桌板,使用usingn來拆換編號為n的桌板.



關(guān)鍵詞: 51中斷用usin

評論


技術(shù)專區(qū)

關(guān)閉