PIC I/O Port Programming
Chapter 3
PIC18F4550 Pin Diagram
I/O Ports on PIC18F4550
Consist of five ports: PORTA (RAx) 7 Pins PORTB (RBx) 8 Pins PORTC (RCx) 8 Pins PORTD (RDx) 8 Pins PORTE (REx) 3 Pins
- 3 SFR PORTx. TRISx, and LATx
TRIS Register (TRIState)
All the five ports can be used as either output or input.
If If
TRISx = 0 ; PORTx = output TRISx = 1 ; PORTx = input
Setting up the I/O Ports
MOVLWB11110000 MOVWFTRISB;RB7,RB6,RB5,RB4 AS INPUT
;RB3,RB2,RB1,RB0 AS OUTPUT
CLRFTRISB;clear all TRISB bits PORTB output
SETFTRISC;set all TRISC bits PORTC input
Writing output bits to I/O PORT
CLRFTRISB;set PORTB as output MOVLW0x55; W=01010101 MOVWFPORTB;PORTB = 01010101
Reading input bits from I/O PORT
SETFTRISC;set PORTC as input MOVFPORTC,W;get PORTC & move to W MOVWFMYREG;MYREG = 01010101
Study Example 4-1
Write a test program for the PIC18 chip to toggle all the bits of PORTB, PORTC, and PORTD every of a sec. Assume crystal frequency is 4 MHz.
#include <P18f4580.inc> R1 equ 0x07 R2 equ 0x08 ORG 0 CLRF TRISB ;make Port B an output port CLRF TRISC ;make Port C an output port CLRF TRISD ;make Port D an output port MOVLW 0X55; WREG = 55h MOVWF PORTB;put 55H to Port B pins MOVWF PORTC ;put 55H to Port C pins MOVWF PORTD ;put 55H to Port D pins LOOPCOMF PORTB, F;toggle bits of Port B COMF PORTC, F ;toggle bits of Port C COMF PORTD, F ;toggle bits of Port D CALL DELAY BRA LOOP
8
QDELAY MOVLW D 200 MOVWFR1 D1MOVLW D 250 MOVWFR1 D2NOP NOP DECF R2, F BNZD2 DECFR1 BNZD1 RETURN END
Inst Cycle Duration = 4/fosc = 1 Sec Delay = 250 X 200 X
I/O Bit Manipulation
10
BIT-ORIENTED OPERATIONS
1. Some times, we are interested to access only 1 or 2 bits of the port instead of the entire 8 bits. 2. BIT Oriented operation are supporting to do the above things in PIC18
11
BSF (bit set fileReg) BCF (bit clear fileReg)
Syntex:
BSFfileReg, bit_num BCFfileReg, bit_num Example: BSFPORTD,2;Set bit RD2 BCFPORTD,5;Clear bit RD5
12
Study Example:
An LED is connected to each pin of PORT D. Write a program to turn on each LED from pin D0 to pin D7. Call a delay module before turning on the next LED CLRFTRISD;Make PORT D an output BSFPORTD, 0; Bit set turns on RD 0 CALL DELAY BSF PORTD,1 ; Bit set turns on RD 1 CALL DELAY ----------------------------------------------------------------------------------------BCFTRISB, 2 ;bit = 0, make RB2 an output pin AGAINBSFPORTB,2 ; bit set (RB2 = high) CALL DELAY BCFPORTB, 2 ; bit clear (RB2=low) CALL DELAY BRA AGAIN
13
Exercise 1
Write a program to generate 1kHz square wave with 80% duty cycle on bit3 of PORTC. Prepare the flowchart.
14
BTFSS (bit test fileReg, skip if set) BTFSC (bit test fileReg, skip if clear)
In order to make a decision on based on the status of a given bit in the file register, we use the following instructions.
BTFSS - to monitor the status of single bit HIGH
BTFSC to monitor the status of single bit LOW
15
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
16
Example 4-4 (Solution) BSFTRISB,2; RB2 as input
CLRFTRISC; PORT C as outputBCF PORTD,3; RD3 as output MOVLW0x45; WREG = 45h AGAINBTFSSPORTB,2 ; bit test RB2 for HIGH BRAAGAIN; Keep checking LOW MOVWFPORTC; issue WREG to Port C BSFPORTD,3; bit set file reg RD3 (H-L) BCFPORTD,3; bit clear file reg RD3 (L)
17
Example 4-5
BSFTRISB,3 BCFTRISC,5 HEREBTFSC PORTB,3 BRAHERE BSFPORTC,5 BCFPORTC,5 BRAHERE
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.
18