Chapter 3-PIC IO Port Programming
Chapter 3-PIC IO Port Programming
4-1
PIC Microcontroller and E b dd d S d Embedded Systems Muhammad Ali Mazidi, Rolin McKinlay and Danny Causey, F b D C s February
2007.
4-2
Objective
List all the ports of the PIC18 ll h f h P 1 Describe the dual role of PIC18 pins p Code Assembly to use ports for input or output Code PIC instructions for I/O handling Code I/O bit manipulation Programs for bit-manipulation PIC Explain h bi dd E l i the bit addressability of PIC ports bili f
4-3
4-4
Pins
Chip
PORT A F80H PORT B F81H PORT C F82H PORT D F83H PORT E F84H PORT F F85H PORT G F86H PORT H PORT J PORT K PORT L
X X
X X X
X X X X X
X X X X X X X X X
X X X X X X X X X X X
4-6
I/O SFR
Each E h port has three registers for its h h f operation:
TRIS register (Data Direction register)
If the corresponding bit is 0 If the corresponding bit is 1 h di bi i Output Input I
PORT register (reads the levels on the pins of the device) LAT register (output latch)
The D t L t h Th Data Latch (LAT) register is useful ist s f l for read-modify-write operations on the value that the I/O pins are driving. l th t th d i in
The PIC uCs 4-7
I/O SFR
PIC18F458 h 5 P 1 F4 has ports Upon reset, all p ports are configured as input TRISx register has 0FFH
Pins PORT A PORT B PORT C PORT D PORT E LATA LATB LATC LATD LATE TRISA TRISB TRISC TRISD TRISE
Address F80H F81H F82H F83H F84H F89H F8AH F8BH F8CH F8DH F92H F93H F94H F95H F96H
4-8
Port A
PORTA is a 7-bit wide, b d P b d bidirectional port. l
Sometimes A6 is not available. why?
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. 0
The PIC uCs 4-14
Example 1
BACK MOVLW MOVWF CALL MOVLW MOVWF CALL GOTO 0x55 PORTA DELAY 0xAA PORTA DELAY BACK MOVLW B00000000 MOVWF TRISA BACK B CK MOVLW 0 55 0x55 MOVWF PORTA CALL DELAY MOVLW 0xAA MOVWF PORTA CALL DELAY GOTO BACK
4-15
Example 2
MYREG EQU 0 20 0x20 MOVLW B11111111 MOVWF TRISA MOVF PORTA, w MOVWF MYREG F EG
4-16
4-17
Data D D t Dependency d
A NOP is needed to make th t data i k that d t is written into WREG before it read for outputting to PortB.
The PIC uCs
4-18
Two Solutions
Solution1
CLRF SETF L4 MOVF NOP MOVWF BRA L4
The PIC uCs The PIC uCs
Solution2
CLRF TRISB SETF TRISC L4 MOVFF PORTC, PORTB BRA L4 MOVFF is 4-byte instruction. instruction
4-20
Example 4-1
Write test W it a t t program for the PIC18 chip to toggle all th t t l ll the bits of PORTB, PORTC and PORTD d every 0.25 of a second. d (suppose ( that there is a 4 MHz) MH )
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
4-21
Solution
L3 COMF COMF COMF CALL BRA L3 PORTB,F PORTC,F PORTC F PORTD,F QDELAY
QDELAY MOVLW D'200' 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 4-22
Section 4-2
The PIC uCs 3-23
D0 D1 D2 D3 D4 D5 D6 D7
4-24
Bit Toggl File Register Bit Test File Register, skip if i il i ki BTFSC fileReg, bit clear Bit Test File Register, skip if BTFSS fileReg, bit set
The PIC uCs 4-25
Example 4-2
A LED is connected ED d to each pin of port D. D Write a program to turn on each LED from pin D0 to f D4.
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 PORTD,4 CALL DELAY
4-26
Example 4-3
Write the following h f ll programs A. Create a square wave of 50% duty y cycle on bit 0 of C
Solution 1 BCF HERE BSF CALL BCF CALL BRA TRISC,0 TRISC 0 PORTC,0 PORTC 0 DELAY PORTC,0 DELAY HERE
Example 4-3
Write the following h f ll programs A. Create a square wave of 50% duty y cycle on bit 0 of C
Solution 2
BCF TRISC,0 BACK BTF PORTC,0 CALL DELAY BRA BACK How many byte are H m b t used?
4-28
Example 4-4
Write a program to perform the following: a) Keep monitoring the RB2 bit until it becomes HIGH (1) b) When RB2 becomes HIGH, write value 45H to portC and send a HIGH to LOW plus to RD3 D3
The PIC uCs
BSF F TRISB,2 , CLRF TRISC BCF PORTD,3 PORTD 3 MOVLW 0x45 AGAIN BTFSS PORTB,2 , BRA AGAIN MOVWF PORTC BSF PORTD,3 CALL DELAY , BCF PORTD,3
4-29
Example 4-5
Bit B B RB3 is an input and d represents the condition of a d d f door alarm. Whenever it goes LOW, send a HIGHto-LOW pulse to RC5 to turn on a buzzer
4-30
Solution
BSF B F TRISB,3 B BCF TRISC,5 HERE BTFSC PORTB 3 PORTB,3 BRA HERE BSF B F PORTC,5 PORTC 5 BCF PORTC,5 CALL DELAY BRA HERE
The PIC uCs 4-31
BSF TRISB,0 BCF TRISB,7 AGAIN G N BTFSS PORTB,0 GOTO OVER BSF PORTB,7 GOTO AGAIN OVER BCF PORTB,7 GOTO AGAIN
4-32
Instruction Function ADDWF fileReg,d Add WREG from f BSF fileReg,bit fil R bit Bit Set fileReg S t fil R BCF fileReg,bit Bit Clear fileReg COMF fileReg,d Complement f INCF g fileReg,d Increment F SUBWF fileReg,d Subtruct WREG from f XORWF fileReg,d Exclusive-OR WREG with f Exclusive OR
The PIC uCs -
Chapter 4: Summary
We focused on the I/O Ports of the PIC. These ports used for input or output. Programming We discussed Bit manipulation m nip l ti n instructions
4-36