Вы находитесь на странице: 1из 24

CHP 3

INPUT OUTPUT PORTS PROGRMMING

Lesson Learning Outcomes :


At the end of this topic, students should be able to:
Identify input and output (I/O) pins and their function Code PIC instruction for I/O handling Code I/O bit-manipulation programs for the PIC Examine the bit addressability of PIC ports

Input and Output (I/O) pins

PIC18 Pin Diagram

Input and Output (I/O) pins


PIC18 has many ports
Depending on the family member Depending on the number of pins on the chip

Each port can be configured as input or output. Bidirectional port


Each port has some other functions Such as timer , ADC, interrupts and serial communication.

Input and Output (I/O) pins


Each port has three registers for its operation. These registers are:
TRIS register (data direction register)
If the corresponding bit is 0 Output If the corresponding bit is 1 Input

PORT register (reads the levels on the pins of the device) LAT register (output latch)

Input and Output (I/O) pins


Consist of :
PORTA (RAx) - 6 pins PORTB (RBx) 8 pins PORTC (RCx) 7 pins PORTD (RDx) 8 pins PORTE (REx) 4 pins VUSB Vdd (Vcc) Vss (GND) OSC1,OSC2 MCLR( reset)

PORTA, TRISA and LATA Registers


PORTA is an 8-bit wide, bidirectional port. 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. The Data Latch register (LATA) is also memory mapped.Useful for read-modify-write operations on the value driven by the I/O pins.

PORTA, TRISA and LATA Registers

Registers associated with Port A

Port A as Input

Port A as Output

PORT B, PORT C, PORT D and PORT E


PORTB is 8 pins PORTC is 7 pins PORTD is 8 pins PORTE is 4 pins

PORTB, TRISB and LATB Registers


PORTB is an 8-bit wide, bidirectional port. The corresponding data direction register is TRISB. Setting a TRISB bit (= 1) will make the corresponding PORTB pin an input Clearing a TRISB bit (= 0) will make the corresponding PORTB pin an output. The Data Latch register (LATB) is also memory mapped.Useful for read-modify-write operations on the value driven by the I/O pins.

PORTC, TRISC and LATC Registers


PORTC is an 7-bit wide, bidirectional port. The corresponding data direction register is TRISC. Setting a TRISC bit (= 1) will make the corresponding PORTC pin an input Clearing a TRISC bit (= 0) will make the corresponding PORTC pin an output. The Data Latch register (LATC) is also memory mapped.Useful for read-modify-write operations on the value driven by the I/O pins.

PORTD, TRISD and LATD Registers


PORTD is an 7-bit wide, bidirectional port. The corresponding data direction register is TRISD. Setting a TRISD bit (= 1) will make the corresponding PORTD pin an input Clearing a TRISD bit (= 0) will make the corresponding PORTD pin an output. The Data Latch register (LATD) is also memory mapped.Useful for read-modify-write operations on the value driven by the I/O pins.

PORTE, TRISE and LATE Registers


PORTE is an 4-bit wide, bidirectional port. The corresponding data direction register is TRISE. Setting a TRISE bit (= 1) will make the corresponding PORTE pin an input Clearing a TRISE bit (= 0) will make the corresponding PORTE pin an output. The Data Latch register (LATE) is also memory mapped.Useful for read-modify-write operations on the value driven by the I/O pins.

I/O Bit Manipulation Programming

Single Bit Instructions for PIC18

BSF (bit set fileReg) BCF (bit clear fileReg)


BSF fileReg, bit_num
- to set HIGH a single bit of a given fileReg.

BCF fileReg, bit_num


- to clear a single bit of a given fileReg.

Example: BSF PORTD,2 BCF PORTD,5

;Set bit RD2 ;Clear bit RD5

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.

Answer

BTFSS (bit test fileReg, skip if set) BTFSC (bit test fileReg, skip if clear)

EXAMPLE

EXAMPLE

EXAMPLE