0
votes

I have programmed a little ensambler with pic16F84A. It's a clock that it shows a seconds counter on port b using a 1cs delay routine.

    include P16F84A.INC
    list p=16F84A
org 0x00        ;Vector de reset
N   equ 0x0C
    goto Inicio     ;Salto incondicional al principio del programa
    org 0x05        ;Vector de interrupción

Retcs   bsf STATUS,RP0 ;Selección de la página 1
    movlw b'00000111' ;Inicialización del registro OPTION con un divisor de frecuencia
    movwf OPTION_REG ;de 256
    bcf INTCON,T0IF ;Borrado del bit de fin de cuenta
    bcf STATUS,RP0 ;Selección de la página 0
    movlw 0x27 ;Complemento a 2 del número de ciclos
    sublw 0x00 ;0x00-0x27
    movwf TMR0 ;Inicialización del registro TMR0 con la resta anterior          
Bucle   btfss INTCON,T0IF ;Comprobación del final de la cuenta
    goto Bucle ;Si no es el final, se sigue esperando
    return

RetNcs  movwf N
Bucle3  call Retcs
    DECFSZ N,1; Decrementa N en una unidad, si es 0 salta
    goto Bucle3
    return

Inicio  bsf STATUS,RP0      ;Se pone a 1 el bit 5 RP0 de STATUS (03h) y se pasa a la página 1   
    movlw b'00000000'   ;Se carga 0 en w
    movwf TRISB     ;Se configura TRISB (06h) como salidas.
    clrf PORTB      ;Se limpia el puerto
    movlw d'100'

Bucle2  call RetNcs     ;Retardo de 1 segundo
    incf PORTB      ;Se incrementa en 1 unidad el contenido de PortB
    goto Bucle2
    end

The problem is in "Bucle3" loop after call Retcs due to N return to 100 value, why?? I need to keep this routines format.

Thanks in advance!!!! And sorry for my english!

1

1 Answers

0
votes

The issue is banking. Remember on a PIC, you have limited address range. There are two issues: 1) Within Inicio, you set to bank 1 right away to write to port b direction register and then load W with the literal 100d. The first line of Bucle2 call RetNcs, which stores W register to N, but the status register is still pointing to bank 1 not 0. Then in Retcs, you set to bank 1 (already there) but reset to bank 0 before you return. To fix this, just reset to bank 0 before you clear portb. 2) The other issue you haven't seen yet is in Bucle2. Once you loop for N times (100) you will eventually return to Bucle2, incremetn Port B and loop..... but you forgot to reload W with 100d. If you have to keep this structure, add "movlw d'100'" right after the incf PortB.