PIC16F1508 - Output Voltages

trebla

Joined Jun 29, 2019
599
For output and clock testing purposes is IMO best way to write some basic routine:
Code:
void main() {

TRISC = 0;

while(1) {

   LATC = 0xff;
   __delay_ms(1000);
   LATC = 0;
   __delay_ms(1000);

}

}
 

tumbleweed

Joined Jun 27, 2023
19
in 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.
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
Code:
void lowSwitch(int y)
{
    TRISCbits.TRISC2 = 0;
    if (y==1) {
        LATCbits.LATC2 = 1;
    }
    else if (y==0) {
        LATCbits.LATC2 = 0;
    }
}
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.
 

Thread Starter

agsuresh

Joined Dec 28, 2023
66
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
Code:
void lowSwitch(int y)
{
    TRISCbits.TRISC2 = 0;
    if (y==1) {
        LATCbits.LATC2 = 1;
    }
    else if (y==0) {
        LATCbits.LATC2 = 0;
    }
}
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.
You are absolutely right.

I had already written that the code was childish. My Idea at that moment was to look for what is working and what is not in various condition. I am aware that it is not necessary and an overkill.

as you pointed out , I finally reached at the same conclusion as you mentioned. - setting the ANSEL bit in the beginning of the code.

In the beginning my mind was latched on to that bit of info in the datasheet that on rest the ports default to analog. I posted this bit of code only to show another friend here - that I had used the LAT.

switching between analog / digital in between operations should be watched carefully on some of the pins and code positioned accordingly. This is what I learned.

Thank you once again for your pointers
 

Thread Starter

agsuresh

Joined Dec 28, 2023
66
I would thank all the members here for responding to my post. Your pointers really help in understanding the functional logic of these micro-controllers.

rgds.
 

BobTPH

Joined Jun 5, 2013
11,521
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.
 

Thread Starter

agsuresh

Joined Dec 28, 2023
66
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.
Yes true and good idea. I had noticed the phenomena , though I did not care to the ANSEL for the unused pins.
 
Top