51中斷 用using 0
中斷服務(wù)程序?qū)幾g器來說首要需要考慮的就是現(xiàn)場寄存器的入棧保護(hù)。也就是說,一段在主程序內(nèi)正在運(yùn)行的代碼,向R1內(nèi)寫了0xFF做為中間變量。而此時,突然來了中斷,那么為了保護(hù)原有數(shù)據(jù)不被破壞,就必須將原R1壓入堆棧。而PUSH動作是要花費(fèi)指令周期去執(zhí)行的,為了提高代碼執(zhí)行速度,若一開始就規(guī)劃好主程序使用第0和第1組寄存器,而中斷服務(wù)程序則指定為第2組。那么,編譯器在編譯的時候就可以做優(yōu)化,不必再執(zhí)行原PUSH動作(應(yīng)為兩組寄存器的物理地址不同)。
所以,上述使用分組寄存器的辦法是一種代碼優(yōu)化手段。
下附KEIL的HELP文檔,作為參考。
//-----------------------------------------
RegisterBanks
Inallmembersofthe8051family,thefirst32bytesofDA
Registerbanksareusefulwhenprocessinginterruptsorwhenusingareal-timeoperatingsystembecausetheMCUcanswitchtoadifferentregisterbankforataskorinterruptratherthansavingall8registersonthestack.TheMCUcanthenrestoreswitchbacktotheoriginalregisterbankbeforereturning.
Theusingfunctionattributespecifiestheregisterbankafunctionuses.Forexample:
voidrb_function(void)using3
{
.
.
.
}
Theargumentfortheusingattributeisanintegerconstantfrom0-3.Expressionswithoperatorsarenotallowed.Theusingattributeisnotallowedinfunctionprototypes.Theusingattributeaffectstheobjectco
Thecurrentlyselectedregisterbankissavedonthestackatfunctionentry.
Thespecifiedregisterbankisset.
Theformerregisterbankisrestoredbeforethefunctionisexited.
Thefollowingexampleshowshowtospecifytheusingfunctionattributeandwhatthegeneratedassemblyco
stmtlevelsource
1
2externbitalarm;
3intalarm_count;
4externvoidalfunc(bitb0);
5
6voidfalarm(void)using3{
71alarm_count++;
81alfunc(alarm=1);
91}
ASSEMBLYLISTINGOFGENERATEDOBJECTCO
;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,theco
Note
Theusingattributemaynotbeusedinfunctionsthatreturnavalueinregisters.Youmustexerciseextremecaretoensurethatregisterbankswitchesareperformedon
Theusingattributeismostusefulininterruptfunctions.Usuallyadifferentregisterbankisspecifiedforeachinterruptprioritylevel.Therefore,youcouldassignon
CopyrightKeil,AnARMCompany.Allrightsreserved.
假定你跟你老板共用一張辦公桌,每次你老板來了你就得讓開桌子給他用,等他用完,桌上原來的擺放已經(jīng)亂七八糟了.
高人出了個主意,桌子仍共用,但桌面上的板子可整塊拆走,老板來了,換另一塊桌板就好了,等他走了,把你的板搬回來.
51這張桌子上一共配了4張桌板,使用usingn來拆換編號為n的桌板.
評論