Jump to content
Sign in to follow this  
erikhaverkamp

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

Recommended Posts

Hi,

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);
nop();
set_bit(portb, 5);

 

Thanks,

 

Share this post


Link to post
Share on other sites

erikhaverkamp,

Hi,

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);

nop();

set_bit(portb, 5);

 

Thanks,

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.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×