3
\$\begingroup\$

I have always wondered if it is possible to switch the port direction of the PIC GPIO during the course of the program execution. So, for instance I start with a particular port set as an input(digital level). I monitor that pin, and if the level changes, I change the direction of that pin and drive a signal to turn on a LED. Is this too far fetched or is it doable? If so, some pseudo-code to would be very helpful.

\$\endgroup\$
1
  • \$\begingroup\$ Have a look at the 1-wire protocol, it does what you want and more. It can signal to ground, supply power and listen for signals with a pullup. \$\endgroup\$
    – KalleMP
    Commented May 12, 2015 at 9:57

3 Answers 3

6
\$\begingroup\$

Yes, it's completely possible you simply change the relevant bit in the associated TRIS register from 1 to 0 in order to change the pin from input to output.

In general on the PIC18 series you should read pins using the PORT register and write using the LAT register.

So suppose you had a pin like this:

schematic

simulate this circuit – Schematic created using CircuitLab

You could periodically read RA0 as an input and drive the LED the rest of the time. To read the switch state you would set bit 0 in the TRISA register high, wait a bit, then read the PORT pin (bit 0 of PORTA), and then clear bit 0 in the TRISA register.

To avoid contention, only set the pin to output if the LED is to be driven low. The LED will always come on as long as the switch is pressed.

\$\endgroup\$
3
  • \$\begingroup\$ I am using a PFET to drive the LED so it will be driven low. Thanks. \$\endgroup\$ Commented May 11, 2015 at 18:47
  • \$\begingroup\$ Why is it that you should only write with PORT? I see in the data sheet that reading with LAT would give you the last written value to the latch rather than what's on the pin but I don't see a difference between writing with PORT vs LAT. \$\endgroup\$
    – C. Spencer
    Commented Apr 19, 2017 at 18:15
  • \$\begingroup\$ @C.Spencer Only write with LAT, not PORT, with BCF/BSF, because otherwise you can get READ-MODIFY-WRITE issues for other pins on the same port. Eg. you clrf PORTA then BSF RA0, but some pin(s) on PORTA have not yet reached low logic level (maybe because of some capacitance) so those pins go high and stay high (RA0 will be high, as desired). The problem is that there is a hidden read in those instructions. \$\endgroup\$ Commented Apr 19, 2017 at 19:07
2
\$\begingroup\$

Of course it is.
Its just a write to the TRIS register for that port.
And since PIC ports default to inputs on power-up, any pins you want to use as outputs have to have their direction switched during program execution - usually in your startup/init code.
And there's nothing to stop you changing your mind later on and making some more changes whenever you feel like it ...

\$\endgroup\$
1
  • \$\begingroup\$ thanks So, I just need to add a series current limiting resistor to the pin and I am in business? \$\endgroup\$ Commented May 11, 2015 at 18:44
1
\$\begingroup\$

Think when you put the TRIS register as output, and you send a zero, if you hit the switch you are making a short circuit. You need a resistor in the switch to limit current. Try this instead:

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$

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.