4) PIC IO Port Programming
4) PIC IO 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
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
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
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