GPIO( General Purpose IO) is the most basic method of communication between MCU and external world. These are done with what is called a PORT. Ports are nothing but a set of bits physically connected to PINs of Microcontroller and available outside the chip. As we are working on PIC micros and they are 8 bits so the maximum size of a PORT is 8 bits. Some PORTs have fewer than 8 bits. You can turn each bit in a PORT on and off under program control. The bits which you set as 1 becomes HIGH and the physical PIN of Micro is at Vcc(supply voltage, usually 3.3v or 5v). And the PINs which you clear (=0) becomes low and physical level is 0v(GND).
![]() |
Fig.: PIC IO Port example, PORTB. |
![]() |
Fig.: PIC IO Port example, Using PORTB, Turning BITs on and off. |
You can test the operation of an IO port by using LEDs as shown below.
![]() |
Fig.: When the bit is set to 0 the LED remains off. |
When you write a value 1 to corresponding bit the LED starts glowing.
![]() |
Fig.: When the bit is set to 1 the LED starts glowing. |
PORTs are Named like PORTA,PORTB,PORTC etc. The PIC we are using PIC4550 has the following PORTs
- PORTA(8bit wide)
- PORTB(8bit wide)
- PORTC(7bit wide)
- PORTD(8bit wide)
- PORTE(4bit wide)
Inputs using PORTs
You have seen how ports can be used to control the outside word. In that we have used the PORTs as Output. They can also be used to get input from outside. In input mode we can apply either logic 0 (GND) or 1 (Vcc = 5v) and we can read the status of PORT and get what input we have on ports. This can be used to interface switches and sensor with MCU. Any value below Vcc/2 is taken as low and any voltage above Vcc/2 is high.
A simple switch interface is shown below.
![]() |
Fig.: A Simple switch as Input to PIC Micro. |
The CPU can read the required port and take decisions accordingly.
Practical Stuff.
Having the knowledge about digital IO Ports on PIC micro we can begin some practical experimentations. So how can we actually access the ports in C? Well the answer follows.
Each PORT in PIC micro has 3 associated registers.
- TRIS: This is the data direction register - As I have told you that port can be used as both input and output purpose, the direction control (input or output) is done using this register. Simply make the bit as 1 when you want its as input or 0 for output.
Example:
TRISB=0xFF; //1111-1111 All InputsTRISB=0x00; //0000-0000 All OutputsTRISB=0x0F;//0000-1111 Bit 7,6,5,4, as Output and Bit 3,2,1,0 as InputNote: On power up all PORTs are Input.
To Remember this note that 1 looks like 'i' therefore Input, 0 looks line 'o' so Output
- LAT: (LATCH): This register is used to set output values. Anything you want output on related pins must be put on this.
Example
LATB=0xFF; //All PINs becomes highLATB=0x00; //All pins becomes LowLATB=0x03; //0000-0011 BIT 0 and 1 becomes high.
- PORT: This is used to get input from the PORTs. Reading its value gives the actual levels applied on the PINs. It must be noted that PORT is first configured as input using the TRIS register. Suppose you connected a switch as shown above on PORTB-0 then if you read PORTB it will be 0000-0000 if switch is pressed and 0000-0001 if switch is released.








Hi!
I am doing a project on leds using PIC12f675 .
I am using GPO,GPI,GP@ as outputs and GP$ as input to switch
Iwant to write aprogram so that when i press the switch once
all leds glow agin when i press switch
all three leds blink again when i press switch
only red led at GPO glows again press switch
only green led at gp1 glows ” ”
only blue led gP2 glows ” ”
only red blinks ” ”
only green blinks ” ”
only blue blinks ” ‘
back to all leds glow.
can you please help me. i would be very higly obliged.
Thanks in advance.
Naren
Naren309@gmail.com
So far I know PORT is used to get input from PORTs as well as set outputs depending on TRIS register value. But in this tutorial it is shown that LAT is used to set outputs. I did not find this LAT in the datasheet of PIC16F628A. Could you please clarify this thing?
@shoeb,
This is for high performance PIC18F series MCU not midrange PIC16F series. So have a look a PIC18F4550′s data sheet not PIC16F628