Peripheral Library I/O Functions

Appendices: P01

Appendices:

Peripheral Library I/O Functions

Introduction

Some I/O pins can support multiple peripheral resources. Although the preferred approach that will be used throughout this and following projects will be to use the peripheral library functions whenever possible, the following equivalent statements are shown in the event that the reader encounters these alternate forms. Pins that can be configured to be either analog inputs or digital I/O must be set accordingly. (See page 9 of the PIC™32 family data sheet to determine which pins can be configured as either analog or digital inputs.) Section 10 of the Microchip® C32 peripheral library contains functions to assist in setting the direction of the PIC32MX7 I/O pins. (PIC32 family reference guide, Section 12.3.1 specifically states that the default to analog inputs on power up reset, thus making the use of the AD1PCFGSET instruction in conjunction with the TRISx, TRISxSET, TRISxINV, and TRISxCLR instructions required.) The following two functions may be used in place of setting the port TRIS registers to set the pin directions.

Step 1: Setting I/O Pins as Inputs

To set the I/O pins as digital inputs, use the function:

void PORTSetPinsDigitalIn(IO_PORT_ID port, unsigned int inputs);

The port to be set is identified by the first variable and is specified by the identifier IOPORT_x, where x is A through G.

The second variable passed to this function is the pin values for those pins to be set as outputs. It is possible to set the pins individually by combining the pins with the bitwise or function using the bit constants BIT_0 through BIT_15 that are predefined in the plib.h system header file.

Step 2: Setting I/O Pins as Outputs

To set the I/O pins as outputs, use the function:

void PORTSetPinsDigitalOut(IO_PORT_ID port, unsigned int outputs);

The port to be set is identified by the first variable and is specified by the identifier IOPORT_x, where x is A through G.

The second variable passed to this function is the pin values for those pins to be set as outputs. It is possible to set the pins individually by combining the pins with the bitwise or function using the predefined bit constants BIT_0 through BIT_15.

Step 3: Setting I/O Pin Operations

The following examples illustrate equivalent alternate methods of implementing I/O pin operations.

/* Configuring Port B pin 10 as an output */
PORTSetPinsDigitalOut(IOPORT_B, BIT_10);

/* The following two instructions are both required to accomplish the same configuration */

AD1PCFGSET = BIT_10;    // Set for digital I/O 
TRISBCLR = BIT_10;      // Set direction

/* The following statements are equivalent and result in setting Port B pin 10 output high */
PORTSetBits(IOPORT_B, BIT_10);      // Provided by Peripheral Library
mPORTBSetBits(BIT_10);          // Provided by Peripheral Library
LATBSET = BIT_10;
PORTBSET = BIT_10;
LATB = LATB | BIT_10;

/* The following statements are equivalent and result in setting Port B pin 10 output low */
PORTClearBits(IOPORT_B,BIT_10);     // Provided by Peripheral Library
mPORTBClearBits(BIT_10);            // Provided by Peripheral Library
LATBCLR = BIT_10;
PORTBCLR = BIT_10;
LATB = LATB & ~BIT_10;

/* The following statements are equivalent and result in writing Port B pin 10 high and
 * all other pins low */
LATB = 0x0400;
LATB = BIT_10;
PORTB = 0x0400;
PORTB = BIT_10;

/* The following statements are equivalent and result in configuring Port B pin 10 as a
 * digital input */
PORTSetPinsDigitalIn(IOPORT_B, BIT_10);

/*The following two instructions are both required */
AD1PCFGSET = BIT_10;    // Set for digital I/O  
TRISBSET = BIT_10;      // Set direction

/* The following statements are equivalent and result in reading all Port B I/O pins */
                        
(int) pins = PORTB;
(int) pins = PORTRead(IOPORT_B);    // Provided by Peripheral Library
(int) pins = mPORTBRead();          // Provided by Peripheral Library

/* The following statements are equivalent and result in reading only Port B pin 10 */
(int) pin10 = PORTB & BIT_10;
(int) pins = PORTReadBits(IOPORT_B, BIT_10);    // Provided by Peripheral Library
(int) pins = mPORTBReadBits(BIT_10);        // Provided by Peripheral Library

  • Other product and company names mentioned herein are trademarks or trade names of their respective companies. © 2014 Digilent Inc. All rights reserved.