PIC assigning number to a port
It is indeed possible to assign a number to a port like you want to do. But some of the pins might be in tri-state mode. In this mode they function as inputs. This is controlled by the TRISx register.

To change the whole A port to output, you can use:

TRISA=0;


This puts the port in output mode. Then, LATA=31; should give you the expected 0b00011111.

Depending on what PIC you are using, some of the pins might also be configured as a special purpose pin (peripheral function). Sometimes you have to disable that to make sure you can use all pins as output. Some pins might be configured as comparator or AD channel for instance. But it depends on what PIC you are using what peripheral functions are enabled on power-on-reset (POR).

E.g.: To disable AD channels for a port on the PIC18F26K83, use the ANSELx register. ANSELx=0; puts the pins in digital IO mode instead of analog mode. For port A that's:

ANSELA=0;


If you want to read a port (to receive data), you need to put the port in tri-state first, with TRISx, so you can read the data. For port A that is:

TRISA=0xFF;


Then you can read the value with:

number=PORTA;


So PORTx is for reading, LATx is for writing.



Not related to this specific case, but some PIC's also have comparators on board. E.g.: On the PIC16F630 they are enabled on POR. To make full use of port A as digital IO port, you need to turn them off:

CMCONbits.CM = 0b111;


That puts RA0, RA1 and RA2 in digital IO mode.

Other PIC's might have other peripherals that are enabled on POR. Look at the datasheet of the PIC you're using to know what they are and how to disable them if necessary.