0
\$\begingroup\$

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.

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$
2
  • 1
    \$\begingroup\$ How long of a delay do you have between toggles of the LED? This smacks of a Read-Modify-Write problem. \$\endgroup\$
    – Dan Laks
    Commented Oct 21, 2014 at 4:50
  • \$\begingroup\$ 500ms. The problem had to do with the analog register associated with the port. \$\endgroup\$ Commented Oct 21, 2014 at 12:59

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ That seemed to do the trick. From the datasheet: "The operation of pins RA<3:0> and RA5 as analog is selected by setting the ANSELA<5, 3:0> bits in the ANSELA register which is the default setting after a Power-on Reset." Thanks! \$\endgroup\$ Commented Oct 21, 2014 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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