Simple PORTA Control via PIC


I am trying to setup a simple blinking LED on a PIC18F25K50. I can get RA0 to toggle using

LATAbits.LA0 = ~LATAbits.LA0;

However, if I try to use

LATAbits.LA0 = ~PORTAbits.RA0;

The LED stays on.

According to the datasheet (among other sources), the PORT register reads the levels on the pins, whereas the LAT register sets the output. Why is the PORT register not reading the correct levels? The circuit is below.


simulate this circuit – Schematic created using CircuitLab

Best Answer

I suspect that you have not set the ANSELA register correctly.

The port defaults its analog-capable pins to analog mode, which means that when you read them through the PORTA register they will read as 0's, irrespective of what the voltage level actually is on the pin. But, you can still set output values, which is why you can toggle the pin.

So, set your ANSELA bit 0 to 0 to set it to digital mode (and for any other pin you plan to use as an input) and you'll be able to read inputs correctly.