Jump to content

Why Is A Nop() Required In Between Two Set_Bit() Calls?

Recommended Posts



I am working with a PIC16F1782 and use the following code:


set_bit(portb, 4);

set_bit(portb, 5);


This results in bit 4 not being set, however if add a nop it works.

I have seen this more.... any ideas?


set_bit(portb, 4);


set_bit(portb, 5);



Remember that setting single bits directly on a port cause a Read Modify Write process. The read process reading what is actually on the port, rather than what was last written.

When the port is read as part of setting bit 5 bit 4 must be read as being low, hence low is written back to it.

Typical cause of such a problem is capacitive loading of the port, causing the voltage on the actual pin to take time to rise.

On some devices LAT registers exist which don't suffer from this problem as reading them give the last written value rather than the actually pin value.

Another method is to keep your own shadow register of the whole port and modify that, and then it will be written without reading the port.


I hope that makes sense.




Link to post
Share on other sites

Join the conversation


Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.


×   Your previous content has been restored.   Clear editor


  • Create New...