I am trying to use PORTA of PIC16F877A as digital output port. But somehow it does not seems to be working.

I am using MPLAB X IDE MPASM to assemble the code. Below is the code I am using to initialize PORTA -

banksel PORTA;
clrf PORTA;

banksel ADCON1
movlw 0x06
movwf ADCON1

banksel CMCON
movlw 0x07
movwf CMCON

banksel TRISA
movlw 0x00
movwf TRISA

banksel PORTA
movlw 0xFF
movwf PORTA

The LEDs at the output does not glow at all. What am I missing? I am not expecting RA4 to glow as I know it requires an external pull-up resistor but what about other LEDs.

Any help would be much appreciated. Thanks in advance.

  • 2
    \$\begingroup\$ Are you removing the programmer and allowing /MCLR to go high? Do you go into an infinite loop after the above code? Is the WDT disabled? \$\endgroup\$ Mar 17, 2014 at 12:55
  • \$\begingroup\$ Do you have resistors in series with the LEDs? Show a circuit digram, or at least a photo. \$\endgroup\$ Mar 17, 2014 at 13:03
  • \$\begingroup\$ Yes. This is configuration bits that were set --> __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF. The code is working with PORTD as expected. It seems that I am not able to configure PORTA as digital output port. Thanks. \$\endgroup\$
    – Swapnil
    Mar 17, 2014 at 13:04

2 Answers 2


It looks like this code, such as it is, is setting up the digital outputs correctly. It is good that you used BANKSEL to eliminate any issue of possibly bad bank setting. However, this code still has some serious problems:

  1. There is not a single comment in sight.

  2. How did this ever assemble!? Opcodes aren't allowed in column 1.

  3. Just using a HEX constant for a number of bit fields is irresponsible programming, such as your "MOVLW 0x07". The code should be explaining what each individual bit field is and what you are therefore setting it to. Something like this:

        banksel blonkcon1
        movlw   b'10010010'
                ; 1-------  enable the blonkulator module
                ; -XX-----  unused
                ; ---10---  prescaler of 7
                ; -----010  select single-shot operation
        movwf   blonkcon1

To test the pin setup by itself, disconnect the LEDs and just look at what the pins are doing with a scope. There is a good chance your problem is with the circuit. Show the complete circuit.

  • \$\begingroup\$ Thanks Olin for such a quick response. I noted your suggestions and would surely implement them in my code and in future as well. I will try to figure out what is wrong with the circuit as per your advice. I will keep posted it there are any updates. Thanks again. \$\endgroup\$
    – Swapnil
    Mar 17, 2014 at 13:08
  • 4
    \$\begingroup\$ +1 I've always wondered how to properly enable the blonkulator. \$\endgroup\$ Mar 17, 2014 at 13:35
  • \$\begingroup\$ I figured out where I was going wrong. The problem was with the code itself. I was switching OFF the LEDs elsewhere in the code on reset or power up. I was using 'bcf' instruction instead of 'bsf' and did not notice it. Maybe, I was not expecting such a lapse from myself. I somehow got lost and took time for me to figure out. Thank you all very much for your time and effort. I will be more careful next time. Especially, Olin, I have implemented your suggestions and are quite useful. Thanks. \$\endgroup\$
    – Swapnil
    Mar 24, 2014 at 5:45

You would do well to ensure the pins are not taken by the ADC, as they are following a Reset: PORTA pins are multiplexed with analog inputs and analog VREF input. The operation of each pin is selected by clearing/setting the control bits in the ADCON1 register (A/D Control Register1). The TRISA register controls the direction of the RA pins, even when they are being used as analog inputs. The user must ensure the bits in the TRISA register are maintained set when using them as analog inputs. Note: On a Power-on Reset, these pins are configured as analog inputs and read as '0' Hugh

  • \$\begingroup\$ From the code the OP posted, you can see that he is setting up TRIS, the A/D, and the comparator appropriately to have the pins be digital I/O. \$\endgroup\$ Mar 21, 2014 at 21:42

Your Answer

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

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