//1. 精确1ms延时函数的函数体及汇编格式:
//延时计算:t_delay = n*6t + n/128t + 8t (t为时钟周期)
void Delay_1ms_Preci(void) //1ms延时函数
{
unsigned int n;
n=1130; //8MHz
while (i--)
;
}
//延时1ms的调用:
void main(void)
{
Delay_1ms_Preci();
}
//汇编格式:
//延时时间计算: t_delay= 3t + 1t + 1t + n*6t + ( (int(n/256) + 1) * 2t -1t) + 4t
//即: t_delay= n*6t + int(n/128)t + 10t
//计数计算(1ms):n= (1000 * f - 10) * 128 / 769
// n= (1000 * 8 -10 ) * 128 /769 = 1329.93 (取整为n=1330, 再把1330去验证延时时间t_delay的计算)
_Delay_1ms_Preci:
i --> R16
004B E705 LDI R16,0x32 //1t --把值0x0532(1330)存入R16,R17
004C E014 LDI R17,5 //1t
004D 0118 MOVW R2,R16 //1t
004E 5001 SUBI R16,1 //1t
004F 4010 SBCI R17,0 //1t
0050 2022 TST R2 //1t
0051 F7D9 BNE 0x004D //2t/1t
0052 2033 TST R3 //1t
0053 F7C9 BNE 0x004D //2t/1t (BNE是M16中的BRNE)
0054 9508 RET //4t
_main:
0055 DFF5 RCALL _Delay_1ms_Preci //3t
0056 9508 RET
|