TODAY’S LECTURE
I/O Port Programming in PIC
Microcontroller
I/O Bit Manipulation Programming
Objective
List all the ports of the PIC18
Describe the dual role of PIC18 pins
Code Assembly to use ports for input or
output
Code PIC instructions for I/O handling
Code I/O bit-manipulation Programs for
PIC
Explain the bit addressability of PIC ports
I/O Port Programming in PIC18
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
I/O Port Programming in PIC18
CALL instruction is used to call a
subroutine.
Subroutines are often used to perform
tasks the need to performed frequently.
In the PIC18 there are two instruction for
call
CALL (long call)
RCALL (relative call)
I/O Special Function Registers
Each port has three registers for its
operation:
TRIS register (Data Direction register)
oIf the corresponding bit is 0 Output
oIf the corresponding bit is 1 Input
PORT register (reads the levels on the pins of
the device)
LAT register (output latch)
The Data Latch (LAT) register is useful for
read-modify-write operations on the
value that the I/O are driving.
I/O Special Function Registers
PIC18F458 has 5 Ports
Upon reset, all ports are
configured as input.
TRISx register has 0FFH
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
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 carful
Don’t have two I/O operations one right
after the others.
Data Dependency
A NOP instruction is needed to make that
data is written in the WREG before it
read for outputting to PortB
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
Bit Oriented Instruction for PIC18
BSF (bit set fileReg)
Used to set HIGH for a single bit in
fileReg
The Syntax :
BSF FileReg, Bit_number
BCF(bit clear fileReg)
Used to clear a single 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 port’s 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 μCs bits are cleared.
Some RMW Instructions
ANY QUESTIONS?
Anyone willing to present?
Time allowed : 5 mins
Topic : Any
Bonus Points : +3
References
Chapter 4, ‘PIC I/O Port Programming’
PIC Microcontrollers and Embedded Systems by
Muhammad Ali Mazidi
Wazen M. , Embedded System , Spring 2011.
‘Introduction to Real Time Embedded Systems’, v2-
IIT Kharagpur