Hello everyone,

Before posting this, I've documented myself, but still I have the same annoying problem for one week now.
I have designed a circuit, using a 16F877A, that is intended to display different
messages on an HD44780 based LCD, when different keys are pressed, along with switching on a LED, corresponding to the key.

For my purpose I use PORT B, as 4bit and control lines for the LCD, and PORT D
as keys interface and LED driver.

The LCD works fine, but my problem is with PORT D. Even if it is switched to general purpose I/O port (bcf TRISE,PSPMODE), and configured accordingly to my needs (movlw b'00011111'; movwf TRISD), it acts chaotically, especially on RD 0 to 3, by interpreting the inputs as being key-pressed(0 V), and this combined with the fact that the corresponding LED is not lighting.

Did anyone had this problem also and has a solution to this?

Thank you in advance,

PS: Downwards is a chunk of my code, the init part and how I verify my PORTD imputs and command the LEDs.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
list p=16f877A ; list directive to define processor

#include <p16f877A.inc> ; processor specific variable definitions



__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF





ERRORLEVEL 0, -302



;;;;;;;;;;;;;;; pini necesari LCD;;;;;;;;

#define D7 PORTB,1

#define D6 PORTB,2

#define D5 PORTB,3

#define D4 PORTB,4

#define E PORTB,5

#define RS PORTB,7

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

lcd_port equ PORTB

;;;;;;;;;;;;;;;;;;;;;;;;;;;;; LCD





;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;BUTOANE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

btn_port equ PORTD

btn1 equ 0

btn2 equ 1

btn3 equ 2

btn4 equ 3

btn5 equ 4



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; iesiri

#define out5 PORTB,0

#define out4 PORTB,6

#define out3 PORTD,7

#define out2 PORTD,6

#define out1 PORTD,5



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; sfarsit definire iesiri

cblock 0x20

d1,d2,d3,var,var_dly,count

endc

;;;;;;;;;;;;;;;;;;;;;;;;; sfarsit declaratii etc







;;;;;;;;;;;;;;;;;;;;;;;;; progr principal

init_porturi:
; ports init
clrf STATUS

clrf PORTB

clrf PORTD

banksel TRISB

;bsf OPTION_REG,7

;bcf INTCON,4

clrf TRISB
; my LCD uses only 6 lines, and 2 pins are used for light switch on, and one for powering up the LCD for proper INITIALIZATION when a
PIC hardware RESET
bcf TRISE,PSPMODE

movlw 0x1f

movwf TRISD

banksel PORTB

clrf PORTB

clrf PORTD

clrf STATUS

call LCD_Init

; ;;;;;;;;;;;;;;;;;end of init side




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; main loop

pre_main:

call stare_de_veghe
; this is something for LCD
main:

;btfss btn_port,btn1

;call cmd_1

btfss btn_port,btn2

call cmd_2

btfss btn_port,btn3

call cmd_3

btfss btn_port,btn4

call cmd_4

btfss btn_port,btn5

call cmd_5

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;fina l main loop

cmd_1 ;; the same for rest of cmd mnemonics
; bla bla bla
bsf out1; set the LED for out1
call delay ; generic 1s delay
bcf out1; clear LED

;bla bla bla
return