Is it stable to change the direction of I / O on the microcontroller?

I am new to microcontroller programming and I have connected my microcontroller board to another device that provides a status based on sending a command, but this status is provided on the same I / O pin that is used to provide data, So basically have I have an 8 bit data line that is used as the output of the microcontroller, but for some commands I get a status back on one of the data lines if I want to read it. So I would need to reverse the direction of that line in order to read the status, converting that line as output to input and then back to output. Is this acceptable programming or is this change in I / O output often causing instability?

Thanks.

+2


a source to share


4 answers


There should be no problem reversing the direction of the I / O line to read the state returned by the peripheral, provided you change the state of the line to input before the peripheral starts to drive the line, and then do not try to drive the line as an output. until the peripheral stops it. What you should try to avoid is a conflict between the two driver devices, that is, with the two ends being driven into opposite states by the processor and peripherals. This would lead, at best, to a large spike in power consumption, or worse bloated pin driver circuitry in the processor, peripheral, or both.



You don't say what the processor or peripherals are, so I can't tell if the interface has any control bits that allow the remote device to output status so you know if the peripheral will control the line at all times.

+4


a source


I did it on digital I / O pins without any problem, but I am very far from an expert on the subject. It probably depends entirely on which microcontroller you are using.



+1


a source


Yes, it's great to reverse the I / O direction on the microcontroller. This is the standard way to communicate over open collector buses such as I2C and iButton. (see PICList: Buses for links to assembly language examples).

  • transmit 0 bit: Set the output LATx bit to 0 and then set the TRISx bit to OUTPUT.
  • transmit 1 bit: keep the LATx output bit at 0 and set the TRIS bit for INPUT (so that the pull line of the external resistor is high)
  • listen for a response from the peripheral: keep the output LATx bit at 0 and set the TRIS bit for INPUT. Connect the external resistor line high when the peripheral is sending 1, or have the peripheral pull the line low when the peripheral is transmitting 0. Read the bit from the PORTx pin.

If both ends of the bus follow this protocol correctly (in particular, if neither end is actively driving the line high), then you never have to worry about conflicts or current spikes.

+1


a source


It is important to remember that any high speed IO switching will generate EMI. Depending on switching frequency, board layout, and device sensitivity, this EMI can impact the performance and reliability of your application.

If you are having problems with your application, use an oscilloscope to check the irradiated EMIs on your tracks.

0


a source







All Articles