0% found this document useful (0 votes)
278 views

The One With I - o Port Programming

The document discusses the I/O ports of PIC microcontrollers. It describes the various ports (A, B, C, etc.), their pin configurations, and the SFR registers used to control each port. It provides examples of configuring the ports as inputs or outputs and reading from or writing to individual port pins.

Uploaded by

yug varshney
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
278 views

The One With I - o Port Programming

The document discusses the I/O ports of PIC microcontrollers. It describes the various ports (A, B, C, etc.), their pin configurations, and the SFR registers used to control each port. It provides examples of configuring the ports as inputs or outputs and reading from or writing to individual port pins.

Uploaded by

yug varshney
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

•.

No. Of 18 Pin 28 Pin 40 Pin 64 Pin 80 Pin


Pins
Chip PIC18F1220 PIC18F2220 PIC18F458 PIC18F6525 PIC18F85
ports 25
Port A X X X X X
Port B X X X X X
X X X X
Port C
Port D X X X
Port E X X X
Port F X X
Port G X X
Port H X X
Port I X X
Port J X
Port K X
Port L X
• Before using thePorts, it must be programmed.

• In addition to being used for simple I/O, each port has


some other functions such as ADC, timers, interrupts, and
serial communication pins.
• Port A has 7 pins; Ports B, C, and D each have 8 pins; Port
E has only 3 pins.
• Three SFRs related to Ports are: PORTx, TRISx, and LATx.
•Eg. : For Port B we have PORTB, TRISB, and LATB.

•TRIS stands for TRIState and LAT stands for LATch.


SFR Address
PORTA F80H
PORTB F81H
PORTC F82H
PORTD F83H
PORTE F84H
LATA F89H
LATB F8AH
LATC F8BH
LATD F8CH
LATE F8DH
TRISA F92H
TRISB F93H
TRISC F94H
TRISD F95H
TRISE F96H
SFR

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).

• On a Power-on Reset, RA5 and RA3:RA0


are configured as Analog Inputs and read
as ‘0’. RA6 and RA4 are configured as
digital inputs.


• 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).

Legend: x = unknown, u = unchanged, - = unimplemented locations read as '0'. Shaded


cells are not used by PORTA.
MOVLW 0x07 ;ADCON1=7 or 6 TO MAKE PORTA
as a Digital port.
MOVWF ADCON1 ;By default, PORTA bits are analog and
reads '0'
MOVLW OxO ;WREG = 00
MOVWF TRISA ;make Port B an output
port 0000 0000
L1 MOVLW Ox55 ;WREG = 0101 0101 (55h)
MOVWF PORTA ;put 55h on port B pins
CALL DELAY
MOVLW OxAA ;WREG = 1010 1010 (Aah)
MOVWF PORTA ;put AAh on port B pins
CALL DELAY
GOTO Ll
• This code will continuously send out to Port A, the alternating values of 55H and AAH: ie,
it will toggle the port a bits continuously. ‘DELAY ‘ is the subroutine to be realised.
•To make all the bits of Port A to ‘input’ mode, TRISA
must be programmed by writing 1 to all its bits.
• Then data can be received from Port A pins.

SETF TRISA ;make Port A an input


port :TRISA = 1111 1111
MOVF PORTA, W
MOVWF Ox55 ;RAM location 55h = Value
Read from PORTA pins.
CALL DELAY
Read data from external world through PORTB and
Write it to PORTC after the required processing.
MOVLW B’00000000’ ;WREG = 00
MOVWF TRISC ;make Port C as an
output port
MOVLW B’11111111’ ;WREG - FFh
MOVWF TRISB ;make Port B as an
input port
L1 MOVF PORTB, W ;Read data from Port B to WREG
NOP
ADDLW D’10’ ;Add decimal 10 to it
MOVWF PORTC ;put AAh on port B
pins
CALL DELAY
GOTO Ll
Bit Function Bit Function
RA 0 AN0 / CVREF RB 0 INT 0
RA 1 ANI RB 1 INT 1
RA 2 AN2 / VREF- RB 2 INT2 / CANTX
RA 3 AN3 / VREF+ RB 3 CANRX
RA 4 TOCK1 RB 4
RA 5 AN4/SS/LVDIN RB 5 PGM

RA6 OSC2/CLKO RB 6 PGC

RB 7 PGD
• Toggle the pins (bits), Read from ports, Write to
the ports (To any device connected to the pins)…..

•Port C as input port, output port.

• Port D as input port, output port.

• Port E as input port, output port.

• Other roles of pins assigned to Port C, Port D

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

Pipeline Activity for Both Fetch and Execute


• We can access (Read from or Write to) the
individual bits of any of the ports (Instead of
accessing all the bits together).

• A powerful feature of PIC I/O ports - capability


to access individual bits of the port without
altering the rest of the bits in that port.
Instruction Function

BSF fileReg,bit Bit Set fileReg (set the bit: bit = 1)


BCF fileReg,bit Bit Clear filekeg (clear the bit: bit = 0)
BTG fileReg,bit Bit Toggle fileReg (complement the bit)

Bit test fileReg, skip if clear (skip next


BTFSC fileReg,bit
instruction ifbit = 0)

Bit test fileReg, skip if set (skip next


BTFSS fileReg,bit
instruction if bit = 1)
Port
PORTA PORTB PORTC PORTD PORTE
Bit
RA0 RB0 RC0 RD0 RE0 D0
RA1 RB1 RC1 RD1 RE1 D1
RA2 RB2 RC2 RD2 RE2 D2
RA3 RB3 RC3 RD3 D3
RA4 RB4 RC4 RD4 D4
RA5 RB5 RC5 RD5 D5
RA6 RB6 RC6 RD6 D6
RB7 RC7 RD7 D7
• To set HIGH a single bit of a given fileReg.

• fileReg can be any location in the file register


and bit is the desired bit number from 0 to 7.

• "BSF PORTA, 4“ sets HIGH bit 4 of Port A.

• “BSF 0x35, 2” make the third bit (b2) of file


register 35 to 1.
• To set LOW a single bit of a given fileReg.

• fileReg can be any location in the file register


and bit is the desired bit number from 0 to 7.

• "BCF PORTA, 4“ Clears (make it at 0 level) bit


4 of Port A.

• “BCF 0x35, 2” make the third bit (b2) of file


register 35 to 0.
BCF TRISB, 2 ;bit = 0, make RB2 an output pin
AGAIN BSF PORTB, 2 ;bit set (RB2 = high)
CALL DELAY
BCF PORTB, 2 ;bit clear (RB2 = low)
CALL DELAY
BRA AGAIN
BCF TRISB, 2
BACK BTG PORTB, 2 ;toggle pin RB2 only
CALL DELAY
BRA BACK
• To monitor the status of a single bit for HIGH.

• Tests the bit and skips the next instruction if it


is HIGH.

•0≤b≤7

• 0 ≤ f ≤ FF
• To monitor the status of a single bit for LOW.

• Tests the bit and skips the instruction right


below it if the bit is 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.

• Must make a distinction between these two


categories of instructions

• Confusion between them is a major source of


errors in PIC programming, especially where
external hardware is concerned.
•"COMF PORTB" instruction.
• The instruction reads the internal latch of the LATB
and brings that data into the CPU.
• This data is complemented.
• The result is rewritten back to the LATB latch.
• This is Read – Modify – Write.

• The data on the pins are changed only if the


TRISB bits are cleared to Os.

•. IOL = 8.5 mA

•IOH = 3 mA (Sourcing by the pin.)

•IIL = 1 uA

•IIH = 1 uA

You might also like