CCS News

Tech Note: Using pin_select() to configure the RP pins of a PIC® MCU.

Thursday 15 June, 2017

Many newer Microchip PIC® microcontrollers have re-programmable peripheral pins (RP). These pins allow the user to dynamically allocate peripherals to these pins, such as external interrupts, input capture, PWM, serial, timers and more. This offers the designer great flexibility when designing a product since the functionality of these pins can be changed at run-time. The data sheet for a device will list he pin assignments and these pins are denoted as either RPxx or RPIxx, where xx is the RP pin number. Pins that are RPIxx can only be programmed as an input (timer input, serial input, interrupt input, etc), whereas RPxx pins can be programmed either as an input or output (PWM output, serial output, etc). PIC® MCUs that have this feature can be configured in the CCS C Compiler using the pin_select preprocessor and function.

The first method for assigning I/O pins to a peripheral is the #pin_select directive. The #pin_select directive is a preprocessor directive for assigning I/O pins to peripherals and is executed before main() starts. The syntax for this command is as follows:

#pin_select function=pin

A list of functions and pins that can be used with the #pin_select directive is located in the device's header file near the top of the file, opening the device's header file and searching for #pin_select is the quickest way to find them. The following is an example of how to assign pins to the UART1 RX and TX pins:

#pin_select U1TX=PIN_C6
#pin_select U1RX=PIN_C7

When using RP pins with a peripheral library, such as #use rs232(), the #pin_select must come before the #use directive, for example:

#pin_select U1TX=PIN_C6
#pin_select U1RX=PIN_C7
#use rs232(UART1, baud=9600, stream=U1)

In addition to #pin_select the CCS C Compiler also provides the pin_select() function for assigning pins to a peripheral. The pin_select() function can be used to assign, reassign and unassign pins to/from a peripheral at run-time. This allows the flexibility of using the same pin for multiple peripherals or using pins as both peripheral pins and I/O pins. The pin_select() function uses the following syntax: pin_select("function", pin);. The functions and pins are the same as what is used with the #pin_select directive, the only difference being that the function is passed as a constant string. The following is an example of how to assign pins to the UART1 peripheral:

pin_select("U1TX", PIN_C6);
pin_select("U1RX", PIN_C7);

To unassign a pin from a peripheral depends on whether it an input peripheral or an output peripheral. To unassign a pin from an output peripheral is done as follows:

pin_select("NULL", PIN_C6); //unassign PIN_C6 from output peripheral.

To unassign a pin from an input peripheral is done as follows:

pin_select("U1RX", FALSE); //unassign pin from U1RX input peripheral.

Because of how output peripherals are assigned to RP pins it is possible to assign multiple pins to the same output peripheral when using the pin_select() directive. For example the following will assign multiple pins to the CCP1 peripheral:

pin_select("CCP1OUT", PIN_B0);
pin_select("CCP1OUT", PIN_B1);

This method of tying several pins to the same output can only be performed with the pin_select() function, #pin_select cannot be used to do this.

Like us on Facebook. Follow us on Twitter.

About CCS:

CCS is a leading worldwide supplier of embedded software development tools that enable companies to develop premium products based on Microchip PIC® MCU and dsPIC® DSC devices. Complete proven tool chains from CCS include a code optimizing C compiler, application specific hardware platforms and software development kits. CCS' products accelerate development of energy saving industrial automation, wireless and wired communication, automotive, medical device and consumer product applications. Established in 1992, CCS is a Microchip Premier 3rd Party Partner. For more information, please visit

PIC® MCU, MPLAB® IDE, MPLAB® ICD2, MPLAB® ICD3 and dsPIC® are registered trademarks of Microchip Technology Inc. in the U.S. and other countries.