配置 PIC 引脚的输入和输出
我正在开发一个使用 PIC24FJ64GA002 MCU 的项目。 我正在开发一种位串行通信功能,该功能将使用一根线发送数据,然后切换到接收模式以在同一引脚上接收数据。一个单独的引脚将用于时钟,该时钟始终由不同的板控制(始终是输入)。我想知道是否有一种方法可以将引脚配置为集电极开路操作,使其可以用作输入和输出,或者每次从读取到写入时都必须更改引脚配置?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次都需要使用 TRIS 寄存器来更改引脚的方向。如果引脚设置为输出,则读取 PORT 寄存器很可能只会告诉您将引脚驱动到什么电平(假设引脚上有高阻抗)。如果引脚设置为输入,您将无法驱动所需的输出值。
另外,请确保使用 PORT 寄存器读取传入数据,但使用 LAT 寄存器输出数据。这确保了如果您的代码(我假设您在这里用 C 语言编程)被转换为读-修改-写的 bset/bclr/btgl 指令,您不会遇到任何问题。如果您使用汇编程序编写,则适用相同的规则,但您知道何时使用这些 RMW 类型指令。如果您想对此有更多推理,请询问。
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.