Bytes, Bits and Nibbles

Copyright 2000 G. Shearer

To begin with, a microcontroller really only understands the presence or absence of a voltage. TTL (transistor to transistor logic) specifications consider voltages >1.4 Volts to represents a 1 and voltages <1.4 Volts are considered as a 0. In practice, when the microcontroller detects a voltage above the specified level, it considers this to be a 1. The absence of voltage is considered by the microcontroller to be a 0. Other terms such as: true, set, on and high also refer to the 1 condition. Terms such as: false, clear, off and low refer to the 0 condition. In most cases, these words can be used interchangeably to mean the same thing. Counting to 1 will not get you very far in controlling other devices. You could settle for a wall switch to do that job. We will need to expand a bit on this principle in order to do much of anything.

When we count 1, 2, 3, 4, 5, 6, 7, 8, 9 … we are using the base 10 system, often referred to as decimal. Now, if we only had 1 and 0 to count with, we would have to count 1, 10, 11, 100, 101, 110, 111, 1000 … I think you get the picture. When we count in this manner, we are using the base 2 or binary system. This is a system that our microcontroller has no problem understanding. Back in the base 10 system, we have 1’s place, 10’s place, 100’s place and so on. In the binary system, we have 1’s place, 2’s place, 4’s place, 8’s place and so on. A quick way to convert back to base 10 would be to add the values of all the places in a binary number which are occupied by a 1. For example, the binary number 1010 would be 8+2=10. Each of these binary places is called a bit. A bit could be electrically represented as a wire, terminal or pin on a chip which either has a voltage present on it or has no voltage present, representing a 1 or a 0.

With enough bits, you can count to or represent quite a large number. Notice also that each time we add another place, we double the highest number we can represent. If we have 8 bits representing our numbers we have what is called a byte. With a byte, we can count from 0 to 255. By manipulating bytes, we can do mathematics with the largest numbers we need to work with. You can also see that our microcontroller will need to do more manipulation the larger the numbers we wish to work with. If 8 bits is a byte, then it only makes sense that half a byte or 4 bits would be called a nibble. At least it makes perfect sense to anyone who speaks fluent English.

If we have a nibble to work with, our 4 bits enable us to count from 0 to 15. Wouldn’t it be lovely to be able to count from 0 to 15 with just single place numbers? We can do that if we use the base 16 system, also called hexadecimal or just hex for short. Counting in hex would be 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10 … I think you understand. The place conversion trick I showed you earlier with binary also works with hex if you use 1’s place, 16’s place, 32’s place … however this time you must multiply the value in each place by the place value then add the results together to obtain the decimal value.

So, what good is hex anyway? Well, you can represent an entire nibble with just one place. You can also represent an entire byte (2 nibbles) with just 2 places. Last, but not least, this is the final form your program will take before you ship it off to the microcontroller by marching all the bits, representing the hex values, out of your PC like little soldiers in single file (serial) fashion. Before you download your program to the microcontroller, it will need to be in the hex format. When the JAL compiler compiles your source code, it will create two files. One file will have the .asm extension and the other will have the .hex extension. The latter will be the one you will need to specify when you select the file to be programmed into the microcontroller.

I have included this little chart below that you might find handy to refer to until you memorize exactly what a value in each base would be. If you use the calculator program in Windows, you will discover that it provides an easy method for converting between the different bases.

 

Decimal Hex Binary
     

0

0

0000

1

1

0001

2

2

0010

3

3

0011

4

4

0100

5

5

0101

6

6

0110

7

7

0111

8

8

1000

9

9

1001

10

A

1010

11

B

1011

12

C

1100

13

D

1101

14

E

1110

15

F

1111

Code:

Questions:

Comments: