# PIC assigning number to a port

I am confused with assigning number to a specific port. When I try:

LATA=31;


In this example when I check pins when by one, I get this result = 0b00001010. It is supposed to be 0b00011111. So cannot I assign number to a port? Or isn't it a proper way? Also let's say I want to make a simple communication by 2 PIC's with 8 PINS. Can I use

LATA=number;


From first PIC then

number=LATA;


From the other PIC. Would it work if timing is not a concern?

• The "latch" register (LATA) is not the only register that affects the state of the pins. What value does the driver enable register (TRISA) have? Related: On PIC18 series, when should I use LAT register and when PORT? and What happens when data is written to LATCH? – Dave Tweed Apr 8 '19 at 13:57
• I made TRISA =0; before using LATA=number; – Günkut Ağabeyoğlu Apr 8 '19 at 14:08
• What PIC are you using? – MartinF Apr 8 '19 at 14:13
• Page 251 of the datasheet reads: "The ANSELx bits default to the Analog mode after Reset. To use any pins as digital general purpose or peripheral inputs, the corresponding ANSEL bits must be initialized to ‘0’ by user software." So yes, that could be (one of) the solution(s). Try ANSELA=0; – MartinF Apr 8 '19 at 14:28
• Yup, that solved the issue, thanks for the guidance. – Günkut Ağabeyoğlu Apr 8 '19 at 14:29

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.

• This is a fine answer, but it doesn't include the specific info about ANSELx that you mentioned in the question's comments (and which helped the OP solve their problem). You may wish to edit your answer to include it! (comments may be deleted at any time in the future ...) – bitsmack Apr 8 '19 at 19:49
• @bitsmack I've updated as you've requested. Please note that the OP didn't mention his specific PIC in the question either - it was also in a comment. I don't know if the ANSELx method works for ALL PIC's, hence I updated my answer with an example for the specific PIC of the OP. That should solve any issues in the future as his comment might get deleted as well. – MartinF Apr 9 '19 at 18:05