Pic Example
Pic Example
Pic Example
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.
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:
The PORT register is to read the levels on the pins of the device and assign logic
values (0/1) to the ports.
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'.
*****************************************************************************/
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