Adding a 1-second delay at 20 MHz (?)

Edit: PIC 16F684

Well, I have a simple 3 LED binary clock that counts from 0-7 and wants to add a delay of about 1 second between each light on.

I developed that each light should be in the form of a cycle, and I should use a counter to measure ticks, rollovers, etc.

I think the clock is 4 MHz, here is a screenshot of the manual: http://i.imgur.com/tJatQ.png

Here are the relevant excerpts from my code:

COUNT1 EQU 20h      ; Delay counter #1
COUNT2 EQU 21h      ; Delay counter #2

...

LOOP0
        MOVLW   TRIS_D0_D1      ; Move value defined in Constants to TRISA, to switch on LED 0.
        TRIS    PORTA           ;
        CLRF    PORTA           ; Clear all outputs.
        MOVLW   D0              ; Set the accumulator to the value of D0.
        MOVWF   PORTA           ; Move the accumulator to PORTA, to switch on LED 0.

    ; Using COUNTs to add a delay
        decfsz    COUNT1,1       ; Decrease COUNT1 by 1, and skip the next line if the result is 0.
        goto      LOOP0          ; If COUNT1 is 0, carry on. If not, go to LOOP0.   
        decfsz    COUNT2,1       ; Decrease COUNT2 by 1, and skip the next line if the result is 0.
        goto      LOOP0          ; If COUNT1 is 0, carry on. If not, go to LOOP0.

However, I am sure that I am imposing time, can someone give me a hand?

+3
source share
2 answers

: LOOP0 - , , , . , COUNT1 COUNT2 - - "", .

, , LOOP0 COUNT1 + COUNT2 . , . 510 . , PIC16 , 1 000 000 , 4 .

, 196392, 16- . , . , , . , . :

COUNT1 EQU 20h
COUNT2 EQU 21h

LOOP0
    ;do some stuff here
        ...

    ;delay loop starts here:
    ;assume COUNT1=0 and COUNT2=0
Delay_0
    decfsz COUNT1
    goto Delay_0
    decfsz COUNT2   ;COUNT1 = 0 so 0xff cycles have passed
    goto Delay_0
    goto LOOP0 ;both COUNT1 and COUNT2 = 0 - 196392 cycles have now passed

1 , , 2, . goto 2 , , , 767 (255 * 3 + 2). , ((255 * 3 + 2) + 3) * 255 + 2.

Dos4Ever . , .

, , PIC- .

+4

, , , 197119 ( 196392) ?

:

  • Delay_0 decfsz COUNT1
  • goto Delay_0
  • decfsz COUNT2
  • goto Delay_0

, , count1, 255 , {255 3 } decfsz 2 . , , , (d1F) d1F = 255 * 3 + 2 = 767 . , decfsz count2. , , decfsz count2; decfsz count2, goto Delay_0, 'goto' ( d1F). , , decfsz count2, goto Delay_0 d1F, count2 255. ... 254, 253... count2 index 1. , 255 . , , decfsz count2 ( 0). decfsz count2 ' 2 1. , (d1F + 3) * 255 + 2. "3" ( ) decfsz goto .

, , :

  • d2F = d1F + (d1F + 3) x255 + 2 = 767 + (767 + 3) x255 + 2 = 197119

, , :

  • d (n) _F = d (n-1) _F + {d (n-1) _F + 3} x255 + 2 = 256xd (n-1) _F + 767

  • d (n) _C = d (n-1) _C + {d (n-1) _F + 3} x {count_n-1} + 2

"F" d (n) _F d (n-1) _F , ZERO. 'C' d (n) _C , n- , . 'n' n- . 'n-1' (n-1) - .

, 2 , d (1) _F - , β„–1, "" (.. counter1 - ZERO 256); d (2) _F - - β„– 1 β„– 2 , 1 2 ZERO 256.

  • d (1) _C - - # 1 , count1 , .
  • d (2) _C - - β„–1 β„– 2 , count2 , .

, count_n INITIAL n- . , ZERO, "256". , EIGHT. , count1 = 0, count1 = 256 ( 0).

  • : d (0) _F = 0 d (0) _C = 0.

, 3- count1 = 1, count2 = 4 count3 = 2,

  • d (1) _F = 256xd (0) _F + 767 = 256x0 + 767 = 767

  • d (1) _C = 0 + {0 + 3} x {1 - 1} + 2 = 2

  • d (2) _F = 256xd (1) _F + 767 = 256x767 + 767 = 197119

  • d (2) _C = d (1) _C + {d (1) _F + 3} x {4 - 1} + 2 = 2 + {767 + 3} x3 + 2 = 2314

  • d (3) _F = 256xd (2) _F + 767 = 256x197119 + 767 = 50463231

  • d (3) _C = d (2) _C + {d (2) _F + 3} x {2 - 1} + 2 = 199438

:

  • Delay_0 decfsz count1
  • goto Delay_0
  • decfsz count2
  • goto Delay_0
  • decfsz count3
  • goto Delay_0

Kenny leong

0
source

All Articles