PIC 中的周期计数计算

Cycle Count Calculation in PIC

给你一个为工作在 4MHz 的 PIC16F877 编写的子程序 获得大约 30 毫秒的延迟。

1 COUNTER1 equ 0x20
2 COUNTER2 equ 0x21
3
4 delay_loop:
5
6 movlw d'30'
7 movwf COUNTER2
8
9 ; Loop1 body takes about 1ms
10 LOOP1:
11 movlw d'200'
12 movwf COUNTER1
13
14 ; Loop2 body takes about 5us
15 LOOP2:
16 nop
17 nop
18 decfsz COUNTER1, F
19 goto LOOP2
20
21 decfsz COUNTER2, F
22 goto LOOP1
23 return

Instruction Cycles
movlw          1
movwf          1
 nop           1
decfsz         1*
goto           1
return         2 

好吧,我无法在第 9 行和第 14 行得到相同的值。你能告诉我详细的计算结果吗?

首先,您的指令周期不正确。 GOTO 是两个循环,DECFSZ 是一个或两个循环,具体取决于结果。

因此,对于 LOOP2

LOOP2:
    nop                  ; 1 cycle
    nop                  ; 1 cycle
    decfsz COUNTER1, F   ; 1 cycle except at loop end, then 2 cycles
    goto LOOP2           ; 2 cycles

所以总共是 5 个周期,4MHz 时钟的周期时间是 1us1 是 5us。 COUNTER1 为 200,该内循环需要 200 * 5us = 1000us = 1ms。

现在是封闭循环,LOOP1:

LOOP1:
    movlw d'200'         ; 1 cycle
    movwf COUNTER1       ; 1 cycle

    ; 1ms delay from LOOP2 (removed for clarity)

    decfsz COUNTER2, F   ; 1 cycle except at loop end, then 2 cycles
    goto LOOP1           ; 2 cycles
    return               ; 2 cycles

所以 LOOP1 管家总共有 5 个周期(忽略最后的 RETURN),从内部循环添加到 1000us = 1005us。 COUNTER2 为 30,总延迟为 30 * 1005us = 30.15ms(提供或接受少量初始内务管理指令和最终 return,但你明白了)。

1请记住,这些 PIC 每条指令使用 4 个时钟周期,因此 4MHz 时钟以 1MIPs 执行,指令时间为 1us。