PIC 8 Port Settings

Thread Starter

mukesh1

Joined Mar 22, 2020
68
Hello
I have a general question for Port pin initialization. We set PORT to low or high using the PORT or LAT register

Sensor connected to Port B Pin 7
Led connected to Port C Pin 7

If I set PORTB = 0; PORTC = 0;

When the sensor is in high states it sends 5v to Port B Pin 7 and when It is in low states it sends 0v to Port B Pin 7

What will happens if I set PORTB = 1; PORTC = 1;
 

AlbertHall

Joined Jun 4, 2014
12,625
First you need to sort out TRIS to decide which pins will be outputs and inputs.
In general read the port state from the PORT register and write port values to the LAT register.
 

jpanhalt

Joined Jan 18, 2008
11,087
If you are using a PIC, 8-bit MCU, the ports come up as analog inputs. You need to set to digital (ANSEL) and output or input as desired (TRIS). You may also need to turn off comparators, if present.
 

BobTPH

Joined Jun 5, 2013
11,515
Each pin is set as either an input or an output by the TRIS register for that port. If a pin is an input, it does not matter what the micro writes to the LAT register, it is ignored. And, to repeat what others have said, do not write to the PORT register. It can cause unexpected effects.

To be pedantic, it is only a read / modify / write operation that is problematic, a write to the whole port works correctly. But get in the habit of writing only to the LAT register to avoid mistakes.

Bob
 

Thread Starter

mukesh1

Joined Mar 22, 2020
68
For example,
Sensor connected to Port B Pin 7
Led connected to Port C Pin 7

PORTx register- Read or write to PORTx
LATx register. - Write to PORTx
TRISx - Configure as input or output

If I set
TRISB = 0x80; TRISC = 0x80;
PORTB = 0x00; LATC = 0x00;

PORTB = 0x00; What will happens if I set PORTB = 0xff; ?

What will happen if i set PORTx = 0 or PORTx = 1
 

atferrari

Joined Jan 6, 2004
5,011
To the OP: are you aware that the variety of possible micros is huge? What is exactly a PIC 8 for you?

Why do you think I asked in post #3 to say what micro are you using?
 

jpanhalt

Joined Jan 18, 2008
11,087
PORTB = 0x00; What will happens if I set PORTB = 0xff; ?

What will happen if i set PORTx = 0 or PORTx = 1
PORTx = 0 probably clears the port. PORTx = 1 may only set bit<0> =1. I think PORTx = 0xFF is a safer way to set all bits to 1. Remember, not all ports of your device may be 8 bits. If it's a PIC16F1829, and the port is only bits<4..7> P0RTx = 1 may not do anything.

You really do need to tell us the device you are using for meaningful responses.
 
Top