Bit Operation in AVR Microcontroller
We need to create a program (code) to deliver our instructions to MCU. For this AVR-based microcontrollers, the language we are using is called Embedded-C, which is basically the extension of C language, so if you have basic in C, that’s a plus.
There is something so basic yet important part in programming AVR in C, which is called the bit operation. In this tutorial, I will introduce you to some basic introduction to what bit is, the operator and how to operate it.
What is a bit?
According to this, a bit is a basic unit of information used in computing and digital communication, and yes I agree with this definition. It is the basic, the very basic, so basic that we could not understand it, why? Because it’s not a human language it’s a machine language.
A bit is consist of zero’s and one’s combined together to achieved some instruction set that can be understood by the computer, I assume that you already know how to convert from binary to hexadecimal and vice versa, as it might be needed along with your AVR journey.
This is an example of writing 8-bits to declare the status of MCU’s pin.
PORTA = 0b01001001
Don’t worry if you don’t get it now, we’ll get back to that later.
It is really important to understand how bits work and how to manipulate them in order to communicate well with the machine. There are six operators that I’m going to introduce in this article, they are AND, OR, XOR, INVERT, SHIFT RIGHT, and SHIFT LEFT. Each operator belongs to one symbol to represent them.
AND, OR, XOR
To get you familiar with the basic logic of these operators here are some truth tables for those bitwise operators. An important thing to keep in mind is that bit value and logic value is different! You will see what it means later on.
~(10011010) = 01100101
Each bit value is inverted to its opposite value.
1000 1111 << 4bits will produce 1111 000
Shift 4 bits to the left, notice the position of ‘1’ bit is replaced by ‘0’ as the bit is moving. This is a step by step process happened during the shifting.
10001111 >> 4bits will produce 0000 1000
The same theory applies to right shifting. Shifting 4 bits to the right direction means replacing the position of bit ‘1’ with ‘0’ as the bit moves to the right.
Using bit to control pin in AVR MCU
Controlling bit is a way for the programmer to activate/deactivate certain pins in MCU. Following is a few examples on the syntax of bit operation in C plus their meanings.
- PORTA = 0b00001111
First, forget about the ‘PORTA’ we’ll get to that later, let’s just focus on the righthand side of the equation the ‘0b0001111’. The ‘0b’ means we will express the value of PORTA in binary, I suppose the ‘b’ means binary. Another way to express this value is by writing ‘0x’ which means ‘hexadecimal’, the above code then could be translated to ‘PORTA = 0x0F.’ (Again I assume that you understand binary to hexadecimal conversion).
Another important term here is MASK BIT, or simply MASK is the ‘0b00001111’. Mask define which bits we want to modify.
So far we’ve got.
I highly recommend for beginners to start programming using the binary value since by expressing the value in binary you could clearly see which values assigned to which pin.
2. PORTA |= 0b00001111
Notice the use of ‘|=’ this symbols simply means PORTA = PORTA | 0b00001111. You will find operators symbol that is followed by ‘=’, just keep in mind what it means.
So far we’ve got.
3. PORTA &= ~0b00001111
Can you guess what the above code will equal to? Yes, you are correct! (Answer: PORTA &= 0b11110000)
We will see later why using complement ‘~’ is preferred. So often, you will encounter if logic in your code, now let’s dig some codes that involve it.
Bit Manipulation Around If Statement
Let’s see this code :
If (PINA & 0x01)
Remember earlier in this tutorial I mentioned about bit value and logic value? Now it’s time to take a look at these definitions. A bit value is literally the value of bit itself, e.g. 0 AND 1 will value as 0. But logic value consists of TRUE or FALSE. A statement will be TRUE if whatever written inside the if condition is fulfilled, otherwise it will be FALSE.
The ‘&’ operator is used to check a certain bit value in a PINA (again for now don’t get confused with the definition of PIN). Let’s focused on the bit. Suppose you want to check the first bit in PINA, therefore, you need to mask the first bit of PIN A with the appropriate mask which is ‘0x01’ or ‘0b00000001’. Let’s say the initial bit of PINA is ‘0b00000001’, so we have :
I hope you get it what I mean by logic value by the explanation above. Often we will encounter the NOT symbol (!) inside if statement. Understanding of operator ! will deepen your knowledge on the logic behind if statement.
The ! NOT Symbol
Notice that on the table above, the logic value is TRUE if the value of the bit is 1 (bit it set), in short, we can say if a bit is 1 then the logic value is TRUE then do action 1. But can we reverse it? What if we want to do action 1 if the if statement is not fulfilled (FALSE or the bit is not set). Don’t worry, The ! NOT symbol comes to the rescue!
This is how you code it :
If (! (PINA & 0x01))
Let’s create a similar table as above to simplify the explanation :
There are three ways of writing this logic that mean the same thing :
For beginners, I personally recommend writing with the second one as it is the easiest to grasp for beginners. Now pay attention to the second one, it’s written as :
if ((PINA & 0x01) == 0)
Now, you may be wondering, can we apply the same principle to the first example? Can we write
if ((PINA & 0x01) == 1) instead of if (PINA & 0x01)
I regret to inform you that the answer is no, we cannot. So, let’s just stick to the original one.
This is the end of the tutorial of bitwise operation. If you find anything wrong or something need to be edited please feel free to reach me out. Next, we will take a closer look at what is PIN, PORT and DDR in GPIO section. Here are some terms we’ve covered in this chapter.
Here are some new words we covered in this session :