Pic Example

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

Use Input Output Ports of PIC18F452 Microcontroller

In this tutorial, we will learn how to use GPIO pins of Pic microcontroller. We will see
how to configure general purpose pins either as a digital input and digital output.
For demonstration, we will use PIC18F452 microcontroller but you can also any
other Microchip microcontroller also.

Pic Microcontroller GPIO Pins Introduction


The PIC18F452 microcontroller uses a number of input/output pins for connection
with external devices. It has a total of 40 pins, out of which 34 can serve as
input/output pins. These pins are grouped into five ports, which are Port A, Port
B, Port C, Port D, and Port E. This article will teach you how to utilize the I/O ports
of the PIC18F452 microcontroller.
PortA

Port A consists of seven pins named as RA0-RA6.


PortB

Port B consists of eight pins named as RB0-RA7.


PortC

Port C consists of eight pins named as RC0-RC7.


PortD

Port D consists of eight pins named as RD0-RD7.


PortE

Port E consists of three pins named as RE0-RE2.


GPIO Pins Configuration Registers
Each port has three registers for its operation. These registers are:

1. TRIS register (data direction register)


2. PORT register (reads the levels on the pins of the device)
3. LAT register (output latch)
1. TRIS Register

TRIS is a data direction register. Setting the TRIS bit for the corresponding port
determines the data direction (read or write) of the microcontroller. Each port has
its own TRIS register. For example, to make Port A an output port, we can use the
following code:
TRISA = 0; // making port as output port (write)
To make a specific bit of the port as input or output, we can use the following code:

TRISA.RA2 = 1; // configure PORTA pin RA2 as input


2. PORT Register

The PORT register is to read the levels on the pins of the device and assign logic
values (0/1) to the ports.

Input Mode of Port A

In the input mode of Port A, the status of the pins can be read by reading the
PORTA register. First, we need to set the direction of data by setting the TRISA bit to
1, which makes the corresponding PORTA pin an input and puts the corresponding
output driver in a Hi-Impedance mode. Example code:
TRISA = 1; // making port as input port (read)
PORTA = 0xFF; // reading the status of the pins (input mode)
Output Mode of Port A

In the output mode of Port A, we need to set the direction of data using the TRISA
register and assign a value to the port for output. A write to the PORT register
writes the data value to the port latch. Example code:
TRISA = 0; // making port as output port (write)
PORTA = 0x03; // assigning high logic to RA0 and RA1

3. LAT Register

The LAT (Latch) register is associated with an I/O pin and eliminates the problems
that could occur with read-modify-write instructions.

Latch Read

A read of the LAT register returns the values held in the port output latches instead
of the values on the I/O pins.

Latch Write

A write to the LAT register has the same effect as a write to the PORT register. A
write to the PORT register writes the data value to the port latch.
ANSEL and ANSELH
There are several registers that affect the opera on of the digital I/O pins. If you look at the pin
diagram you will see that pin 3 is called RA4/AN3. This is because it can serve as digital I/O port
RA4, or analog input pin AN3. The two registers ANSEL and ANSELH control whether or not AN0
through AN11 are opera onal. Each bit in the register controls one pin as outlined in the register
tables shown below. A '0' sets the pin to digital mode and a '1' sets the pin to analog mode. For
example, if you make ANSEL equal to 0b10000001 (binary), then AN7 will be enabled and RC3 will
be disabled because they share the same pin (pin 7). AN0 will also be in analog mode (RA0
disabled). Reading a pin that is set to analog mode will return a '0'.

ANSEL = 0x81; //AN7 and AN0 enabled


ANSELH = 0x05; //AN8 and AN10 enabled
CHASING LEDS
============
In this project 8 LEDs are connected to PORTC of a PIC18F45K22 microcontroller and the
microcontroller is operated from an 8 MHz crystal.
The program turns ON the LEDs in an an clockwise manner with 1 s delay between
each output. The net result is that LEDs seem to be chasing each other.
Author: Dogan Ibrahim
Date: August 2013
File: MIKROC-LED1.C
**************************************************************************/
void main()
{
unsigned char J = 1;
ANSELC = 0; // Configure PORTC as digital
TRISC = 0; // Configure PORTC as output
for(;;) // Endless loop
{
PORTC = J; // Send J to PORTC
Delay_ms(1000); // Delay 1 s
J = J << 1; // Shi _ le _ J
if(J == 0) J = 1; // If last LED, move to first one
}
}
If you are using the EasyPIC V7 development board, then make sure that the following
jumper is configured:
DIP switch SW3: PORTC ON
COMPLEX FLASHING LED
===================
In this project an LEDs is connected to port pin RC0 of a PIC18F45K22 microcontroller and the
microcontroller is operated from an 8 MHz crystal.
The program flashes the LED con nuously with the following pa ern:
3 flashes with 200 ms delay between each flash
2 s delay
Author: Dogan Ibrahim
Date: August 2013
File: MIKROC-LED2.C

*****************************************************************************/
void main()
{
unsigned char i;
ANSELC = 0; // Configure PORTC as digital
TRISC = 0; // Configure PORTC as output
for(;;) // Endless loop
{
for(i = 0; i < 3; i++) // Do 3 _mes
{
PORTC.RC0 = 1; // LED ON
Delay_ms(200); // 200 ms delay
PORTC.RC0 = 0; // LED OFF
Delay_ms(200); // 200 ms delay
}
Delay_ms(2000); // 2 s delay
}
}
DIP switch SW3: PORTC ON

You might also like