The connection to a microcontroller is via input/output (I/O) pins. These pins can be either LOW (representing a binary 0) or HIGH (binary 1).
In most microcontrollers, each pin can be controlled independently. Many controllers also let you work with multiple pins at a time. When pins are combined, they form a port. I/O pins are most often grouped in sets of eight, to create 8-bit ports. This makes the programming code easier for certain tasks, like operating a string of LEDs or a bunch of motors. In the typical microcontroller, I/O pins are grouped in sets of eight, to create 8-bit ports.
The idea of working with an entire port-o’-bits at a time is that you send or receive a whole value—from 0 to 255 for 8 bits (or 0 to 15 for 4 bits)—to the port at the same time. This value corresponds to the bits you want to control. For example, given an 8-bit port, the number 54 in binary is 00110110.
Fortunately, with programming it’s not hard to convert numeric values into their corresponding bits, and vice versa. Each programming language provides a different mechanism for these procedures, and what follows in the next few sections are simple approaches using Visual Basic. I picked this language because many of you may already be at least somewhat familiar with it, even though you may not use it to program your robots.
Other languages, such as C, offer more robust bit-handling operators that you can take advantage of. The sample code that follows is meant more to teach you the fundamentals than to be applied directly with a robot. Take the ideas and adapt them to your particular case.
When combined as a group, binary 0s and 1s can represent any number in existence, though for robots, at least, numbers on the small end of the scale are most common. As an example, the 4-bit “nibble” 0010 is the same as decimal 2:
Bit Weight | 8 |
4 |
2 |
1 |
|
Value |
0 |
0 |
1 |
0 |
= 2 |
Bit Weight indicates how the position of the bits influences the resulting numeric value. Bits toward the left carry a higher “weight” because they affect the value of the number more profoundly.
Here’s another example: the number 11 is expressed as a 4-bit binary value as 1011:
Bit Weight | 8 |
4 |
2 |
1 |
|
Value |
1 |
0 |
1 |
1 |
= 11 |
You can count the bit weights yourself to arrive at the decimal value. Whenever there is a 1, you add its bit weight to the total:
8 + 2 + 1 = 11
There are a number of programming approaches for determining the individual bits of a binary-formatted value. For instance, you may want to determine which bits are 0 and which are 1 given any 4-bit value from 0 to 15. You could create a lookup table that matches all 16 possibilities to their binary equivalents, but there are several other methods you may want to use instead, depending on the commands and statements provided in the programming language you are using.
Recall from Chapter 36, “Programming Concepts: The Fundamentals,” that microcontrollers and computers can deal with numeric values as string values; strings are a series of numbers or letters like the ones you’re reading right now.
If the bits are in string format, one approach is to use the Mid statement to “parse” through a string and return the value of each character. This code is Visual Basic–compatible and places the bit value (0 or 1) into a four-element array, Bit(0) to Bit (3). While this example shows only 4-bit binary strings being used, the same technique can be used with 8- or 16-bit strings:
Sub BitArray() Dim Count As Integer, BitString As String Dim Bits(4) As Integer ' Use Bits(8) or Bits(16) as appropriate BitString = "0110" ' Sample binary-format string For Count = 0 To Len(BitString) -1 Bits(Count) = Val(Mid(BitString, Count + 1, 1)) Next Count MsgBox Bits(3) ' Example: peek into element 3 End Sub
The working part of this code is the For loop. It, along with the Mid statement, separates each character of the string—in this case, “0110”—into the four elements of the array. The Val statement converts the isolated string character into a numeric digit.
Numeric values can be readily converted into strings once made into a string of 0s and 1s. Here’s one rudimentary approach used in many versions of Basic:
Sub MakeBitString() Dim Temp As String, Count As Integer, X As Integer X = 9 ' Example value Temp = "" For Count = 0 To 3 If (X And (2 ^ Count)) = 0 Then Temp = "0" & Temp Else Temp = "1" & Temp End If Next Count MsgBox Temp End Sub
X is the value you want to convert into a string, in this case 9. The message box displays the binary equivalent in string format (which is “1001”).
You can determine each bit of a decimal value by using the bitwise And operator, first introduced in Chapter 36, “Programming Concepts: The Fundamentals.” The use of the And operator is straightforward:
X = Value And BitWeight
where Value is the value you want to test (0 to 15 for a 4-bit number), and BitWeight is the binary digit you wish to determine. The bit weights for a value between 0 and 15 are 1, 2, 4, and 8. For example:
X = 7 And 4
This tests to see if the third bit (bit weight of 4) is 0 or 1 for the number 7. Expressed in binary form, 7 is 0111:
Bit Weight | 8 |
4 |
2 |
1 |
|
Value |
0 |
1 |
1 |
1 |
= 7 |
So, for this example, X would contain a nonzero result—4, in this case. But suppose you want to test for bit weight 8:
X = 7 And 8
Now, X contains zero because the fourth bit is not 1; it doesn’t contribute to the value of 7.
The following is a quick demonstration routine, suitable for use in Visual Basic and other compatible programming environments. Bits are counted 0 to 3. The message box displays the result of the And’ing expression. Change X to different values; change the If expression to experiment with different bit weights:
X And 1—bit 0 |
X And 2—bit 1 |
X And 4—bit 2 |
X And 8—bit 3 |
Sub DefBit() Dim X As Integer X = 8 ' Experiment with values 0 thru 15 If (X And 8) <> 0 Then ' Bit weight = 8 MsgBox "Bit 3 is 1" Else MsgBox "Bit 3 is 0" End If End Sub
You will have plenty of occasions to convert a set of binary digits into a decimal value. This can be done with simple addition and multiplication, as shown in the Visual Basic–compatible code that follows. Here, values for 4 bits have been specified and named D0 through D3. The message box displays the numeric equivalent of the bits you specify. For example, the bits 1001result in 9.
@code:Sub SumBits() Dim X As Integer Dim D0 As Integer, D1 As Integer, D2 As Integer, D3 As Integer D0 = 1 D1 = 0 D2 = 0 D3 = 1 X = (D3 * 8) + (D2 * 4) + (D1 * 2) + D0 MsgBox X End Sub
It is not uncommon to manipulate the individual bits of a computer or microcontroller port. Quite often, however, it is not possible or practical to address each individual bit. Rather, you must control the bits in sets of 4 or 8. As we’ve already seen, in binary notation bits have different bit weights, so a number like 12 is actually composed of these bits:
Bit Weight | 8 |
4 |
2 |
1 |
|
Value |
1 |
1 |
0 |
0 |
= 12 |
Imagine you have a 4-bit port on your computer or microcontroller. You control the setting of each bit by using a value from 0 to 15, with 0 representing the bits 0000, and 15 representing the bits 1111. You need a way to control each bit separately, without changing the state of the other bits. This is done by using an Or mask, and it’s painfully simple:
X = CurrentValue Or BitToTurnOn
where
For instance, suppose CurrentValue is 7 (0111) and you want to turn on bit 3 (bit weight 8) as well:
X = 7 Or 8
X is 15, or 1111.
The following is Visual Basic–compatible code that demonstrates the use of Or masking to turn on specific bits, without changing the others. Assume X is the CurrentValue you previously obtained from the port and Mask is the BitToTurnOn value from above:
Sub MaskValues() Dim X As Integer, Mask As Integer, Result As Integer X = 3 ' 0011 Mask = 2 ' 0010 Result = X Or Mask MsgBox Result End Sub
With the values specified (X = 3, Mask = 2), the result is 3. The reason: The bit for a “2” is already set. For practice, change X to another value—say, 4 (binary 0100). The result is now 6, which represents binary (0110). You’ve kept the 2 and added 4.