Page 1 of 1

Configuring Ports

PostPosted: Tue Mar 22, 2022 4:30 am
by birghtnight
I'm using the PIC16F15313 which only has one TRISA PortA that's 6 bits wide (RA0 - RA5). When I configure a pin to be an output to blink and LED I write:

TRISA = 0x00; //SETS PORT A as an output

As I understand it this configure the entire port to be outputs. Does this mean that I can't have two of the pins to be inputs and the rest outputs? I don't actually need inputs but am curious if this is how it works.

Re: Configuring Ports

PostPosted: Tue Mar 22, 2022 1:20 pm
by ric
The PIC16F15313 is an enhanced "14 bit" core, so I have moved your topic to the correct forum.

TRISA = 0x00 sets all six PORTA pins to be outputs, but there's nothing stopping you setting some of them as inputs.
It's easier to see what is happening if you use a binary constant. e.g.
TRISA = 0b00101010; sets RA0, RA2 and RA4 as outputs, and RA1, RA3 & RA5 as inputs.
Each bit in the TRISA register controls one pin, as documented in the datasheet.

Re: Configuring Ports

PostPosted: Sat Mar 26, 2022 12:19 am
by birghtnight
Thanks that was very helpful. If PORTA is only a 6bit wide bidirectional port, why write 8 bits to it like you did? I'm guessing your way is right but don't understand wahy.

TRISA = 0b00101010;

Re: Configuring Ports

PostPosted: Sun Mar 27, 2022 11:50 pm
by ric
This is an 8 bit processor, so all register writes are 8 bits.
In this specific case, the top two bits of the TRISA register are not implemented, so it doesn't matter what you write to them.
You don't have to specify leading zero bits in a binary value, so you would get exactly the same results coding that as:
Code: Select all
TRISA = 0b101010;

Re: Configuring Ports

PostPosted: Sun Mar 27, 2022 11:52 pm
by ric
n.b. if you have a look at "EXAMPLE 14-1: INITIALIZING PORTA" in the PIC16F15313 datasheet, you will see that common assembly language to set the TRISA register loads an 8 bit value into the W register, then copies W to TRISA.