Sie sind auf Seite 1von 5

Islamic University Gaza

Engineering Faculty
Department of Computer Engineering
ECOM 3022: Embedded Systems Discussion

Chapter 4
PIC I/O PORT PROGRAMMING

Eng. Eman R. Habib


February, 2014

Embedded Systems Discussion

I/O Port Programming in PIC18


-

PIC 18F458 has 40-pin chip, 33 pins are set aside for the five ports PORTA, PORTB,
PORTC, PORTD, and PORTE.
Each port has some other functions, such as timer, ADC, interrupts and serial
communication.
Port A has 7 pins; Ports B, C, and D each have 8 pins; and Port E has only 3 pins.
Each port can be configured as input or output.

I/O port pins and their functions


-

Each port has three SFRs associated with it:


TRIS register (Data Direction register)
o If the corresponding bit is 0 Output
o If the corresponding bit is 1 Input
PORT register (reads the levels on the pins of the device)
LAT register (output latch)
The Data Latch (LAT) register is useful for read-modify-write operations on the value
that the I/O ports are driving.
On a Power-on Reset, configured as inputs and read as 0
For example, for Port B we have PORTB, TRISB, and LATB.

Read followed by write I/O operation


We must be careful not to have two I/O operations one right after the other.
MOVF PORTC, W
NOP
MOVWF PORTB
A NOP is needed to make that data is written in the WREG before it read for outputting to
PORTB.

BIT MANIPULATION PROGRAMMING

Embedded Systems Discussion

BSF (bit set fileReg)


Used to set HIGH for a single bit in fileReg.
Example:
BSF PORTB, 2
;RB2 = 1

BCF (bit clear fileReg)


Used to clear a sigle bit of given fileReg.
Example:
BCF PORTC, 4
;RC4 = 0

BTG (bit toggle tileReg)


Used to toggle a single bit in fileReg.
Example:
BTG PORTD, 3
;toggle RD3

BTFSS (bit test tileReg, skip it set)


This instruction tests the bit and skips the next instruction if it is HIGH.
Example:
BTFSS PORTB, 3 ; skip the next instruction if RB3 is HIGH

BTFSC (bit test tileReg, skip it clear)


This instruction tests the bit and skips the next instruction it is LOW.
Example:
BTFSC PORTB, 1 ; skip the next instruction if RB1 is LOW

PROBLEMS
11. Write a program to get 8-bit data from PORTC and send it to ports PORTB and PORTD.
SETF TRISC
; Define PORTC as input
CLRF TRISB
; Define PORTB as output
CLRF TRISD
; Define PORTD as output
MOVF PORTC, W
NOP
MOVWF PORTB
MOVWF PORTD

Embedded Systems Discussion

15. Write a program to toggle all the bits of PORTB and PORTC continuously
(a) using AAH and 55H (b) using the COMF instruction.
(a)
CLRF TRISB
CLRF TRISC
BACK MOVLW 55H
MOVWF PORTB
MOVWF PORTC
CALL DELAY
MOVLW AAH
MOVWF PORTB
MOVWF PORTC
CALL DELAY
GOTO BACK
(b)
CLRF TRISB
CLRF TRISC
MOVLW 55H
MOVWF PORTB
MOVWF PORTC
BACK CALL DELAY
COMF PORTB
COMF PORTC
BRA BACK
20. Write a program to toggle RB2 and RB5 continuously without disturbing the rest of the bits.
BCF TRISB,
BCF TRISB,
BACK BTG PORTB,
BTG PORTB,
CALL DELAY
BRA BACK

2
5
2
5

; Define PB2 as output


; Define PB5 as output

22. Write a program to monitor bit RC3. When it is HIGH, send 55H to PORTD.
BSF TRISC, 3
; Define PC3 as input
CLRF TRISD
; Define PD as output
BACK BTFSS PORTC, 3
BRA BACK
MOVLW 0x55
MOVWF PORTD

Embedded Systems Discussion

26. Write a program to get the status of RC3 and put it on RC4.
BSF TRISC, 3
BCF TRISC, 4
AGAIN BTFSS PORTC, 3
GOTO OVER
BSF PORTC, 4
GOTO AGAIN
OVER BCF PORTC, 4
GOTO AGAIN

Best Wishes

Das könnte Ihnen auch gefallen