Can’t get PORTB value in pic18f2550

microchippicserialuart

I'm trying to send The PORTB to the serial port and receiving the data on pickit2 UART mode..

void main()
{
  TRISB = 0xFF;
  PORTB = 0;
  UART1_Init(9600); // Initialize UART module at 9600bps
  Delay_ms(100); // Wait for UART module to stabilize
  while (1)
  { // Endless loop
    UART1_Write(PORTB); // and send data via UART
    Delay_ms(500);
  }
}

I'm using MikroC pro. and the problem is I'm not getting proper data.

I've connected pullup resistors to each pin of PORTB and all i get is

RX: E0

which means only RB5, RB6, and RB7 are pulled up rest pins are low.

also noticed if i try to pull down any pin of portb then only these pins cause the value to change. any ideas what might be wrong?

Best Answer

According to the datasheet, on start-up RB4:RB0 are configured as analog inputs and will read as zero.

You need to make them digital inputs instead during initialisation, for example:

ADCON1 = 0x0E;

Alternatively, you can clear the PBADEN bit in the CONFIG3H register by changing your configuration settings.