0% found this document useful (0 votes)
117 views18 pages

4) PIC IO Port Programming

The document discusses I/O port programming for the PIC18F4580 microcontroller. It describes the I/O ports PORTA through PORTE and how to configure them as inputs or outputs using the TRIS register. It provides examples of how to set and clear individual bits on the ports using BSF, BCF, BTFSS and BTFSC instructions. It also gives examples of generating waveforms on the ports and monitoring the states of input bits to trigger outputs.

Uploaded by

Hamdi Ramlee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views18 pages

4) PIC IO Port Programming

The document discusses I/O port programming for the PIC18F4580 microcontroller. It describes the I/O ports PORTA through PORTE and how to configure them as inputs or outputs using the TRIS register. It provides examples of how to set and clear individual bits on the ports using BSF, BCF, BTFSS and BTFSC instructions. It also gives examples of generating waveforms on the ports and monitoring the states of input bits to trigger outputs.

Uploaded by

Hamdi Ramlee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

PIC I/O Port Programming

Chapter 4
PIC18F4580 Pin Diagram
I/O Ports on PIC18F4580
• Consist of:
▫ PORTA (RAx)
▫ PORTB (RBx)
▫ PORTC (RCx)
▫ PORTD (RDx)
▫ PORTE (REx)
TRIS Register
• Used for setting up the Ports as input or output

• If TRISx = 0 ; PORTx = output

• If TRISx = 1 ; PORTx = input


Setting up the I/O Ports
MOVLW B’11110000’
MOVWF TRISB ;RB7,RB6,RB5,RB4 AS INPUT
;RB3,RB2,RB1,RB0 AS OUTPUT

CLRF TRISB ;clear all TRISB bits – PORTB output

SETF TRISC ;set all TRISC bits – PORTC input


Writing output bits to I/O PORT
CLRF TRISB ;set PORTB as output
MOVLW 0x55 ; W=01010101
MOVWF PORTB ;PORTB = 01010101
Reading input bits from I/O PORT
SETF TRISC ;set PORTC as input
MOVF PORTC,W ;get PORTC & move to W
MOVWF MYREG ;MYREG = 01010101
Study Example 4-1
Analyze the program in Example 4-1
#include <P18f4580.inc>
R1 equ 0x07
R2 equ 0x08

Org 0
CLRF TRISB ;make Port B an output port
CLRF TRISC
CLRF TRISD
MOVLW 0X55
MOVWF PORTB ;put 55H to Port B pins
MOVWF PORTC
MOVWF PORTD
LOOP COMF PORTB, F ;toggle bits of Port B
COMF PORTC, F
COMF PORTD, F
CALL DELAY
BRA LOOP
BIT-ORIENTED OPERATIONS
BSF (bit set fileReg)
BCF (bit clear fileReg)
BSF fileReg, bit_num

BCF fileReg, bit_num

Example:
BSF PORTD,2 ;Set bit RD2
BCF PORTD,5 ;Clear bit RD5
Study Example:
CLRF TRISD ;Make PORT D an output
BSF PORTD, 0 ; Bit set turns on RD 0
CALL DELAY
BSF PORTD,1 ; Bit set turns on RD 1
CALL DELAY
• -----------------------------------------------------------------------------------------
BCF TRISB, 2 ;bit = 0, make RB2 an output pin
AGAIN BSF PORTB,2 ; bit set (RB2 = high)
CALL DELAY
BCF PORTB, 2 ; bit clear (RB2=low)
CALL DELAY
BRA AGAIN
Exercise 1
Write a program to generate 1kHz square wave
with 80% duty cycle on bit3 of PORTC. Prepare
the flowchart.
BTFSS (bit test fileReg, skip if set)
BTFSC (bit test fileReg, skip if clear)
• BTFSS - to monitor the status of single bit HIGH

• BTFSC – to monitor the status of single bit LOW


Example 4-4
Write a program to perform the following:
a)Keep monitoring the RB2 bit until it becomes
HIGH
b)When RB2 becomes HIGH, write value 45H to
PORTC, and also send a HIGH-to-LOW pulse to
RD3
Example 4-4 (Solution)
BSF TRISB,2
CLRF TRISC
BCF PORTD,3
MOVLW 0x45
AGAIN BTFSS PORTB,2
BRA AGAIN
MOVWF PORTC
BSF PORTD,3
BCF PORTD,3
Example 4-5
Assume RB3 is an input. If it goes LOW, it means that the door open.
Monitor the bit continuously. Whenever it goes LOW, sent a HIGH-to-LOW
pulse to port RC5.

BSF TRISB,3
BCF TRISC,5
HERE BTFSC PORTB,3
BRA HERE
BSF PORTC,5
BCF PORTC,5
BRA HERE
Study!!!
• Example 4-6
• Example 4-7
• Example 4-8
• Example 4-9

You might also like