# PIC16f84A Unable to delay properly

It was one of my first experiments related to embedded systems.I was following this document : http://groups.csail.mit.edu/lbr/stack/pic/pic-prog-assembly.pdf Problem emerged at Tutorial 6.I was trying to blink the led at different pace when pressed button.First, I took what tutorial give as an example word by word ,however, it didn't work.When not pressing it sometimes blinks slower sometimes faster, when I push button it blinks in an expected frequency.Then I changed the code to the program below for simplification:

LIST p=16F84A
INCLUDE "p16f84a.inc"

RES_VECT  CODE    0x0000
GOTO    START

MAIN_PROG CODE

TIME equ 0x0f
Delay

COUNT1 equ 0x0c
COUNT2 equ 0x0d
COUNT3 equ 0x0e

movlw   0xFF
movwf   COUNT1
LOOP1

movlw   0xFF
movwf   COUNT2
LOOP2

movfw   TIME
movwf   COUNT3
LOOP3
decfsz   COUNT3,1
goto LOOP3

decfsz   COUNT2,1
goto LOOP2

decfsz   COUNT1,1
goto LOOP1

return

START

bsf     STATUS,5
movlw   b'00011111'
movwf   TRISA
movlw   0x00
movwf   TRISB
bcf     STATUS,5
movlw   0x00
movwf   PORTB

MAIN_LOOP

movlw   0x01
btfss   PORTA,0
movlw   0x14
movwf   TIME

bsf     PORTB,3
call    Delay
bcf     PORTB,3
call    Delay

goto MAIN_LOOP

END


Same thing happened. While it flashed irregularly without pressing the button, when I pressed button at RA0 it blinked faster. I want first phase to be always slow. Where is the problem ? Is the problem in circuit, in software, or in PIC itself. Given physical conditions, as if btfss doesn't work properly either because the switch is problematic or because PIC is burnt. It is more likely that I am failure rather than the hardware is. Maybe there is a logical error in software in spite of similar program flow being expressed with different codes.Could oscillator not have been set up properly ? I had difficulty to set up an RC oscillator. When I add its R it works normal(or I think so). But after adding a capacitor and connecting its other terminal to negative voltage the led always shines.Can you say exactly where the issue lies? What's going on the background?

At first, I used RBO/INT to do bit test with the switch. But the INT part was suspicious to me. Then I changed it to RA0.(which is what is asked in the question) The result didn't change.

Additional Information 2 : Configuration bits -> __CONFIG _FOSC_EXTRC & _WDTE_OFF & _PWRTE_OFF & _CP_OFF

Additional Information 3 : It turns out that there is no any unintentional reset.Problem remains unsolved.

• At first, I used RBO/INT. But the INT part was suspicious to me. Then I changed it to RA0. Problem still remains unsolved :( – newbie_developer93 Jul 18 '16 at 6:35
• Whoever it is, someone deleted the first comment above. Nevertheless, I won't delete my answer because it may provide additional information to the other problem solver candidates for which I am thankful. – newbie_developer93 Jul 18 '16 at 7:10
• What are your configuration word settings? Did you disable the Watchdog? – Bruce Abbott Jul 18 '16 at 8:10
• You could add another LED that you flash once when the code initially starts running. This will let you know if the processor is restarting for some reason. – HandyHowie Jul 18 '16 at 9:34
• Have you got a pull up/down resistor on the input, so that the input is not floating when the button isn't pressed? – HandyHowie Jul 18 '16 at 9:55