PIC Experimenter's Board Manual
Experiments with Binary and the PIC Microcontroller
The term binary means "based on two" as in two numbers, 0 and 1. It's also like an electrical switch that has two values on (1) and off (0).
The term bit is an acronym that stands for the term Binary digit. A bit or binary digit can have two values, either 0 or 1. A byte is a digital expression (number) containing 8 bits.
Binary is important to computers and microcontrollers. The bit values of (0) and (1) are the only things a computer can read. Actually the computer or microcontroller can't really read, but it can sense voltage values. So a bit that is on (1), is represented by a positive voltage. Consequentially a bit (0) is off (0) and is represented as no voltage.
A single bit by itself is of little value, but start putting them together to make Bytes (8-bits), Words( 16-bits) ((32 bits) (64 bits) (128) and so on) and we can make the computers perform mathematics, create word processors, spreadsheets , create a cyberspace (internet) etc. All these amazing things based on a bit.
To read or write to a port register requires understanding a little binary. When we read and write to any port we use standard decimal numbers. However it's the binary equivalent of those decimal numbers that the PIC Microcontroller uses.
The 16F84 uses 8-bit port registers so we only need to concern ourselves with small 8 bit numbers and their decimal equivalent. Remember an 8 bit number is called a byte. An 8-bit number can represent any decimal value between 0 and 255. When we write a decimal number into a register, the PIC microcontroller can only see the binary equivalent of that decimal number (byte) we wrote to the register. For us to understand what's happening inside the register we need to be able to look at the binary equivalents of the decimal (byte) number also. Once we can do this, our ability to effectively and elegantly program the PIC microcontrollers is greatly enhanced.
Examine the binary number table below, it shows all the decimal and binary number equivalents for numbers 0 through 32. Using this information, the binary numbers from 32 to 255 can be extrapolated.
Each decimal number on the left side of the equal sign has its binary equivalent on the right side. So where we see a decimal number, the microcontroller will see the same number as a series of eight bits. (bits -- eight bits to a byte)
Binary Number Table
0 = 00000000
1 = 00000001
2 = 00000010
3 = 00000011
4 = 00000100
5 = 00000101
6 = 00000110
7 = 00000111
8 = 00001000
9 = 00001001
10 = 00001010
11 = 00001011
12 = 00001100
13 = 00001101
14 = 00001110
15 = 00001111
16 = 00010000
17 = 00010001
18 = 00010010
19 = 00010011
20 = 00010100
21 = 00010101
22 = 00010110
23 = 00010111
24 = 00011000
25 = 00011001
26 = 00011010
27 = 00011011
28 = 00011100
29 = 00011101
30 = 00011110
31 = 00011111
32 = 00100000
.
.
.
64 = 01000000
.
.
.
128 = 10000000
.
.
.
255 = 11111111
Figure 1 (below) shows the relationship between a binary number and the two PIC microcontroller registers that control Port-B. Notice each register has eight open positions. This register can hold an 8-bit (1 byte) number. Lets look at the 2nd binary number table below. Notice for each progression of the binary "1" to the left, the exponential power of 2 is increased by one.
2nd Binary Table
Bit #
Bit 0
Bit 1
Bit 2
Bit 3
Bit 4
Bit 5
Bit 6
Bit 7
Decimal Binary
1 = 00000001
2 = 00000010
4 = 00000100
8 = 00001000
16 = 00010000
32 = 00100000
64 = 01000000
128 = 10000000
These are relevant numbers, because each progression to the left identifies another bit location and bit weight within the 8-bit byte.
For instance, suppose we wanted to write binary 1's at the RB6 and RB2 locations. To do so we add their bit weights together, in this case 64 (RB6) and 4 (RB2), equals 68. The binary equivalent of decimal number 68 is 01000100. If you push that number into the Port B register you will see that the binary 1's are in the RB6 and RB2 position. Remember this, it is important.
The open TRISB register shown in figure 1 may be used to examine numbers placed in the TRISB. The Port-B register may be used to examine numbers placed at the Port B register.
Notice the correlation between the register bit locations, bit weights and Port B I/O pins. This correspondence between the bit number, bit weight and the I/O line is used to program and control the port.