You need to look at the PIC16F877A data sheet page 51 which says that port D is a parallel Slave Port.
I think you need to disable the PSP.