I am writing assembly code for the 16F84A microcontroller and have run into some issues. I've implemented a 1 second time delay code which repeats itself consecutively for a total of 3 seconds, instead of returning to the program it instead jumps back to 'start'. I've already tried the same code with a 3 second delay and I've run into similar issues, it doesn't seem to want to run a time delay above 2 seconds.

So I figured I would go here and see if anybody has an idea as to why this keeps happening. I realize my code isn't the very best and could use a lot of improvement so I apologize for that.


;Register Label Equates
PCL     EQU 02  ;Program Counter Low
PORTB   EQU 06  ;Port B Data Register
TRISB   EQU 86  ;Port B Data direction register
STATUS  EQU 03  ;Status register
RP0     EQU 05  ;Bank select bit
point   EQU 0D  ;GPR2 used as table pointer

;Variable definitions 
COUNT_1 RES 1   ;
COUNT_2 RES 1   ;

ORG 000
GOTO    start   ;Goto main program

;1 second delay subroutine.............................

delay   MOVLW   H'32'       ;(1 cycle)
        MOVWF   COUNT_1     ;(1 cycle)
OLOOP   MOVLW   H'7C'       ;(1 cycle)
        MOVWF   COUNT_2     ;(1 cycle)
ILOOP   NOP                 ;(1 cycle)
        DECFSZ  COUNT_2,F   ;(1 cycle) 
        GOTO    ILOOP       ;(2 cycles)
        DECFSZ  COUNT_1,F   ;(1 cycle)
        GOTO    OLOOP       ;(2 cycles)
        RETURN              ;return to main program

;Table of Output Code.......................

table   ADDWF   PCL     ;Add pointer to PCL
        RETLW   D'0'    ;Decimal 0

;Initialise Port B...................................
start   bcf     STATUS,RP0  ;Bank select 0
        clrf    PORTB       ;Clear Port B data latches
        bsf     STATUS,RP0  ;Bank select 1
        movlw   0x00        ;
        movwf   TRISB       ;Set port B lines to output
        bcf     STATUS,RP0  ;Bank select 0

;Main loop...........................................

        CLRF    point       ;Reset pointer
        MOVLW   D'0'        ;Point to ASCII 2
        CALL    table       ;access table...
        MOVWF   PORTB       ;and output to LEDs
        CALL    delay       ;Call 1 second delay
        CALL    delay       ;Call 1 second delay
        CALL    delay       ;Call 1 second delay
        END                 ;Terminate source code
  • 6
    \$\begingroup\$ Is your watchdog timer (WDT) enabled? Disable if so. \$\endgroup\$ – AlmostDone May 16 '18 at 12:21
  • \$\begingroup\$ Hello, AlmostDone! Sorry for the long reply. May I ask how you would disable the WDT? I am creating this code within MPLAB and this the first time I am hearing about this. \$\endgroup\$ – John May 17 '18 at 11:44
  • \$\begingroup\$ Look in the 16F84 datasheet for the configuration word, and in the config word look for WDTE. You can set this directly in MPLAB also. \$\endgroup\$ – AlmostDone May 17 '18 at 12:23
  • \$\begingroup\$ Additional reference: microchipdeveloper.com/8bit:wdt \$\endgroup\$ – AlmostDone May 17 '18 at 12:26
  • \$\begingroup\$ Also, in your header or include file, you should be able to add the following line: #pragma config WDTE=OFF \$\endgroup\$ – AlmostDone May 17 '18 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.