The One With I - o Port Programming
The One With I - o Port Programming
Jagadanand NITC
• Each of the Ports, A-E, can be used as an
input or output port.
• For making a given port an input or output port,
SFR, TRISx is used.
• To make a port an output, write Os to the
corresponding TRIS register. To make it an input
port, write 1s to the corresponding TRIS register.
• To output data to any of the pins of the Port B, we
must first put Os into corresponding bits of the
TRISB register to configure it as an output port,
and then send the data to the Port B SFR itself.
• On Reset / Power On, all ports will have value FFH
in their TRIS registers.
• All the ports are configured as input ports by
default.
MOVLW OxO ;WREG = 00
MOVWF TRISB ;make Port B an output
port 0000 0000
L1 MOVLW Ox55 ;WREG - 55h
MOVWF PORTB ;put 55h on port B pins
CALL DELAY
MOVLW OxAA ;WREG = AA H
MOVWF PORTB ;put AAh on port B pins
CALL DELAY
GOTO Ll
If we are not configuring the PORT B as an output port by
making the TRIS B bits to zero, the data will not go from the
PORT B register to the pins of the PIC.
If we remove the first two lines of the above code, the 55H and
AAH values will not get to the pins.
They will be sitting in the SFR, Port B inside the CPU.
• Port A occupies a total of 7 pins (RAO-RA6).
• For the PIC18F452, pin A6 is also used for the OSC2
pin (Pin No. 14 of DIP).
• A6 is not available if we use a crystal oscillator to
provide clock to the PIC18 chip.
• To use the pins of Port A as either input or output
ports, each bit in SFR - ‘PORTA’ must be connected
to the pin by enabling the corresponding bits of the
TRISA register.
• The other PORTA pins are multiplexed with
analog inputs and the analog VREF+ and VREF-
inputs. The operation of each pin is selected by
clearing/setting the control bits in the ADCON1
register (A/D Control Register1).
•
• A/D Port Configuration Control Bits) PCFG0 –PCFG3 of
ADCON1 register must be ‘011x’ for making all the bits ‘Digital
I/O’. Then use TRISA to make the direction (In /Out).
RB 7 PGD
• Toggle the pins (bits), Read from ports, Write to
the ports (To any device connected to the pins)…..
and Port E.
• Timing issue – Avoid I/O operation – one right after
the other.
• CLRF TRISB
SETF TRISC
T1 MOVF PORTC, W
NOP ;Have this NOP to take care of data dependency
MOVWF PORTB
GOTO T1
Pipeline Activity After the Instruction Has Been Fetched
•0≤b≤7
• 0 ≤ f ≤ FF
• To monitor the status of a single bit for LOW.
• 0≤b≤7
• 0 ≤ f ≤ FF
A switch is connected to pin RB2. Write a program to
check the status of SW and perform the following:
(a) If SW = 0, send letter 'N' to PORTD.
(b) If SW = 1, send letter 'V' to PORTD.
BSF TRISB,2 ;make RB2 an input PORT
CLRF TRISD ;make PORTD an output port
AGAIN BTFSC PORTB,2 ;bit test RB2 for LOW
BRA OVER ;it must be HIGH
MOVLW A'N' ;WREG = 'N' ASCII letter N
MOVWF PORTD ;issue WREG to PORTO
BRA AGAIN ; we can use GOTO
OVER MOVLW A'Y' ;WREG = 'Y' ASCII letter Y
MOVWF PORTD ;issue WREG to PORTO
BRA AGAIN ; we can use GOTO
• when reading ports there are two possibilities:
•Read the status of the input pin.
•Read the internal latch of the LAT register.
•IIL = 1 uA
•IIH = 1 uA