Question

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?

Was it helpful?

Solution

You need to change the direction of the pin each time by using the TRIS register. If the pin is set up as an output, reading the PORT register will most likely only tell you what level you are driving the pin to (assuming there is a high impedance on the pin). If the pin is set for input, you won't be able to drive your desired output value. Also, make sure that you read incoming data using the PORT register, but output the data using the LAT register. This ensures that you don't suffer any issues if your code (I assume you are programming in C here) gets converted into bset/bclr/btgl instructions which are Read-Modify-Write. If you are writing in assembler, the same rule applies but you know when you are using these R-M-W type instructions. If you want more reasoning on this, please ask.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top