Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

CHAPTER 3: INPUT OUTPUT PORTS PROGRAMMING

Outcomes
At the end of this chapter, you will be able to:
1. Identify input and output (I/O) pins and their function
2. Explain dual role ports of data I/O

3.1 I/O Ports pins and their function


PIC18F4550 belongs to pic18f family of microcontrollers from the microchip technology.
Examine Figure 3.1 for the PIC18F4550 40-pin chip. A total of 35 pins are Input-Output
pins which can be configured for general Input or Output by setting registers associated
with them. The rest of the pins are designated as Vdd (Vcc), Vss (GND), OSC1, OSC2,
MCLR (reset) and another set of Vdd and Vss.

Figure 3.1: PIC18F4550’s pin out diagram

The PIC18F4550 has 5 ports. They are PORTA, PORTB, PORTC, PORTD and
PORTE. Not all port has 8 pin. Table 3.1 shows the number of pin and pin name of the
port.

1
Table 3.1: Number of pin and pin name
Ports Number of pins Pin Name
PORTA 7 RA0-RA6
PORTB 8 RB0-RB7
PORTC 7 RC0-RC2, RC4-RC7 (Check the Pinout Diagram)
PORTD 8 RD0-RD7
PORTE 4 RE0-RE3

3.2 Dual role ports of data I/O


For practical reasons, many I/O pins have two or three functions.
1. used as a general purpose input/output pin;
2. alternate function - can be seeing next after symbol “/” .

Figure 3.2
Example 3.1:
I/O pin name: RA0/AN0

RA0 – used as digital I/O


AN0 - alternate function (used as input for analog-to-digital converter channel 0)

2
3.2.1 Port A

Port A is an 8-bit wide, bidirectional port. All Port A pins act as digital inputs/outputs.
Five of them can also be analog inputs (denoted as AN):
Because many projects use an ADC, we do not use Port A for simple I/O functions.

Table 3.1: PORTA I/O SUMMARY

3
Table 3.2: Port A alternate function
Bit Alternate Function
RA0 AN0
RA1 AN1
RA2 AN2/VREF-
RA3 AN3/VREF+
RA4 T0CK1
RA5 AN4/SS/LVDIN
RA6 OSC2/CLKO

3.2.2 Port B
Port B is an 8-bit wide, bidirectional port.. Six pins on this port can act as analog inputs
(AN).

Table 3.3: Port B alternate function


Bit Alternate Function
RB0 INT0
RB1 INT1
RB2 INT2/CANTX
RB3 CANRX
RB4
RB5 PGM
RB6 PGC
RB7 PGD

4
3.2.3 Port C
Port C is an 8-bit wide, bidirectional port.

Table 3.4: Port C alternate function


Bit Function
RC0 T1OS0/T1CK1
RC1 T1OS1
RC2 CCP1
RC3 SCK/SCL
RC4 SDI/SDA
RC5 SDO
RC6 TX/CK
RC7 RX/DT

3.2.4 Port D
Port D is an 8-bit wide, bidirectional port.

Table 3.4: Port D alternate function


Bit Alternate Function
RD0 PSP0/C1IN+
RD1 PSP1/C1IN-
RD2 PSP2/C2IN+
RD3 PSP3/C1IN-
RD4 PSP4/ECCP1/PIA
RD5 PSP5/P1B
RD6 PSP6/P1C
RD7 PSP7/P1D

5
3.2.5 Port E

Port E is a 4-bit wide, bidirectional port. Similar to Ports A and B, three pins can be
configured as analog inputs in this case. The ANSELH register bits determine whether a
pin will act as analog input (AN) or digital input/output:
Three pins (RE0/AN5/CK1SPP, RE1/AN6/CK2SPP and RE2/AN7/OESPP) are
individually configurable as inputs or outputs.
On a Power-on Reset, RE3 is enabled as a digital input only if Master Clear functionality
is disabled

Table 3.5: Port E alternate function


Bit Alternate Function
RE0 AN5/CK1SPP
RE1 RE1/AN6/CK2SPP
RE2 RE2/AN7/OESPP)
RE3 MCLR/VPP/RE3

Activity 3.1:
Name ONE (1)of PIC18 I/O pin and identify how that pin used in I/O control.

____________________________________________________________________
Review question

1. There are a total of _____ ports in the PIC18f4550.


2. True or false. All of the PIC18F4550 ports have 8 pins.
3. List all ports that have 8 pins.
___________________________________________________________________

6
3.3 Code PIC instruction for I/O handling

To use any of I/O ports as an input or output port, it must be programmed.


Each port in pic18f4450 is associated with three 8 bit registers for IO operations.
1. TRISx (8 bit)
2. PORTx (8 bit)
3. LATx (8 bit)

3.3.1 TRISX:

TRISX: where X is the name of the ports either of A, B, C, D, E.


Every port has its own TRISX register, thus there are 5 TRISXs to be configured:
a. TRISA
b. TRISB
c. TRISC
d. TRISD
e. TRISE

Data direction (TRIS) register needs to be set before the I/O operation
 1 to make a port an input
 0  to make a port an output
After a Reset all port pins are defined as inputs

3.3.2 PORT X
PORT X : Reads the device level, stores the Input level of the pins and reads the input
signal from the external device if the pin is configured as Input.

3.3.3 LAT X
LAT X: The latch registers reads and modifies the write operation on the value of I/O
pin and stored the output data that is to be passed on to the external hardware.
A write to the LATx register has the same effect as a write to the PORTx register.

7
3.2.1 TRIS register role in inputting and outputting data

By clearing some bit of the TRIS register (bit=0), the corresponding port pin is
configured as output. Similarly, by setting some bit of the TRIS register (bit=1), the
corresponding port pin is configured as input.
This rule is easy to remember 0 = Output, 1 = Input.

Example 3.2
Determine the value of TRIS register for the following circuit .

Figure 3.3
Solution:

Step 1: Analyze I/O pin:-


INPUT: Pin 2 and pin 4 of I/O port
OUTPUT: Pin 6 and pin 7 of I/O port
Step 2:
Set Bit 2 and Bit 4 of TRIS register
Clear Bit 6 and Bit 7 of TRIS register

The value of TRIS register in binary is 00110101.


If the external circuit is connected to Port D, we will write TRISD=0b00110101;

8
.
Example 3.3:
Write C program statement to initialize I/O port for the following diagram.

Figure 3.4

Solution:

INPUT: RB3, RB4, RB6 and RB7


OUTPUT: RB0-RB2, and RB5

TRISB = 0b11011000;

Example 3.4
Write C statement to make Port B of PIC18F4550 become output.

Solution:
To make these port serves as Output, we need to put 0’s bit in the register TRISB0 to
TRISB7. The answer is:

TRISB = 0b00000000;

____________________________________________________________________
Activity 3.2

1. True or false. Upon power-up, the I/O pins are configured as output ports.

9
2. To make Port B an output, we must place __ in register _________.

3. To make Port B an input port, we must place ___ in register __________.

4. Write the I/O initialization for the following situation:


i. SW1 connected to RB1 and LED1 connected to RD2.
ii. Light sensor connected to RC0 and lamp connected to RC3.
______________________________________________________________________

3.4 I/O Port and bit addressability


All PIC18 register is 8 bit size. We will access the register either:
1. all 8 bits - byte addressable
2. any single bit without altering the rest. – bit addressable

Access register by bit:


NAMEbitx = 1/0;
e.g.
RB5 = 1; //Set bit 5 of Port B

Access register by byte:


NAME= 0bxxxxxxxx;
e.g.
PORTB= 0b00001111; //Send binary 00001111 to Port B.

Example 3.5 :
Write a program statement to set bit5 of Port B to logic HIGH by:
a. Bit
b. Byte

10
Solution:
a. By bit:
RB5 = 1; //Set bit 5 of Port B

b. By byte:
PORTB= 0b00100000; //Send logic HIGH to pin 5 of Port B.

Activity 3.3:
Refer the following table to write a program statement to access LATC and TRISC by:
a. Bit
b. Byte

11
Activity 3.4:
Refer the following table to write a program statement to access PORTD and LATD by:
a. Bit
b. Byte

12
Hardware connections (Extra notes)

Output

For the purpose of interfacing the PIC, we can consider a 'high' output to be 5V, and a
low output to be 0V. If you check the PIC datasheets, you will find that the output pins
can actually 'sink' or 'source' a reasonable amount of current - about 25mA or so.

'Sink' means that the chip 'sinks' current down into itself, so the load is connected from
the positive rail to the I/O pin and the load is switched ON by the pin going 'low'.

'Source' is just the reverse, the I/O pin is the actual source of the current, and it flows
out of the pin, through the load down to ground - in this case the load is switched ON by
the pin going 'high'.

Connecting the LED without a resistor is likely to damage both the LED and the PIC.
The two examples below show how to connect an LED in either 'sink' or 'source' modes

Sink mode example

This is how to connect an LED for the PIC to SINK current. The LED will light when the
PIC pin is low.

13
Source mode example

This is how to connect an LED for the PIC to SOURCE current. The LED will light when
the PIC pin is high

Inputs
Inputs to a PIC have the same 5V logic requirements, so the inputs can be active
'high' or active 'low'. Basically this is just a variation on the same theme - but, depending
on the actual input device, you may be forced to use a particular method.

Active LOW input

'Active LOW': The resistor R1 'pulls' the PIC input high (logic '1') - when you press the
switch it pulls the voltage down to zero, changing the input to logic '0'.

14
Active HIGH input

The 'Active HIGH' example works in the opposite way, R2 is a 'pull down' resistor,
holding the PIC input at logic '0', pressing the switch connects the PIC input to the 5V
rail, forcing it to logic '1'.

Example 3..5
A control system consist of a PIC microcontroller, 2 switches (active LOW) and one
LED (active HIGH). Switch 1 is connected to pin RA3 and switch 2 connected to pin
RB0. One LED connected to RC1.
a. Draw a circuit diagram for the system.
b. Write I/O pin initialization for the system
c. Write a program statement to linght on the LED

15
Solution
a.

b. TRISA3=1;
TRISB0=1;
TRISC1=0;

c. RC1=1;

References

http://www.matrixmultimedia.com/wiki/index.php?title=Component:_Switch_base
_(Inputs)
http://www.winpicprog.co.uk/pic_tutorial_extras.htm

16

You might also like