Let’s make a line follower robot part:: 6

0

Hi everybody.

Missed parts:

part 1

part 2

part 3

part 4

part 5

 

We are going to know about electronics by micro-controllers. We know about the gates. OR, AND, NOT, XOR, etc.

We have the different ICs also to use the gates. We can make the gates by our micro-controller.  Now we will discuss about the Gates in micro controller and their values.

  • Bit-wise Operations

Since individual bits often have a significant meaning when programming AVR microcontrollers, bit-wise operations are very important.

A bit-wise AND operation results in bits being set only if the same bits are set in both of the operands. In other words: bit n will be set in the result if bit n is set in the first operand and the second operand.

AVR microconroller bitwise AND
Figure 3.3 – Bit-wise AND operation

A single ampersand character (&) is the bit-wise AND operator in C.

uint8_t a = 0xAA; /* 10101010 */
uint8_t b = 0x0F; /* 00001111 */
uint8_t = a & b;  /* 00001010 */

A bit-wise OR operation results in bits being set if the same bits are set in either of the operands. In other words: bit n will be set in the result if bit n is set in the first operand or the second operand.

AVR microconroller bitwise OR
Figure 3.4 – Bit-wise OR operation

A single pipe character (|) is the bit-wise OR operator in C.

uint8_t a = 0xAA; /* 10101010 */
uint8_t b = 0x0F; /* 00001111 */
uint8_t = a | b;  /* 10101111 */

A bit-wise XOR operation (“exclusive or”) results in bits being set if, and only if, the same bit is set in one of the operands but not the other. In other words: bit n will be set in the result if bit n is exclusively set in only one of the operands.

AVR microconroller bitwise XOR
Figure 3.5 – Bit-wise XOR operation

The caret character (^) is the bit-wise XOR operator in C.

uint8_t a = 0xAA; /* 10101010 */
uint8_t b = 0x0F; /* 00001111 */
uint8_t = a ^ b;  /* 10100101 */

A NOT operation, also known as a one’s complement, is a unary operation. That means the operation is performed on a single value instead of two. The NOT operation will simply negate each bit. Every 1 becomes 0 and every 0 becomes 1.

AVR microconroller bitwise NOT
Figure 3.6 – Bit-wise NOT operation

A tilde character (~) is the NOT operator in C.

uint8_t a = 0xAA; /* 10101010 */
uint8_t b = ~a;   /* 01010101 */

A shift operation shifts all of the bits to the left or the right. In a left shift, bits get “shifted out” on the left and 0 bits get “shifted in” on the right. The opposite goes for a right shift.

AVR microconroller bitwise shift
Figure 3.7 – Bit-wise shift operation

Two less-than symbols (<<) is the left shift operator and two greater-than symbols (>>) is the right shift operator in C. The right side of the operator is the number of bits to shift.

uint8_t a = 0x99; /* 10011001 */
uint8_t b = a<<1; /* 00110010 */
uint8_t c = a>>3; /* 00010011 */
  • Clearing and Setting Bits

Setting and clearing a single bit, without changing any other bits, is a common task in AVR microcontroller programming. You will use these techniques over and over again.

When manipulating a single bit, it is often necessary to have a byte value in which only the bit of interest is set. This byte can then be used with bit-wise operations to manipulate that one bit. Let’s call this a bit value mask. For example, the bit value mask for bit 2 would be 00000100 and the bit value mask for bit 6 would be 01000000.

Since the number 1 is represented in binary with only bit 0 set, you can get the bit value mask for a given bit by left shifting 1 by the bit number of interest. For example, to get the bit value mask for bit 2, left shift 1 by 2.

AVR microconroller bit value mask
Figure 3.7 – Bit value mask

To set a bit in C, OR the value with the bit value mask.

uint8_t a = 0x08; /* 00001000 */
                  /* set bit 2 */
a |= (1<<2);      /* 00001100 */

Use multiple OR operators to set multiple bits.

uint8_t a = 0x08;   /* 00001000 */
                    /* set bits 1 and 2 */
a |= (1<<2)|(1<<1); /* 00001110 */

To clear a bit in C, NOT the bit value mask so that the bit of interest is the only bit cleared, and then AND that with the value.

uint8_t a = 0x0F; /* 00001111 */
                  /* clear bit 2 */
a &= ~(1<<2);     /* 00001011 */

Use multiple OR operators to clear multiple bits.

uint8_t a = 0x0F;      /* 00001111 */
                       /* clear bit 1 and 2 */
a &= ~((1<<2)|(1<<1)); /* 00001001 */

To toggle a bit in C, XOR the value with the bit value mask.

uint8_t a = 0x0F; /* 00001111 */
                  /* toggle bit 2 */
a ^= (1<<2);      /* 00001011 */
a ^= (1<<2);      /* 00001111 */

That’s all for today try to understand and if you are failed to understand then contact here.

NB:

I am a student and a learner as well.These are from the basics of electronics and I learned it from surfing net. If anything is missed here please forgive me. Happy Robotics. 😛 😛 😛

Good bye………..

By the way I have my own blog now. I am going to start it with some tricks and hacks of programming and technology. If you wish to visit there please click here. Or visit blog.techiehunter.org

Share.

About Author

Rajumeazi

I am a free learner like to fly in the whole cyber world to gather knowledge. And I know I have only a little to share but I will try my best to give you what I have. :) Thank you :)

Comments are closed.