This answer is general to processors and peripherals, and has an SRAM specific comment at the end, which is probably pertinent to your specific RAM and CPU.
Output pins can be driven in three different modes:
- open drain - a transistor connects to low and nothing else
- open drain, with pull-up - a transistor connects to low, and a resistor connects to high
- push-pull - a transistor connects to high, and a transistor connects to low (only one is operated at a time)
Input pins can be a gate input with a:
- pull-up - a resistor connected to high
- pull-down - a resistor connected to low
- pull-up and pull-down - both a resistor connected to high and a resistor connected to low (only useful in rare cases).
There is also a Schmitt triggered input mode where the input pin is pulled with a weak pull-up to an initial state. When left alone it persists in its state, but may be pulled to a new state with minimal effort.
Open drain is useful when multiple gates or pins are connected together with an (external or internal) pull-up. If all the pin are high, they are all open circuits and the pull-up drives the pins high. If any pin is low they all go low as they tied together. This configuration effectively forms an AND
gate.
_____________________________
Note added November 2019 - 7+ years on: The configuration of combining multiple open collector/drain outputs has traditionally been referred to as a "Wired OR" configuration. CALLING it an OR (even traditionally) does not make it one. If you use negative logic (which traditionally may have been the case) things will be different, but in the following I'll stick to positive logic convention which is what is used as of right unless specifically stated.
The above comment about forming an 'AND' gate has been queried a number of times over the years - and it has been suggested that the result is 'really' an 'OR' gate. It's complex.
The simple picture' is that if several open collector outputs are connected together then if any one of the open collector transistors is turned on then the common output will be low. For the common output to be high all outputs must be off.
If you consider combining 3 outputs - for the result to be high all 3 would need to have been high individually. 111 -> 1. That's an 'AND'.
If you consider each of the output stages as an inverter then for each one to have a high output it's input must be low. So to get a combined high output you need three 000 -> 1 . That's a 'NOR'.
Some have suggested that this is an OR - Any of XYZ with at least 1 of these is a 1 -> 1.
I can't really "force" that idea onto the situation.
_________________________________
When driving an SRAM you probably want to drive either the data lines or the address lines high or low as solidly and rapidly as possible so that active up and down drive is needed, so push-pull is indicated. In some cases with multiple RAMs you may want to do something clever and combine lines, where another mode may be more suitable.
With SRAM with data inputs from the SRAM if the RAM IC is always asserting data then a pin with no pull-up is probably OK as the RAM always sets the level and this minimises load. If the RAM data lines are sometimes open circuit or tristate you will need the input pins to be able to set their own valid state. In very high speed communications you may want to use a pull-up and a pull-down so the parallel effective resistance is the terminating resistance, and the bus idle voltage is set by the two resistors, but this is somewhat specialist.
While this datasheet is making my eyes bleed with the complexity of doing a simple conversion/read, you need to make some simple changes.
I'm not sure why people are making this harder than it really is. You pull the chip select line low, and the target IC should simply read whatever you give it until you pull it high again. Just because your method takes an 8-bit value doesn't mean you can't call it twice to pass a 16-bit value to your IC. There is no need to bit-bang anything. That is nonsense.
Call your SPI method to tell the ADC you're looking to write to the configuration register:
spi(0x10);
As I understand from the datasheet, you can immediately write your 16-bit value to the configuration register after that, so you'd do:
spi(0x07);
spi(0x10);
I forget which way that'll end up being assembled on the target IC side, so you could simply reverse them if things don't work right. There's no need to bit shift any values at all. Worst case scenario is you have to pull the CS line back high before pulling it low again to actually send the data to write to the configuration register.
Otherwise, this is super simple. Remember, if the target SPI device expects more than one byte, i.e. a 16-bit value, chances are good that sending the bytes in the order they'd otherwise appear normally (so if you want to send 0x1234, you'd have 0x12 and 0x34) will work fine and you won't need to shift anything.
Best Answer
Think about it. The open drain feature would not work as intended if you had to keep the TRIS bit set. That would keep the output driver high impedence. What open drain does instead is switch to high impedance when the output would otherwise be high, but still actively pull low when the output is supposed to be low.
So if you want to use a pin in open drain mode, set the ODC bit to 1, the TRIS bit to 0, and then set the LAT bit according to the data you want. When the LAT bit is 0, the pin will actively pull low. When the LAT bit is 1, the pin will be in high impedance mode.