Configuring Ports

Enhanced mid-range devices. PIC12F1xxx and PIC16F1xxx

Configuring Ports

Postby birghtnight » Tue Mar 22, 2022 4:30 am

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.
birghtnight
 
Posts: 4
Joined: Wed Mar 09, 2022 1:29 am

Re: Configuring Ports

Postby ric » Tue Mar 22, 2022 1:20 pm

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.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Configuring Ports

Postby birghtnight » Sat Mar 26, 2022 12:19 am

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;
birghtnight
 
Posts: 4
Joined: Wed Mar 09, 2022 1:29 am

Re: Configuring Ports

Postby ric » Sun Mar 27, 2022 11:50 pm

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;
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Configuring Ports

Postby ric » Sun Mar 27, 2022 11:52 pm

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.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products


Return to 14-Bit Core (enhanced)

Who is online

Users browsing this forum: No registered users and 12 guests

cron