# PIC16F84A - Loop not working on MPLAB

Status
Not open for further replies.

#### dannyelloko20

##### Newbie level 5
I need to sum and subtract two hex values as it is shown in my code below. However, when I export the hex file to Pickit2 and then to my board, it will not go to SUM for some reason. Can you guys give me a hint as to why this is not working?
Thank you!

Code:
***********************************************************************
#include <GENERAL.H>
; ================	EQUATES  =========================================

NUM1		EQU		0x5A
NUM2		EQU		0x39

REG1       EQU     0x40
REG2       EQU     0x41
; =======================================================================

__CONFIG	0X3FF2		;This is the control bits for CONFIG register
ORG			0X0000		;RESET or WDT reset vector
GOTO		START
ORG			0X0004		;Regular INT vector RESERVE THIS SPACE.  DON'T USE IT
RETFIE

START
CLRW
BSF      STATUS,RP0
MOVWF    TRISB
BCF      STATUS,RP0
MOVWF    PORTB

BTFSC   PORTA,0			        ;skip next instruction if RA0 = 0 and go to SUM
GOTO     SUM

CLRW
MOVLW    NUM2				;put NUM2 in W
SUBLW    NUM1				;subtract NUM2 in W from NUM1
MOVWF    REG1				;save the result in REG1
MOVF     STATUS,W			;copy the STATUS register value to W (not necessary but requested by professor)
MOVWF    REG2				;and then to REG2 (not necessary but requested by professor)
MOVWF    PORTB
GOTO     DONE

SUM
CLRW
MOVLW    NUM1				;put NUM1 in W
MOVWF    REG1				;save the result in REG1
MOVF     STATUS,W			;copy the STATUS register value to W
MOVWF    REG2				;and then to REG2
MOVWF    PORTB

DONE
END

#### Aussie Susan

When you jump to the "DONE" label, what are you expecting the device to do?
Remember that there is not operating system for the program to exit to.
Almost without exception, code for embedded devices operate in an infinite loop.
How do you know that it is not taking the path you expect?
By the way, you really should write to the LAT registers - the general rule (to avoid the read-modify-write problem) is to read from the PORT and write to the LAT.
Susan

#### betwixt

##### Super Moderator
Staff member

Dannyelloko20 - If you wish to add to an ongoing thread please add your new post to it instead of starting a new one. It lets us see the history of what you have done so we don't go over the same issues again.

Susan - No LAT registers in the 16F84A :shock:

Brian.

#### Aussie Susan

Susan - No LAT registers in the 16F84A :shock:
Brian.
My bad - I saw the "Data Latch" label in the data sheet and "just assumed"!!! Sigh
However it means the OP needs to be even more careful about the RMW problem!
Susan

Status
Not open for further replies.