Ports and other SFRs
All of the microcontroller's SFRs (Special Function Registers), including the ports, can be accessed just like any other variable. This means that they can be read from, written to, or used in expressions directly.
PORTB = %0101010101010101 ' Write value to PORTB
Var1 = MyWord * PORTB ' Multiply variable MyWord with contents of PORTB
Remember, unlike the 8-bit microcontroller's, PIC24® and dsPIC33® devices have 16-bit wide ports and SFRs.
One thing that can affect the operation of a port when used for a peripheral is PPS (Peripheral Pin Select). Most of the newer PIC24® and dsPIC33® devices have PPS which means that there is not a dedicated pin for a specific peripheral. i.e. the USART, and its pins must be designated before the peripheral can be used. The subject of PPS is too detailed for this manual, however, Microchip™ have several reference manuals that cover the use of PPS. "PIC24F® Family Reference Manual - Sect 12 - I/O" being one of them and has the file title "39711b.pdf". It is very important to read the device's datasheet and understand the operation of PPS in order for a peripheral to work correctly.
Within this manual there are several examples that use the line of code:
RPOR7 = 3 ' Make PPS Pin RP14 U1TX
The above code designates pin RB14 (PORTB.14) for use as the TX pin for USART1.
The compiler has helper macros built in for Peripheral Pin Select, these are PPS_Input, PPS_Output, PPS_Unlock, and PPS_Lock. The macros and the related defines can be found within each device's .def file, located within the compiler's Def directory. Below, is a brief explanation of the macros, but this is not a substitute for reading the device's datasheet.
The PPS (Peripheral Pin Select) feature provides a method of enabling the user's peripheral set selection and their placement on a wide range of I/O pins. By increasing the pinout options available on a particular device, users can better tailor the microcontroller to their application, rather than trimming the application to fit the device.
The PPS feature operates over a fixed subset of digital I/O pins. Users may independently map the input and/or output of any one of many digital peripherals to any one of these I/O pins. PPS is performed in software and generally does not require the device to be reprogrammed. Optional hardware safeguards are included that prevent accidental or spurious changes to the peripheral mapping once it has been established.
Example.
PPS_Output(cOut_Pin_RP35, cOut_Fn_U1TX) ' Map UART1 TX pin to RP35
PPS_Input(cIn_Pin_RPI34, cIn_Fn_U1RX) ' Map UART1 RX pin to RPI34
