I'm trying to write some code that waits for a pin to change before continuing but it doesn't work. The code that I already have is:


}while(PORTBbits.RB14== 0);       

LATBbits.LATB6=0; ... d something else

The RB14 pin is already configured as an input. I want it to wait until RB14 is high before continuing the code execution. I'm using a pic24f16ka102 microcontroller.

  • \$\begingroup\$ That looks reasonable. Perhaps the error is elsewhere. Have you verified that you are reading pin 14 OK? \$\endgroup\$ Jun 26, 2016 at 2:35
  • 4
    \$\begingroup\$ Does that PIC have ANSEL registers and if it does, have you configured them correctly to make the port digital and not analog? \$\endgroup\$
    – brhans
    Jun 26, 2016 at 2:51
  • \$\begingroup\$ When you do get it working you should modify the code to avoid the do ... while wait loop. That construction is simple but ties up the processor and prevents it doing anything else. \$\endgroup\$
    – Transistor
    Jun 26, 2016 at 7:12
  • \$\begingroup\$ Your intent is not entirely clear. Are you trying to execute the code in the do...while loop when RB14 changes, or do you want to set LATB6, then wait until RB14 is high, and then clear LATB6? \$\endgroup\$
    – DerStrom8
    Jun 26, 2016 at 12:54

1 Answer 1


If you want to set LATB6 and then wait until RB14 is high before continuing then you don't want a do-while loop. You would want something like this:

LATBbits.LATB6 = 1;           //set LATB6
while(PORTBbits.RB14 == 0);   //wait until RB14 is HIGH
LATBbits.LATB6 = 0;           //execute other code

I don't quite understand why you have LATBbits.LATB6 = 1 inside a do-while loop. While theoretically it does the same thing, it just wastes time and resources. It keeps setting LATB6 over and over again until RB14 is HIGH, but there is no need to keep setting LATB6 to the same value -- It keeps its previous state.

This probably isn't your problem though. I expect your issue is that you have not disabled the ADC (analog-digital converter). RB14 shares a pin with AN10 (analog input 10). Some pics default to analog input, which is not what you want. you need to actively disable the ADC in order for RB14 to be a digital input.

  • \$\begingroup\$ the error was that the rb14 was configured as analog pin \$\endgroup\$
    – Zinedine
    Jul 12, 2016 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.