I have an LCD hooked up to the A ports of a PIC16F877a (A0, A1, A2 etc). However, the display doesn't work because PORTA is configured as analog by default.

In an PIC18F46K22 I'd do it using ANSELA = 0x00; but there is no such option in the PIC16F877A. I looked everywhere on the internet and some solutions suggested turning off the ADCON1 and bits, but nothing, ABSOLUTELY nothing works.

I changed the connection pins to PORTD (D0, D1, D2 etc) and it worked just fine. The problem is the analog configuration of PORTA which I can't change no matter what I try.

I am using MikroC for the program and here's my code:

sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_bit;
sbit LCD_D4 at RA2_bit;
sbit LCD_D5 at RA3_bit;
sbit LCD_D6 at RA4_bit;
sbit LCD_D7 at RA5_bit;

// Pin direction
sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;
sbit LCD_D4_Direction at TRISA2_bit;
sbit LCD_D5_Direction at TRISA3_bit;
sbit LCD_D6_Direction at TRISA4_bit;
sbit LCD_D7_Direction at TRISA5_bit;

void main()

     ADCON1 = 0x00;
     ADCON0 = 0x00;
     TRISA = 0x00;
    while (1)

This site is my last hope, I don't even know if there's another human who uses this microcontroller in 2023 :D

EDIT: ANSELH doesn't exist either. Even ChatGPT fails to solve this issue.


1 Answer 1


You are setting ADCON1 incorrectly. Have a look in the example assembly code for PORTA initialisation in the datasheet:

ADCON1 initialisation

This corresponds to 0b0000_0110 being written to the ADCON1 register. Cross referencing that with register description in REGISTER 11-2:


shows this is the value required to set all of PORTA to digital.

Beyond that, pin RA4 is not a push-pull output - it is an open drain output (note the schematic is wrong, the output from the registers is actually !Q not Q):

RA4 schematic view

This means it can only pull the output to ground. You might be able to solve the problem by putting a pull up resistor from RA4 to VCC.

  • 1
    \$\begingroup\$ Thanks for the response, I did look at that part of the datasheet and updated my code accordingly. It still doesn't work as expected. I went the extra mile and disabled other multiplexed functions still to no avail. I guess It's time to look into "project settings" and all. Thanks again. \$\endgroup\$ Commented Jul 5, 2023 at 14:37
  • \$\begingroup\$ Odd 👀 - have you tried writing 0x00 to PORTA to initialise it as well as shown in the assembly example (CLRF PORTA)? Bit 5 is x at reset, which might affect the LCD init routine. Might be worth disconnecting the LCD and just trying to get some LEDs blinking! \$\endgroup\$
    – awjlogan
    Commented Jul 5, 2023 at 14:46
  • \$\begingroup\$ Yep, I did that as well, still didnt' work. At this point it's worth mentioning that after reconnecting the pins to PORTD everything once again works fine. \$\endgroup\$ Commented Jul 5, 2023 at 14:50
  • \$\begingroup\$ Ah - there's the problem - RA4 is an open drain output, not a push pull. It won't drive the LCD_D6 pin correctly. I'll edit the answer to reflect that... \$\endgroup\$
    – awjlogan
    Commented Jul 5, 2023 at 14:59
  • 1
    \$\begingroup\$ That solves the issue. I'm kind of glad the solution was something I couldn't have come up with since I'm only a sophomore-year intern and I thank you for your answer. \$\endgroup\$ Commented Jul 5, 2023 at 15:13

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.