Electronic – Configure a PIC pin for Input and Output

inputoutputpicport

I am working on a project which uses a PIC24FJ64GA002 mcu.
I am working on a bit-banged serial communication function that will use one wire to send data and then switch to receive mode to receive data on the same pin. A separate pin will be used for clocking which will always be controlled by a different board (always an input). I am wondering is there a way to configure the pin for open-collector operation that that it can be used as an input and and output or do I have to change the pin configuration every time i go from reading to writing?

Best Answer

Change the pin configuration every time. It's the TRIS register that controls if it's an input (1) or output (0). And, when you change direction, you have to wait a moment (check the datasheet) like a cycle or two before you use that pin. a couple Nop(); will do fine.

And, make sure you write to the LAT register, and read from the R register.

e.g. for port D, pin 0.

//output
_TRISD0 = 0;
Nop();
Nop();
_LATD0 = 1;
_LATD0 = 0;


//input
_TRISD0 = 1;
Nop();
Nop();
int i = _RD0;