PIC24FJ tiny remappable pin library




The PIC24FJ microcontroller family feature lots of modules to perform many common operations with hardware. The problem with is is the microcontroller needs a big enough pinout for all of them.
Because this is not possible, the PIC23FJ feature remappable pins (RP) which can be assigned to different modules. How to use this function is no secret, and perfectly explained in the user reference manual. Yet I wanted to share the tiny library I have put together to handle them. The library does only cover a few modules, but can easily be modified to cover them all.



API



Configuration

  1. Modify the microcontroller include if needed
  2. Inside the DEFINE section simply add a name and the number indicated by the user manual for your desired module. For now I have only included the output compare modules and a UART module.
  3. In the CONFIGURATION section assign the desired pins to your defined device.
  4. If needed (because you want to use other remappable pins instead of 10 to 15, add the one needed in the REMAP_WRITE function.


Functions

  • remap_write(void)

Call this function inside your user code early in the initialization to make the remapping effective. If you do not modify the code it will remap the output signals of the output compare modules to RP pins 10, 11, 13 and 14.


Code



I still have no file hosting service. So please pardon me for posting the plain text. I am way to lazy to fix it now ;)

remappable_pin.h


   /*
    * remappable_pin.h - Tiny remmapable pin library for
PIC23FJ32GB002

    * Copyright (C) 2014 Andres Gongora
    * <https://yalneb.blogspot.com>
    *
    * This program is free software: you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation, either version 3 of the License, or
    * (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    */

#ifndef REMAP_H_INCLUDED    //RP_H_INCLUDED
#define REMAP_H_INCLUDED


//#####    DEFINE ###################################################################################

#define REMAP_NONE        0        // No remapping

//OUTPUT COMPARE
#define REMAP_OC1        18        // ID for Output Compare 1
#define REMAP_OC2        19        // ID for Output Compare 2
#define REMAP_OC3        20        // ID for Output Compare 3
#define REMAP_OC4        21        // ID for Output Compare 4
#define REMAP_OC5        22        // ID for Output Compare 5

//UART
#define REMAP_U1TX        3;        // ID for UART-1 TX
#define REMAP_U1RX        9;        // RP to assign for UART-1 RX

// TODO: add other pheriperals to the list


/**///////////////////////////////////////////////////////////////////////////////////////////////
//    CONFIGURATION
///////////////////////////////////////////////////////////////////////////////////////////////**/

/*    Select RP-PIN function. See table 10-3 (Selectable Output Sources) from datasheet for details*/

#define REMAP10            REMAP_OC1
#define REMAP11            REMAP_OC2
//#define REMAP12        REMAP_NONE    // Not available on all PICs
#define REMAP13            REMAP_OC3
#define REMAP14            REMAP_OC4
//#define REMAP15        REMAP_NONE

// TODO: add other RPs to the list


//##### FUNCTIONS ################################################################################

/**///////////////////////////////////////////////////////////////////////////////////////////////
//    remap_unlock
///////////////////////////////////////////////////////////////////////////////////////////////**/
/*    Unlocks IOLOCK (OSCCON<6>) so Remappable Pin confuguraiton can be written (RPINRx and RPORx)
*/
void remap_unlock(void)
{
    __builtin_write_OSCCONL(OSCCON & 0xBF);
}


/**///////////////////////////////////////////////////////////////////////////////////////////////
//    remap_lock
///////////////////////////////////////////////////////////////////////////////////////////////**/
/*    Locks IOLOCK (OSCCON<6>) so Remappable Pin confuguraiton can be written (RPINRx and RPORx)
*/
void remap_lock(void)
{
    __builtin_write_OSCCONL(OSCCON | 0x40);
}


/**//////////////////////////////////////////////////////////////////////////////////////////////
//    remap_remap
//////////////////////////////////////////////////////////////////////////////////////////////**/
/*    Assigns Remappable Pins to peripherals by writting RPINRx y RPORx according to the configuration
    in this same file
*/
void remap_write(void)
{
    remap_unlock();            // Unlocks RPINRx and RPORx

    // REMAP
    _RP10R = REMAP10;
    _RP11R = REMAP11;
    _RP13R = REMAP13;
    _RP14R = REMAP14;
    //TODO: add more RP pins

    remap_lock();            // Locks RPINRx and RPORx
}

#endif//RP_H_INCLUDED


No comments :

Post a Comment