1
\$\begingroup\$

In the original design of a timer RA4 is used as an output pin. The condition of the pin is set with BSF PORTA,OUTPUT or BCF PORTA,OUTPUT. OUTPUT being defined as D'4'. Bank selection unknown.

Since PORTA has it's own unique adress 05h i do not understand why the status does not change. Although not understanding I experimented and selected Bank1 prior to changeing the status of RA4 and that works.

I can leave it that way. But i dont like the change if I do not understand why.

Some enlightment is required.

; Not working without bank1    
BSF PORTA, OUTPUT or BCF PORTA, OUTPUT 

; working
BSF STATUS,RP0 ; Bank1
BSF PORTA,OUTPUT or BCF PORTA, OUTPUT 
BCF STATUS,RPO ; Bank0

From the datasheet PORTA is in Bank0 and not in Bank1 then why the selection of bank1 is working?

Not knowing the actual bank I could have saved the current STATUS but intend to do that when cleaning up after understanding.

Edit after comment. CMCON is set to 0x7.

\$\endgroup\$
5
  • \$\begingroup\$ Figure 4-3 on page 17 of the datasheet shows PORTA is in bank zero, not bank 1, something seems wrong here. Have you tried using BANKSEL PORTA instead of setting the STATUS register directly? \$\endgroup\$ Dec 3, 2016 at 20:44
  • \$\begingroup\$ I have seen the datasheet and do not understand why the selection bank1 is working. Did not use BANKSEL but are going to try. What is wrong with STATUS? \$\endgroup\$
    – Decapod
    Dec 3, 2016 at 20:50
  • \$\begingroup\$ Using STATUS means you need to know the bank. Using BANKSEL means you don't. Helps port code between different devices too. It does the same thing, just macros in the relevant include files. \$\endgroup\$ Dec 3, 2016 at 21:14
  • \$\begingroup\$ Another point, may be unrelated, but did you disable the comparator? If not, RA3 and RA4 are used as comparator outputs, which just might confuse things. Set CMCON to 0x07 to disable comparators. \$\endgroup\$ Dec 3, 2016 at 21:19
  • \$\begingroup\$ Every time I see someone writing PIC assembly I throw up in my mouth a bit. \$\endgroup\$
    – Matt Young
    Dec 4, 2016 at 0:13

1 Answer 1

4
\$\begingroup\$

There are a number of overlapping errors here:

  1. BSF STATUS,RP0 ; Bank1

    This is wrong. This instruction sets the bank to either 1 or 3 since nothing in the code or comments indicates that the RP1 bit is known to be 0.

  2. BSF STATUS, RPx is a bad way to do bank setting, at least at the source code level. Either manage both bank bits properly by using appropriate wrapper macros, or use the built-in BANKSEL.

  3. RA4 is a open drain output. That means you can't explicitly set it high. It can only be active low, or high impedance.

  4. PORTA is at address 5, which is in bank 0. However, TRISA is at 85h, which is at the same offset in bank 1. By setting the bank to 1, you are actually setting the corresponding TRIS bit. It may be that the TRIS bit is normally set, which would cause the PORTA bit setting to be irrelevant. If the PORTA bit is 0, then the TRIS bit is essentially the state of the pin, assuming a external pullup is present.

  5. You need to read the datasheet properly, not just skim what you think you need to know. The TRIS and PORT register functions, and the fact that RA4 is open drain are all well described. There really is no excuse for getting very basic stuff like this wrong.

    BANKSEL is a bit more obscure, but is also well documented in the MPASM, MPLIB, and MPLINK manual. That is, of course, another manual you must read before trying to write MPASM code.

\$\endgroup\$
1
  • \$\begingroup\$ I'd add that BSF and BCF are RMW instructions and the documentation ALSO clearly calls out the fact that when addressing PORTA, for example, that the port pins are read (not the internal latch), this is then modified and written to the port latch. So if using BSF/BCF to some other pin on PORTA happens at some later time, and RA4 is open collector (doing who knows what at the moment, externally), it is possible that the latch bit for RA4 gets modified unexpectedly. Keeping a copy of PORTA internally is often done to handle this. \$\endgroup\$
    – jonk
    Dec 3, 2016 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.