Professional Documents
Culture Documents
Pen Gen Alan
Pen Gen Alan
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
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
Figure 3.2
Example 3.1:
I/O pin name: RA0/AN0
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.
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).
4
3.2.3 Port C
Port C is an 8-bit wide, bidirectional port.
3.2.4 Port D
Port D is an 8-bit wide, bidirectional port.
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
Activity 3.1:
Name ONE (1)of PIC18 I/O pin and identify how that pin used in I/O control.
____________________________________________________________________
Review question
6
3.3 Code PIC instruction for I/O handling
3.3.1 TRISX:
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:
8
.
Example 3.3:
Write C program statement to initialize I/O port for the following diagram.
Figure 3.4
Solution:
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 _________.
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
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': 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