That's because now in addition to using the LAT register, you're still setting RC2 using the PORT registerin the search for the right answer I even wrote his code , though it is childish
void lowSwitch(int y )
{
TRISCbits.TRISC2= 0 ;
if (y==1) {
RC2=1; //lowLed 14
LATCbits.LATC2 = 1 ; // RC2=1; //LOW_PORT=0; //14
// reversed just for testing
LATCbits.LATC2 = 1 ; // RC2=1; //LOW_PORT=0; //14
RC2=1; //lowLed 14
}
else if (y==0) {
RC2=0; //lowLed 14
LATCbits.LATC2 = 0 ; // RC2=1; //LOW_PORT=0; //14
}
}
However it didn't work till ANSEL was set.
void lowSwitch(int y)
{
TRISCbits.TRISC2 = 0;
if (y==1) {
LATCbits.LATC2 = 1;
}
else if (y==0) {
LATCbits.LATC2 = 0;
}
}
You are absolutely right.That's because now in addition to using the LAT register, you're still setting RC2 using the PORT register
(RC2 is shorthand for PORTCbits.RC2). You don't need to do both... setting/clearing the LAT register will control the output.
If you do the following it should work no matter what the setting of ANSELC.2 is
I would recommend that you set ALL the ANSELx registers right at the beginning of main() before setting the TRIS registers or any other initializations (like the LCD). Remember, on this chip ANSEL bit settings 0=digital, 1=analog, and the ANSEL registers all default to 0xFF at poweron/reset. That should get rid of a lot of the issues you're seeing.Code:void lowSwitch(int y) { TRISCbits.TRISC2 = 0; if (y==1) { LATCbits.LATC2 = 1; } else if (y==0) { LATCbits.LATC2 = 0; } }
Yes true and good idea. I had noticed the phenomena , though I did not care to the ANSEL for the unused pins.There is actually an advantage to leaving the ANSEL on for unused pins. If left as inputs, and unconnected pin can develop an intermediate voltage that cause a lot of current to flow in the input. But setting unused pins to output also avoids that problem.
That said, I typically set ANSEL off for all pins not used as analog AND set unconnected pins to outputs.