51中断 用using 0
中断服务程序对编译器来说首要需要考虑的就是现场寄存器的入栈保护。也就是说,一段在主程序内正在运行的代码,向R1内写了0xFF做为中间变量。而此时,突然来了中断,那么为了保护原有数据不被破坏,就必须将原R1压入堆栈。而PUSH动作是要花费指令周期去执行的,为了提高代码执行速度,若一开始就规划好主程序使用第0和第1组寄存器,而中断服务程序则指定为第2组。那么,编译器在编译的时候就可以做优化,不必再执行原PUSH动作(应为两组寄存器的物理地址不同)。
所以,上述使用分组寄存器的办法是一种代码优化手段。
下附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.
假定你跟你老板共用一张办公桌,每次你老板来了你就得让开桌子给他用,等他用完,桌上原来的摆放已经乱七八糟了.
高人出了个主意,桌子仍共用,但桌面上的板子可整块拆走,老板来了,换另一块桌板就好了,等他走了,把你的板搬回来.
51这张桌子上一共配了4张桌板,使用usingn来拆换编号为n的桌板.
51中断用usin 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)