BRA command in PIC16F877A

Thread Starter

Dragnovith

Joined Apr 16, 2021
22
Hi, on PIC18 it is possible to use BRA, but which command is relative to BRA on PIC16? Was it GOTO? For example I would like to use a BRA SENSOR, but on the PIC16.
 

Papabravo

Joined Feb 24, 2006
21,225
If the details of how the destination address is computed do not matter, then you can use the GOTO instruction to make an unconditional transfer of control to the destination address. In the PIC16 architecture you have 'skip' instructions which evaluate a condition and either execute the next instruction or skip it. I do not believe that the original PIC16 architecture (e.g. 16F84 & 16F877A) has the concept of a relative branch instruction. However it appears the the PIC16F1xxx family members do support the BRA and the BRW instructions.
 
Last edited:

Thread Starter

Dragnovith

Joined Apr 16, 2021
22
If the details of how the destination address is computed do not matter, then you can use the GOTO instruction to make an unconditional transfer of control to the destination address. In the PIC16 architecture you have 'skip' instructions which evaluate a condition an either execute the next instruction or skip it. I do not believe that the original PIC16 architecture (e.g. 16F84 & 16F877A) has the concept of a relative branch instruction. However it appears the the PIC16F1xxx family members do support the BRA and the BRW instructions.
All right, clarified me. Thanks!
 

Ian0

Joined Aug 7, 2020
9,805
BRA is an unconditional relative branch: execution transfers to <PC+constant>
GOTO is an unconditional absolute branch: execution transfers to <constant>
where <constant> is the number following the instruction.
Therefore, they do exactly the same thing, but the number following the instruction would be different.
 

Papabravo

Joined Feb 24, 2006
21,225
BRA is an unconditional relative branch: execution transfers to <PC+constant>
GOTO is an unconditional absolute branch: execution transfers to <constant>
where <constant> is the number following the instruction.
Therefore, they do exactly the same thing, but the number following the instruction would be different.
So sayeth the datasheet.
 

LesJones

Joined Jan 8, 2017
4,189
One other instruction that is available on the PIC18 but not on the PIC16 is the MOVFF instruction so you will have to modify the code to deal with this difference. Why not just use one of the PIC18 devices that are pin compatible with the PIC16F877A. For example the PIC18F452 or PIC18F4520.

Les.
 

atferrari

Joined Jan 6, 2004
4,768
Out of curiosity, may I know why would you port some (working code) in the 18F family to another in the 16F realm which is more limited and with the added banking complication?

IIRC they are almost 100% pin compatible.
 

Thread Starter

Dragnovith

Joined Apr 16, 2021
22
Out of curiosity, may I know why would you port some (working code) in the 18F family to another in the 16F realm which is more limited and with the added banking complication?

IIRC they are almost 100% pin compatible.
At my university I specifically have the PIC18F452 and PIC16F877A models. So, I wanted to have the code for both of them.

The code itself, excluding registers and BRA commands, is similar to the PIC16F877A. I hope I can make it work with these changes, although the register map and some other functionality are different.
 

MIS42N

Joined Jan 25, 2013
22
There is a GOTO in both PIC16 and PIC18 instruction set. On a PIC16 it is a 1 word instruction and does not have the complete destination address. On a PIC18 it is a 2 word instruction and does have the complete destination address.
On the PIC16, a GOTO concatenates part of the register PCLATH with the partial address in the GOTO to form the complete address. This is irrelevant if the program is less than 2048 code words, the GOTO partial address can address 2048 locations. With a bigger program things get complex, and it is necessary for the programmer (or compiler) to be aware if a GOTO or CALL is to a different code page.
So why use BRA on a PIC18? firstly, it is a one word instruction so saves a code word. Secondly, a piece of code using BRA to branch from one location to another can be placed anywhere in code memory, because it branches relatively. Code using GOTO has to be recompiled for each location it is loaded into.
Standard PIC16 instruction set, as used in PIC16F877, does not have a BRA instruction. The extended instruction set, as used in the PIC16F1xxx series does have BRA (but the range is much less than the PIC18).
 
Top