[英]Set a one bit in an 8-bit PIN without changing the other bits
Using winAVR for the following code. 将winAVR用于以下代码。
I got an 8-bit PIN here that I need to OR it with 00000010 or 0x02 to set the 7th bit. 我在这里有一个8位PIN,我需要将其与00000010或0x02进行或运算以设置第7位。
Now what I have to do is the following : 现在,我要做的是以下几点:
BUT, I shouldn't change the other bits in these steps. 但是,我不应该在这些步骤中更改其他位。
So I have to : 所以我必须要 :
So my test code is : 所以我的测试代码是:
B=PINB|0x02
Loop delay for 1ms
BP=PINB&0x00
Loop for 19ms
But I think that the other bits are going to be altered in this process, my question is, HOW am I supposed to manipulate one bit of an 8 bit port without changing the other bits ? 但是我认为在此过程中其他位将被更改,我的问题是,我应该如何在不更改其他位的情况下操纵8位端口中的一位?
Thanks alot !! 非常感谢 !!
You need BP=PINB & ~0x02
The ~ operator is logical NOT. 您需要BP=PINB & ~0x02
运算符是逻辑非。 The and operator keeps only the bits other than 2. and运算符仅保留2以外的其他位。
You use the bitwise negation of the setting mask, and AND that: 您使用设置掩码的按位取反,并且AND:
B = PINB & ~0x02
For the selected bit, the bitwise negation sets that bit to zero; 对于选定的位,按位取反将该位设置为零;否则,该位为0。 all the others are one. 其他所有的都是一个。 The ones do not change the value in PINB when ANDed. 与运算后,这些不会更改PINB中的值。
This page has a good summary of several tricks with bitwise operators. 该页面很好地总结了按位运算符的一些技巧。 http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.