PIC output is high before being set high automatically

chi-tech-compilermicrocontrollerpic

I am creating a simple pic project that uses a PIC16F877A and I have noticed something that seems odd. Before using any code to change the state of the pins connected to port b, the port b pins are already high. Here is the code I am using in hi tech c with everything else removed to demonstrate the problem. I was wondering if it was normal for the pins to start high before being set in that state. If that is the case I could just set PORTB to 0 at first to solve the problem, I just wanted to make sure that nothing was wrong. This seems to be caused by the line TRISB = 0xFF but I can't understand why since that register only adjusts weather the pin is a input. If anyone requests it I can post my circuit diagram, however I was hoping to avoid having to draw schematic.

#include<htc.h>
#define _XTAL_FREQ 8000000
__CONFIG(UNPROTECT & PWRTDIS & WDTDIS & HS & LVPDIS); 

int main()
{
    TRISB = 0X00;

    while(1)
    {

    }
}

Best Answer

On reset, all port pins are configured as inputs. If they float high, and then you configure them as outputs, they will be driving high. So it's best to set all pins to their default state before writing to the TRIS registers to set them as outputs.