Welcome back, guys.
In the last post of this tutorial series, we had created our first circuit for interfacing an LED with the AVR ATmega 16 microcontroller. If you remember the code that we wrote for that, we used this line:
DDRB = 0b11111111;
Here, as I mentioned in the previous post, DDRB stands for Data Direction Register. It is used for setting the direction of data(either input or output) for a PORT and in this case, it is PORTB. The value written after it ie. 0b11111111, is a binary number which is the value assigned to the register.
Now, the question arises what is a Binary Number and what other types of number systems are out there.
Computer architectures support the following Number Systems:
- Binary Numbers (Base 2)
- Octal Numbers (Base 8)
- Decimal Numbers (Base 10)
- Hexa-Decimal Numbers (Base 16)
Therefore in this post, I am going to talk about them in details so as to give you a basic understanding of their uses in embedded systems and electronics in general.
Decimal Number System:
The decimal numeral system (also called base-ten positional numeral system, and occasionally called denary) is the standard system for denoting integer and non-integer numbers. This is the most widely used number systems in real life and in fact taught to every child in school. For writing numbers, the decimal system uses ten decimal digits, a decimal mark, and, for negative numbers, a minus sign “−”. The decimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; the decimal separator is the dot “.”
This system has a base of 10 because every number in this system can be written as the sum of the product of place value numbers with some power of 10. For example, let us take a decimal number 234. It can also be written as:
234 = (2*10^2)+(3*10^1)+(4*10^0)
=200 + 30 + 4
This system is one of the earliest numeral systems to be adopted in modern history. One reason might be because we as humans have 10 fingers in our hands so a numeral system with a base of 10 might be ideally suited for us.
Binary Number System:
A binary numeral system or base-2 numeral system uses only two symbols: typically 0 (zero) and 1 (one). The base-2 numeral system is a positional notation with a radix of 2. Each digit is referred to as a bit. Every other number is represented using the combinations of these two digits ie. 0 and 1.
Because of its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used by almost all modern computers and computer-based devices. It is ideally suited for machines as the bits represent the only two states that a machine can achieve. One represents the High or ON state whereas zero represents the Low or OFF state.
There are various methods for converting a decimal number into its binary equivalent and one such has been shown in the figure to the left. In this, we divide the number by 2 and check for 1 or 0 as remainder. If the remainder is some other number then we divide the quotient with 2 and write the remainder on the left. We continue this step until we get 1 or 0 as the remainder. The final binary result is then calculated by writing all the numbers from bottom to top.
As shown in the figure to the left; the binary equivalent of decimal number 35 is 0100011 or just 100011. The 0’s to the left can be ignored as is the case in other number systems.
Counting in binary is similar to counting in any other number system. Beginning with a single digit, counting proceeds through each symbol, in increasing order, but binary only has two symbols 0 and 1 available. Thus, after a digit reaches 1 in binary, an increment resets it to 0 but also causes an increment of the next digit to the left. A comparison chart of all the number systems including binary is given below.
All Binary numbers can then be represented back to their decimal equivalent by the sum of the products of place value numbers with some power of 2. For example, 100011 can be represented as:
100011 = (1*2^5)+(0*2^4)+(0*2^3)+(0*2^2)+(1*2^1)+(1*2^0)
=32 + 0 + 0 + 0 + 2 + 1
Octal Number System:
The octal numeral system, or oct for short, is the base-8 number system and uses the digits 0 to 7. Octal numerals can be made from binary numerals by grouping consecutive binary digits into groups of three (starting from the right). Octal became widely used in computing when systems such as the PDP-8, ICL 1900 and IBM mainframes employed 12-bit, 24-bit or 36-bit words. Octal was an ideal abbreviation of binary for these machines because their word size is divisible by three (each octal digit represents three binary digits).
Similar to the decimal to binary conversion method; an octal equivalent of a decimal number can be found out by dividing the decimal number by 8 and checking for 0 as remainder. If the remainder is some other number than 0, then the quotient is further divided by 8 keeping the previous remainder in store, until 0 is achieved as remainder. Counting all the remainders (as shown in the example to the right) from bottom to top gives us the equivalent Octal number. Therefore as shown in the picture the octal representation of a decimal number 214 is 326.
All Octal numbers can then be represented back to their decimal equivalent by the sum of the products of place value numbers with some power of 8. For example, 326 can be represented as:
326 = (3*8^2)+(2*8^1)+(6*8^0)
=192 + 16 + 6
An interesting fact is that Transponders in aircraft transmit a code, expressed as a four-octal-digit number when interrogated by ground radar. This code is used to distinguish different aircraft on the radar screen and is vital for Air Traffic Controllers.
Hexadecimal Number System:
In mathematics and computing, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A–F (or alternatively a–f) to represent values ten to fifteen.
When working with large digital systems, such as computers, it is common to find binary numbers consisting of 8, 16 and even 32 digits which makes it difficult to both read or write without producing errors, especially when working with lots of 16 or 32-bit binary numbers.
One common way of overcoming this problem is to arrange the binary numbers into groups or sets of four bits (4-bits). Each hexadecimal digit represents four binary digits, also known as a nibble, which is half a byte. For example, a single byte can have values ranging from 0000 0000 to 1111 1111 in binary form, which can be more conveniently represented as 00 to FF in hexadecimal.
Being a Base-16 system, the hexadecimal numbering system, therefore, uses 16 (sixteen) different digits with a combination of numbers from 0 through to 15. In other words, there are 16 possible digit symbols. However, there is a potential problem with using this method of digit notation caused by the fact that the decimal numerals of 10, 11, 12, 13, 14 and 15 are normally written using two adjacent symbols. For example, if we write 10 in hexadecimal, do we mean the decimal number ten, or the binary number of two (1 + 0). To get around this tricky problem, hexadecimal numbers that identify the values of ten, eleven, . . . , fifteen are replaced with capital letters of A, B, C, D, E and F respectively.
The hexadecimal equivalent of a decimal number can be found out by dividing the decimal number by 16 and checking for 0 as remainder. If the remainder is some other number than 0, then the quotient is further divided by 16 keeping the previous remainder in store, until 0 is achieved as remainder. Counting all the remainders (as shown in the example to the right) from bottom to top gives us the equivalent Hexadecimal number. Therefore as shown in the picture the hex representation of a decimal number 3509 is DB5.
The number can easily be returned back to its Decimal equivalent by the sum of products of place values to some power of 16. For example, DB5 can also be written as:
DB5 = (D*16^2)+(B*16^1)+(5*16^0)
=3328 + 176 + 5
Here is a complete table of all the Number Systems side by side for your convenience.
Before we move further into this tutorial series, it is essential for us to have at least a rudimentary understanding of different Logic Gates used in electronics and electrical circuits. A logic gate is an idealized or physical device implementing a Boolean function; that is, it performs a logical operation on one or more binary inputs and produces a single binary output. Logic gates are primarily implemented using diodes or transistors acting as electronic switches, but can also be constructed using vacuum tubes, electromagnetic relays (relay logic), fluidic logic, pneumatic logic, optics, molecules, or even mechanical elements. Logic circuits include such devices as multiplexers, registers, arithmetic logic units (ALUs), and computer memory, all the way up to complete microprocessors, which may contain more than 100 million gates. In modern practice, most gates are made from field-effect transistors (FETs), particularly metal–oxide–semiconductor field-effect transistors (MOSFETs).
In electronics, a NOT gate is more commonly called an inverter. As the name suggests the gate reverses the one-bit Input when producing the output. This means if the Input is 1 or High, the Output is 0 or Low. The symbol used in digital electronics for this gate has been given in the left. The truth table which maps out all the possible inputs and output of the gate has been shown in the right.
The AND gate is a basic digital logic gate that implements logical conjunction. It behaves according to the truth table to the right. A HIGH output (1) results only if all the inputs to the AND gate are HIGH (1). If none or not all inputs to the AND gate are HIGH, LOW output results. The function can be extended to any number of inputs. Basically, the AND function finds the minimum of two inputs.
The OR gate is a digital logic gate that implements logical disjunction – it behaves according to the truth table to the right. A HIGH output (1) results if one or both the inputs to the gate are HIGH (1). If neither input is HIGH, a LOW output (0) results. In another sense, the function of OR effectively finds the maximum between two binary digits.
In digital electronics, a NAND gate (negative-AND) is a logic gate which produces an output which is False only if all its inputs are True; thus its output is the complement to that of the AND gate. A LOW (0) output results only if both the inputs to the gate are HIGH (1); if one or both inputs are LOW (0), a HIGH (1) output results. It is made using transistors and junction diodes. The NAND gate is significant because any boolean function can be implemented by using a combination of NAND gates. This property is called functional completeness. It shares this property with the NOR gate and as a result, they are together called Universal Gates.
The NOR gate is a digital logic gate that implements logical NOR – it behaves according to the truth table to the right. A HIGH output (1) results if both the inputs to the gate are LOW (0); if one or both input is HIGH (1), a LOW output (0) results. NOR is the result of the negation of the OR operator. It can also be seen as an AND gate with all the inputs inverted. NOR is a functionally complete operation—NOR gates can be combined to generate any other logical function. It shares this property with the NAND gate and together they make up the Universal Gates.
The XOR gate (sometimes EXOR gate and pronounced as Exclusive OR gate) is a digital logic gate that gives a true (1 or HIGH) output when the number of true inputs is odd. An XOR gate implements an exclusive or; that is, true output results if one, and only one, of the inputs to the gate is true. If both inputs are false (0 or LOW) or both are true; false output results. XOR represents the inequality function, i.e., the output is true if the inputs are not alike otherwise the output is false. A way to remember XOR is “one or the other but not both”.
The XNOR gate (often written Exclusive-NOR) is a digital logic gate whose function is the logical complement of the exclusive OR (XOR) gate. The two-input version implements logical equality, behaving according to the truth table to the right, and hence the gate is sometimes called an “equivalence gate”. A high output (1) results if both of the inputs to the gate are the same. If one but not both inputs are high (1), a low output (0) results.
Below this is a video where I explain everything I have been talking about in details along with how to do Bit-Wise operations in AVR microcontrollers. So please go through it-
I have also included the code below used in the video for Bit-Wise operations. Feel free to modify it as you see fit.
Title: BitWise Operations
Author: Samiran Patra
Target: ATmega16 Microcontroller
MCU_SPEED: 1000000 Hz
Note: Use makefile for compilation.
//DDR Data Direction Registor
DDRA |= (1<<0); //output is always 1 and input is always 0
PORTA |= (1<<0);
PORTA &= ~(1<<0);
Shop Smart with us for Best Prices:
If you want to continue on with this series I would ask you to visit our YouTube channel and subscribe to receive continuous notifications. You can also subscribe to our Newsletter below to get email notification of each post as and when they are posted.
Thanks a lot for reading through this post. Feel free to follow through this series and comment your queries in the comment box below. Also feel free to contact us regarding any out of stock product and we will try to get back to you as soon as possible.