Thread: PIC32MX Pin remapping

  1. #1
    Registered User
    Join Date
    Jul 2021
    Posts
    20

    PIC32MX Pin remapping

    Hi,
    I am using the PIC32MX575F256L. I have currently got UART1 running on pins RF8 and RF2.
    This was all working fine up until a few hours ago.

    I set up hardware I2C1. I would like it to use pins RA15 and RA14. Instead it appears to be using RF8 and RF2 instead.

    I cant seem to find what i need on the pin remapping. There is nothing in the datasheet for pin remapping either which i find odd.

    Here is my init code, which doesnt set the pins. I have disabled all analogue pins and RA14 and RA15 are set as output high else where in my code.

    Code:
    // I2C_init() initialises I2C1 at at 100Khz  
    void I2C_Init(void)
    {
        I2C1CON = 0;                // Turn off I2C1 module
        I2C1CONbits.DISSLW = 1;     // Disable slew rate for 100kHz
        I2C1BRG = 390;           // Set baud rate to 100KHz
        I2C1CONbits.ON = 1;         // Turn on I2C1 module
    }
    Can anyone help me to tell the PIC which pins i want the I2C to use, please...

  2. #2
    Registered User
    Join Date
    Jul 2021
    Posts
    20
    Not sure why, but the UART has started working again. Im not sure what i have changed to make it work, but for now it does.

    These are the i2c functions i have. It all compiles but i get nothing at all on pins RA14 and RA15 when looking at it on Logic Analyser. So i think i still need to map the pins??


    Code:
    // I2C_init() initialises I2C1 at at 100Khz  
    void I2C_Init(void)
    {
        I2C1CON = 0;                // Turn off I2C1 module
        I2C1CONbits.DISSLW = 1;     // Disable slew rate for 100kHz
        I2C1BRG = 390;           // Set baud rate to 100KHz
        I2C1CONbits.ON = 1;         // Turn on I2C1 module
    }
    
    
    // I2C_wait_for_idle() waits until the I2C peripheral is no longer doing anything  
    void I2C_Wait_For_Idle(void)
    {
        while(I2C1CON & 0x1F);      // Acknowledge sequence not in progress
                                    // Receive sequence not in progress
                                    // Stop condition not in progress
                                    // Repeated Start condition not in progress
                                    // Start condition not in progress
        while(I2C1STATbits.TRSTAT); // Bit = 0 ? Master transmit is not in progress
    }
    
    
    // I2C_start() sends a start condition  
    void I2C_Start()
    {
        I2C_Wait_For_Idle();
        I2C1CONbits.SEN = 1;
        while (I2C1CONbits.SEN == 1);
    }
    
    
    // I2C_stop() sends a stop condition  
    void I2C_Stop()
    {
        I2C_Wait_For_Idle();
        I2C1CONbits.PEN = 1;
    }
    
    
    // I2C_restart() sends a repeated start/restart condition
    void I2C_Restart()
    {
        I2C_Wait_For_Idle();
        I2C1CONbits.RSEN = 1;
        while (I2C1CONbits.RSEN == 1);
    }
    
    
    // I2C_ack() sends an ACK condition
    void I2C_Ack(void)
    {
        I2C_Wait_For_Idle();
        I2C1CONbits.ACKDT = 0; // Set hardware to send ACK bit
        I2C1CONbits.ACKEN = 1; // Send ACK bit, will be automatically cleared by hardware when sent  
        while(I2C1CONbits.ACKEN); // Wait until ACKEN bit is cleared, meaning ACK bit has been sent
    }
    
    
    // I2C_nack() sends a NACK condition
    void I2C_Nack(void) // Acknowledge Data bit
    {
        I2C_Wait_For_Idle();
        I2C1CONbits.ACKDT = 1; // Set hardware to send NACK bit
        I2C1CONbits.ACKEN = 1; // Send NACK bit, will be automatically cleared by hardware when sent  
        while(I2C1CONbits.ACKEN); // Wait until ACKEN bit is cleared, meaning NACK bit has been sent
    }
    
    
    // address is I2C slave address, set wait_ack to 1 to wait for ACK bit or anything else to skip ACK checking  
    void I2C_Write(unsigned char address, char wait_ack)
    {
        I2C1TRN = address | 0;              // Send slave address with Read/Write bit cleared
        while (I2C1STATbits.TBF == 1);      // Wait until transmit buffer is empty
        I2C_Wait_For_Idle();                // Wait until I2C bus is idle
        if (wait_ack) while (I2C1STATbits.ACKSTAT == 1); // Wait until ACK is received  
    }
    
    
    // value is the value of the data we want to send, set ack_nack to 0 to send an ACK or anything else to send a NACK  
    void I2C_Read(unsigned char *value, char ack_nack)
    {
        I2C1CONbits.RCEN = 1;               // Receive enable
        while (I2C1CONbits.RCEN);           // Wait until RCEN is cleared (automatic)  
        while (!I2C1STATbits.RBF);          // Wait until Receive Buffer is Full (RBF flag)  
        *value = I2C1RCV;                   // Retrieve value from I2C1RCV
    
    
        if (!ack_nack)                      // Do we need to send an ACK or a NACK?  
            I2C_Ack();                      // Send ACK  
        else
            I2C_Nack();                     // Send NACK
    When using the above functions for this...

    Code:
    void I2C_Init(void);
        Delay_mS(1000);
        
        I2C_Start();                        /* Send start condition */  
        I2C_Write(0x70 << 1, 1);            /* Send address, read/write bit not set (AD + R) */ 
        I2C_Write(1 << 7,1);                //Send byte to select bus
        I2C_Stop();                         /* Send stop condition */  
        
        I2C_Start();                        /* Send start condition */  
        I2C_Write(0x57 << 1, 1);                 /* Send address, read/write bit not set (AD + R) */
        I2C_Stop();                         /* Send stop condition */
    It doesnt hang, it just seems to skip right over it.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Are you talking hardware analyzer or software?

    You are talking about using pins
    66 SCL1/INT3/RA14
    67 SDA1/INT4/RA15
    correct?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This looks wrong in I2C_Wait_For_Idle
    Code:
    while(I2C1CON & 0x1F);
    Should it not be
    Code:
    while(I2C1STAT & 0x1F);
    Edit: I also question the value 0x1F.
    Edit2: Normally control bits only change when the program changes them; so, unless you have an interrupt that is changing I2C1CON then it is not likely the location you should check for a changing value.

    Tim S.
    Last edited by stahta01; 08-18-2021 at 08:20 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jul 2021
    Posts
    20
    Hi Tim,

    Yeah i want to use pins RA14/RA15.
    I am using a hardware analyser to look at the bus. It is just sitting in a high state, it never pulls low at any point.

    The I2C1CON register in the wait_for_idle function looks OK to me. It is checking that any tasks that have been enable by code have all completed. The 0x1F mask is checking the first five bits. There are no interrupts, all just polled.

    The part i really need to overcome at this point is why there is nothing happening on pins RA14/RA15. I am still unsure of the need to map pins or not and i cant find any method online to do it on this chip.
    There is nothing in ERRATA relating to I2C1 not working either.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Edit: After researching the docs it looks like you are maybe correct.

    Edit2: I missed seeing "Hardware clear at end of eighth bit of master receive data byte." I wonder if I was on the wrong page of the manual or going blind.

    Tim S.
    Last edited by stahta01; 08-19-2021 at 02:47 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    	// Set all analog pins to be digital I/O
       	AD1PCFG = 0xFFFF;
    Try adding the above in the main chip init area of your code.
    I think RA14 and RA15 are analog inputs; but, it been almost a decade since I was an Teaching Assistant for lower class of pic chips.

    Edit: RA14/15 are digital input/output. Still worth trying the above code.
    Edit3: How did I forget AN was analog pin prefix.

    Code:
    //Setup TRIS bits for switches and I2C pins
    	TRISAbits.TRISA14=0;
    	TRISAbits.TRISA15=0;
    Link to where I found the above code sniippets
    PIC32MX: I2C Communication between PIC32s - Northwestern Mechatronics Wiki

    Tim S.
    Last edited by stahta01; 08-19-2021 at 03:37 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Jul 2021
    Posts
    20
    Quote Originally Posted by stahta01 View Post
    Code:
        // Set all analog pins to be digital I/O
           AD1PCFG = 0xFFFF;
    Try adding the above in the main chip init area of your code.
    I think RA14 and RA15 are analog inputs; but, it been almost a decade since I was an Teaching Assistant for lower level of pic chips.

    Edit: RA14/15 are digital input/output. Still worth trying the above code.
    Edit3: How did I forget AN was analog pin prefix.

    Code:
    //Setup TRIS bits for switches and I2C pins
        TRISAbits.TRISA14=0;
        TRISAbits.TRISA15=0;
    Link to where I found the above code sniippets
    PIC32MX: I2C Communication between PIC32s - Northwestern Mechatronics Wiki

    Tim S.
    I already have all analogues disabled using

    Code:
    AD1PCFG = 0xFFFF;
    I have a MCU_Config() function to set up all the pins and in there for the I2C i do this:

    Code:
    //PIC32 I2C1 - SDA
        TRISAbits.TRISA15 = 1;              //PIN RA15 - SET AS OUTPUT
        LATAbits.LATA15 = 1;                //PIN RA15 - SET HIGH
        
        //PIC32 I2C1 - SCL
        TRISAbits.TRISA14 = 1;              //PIN RA14 - SET AS OUTPUT
        LATAbits.LATA14 = 1;                //PIN RA14 - SET HIGH
    Annnnnnddd there it is. As i write this i am seeing that i have set the pins as inputs and noted them as outputs. This is what happens when you stay awake until 3 in the morning trying to fix something.

    I am at work at the moment, but i will set these to output when im at home and see if my issue resolves.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    LATAbits.LATA15 = 1;
    May also need to change both of LATA to zero.
    The chip I used did not have LATA so, this is a wild guess on my part.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Jul 2021
    Posts
    20
    I have now set my pins to this.

    Code:
    //PIC32 I2C1 - SDA
        TRISAbits.TRISA15 = 0;              //PIN RA15 - SET AS OUTPUT
        LATAbits.LATA15 = 1;                //PIN RA15 - SET HIGH
        
        //PIC32 I2C1 - SCL
        TRISAbits.TRISA14 = 0;              //PIN RA14 - SET AS OUTPUT
        LATAbits.LATA14 = 1;                //PIN RA14 - SET HIGH
    I am still getting no activity at all on the pins when calling the i2c functions.

    Is there something i have to enable perhaps in order to get the i2c to turn on?

  11. #11
    Registered User
    Join Date
    Jul 2021
    Posts
    20
    So i have restarted everything. recompiled and it works now.

    The old off and on trick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help on Remapping Keyboard Keys (C++)
    By GraceLC in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2011, 02:49 AM
  2. remapping hotkeys and Winamp
    By confuted in forum Tech Board
    Replies: 0
    Last Post: 08-30-2003, 03:54 PM

Tags for this Thread