Chapter 3
INPUT OUTPUT PORTS
PROGRAMMING
PIC18 has many ports
Depending on the family member
Depending on the number of pins on the chip
Each port can be configured as input or output.
Bidirectional port
Each port has some other functions
Such as timer , ADC, interrupts and serial communication.
Some ports have 8 bits, while others have not
PIC18F4550 connection diagram
Features and Functions
In order pins operation can match internal 8-bit
organization, (all of them are similar to registers)
grouped into five so called ports denoted by A, B, C, D
and E. They all have several features in common:
many I/O pins have two or three functions. If a pin is
used as any other function, it may not be used as a
general purpose input/output pin
Every port has its corresponding TRIS register:
TRISA, TRISB, TRISC etc. which determines
performance, but not the contents of the port bits.
By clearing some bit of the TRIS register
(bit=0), the corresponding port pin is
configured as output.
By setting some bit of the TRIS register
(bit=1), the corresponding port pin is
configured as input.
This rule is easy to remember 0 = Output, 1 = Input
PIC pin function
setting
Port A
PORTA is a 7-bit wide, bidirectional port.
The corresponding Data Direction register is
TRISA.
Setting a TRISA bit (= 1) will make the
corresponding PORTA pin an input
Clearing a TRISA bit (= 0) will make the
corresponding PORTA pin an output
On a Power-on Reset, these pins are configured
as inputs and read as 0
Example: Port A as Output
Example: Port A as Input
Example: more efficient way to set port as
input or output
PORT B, PORT C, PORT D and PORT E
PORTB is 8 pins
PORTC is 8 pins
PORTD is 8 pins
PORTE is 3 pins
Read followed by write
operation
Be careful
Dont have two I/O operations one right after
the others.
Data Dependency
A NOP is needed to make that data is written
in the WREG before it read for outputting to
PortB
Dual role of microcontroller port
Port A and B
Port A usually multiplexed with Analog to Digital converter to
save I/O pins. Port A seldom used for simple I/O operation
Port A
Port B
Dual role of microcontroller port
Port C and D (alternate function)
Dual role of microcontroller port
Port E (alternate function)
Port E occupies a total of 3 pin (RE0-RE2) for
3 additional analog input or I/O.
AN5, AN6 and AN7
I/O Bit Manipulation Programming
I/O ports and bit-addressability
Monitoring a single bit
Reading a single bit
I/O ports and bit addressability
Single bit addressability for PIC18
Bit Oriented Instruction for PIC18
Single bit (Bit oriented) instruction for PIC18
BSF (bit set fileReg)
Used to set HIGH for a single bit in fileReg
The Syntax :
BSF fileReg, bit_num
20
BCF(bit clear fileReg)
Used to clear a sigle bit of given fileReg
The Syntax:
BCF fileReg, bit_number
Checking an input pin
To make decisions base on the status of a
given bit in the file register, we use:
BTFSC (bit test fileReg skip if clear)
BTFSS (bit test fileReg skip if set)
These single-bit instruction allow to
monitor a single bit and make decision
whether it is 0 or 1.
Reading a single bit
We can use bit test instruction to read the
status of the single bit and send it to
another bit of save it
Reading input pins VS. LATx
port
There are two possibilities to read ports value
Through reading the status of the input pin
Through reading the internal latch of the LAT register.
Some instructions do that
The action is
The instruction read the latch instead of the pin
Execute the instruction
Write back the result to the Latch
The data on the pins are changed only if the TRISx
The PIC uCs bits are cleared.
Reading input pins VS. LATx
port