You are on page 1of 22

PIC Microcontrollers

Chapter 6: PIC I/O PORT PROGRAMMING

Gaby A.H- M.S. Fall 2017 - 2018


• In the PIC 18 family, there are many ports for I/O operations,
depending on which family member you choose.
 A total of 33 pins are set aside for the five ports PORTA, PORTB, PORTC,
PORTD, and PORTE.
 The rest of the pins are designated as Vdd (Vcc), Vss (GND), OSC1, OSC2,
MCLR (reset), ….
 To use any of these ports as an input or output port, it must be programmed, as
we will explain throughout this section.
 In addition to being used for simple I/O, each port has some other functions
such as ADC, timers, interrupts, and serial communication pins.
 Not all ports have 8 pins.
 Each port has three SFRs associated with it.
 They are designated as PORTx, TRISx, and LATx.
 TRIS stands for TRIState and LAT stands for LATch.
Gaby A.H- M.S. Fall 2017 - 2018
Gaby A.H- M.S. Fall 2017 - 2018
• TRIS register role in outputting data
▫ The TRISx SFR is used solely for the purpose of
making a given port an input or output port.
▫ To make a port an output, we write 0s to the TRISx
register.

MOVLW 0x0
MOVWF TRISB
L1 MOVLW 0x55
MOVWF PORTB
CALL DELAY
MOVLW 0xAA
MOVWF PORTB
CALL DELAY
GOTO Ll
Gaby A.H- M.S. Fall 2017 - 2018
• TRIS register role in inputting data
▫ To make a port an input port, we must first put ls into the TRISx register
for that port, and then bring in (read) the data present at the pins.
▫ Notice that 0 stands for out and 1 for in.
▫ This is easy to remember because 0 and O look alike the same way that I
looks like 1.

Gaby A.H- M.S. Fall 2017 - 2018


• Example:

• Example:

Gaby A.H- M.S. Fall 2017 - 2018


• Read followed by write I/O operation

Gaby A.H- M.S. Fall 2017 - 2018


• Example: Write a test program for the PIC 18 chip to toggle all the
bits of PORTB, PORTC, and PORTD every 1/4 of a second. Assume a
crystal frequency of 4 MHz.

Gaby A.H- M.S. Fall 2017 - 2018


list P=PIC18F458
#include P18F458.INC
R1 equ 0x07
R2 equ 0x08
ORG 0
CLRF TRISB
CLRF TRISC
CLRF TRISD
MOVLW 0x55
MOVWF PORTB
MOVWF PORTC
MOVWF PORTD
L3 COMF PORTB,F 4MHz/4=1MHz
COMF PORTC,F 1/1MHz=1 μs
COMF PORTD,F
CALL QDELAY
Delay = 250 x 200 x 5 MC x 1 μs
BRA L3 = 250,000 μs (if we include the
QDELAY overhead, we will have 250,800).
MOVLW D'200'
MOVWF R1
D1 MOVLW D'250'
MOVWF R2
D2 NOP
NOP
DECF R2, F
BNZ D2
DECF R1, F
BNZ D1
RETURN
END
Gaby A.H- M.S. Fall 2017 - 2018
• I/O ports and bit-addressability
▫ Sometimes we need to access only 1 or 2 bits of the port instead of the
entire 8 bits.
▫ A powerful feature of PIC I/O ports is their capability to access individual
bits of the port without altering the rest of the bits in that port.
▫ For all PIC ports, we can access either all 8 bits or any single bit without
altering the rest.

Gaby A.H- M.S. Fall 2017 - 2018


• Example: An LED is connected to each pin of Port D. Write a
program to tum on each LED from pin D0 to pin D7. Call a delay
module before turning on the next LED.

Gaby A.H- M.S. Fall 2017 - 2018


CLRF TRISD
BSF PORTD,0
CALL DELAY
BSF PORTD,1
CALL DELAY
BSF PORTD,2
CALL DELAY
BSF PORTD,3
CALL DELAY
BSF PORTD,4
CALL DELAY
BSF PORTD,5
CALL DELAY
BSF PORTD,6
CALL DELAY
BSF PORTD,7
CALL DELAY

Gaby A.H- M.S. Fall 2017 - 2018


• Example: Analyze the following code

Gaby A.H- M.S. Fall 2017 - 2018


• Example: Write the following programs:
▫ Create a square wave of 50% duty cycle on bit 0 of Port C.
▫ Create a square wave of 66% duty cycle on bit 3 of Port C.

BCF TRISC,0 BCF TRISC,3


HERE BSF PORTC,0 BACK BSF PORTC,3
CALL DELAY CALL DELAY
BCF PORTC,0 CALL DELAY
CALL DELAY BCF PORTC,3
BRA HERE CALL DELAY
BRA BACK

Gaby A.H- M.S. Fall 2017 - 2018


• Example: Write a program to perform the following:
▫ Keep monitoring the RB2 bit until it becomes HIGH;
▫ When RB2 becomes HIGH, write value 45H to Port C, and also send a
HIGH-to LOW pulse to RD3.

BSF TRISB,2
CLRF TRISC
BCF PORTD,3
MOVLW 0x45
AGAIN BTFSS PORTB,2
BRA AGAIN
MOVWF PORTC
BSF PORTD,3
BCF PORTD,3

Gaby A.H- M.S. Fall 2017 - 2018


• Example: Assume that bit RB3 is an input and represents the
condition of a door alarm. If it goes LOW, it means that the door is
open. Monitor the bit continuously. Whenever it goes LOW, send a
HIGH-to-LOW pulse to port RC5 to tum on a buzzer.

BSF TRISB,3
BCF TRISC,5
HERE BTFSC PORTB,3
BRA HERE
BSF PORTC,5
BCF PORTC,5
BRA HERE

Gaby A.H- M.S. Fall 2017 - 2018


• Example: A switch is connected to pin RB2. Write a program to
check the status of SW and perform the following:
▫ If SW= 0, send the letter 'N' to PORTD.
▫ If SW= 1, send the letter 'Y' to PORTD.
BSF TRISB,2 BSF TRISB,2
CLRF TRISD CLRF TRISD
AGAIN BTFSS PORTB,2 AGAIN BTFSC PORTB,2
BRA OVER BRA OVER
MOVLW A'Y' MOVLW A'N'
MOVWF PORTD MOVWF PORTD
GOTO AGAIN BRA AGAIN
OVER MOVLW A'N' OVER MOVLW A'Y'
MOVWF PORTD MOVWF PORTD
GOTO AGAIN BRA AGAIN

Gaby A.H- M.S. Fall 2017 - 2018


• Example: A switch is connected to pin RB0 and an LED to pin RB7.
Write a program to get the status of SW and send it to the LED.

BSF TRISB,0
BCF TRISB,7
AGAIN BTFSS PORTB,0
GOTO OVER
BSF PORTB,7
GOTO AGAIN
OVER BCF PORTB,7
GOTO AGAIN

Gaby A.H- M.S. Fall 2017 - 2018


• Example: A switch is connected to pin RB0. Write a program to get
the status of SW and save it in D0 of fileReg location 0x20.

MYBITREG EQU 0x20


BSF TRISB,0
AGAIN BTFSS PORTB,0
GOTO OVER
BSF MYBITREG,0
GOTO AGAIN
OVER BCF MYBITREG,0
GOTO AGAIN

Gaby A.H- M.S. Fall 2017 - 2018


• Reading input pins vs. LATx port
▫ In reading a port, some instructions read the status of the port pins while
others read the status of an internal port latch called LATx.
▫ Therefore, when reading ports there are two possibilities:
 Read the status of the input pin.
 Read the internal latch of the LAT register.
▫ We must make a distinction between these two categories of instructions
because confusion between them is a major source of errors in PIC
programming, especially where external hardware is concerned.

Gaby A.H- M.S. Fall 2017 - 2018


• Reading LATx for ports
▫ Some instructions read the contents of an internal port latch instead of
reading the status of an external pin.
▫ For example, consider the "COMF PORTB" instruction. The sequence of actions
taken when such an instruction is executed is as follows:
 The instruction reads the internal latch of the LATB and brings that data into
the CPU.
 This data is complemented.
 The result is rewritten back to the LATB latch.
 The data on the pins are changed only if the TRISB bits are cleared to 0s.
▫ From the above discussion, we conclude that the instructions that read the
port latch normally read a latch value, perform an operation, then rewrite
it back to the port latch. This is called read-modify-write. To use the read-
modify-write, the port must be configured as output.
Gaby A.H- M.S. Fall 2017 - 2018
• Some of the Read-Modify-Write Instructions

Gaby A.H- M.S. Fall 2017 - 2018

You might also like