Rusty Carruth wrote: > > Keith Howell wrote: > > > Hi Dmitry (Kiryashov) > > > > Thank you for writing. In response: > > ... > > > 2. I'm aware of the Read-Modify-Write CPU access cycle effects. > > I can see how this could cause confusion in byte operations, but not single > > bits. > > whoa there - if you need to change a bit in a byte, how are you going to NOT > change the other bits without reading them? Thus, changing a single bit demands > a RMW cycle. > > Unless I've really missed something, which is highly possible ;-) > The PIC instruction set has instructions that manipulate individual bits. They read the whole register byte, twiddle the relevant bit, then write the whole register byte back. Works fine for data registers. The PIC designers mapped the I/O ports into register space. Write a byte to PORTC and this goes to the output latch. Read a byte from PORTC and this is read from the port pins. Seems fair enough. Off they went and built the thing. However, strange things happen when you try to bit twiddle I/O. For example, when bit-banging I2C, you can make pins act as open-collector outputs by writing binary xxx00xxx to the PORTC output latch. Clear the corresponding bits in TRISC, and the outputs go low. Set them, and the outputs float high. The CPU can read PORTC to check the logic level on the pins. So far so good. Notice that what you read from PORTC is NOT what you wrote. This is important. Now suppose these output pins are set to be tristate, and are floating high. Do a bit operation on any other port C bit. You'd like to think that it would just affect that bit. But you'd be disappointed. The CPU will: a) read a whole byte from PORTC, (SDA and SCL inputs as being high) b) modify the other bit within the byte, c) write the whole byte to the PORTC output latch. This changes bits you did not expect: the SDA and SCL output latch bits. Then when you try to operate SDA and SCL via TRISC, they don't go low when expected, they get driven high. As a result, because the chip architects some sloppily thought putting two different registers at the same logical address, all chip programmers are condemmed to thinking harder and more carefully to compensate. There are work-arounds, but I'm not pleased at having to do this extra work. The words "Oh bugger" form in the mind. Microchip are aware of all this but it's too late to change the fundamental internal operation.